blob: 83a4f3879e014183eff0f2e7e273dd6584a425b8 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Hanno Becker74716312017-10-02 10:00:37 +010021
Paul Bakker5121ce52009-01-03 21:22:43 +000022/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000023 * The following sources were referenced in the design of this implementation
24 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000025 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000026 * [1] A method for obtaining digital signatures and public-key cryptosystems
27 * R Rivest, A Shamir, and L Adleman
28 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
29 *
30 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
31 * Menezes, van Oorschot and Vanstone
32 *
Janos Follathe81102e2017-03-22 13:38:28 +000033 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
34 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
35 * Stefan Mangard
36 * https://arxiv.org/abs/1702.08719v2
37 *
Paul Bakker5121ce52009-01-03 21:22:43 +000038 */
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020042#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010049#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050051#include "mbedtls/platform_util.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000052
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <string.h>
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000061#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/platform.h"
Piotr Nowicki305a5ec2020-08-10 17:42:18 +020064
65#if !defined(MBEDTLS_PLATFORM_C)
Rich Evans00ab4702015-02-06 13:43:58 +000066#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020068#define mbedtls_calloc calloc
69#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010070#endif
71
Hanno Beckera565f542017-10-11 11:00:19 +010072#if !defined(MBEDTLS_RSA_ALT)
73
Hanno Beckerddeeed72018-12-13 18:07:00 +000074/* Parameter validation macros */
75#define RSA_VALIDATE_RET( cond ) \
76 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
77#define RSA_VALIDATE( cond ) \
78 MBEDTLS_INTERNAL_VALIDATE( cond )
79
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010080#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +010081/* constant-time buffer comparison */
82static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
83{
84 size_t i;
85 const unsigned char *A = (const unsigned char *) a;
86 const unsigned char *B = (const unsigned char *) b;
87 unsigned char diff = 0;
88
89 for( i = 0; i < n; i++ )
90 diff |= A[i] ^ B[i];
91
92 return( diff );
93}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010094#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +010095
Hanno Becker617c1ae2017-08-23 14:11:24 +010096int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
97 const mbedtls_mpi *N,
98 const mbedtls_mpi *P, const mbedtls_mpi *Q,
99 const mbedtls_mpi *D, const mbedtls_mpi *E )
100{
101 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000102 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100103
104 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
105 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
106 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
107 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
108 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
109 {
110 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
111 }
112
113 if( N != NULL )
114 ctx->len = mbedtls_mpi_size( &ctx->N );
115
116 return( 0 );
117}
118
119int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100120 unsigned char const *N, size_t N_len,
121 unsigned char const *P, size_t P_len,
122 unsigned char const *Q, size_t Q_len,
123 unsigned char const *D, size_t D_len,
124 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100125{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000126 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000127 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100128
129 if( N != NULL )
130 {
131 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
132 ctx->len = mbedtls_mpi_size( &ctx->N );
133 }
134
135 if( P != NULL )
136 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
137
138 if( Q != NULL )
139 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
140
141 if( D != NULL )
142 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
143
144 if( E != NULL )
145 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
146
147cleanup:
148
149 if( ret != 0 )
150 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
151
152 return( 0 );
153}
154
Hanno Becker705fc682017-10-10 17:57:02 +0100155/*
156 * Checks whether the context fields are set in such a way
157 * that the RSA primitives will be able to execute without error.
158 * It does *not* make guarantees for consistency of the parameters.
159 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100160static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
161 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100162{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100163#if !defined(MBEDTLS_RSA_NO_CRT)
164 /* blinding_needed is only used for NO_CRT to decide whether
165 * P,Q need to be present or not. */
166 ((void) blinding_needed);
167#endif
168
Hanno Becker3a760a12018-01-05 08:14:49 +0000169 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
170 ctx->len > MBEDTLS_MPI_MAX_SIZE )
171 {
Hanno Becker705fc682017-10-10 17:57:02 +0100172 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000173 }
Hanno Becker705fc682017-10-10 17:57:02 +0100174
175 /*
176 * 1. Modular exponentiation needs positive, odd moduli.
177 */
178
179 /* Modular exponentiation wrt. N is always used for
180 * RSA public key operations. */
181 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
182 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
183 {
184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
185 }
186
187#if !defined(MBEDTLS_RSA_NO_CRT)
188 /* Modular exponentiation for P and Q is only
189 * used for private key operations and if CRT
190 * is used. */
191 if( is_priv &&
192 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
193 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
194 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
195 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
196 {
197 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
198 }
199#endif /* !MBEDTLS_RSA_NO_CRT */
200
201 /*
202 * 2. Exponents must be positive
203 */
204
205 /* Always need E for public key operations */
206 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
208
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100209#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100210 /* For private key operations, use D or DP & DQ
211 * as (unblinded) exponents. */
212 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
213 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
214#else
215 if( is_priv &&
216 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
217 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
218 {
219 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
220 }
221#endif /* MBEDTLS_RSA_NO_CRT */
222
223 /* Blinding shouldn't make exponents negative either,
224 * so check that P, Q >= 1 if that hasn't yet been
225 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100226#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100227 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100228 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
229 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
230 {
231 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
232 }
233#endif
234
235 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100236 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100237#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100238 if( is_priv &&
239 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
240 {
241 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
242 }
243#endif
244
245 return( 0 );
246}
247
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100248int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100249{
250 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000251 int have_N, have_P, have_Q, have_D, have_E;
Jack Lloyd32b6e692020-01-29 13:09:55 -0500252#if !defined(MBEDTLS_RSA_NO_CRT)
253 int have_DP, have_DQ, have_QP;
254#endif
Hanno Beckerddeeed72018-12-13 18:07:00 +0000255 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100256
Hanno Beckerddeeed72018-12-13 18:07:00 +0000257 RSA_VALIDATE_RET( ctx != NULL );
258
259 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
260 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
261 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
262 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
263 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100264
Jack Lloyd32b6e692020-01-29 13:09:55 -0500265#if !defined(MBEDTLS_RSA_NO_CRT)
266 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
267 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
268 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
269#endif
270
Hanno Becker617c1ae2017-08-23 14:11:24 +0100271 /*
272 * Check whether provided parameters are enough
273 * to deduce all others. The following incomplete
274 * parameter sets for private keys are supported:
275 *
276 * (1) P, Q missing.
277 * (2) D and potentially N missing.
278 *
279 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100280
Hanno Beckerddeeed72018-12-13 18:07:00 +0000281 n_missing = have_P && have_Q && have_D && have_E;
282 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
283 d_missing = have_P && have_Q && !have_D && have_E;
284 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100285
286 /* These three alternatives are mutually exclusive */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000287 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100288
Hanno Becker617c1ae2017-08-23 14:11:24 +0100289 if( !is_priv && !is_pub )
290 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
291
292 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100293 * Step 1: Deduce N if P, Q are provided.
294 */
295
296 if( !have_N && have_P && have_Q )
297 {
298 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
299 &ctx->Q ) ) != 0 )
300 {
301 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
302 }
303
304 ctx->len = mbedtls_mpi_size( &ctx->N );
305 }
306
307 /*
308 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100309 */
310
311 if( pq_missing )
312 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100313 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100314 &ctx->P, &ctx->Q );
315 if( ret != 0 )
316 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
317
318 }
319 else if( d_missing )
320 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100321 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
322 &ctx->Q,
323 &ctx->E,
324 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100325 {
326 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
327 }
328 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100329
Hanno Becker617c1ae2017-08-23 14:11:24 +0100330 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100331 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100332 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100333 */
334
Hanno Becker23344b52017-08-23 07:43:27 +0100335#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd32b6e692020-01-29 13:09:55 -0500336 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100337 {
338 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
339 &ctx->DP, &ctx->DQ, &ctx->QP );
340 if( ret != 0 )
341 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
342 }
Hanno Becker23344b52017-08-23 07:43:27 +0100343#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100344
345 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100346 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100347 */
348
Hanno Beckerebd2c022017-10-12 10:54:53 +0100349 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100350}
351
Hanno Becker617c1ae2017-08-23 14:11:24 +0100352int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
353 unsigned char *N, size_t N_len,
354 unsigned char *P, size_t P_len,
355 unsigned char *Q, size_t Q_len,
356 unsigned char *D, size_t D_len,
357 unsigned char *E, size_t E_len )
358{
359 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000360 int is_priv;
361 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100362
363 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000364 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100365 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
366 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
367 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
368 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
369 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
370
371 if( !is_priv )
372 {
373 /* If we're trying to export private parameters for a public key,
374 * something must be wrong. */
375 if( P != NULL || Q != NULL || D != NULL )
376 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
377
378 }
379
380 if( N != NULL )
381 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
382
383 if( P != NULL )
384 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
385
386 if( Q != NULL )
387 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
388
389 if( D != NULL )
390 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
391
392 if( E != NULL )
393 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100394
395cleanup:
396
397 return( ret );
398}
399
Hanno Becker617c1ae2017-08-23 14:11:24 +0100400int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
401 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
402 mbedtls_mpi *D, mbedtls_mpi *E )
403{
404 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000405 int is_priv;
406 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100407
408 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000409 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100410 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
411 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
412 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
413 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
414 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
415
416 if( !is_priv )
417 {
418 /* If we're trying to export private parameters for a public key,
419 * something must be wrong. */
420 if( P != NULL || Q != NULL || D != NULL )
421 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
422
423 }
424
425 /* Export all requested core parameters. */
426
427 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
428 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
429 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
430 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
431 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
432 {
433 return( ret );
434 }
435
436 return( 0 );
437}
438
439/*
440 * Export CRT parameters
441 * This must also be implemented if CRT is not used, for being able to
442 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
443 * can be used in this case.
444 */
445int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
446 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
447{
448 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000449 int is_priv;
450 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100451
452 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000453 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100454 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
455 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
456 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
457 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
458 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
459
460 if( !is_priv )
461 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
462
Hanno Beckerdc95c892017-08-23 06:57:02 +0100463#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100464 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100465 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
466 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
467 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
468 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100469 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100470 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100471#else
472 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
473 DP, DQ, QP ) ) != 0 )
474 {
475 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
476 }
477#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100478
479 return( 0 );
480}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100481
Paul Bakker5121ce52009-01-03 21:22:43 +0000482/*
483 * Initialize an RSA context
484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000487 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000488{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000489 RSA_VALIDATE( ctx != NULL );
490 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
491 padding == MBEDTLS_RSA_PKCS_V21 );
492
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200493 mbedtls_platform_memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#if defined(MBEDTLS_THREADING_C)
498 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200499#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000500}
501
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100502/*
503 * Set padding for an existing RSA context
504 */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000505void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
506 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100507{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000508 RSA_VALIDATE( ctx != NULL );
509 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
510 padding == MBEDTLS_RSA_PKCS_V21 );
511
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100512 ctx->padding = padding;
513 ctx->hash_id = hash_id;
514}
515
Hanno Becker617c1ae2017-08-23 14:11:24 +0100516/*
517 * Get length in bytes of RSA modulus
518 */
519
520size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
521{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100522 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100523}
524
525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528/*
529 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800530 *
531 * This generation method follows the RSA key pair generation procedure of
532 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000535 int (*f_rng)(void *, unsigned char *, size_t),
536 void *p_rng,
537 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000538{
539 int ret;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800540 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100541 int prime_quality = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000542 RSA_VALIDATE_RET( ctx != NULL );
543 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Hanno Beckerddeeed72018-12-13 18:07:00 +0000545 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
Janos Follathef441782016-09-21 13:18:12 +0100546 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
547
Janos Follathb8fc1b02018-09-03 15:37:01 +0100548 /*
549 * If the modulus is 1024 bit long or shorter, then the security strength of
550 * the RSA algorithm is less than or equal to 80 bits and therefore an error
551 * rate of 2^-80 is sufficient.
552 */
553 if( nbits > 1024 )
554 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
555
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100556 mbedtls_mpi_init( &H );
557 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800558 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 /*
561 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800562 * 1. |P-Q| > 2^( nbits / 2 - 100 )
563 * 2. GCD( E, (P-1)*(Q-1) ) == 1
564 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
568 do
569 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100570 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
571 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Janos Follathb8fc1b02018-09-03 15:37:01 +0100573 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
574 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800576 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
577 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
578 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 continue;
580
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800581 /* not required by any standards, but some users rely on the fact that P > Q */
582 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100583 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100584
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100585 /* Temporarily replace P,Q by P-1, Q-1 */
586 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
587 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
588 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800589
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800590 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800592 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
593 continue;
594
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800595 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Jethro Beekman97f95c92018-02-13 15:50:36 -0800596 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
597 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
598 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
599
600 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
601 continue;
602
603 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800605 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100607 /* Restore P,Q */
608 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
609 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
610
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800611 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
612
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100613 ctx->len = mbedtls_mpi_size( &ctx->N );
614
Jethro Beekman97f95c92018-02-13 15:50:36 -0800615#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 * DP = D mod (P - 1)
618 * DQ = D mod (Q - 1)
619 * QP = Q^-1 mod P
620 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100621 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
622 &ctx->DP, &ctx->DQ, &ctx->QP ) );
623#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
Hanno Becker83aad1f2017-08-23 06:45:10 +0100625 /* Double-check */
626 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
628cleanup:
629
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100630 mbedtls_mpi_free( &H );
631 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800632 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
634 if( ret != 0 )
635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_rsa_free( ctx );
637 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 }
639
Paul Bakker48377d92013-08-30 12:06:24 +0200640 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000641}
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
645/*
646 * Check a public RSA key
647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000649{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000650 RSA_VALIDATE_RET( ctx != NULL );
651
Hanno Beckerebd2c022017-10-12 10:54:53 +0100652 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000654
Hanno Becker3a760a12018-01-05 08:14:49 +0000655 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100658 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000659
Hanno Becker705fc682017-10-10 17:57:02 +0100660 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
661 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100665 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
667 return( 0 );
668}
669
670/*
Hanno Becker705fc682017-10-10 17:57:02 +0100671 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000674{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000675 RSA_VALIDATE_RET( ctx != NULL );
676
Hanno Becker705fc682017-10-10 17:57:02 +0100677 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100678 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 {
Hanno Becker98838b02017-10-02 13:16:10 +0100680 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 }
Paul Bakker48377d92013-08-30 12:06:24 +0200682
Hanno Becker98838b02017-10-02 13:16:10 +0100683 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100684 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100686 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000688
Hanno Beckerb269a852017-08-25 08:03:21 +0100689#if !defined(MBEDTLS_RSA_NO_CRT)
690 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
691 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
692 {
693 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
694 }
695#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000696
697 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000698}
699
700/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100701 * Check if contexts holding a public and private key match
702 */
Hanno Becker98838b02017-10-02 13:16:10 +0100703int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
704 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100705{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000706 RSA_VALIDATE_RET( pub != NULL );
707 RSA_VALIDATE_RET( prv != NULL );
708
Hanno Becker98838b02017-10-02 13:16:10 +0100709 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100713 }
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
716 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100719 }
720
721 return( 0 );
722}
723
724/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 * Do an RSA public key operation
726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000728 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 unsigned char *output )
730{
Paul Bakker23986e52011-04-24 08:57:21 +0000731 int ret;
732 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_mpi T;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000734 RSA_VALIDATE_RET( ctx != NULL );
735 RSA_VALIDATE_RET( input != NULL );
736 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
Hanno Beckerebd2c022017-10-12 10:54:53 +0100738 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100739 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200743#if defined(MBEDTLS_THREADING_C)
744 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
745 return( ret );
746#endif
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200752 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
753 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 }
755
756 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
758 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
760cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200762 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
763 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100764#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
768 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
771 return( 0 );
772}
773
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200774/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200775 * Generate or update blinding values, see section 10 of:
776 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200777 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200778 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200779 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200780static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200781 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
782{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200783 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200784
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200785 if( ctx->Vf.p != NULL )
786 {
787 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
789 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
790 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
791 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200792
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200793 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200794 }
795
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200796 /* Unblinding value: Vf = random number, invertible mod N */
797 do {
798 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
802 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
803 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200804
805 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
807 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200808
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200809
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200810cleanup:
811 return( ret );
812}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200813
Paul Bakker5121ce52009-01-03 21:22:43 +0000814/*
Janos Follathe81102e2017-03-22 13:38:28 +0000815 * Exponent blinding supposed to prevent side-channel attacks using multiple
816 * traces of measurements to recover the RSA key. The more collisions are there,
817 * the more bits of the key can be recovered. See [3].
818 *
819 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
820 * observations on avarage.
821 *
822 * For example with 28 byte blinding to achieve 2 collisions the adversary has
823 * to make 2^112 observations on avarage.
824 *
825 * (With the currently (as of 2017 April) known best algorithms breaking 2048
826 * bit RSA requires approximately as much time as trying out 2^112 random keys.
827 * Thus in this sense with 28 byte blinding the security is not reduced by
828 * side-channel attacks like the one in [3])
829 *
830 * This countermeasure does not help if the key recovery is possible with a
831 * single trace.
832 */
833#define RSA_EXPONENT_BLINDING 28
834
835/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000836 * Do an RSA private key operation
837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200839 int (*f_rng)(void *, unsigned char *, size_t),
840 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000841 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 unsigned char *output )
843{
Paul Bakker23986e52011-04-24 08:57:21 +0000844 int ret;
845 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100846
847 /* Temporary holding the result */
848 mbedtls_mpi T;
849
850 /* Temporaries holding P-1, Q-1 and the
851 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000852 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100853
854#if !defined(MBEDTLS_RSA_NO_CRT)
855 /* Temporaries holding the results mod p resp. mod q. */
856 mbedtls_mpi TP, TQ;
857
858 /* Temporaries holding the blinded exponents for
859 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000860 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100861
862 /* Pointers to actual exponents to be used - either the unblinded
863 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000864 mbedtls_mpi *DP = &ctx->DP;
865 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100866#else
867 /* Temporary holding the blinded exponent (if used). */
868 mbedtls_mpi D_blind;
869
870 /* Pointer to actual exponent to be used - either the unblinded
871 * or the blinded one, depending on the presence of a PRNG. */
872 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100873#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100874
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100875 /* Temporaries holding the initial input and the double
876 * checked result; should be the same in the end. */
877 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000878
Hanno Beckerddeeed72018-12-13 18:07:00 +0000879 RSA_VALIDATE_RET( ctx != NULL );
880 RSA_VALIDATE_RET( input != NULL );
881 RSA_VALIDATE_RET( output != NULL );
882
Hanno Beckerebd2c022017-10-12 10:54:53 +0100883 if( rsa_check_context( ctx, 1 /* private key checks */,
884 f_rng != NULL /* blinding y/n */ ) != 0 )
885 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100886 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100887 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100888
Hanno Becker06811ce2017-05-03 15:10:34 +0100889#if defined(MBEDTLS_THREADING_C)
890 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
891 return( ret );
892#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000893
Hanno Becker06811ce2017-05-03 15:10:34 +0100894 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100895 mbedtls_mpi_init( &T );
896
897 mbedtls_mpi_init( &P1 );
898 mbedtls_mpi_init( &Q1 );
899 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000900
Janos Follathf9203b42017-03-22 15:13:15 +0000901 if( f_rng != NULL )
902 {
Janos Follathe81102e2017-03-22 13:38:28 +0000903#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000904 mbedtls_mpi_init( &D_blind );
905#else
906 mbedtls_mpi_init( &DP_blind );
907 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000908#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000909 }
Janos Follathe81102e2017-03-22 13:38:28 +0000910
Hanno Becker06811ce2017-05-03 15:10:34 +0100911#if !defined(MBEDTLS_RSA_NO_CRT)
912 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200913#endif
914
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100915 mbedtls_mpi_init( &I );
916 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100917
918 /* End of MPI initialization */
919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
921 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000922 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200923 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
924 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 }
926
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100927 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100928
Paul Bakkerf451bac2013-08-30 15:37:02 +0200929 if( f_rng != NULL )
930 {
931 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200932 * Blinding
933 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200934 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200935 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
936 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000938
Janos Follathe81102e2017-03-22 13:38:28 +0000939 /*
940 * Exponent blinding
941 */
942 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
943 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
944
Janos Follathf9203b42017-03-22 15:13:15 +0000945#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000946 /*
947 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
948 */
949 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
950 f_rng, p_rng ) );
951 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
952 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
953 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
954
955 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000956#else
957 /*
958 * DP_blind = ( P - 1 ) * R + DP
959 */
960 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
961 f_rng, p_rng ) );
962 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
963 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
964 &ctx->DP ) );
965
966 DP = &DP_blind;
967
968 /*
969 * DQ_blind = ( Q - 1 ) * R + DQ
970 */
971 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
972 f_rng, p_rng ) );
973 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
974 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
975 &ctx->DQ ) );
976
977 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000978#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200979 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000982 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100983#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200984 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000985 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100987 * TP = input ^ dP mod P
988 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100990
991 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
992 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
994 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100995 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100997 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
998 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
999 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
1001 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001002 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001004 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1005 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001007
Paul Bakkerf451bac2013-08-30 15:37:02 +02001008 if( f_rng != NULL )
1009 {
1010 /*
1011 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001012 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001013 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001014 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001016 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Hanno Becker2dec5e82017-10-03 07:49:52 +01001018 /* Verify the result to prevent glitching attacks. */
1019 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1020 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001021 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001022 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001023 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1024 goto cleanup;
1025 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001026
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
1030cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001032 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1033 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001034#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001035
Hanno Becker06811ce2017-05-03 15:10:34 +01001036 mbedtls_mpi_free( &P1 );
1037 mbedtls_mpi_free( &Q1 );
1038 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001039
1040 if( f_rng != NULL )
1041 {
Janos Follathe81102e2017-03-22 13:38:28 +00001042#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001043 mbedtls_mpi_free( &D_blind );
1044#else
1045 mbedtls_mpi_free( &DP_blind );
1046 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001047#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001048 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Hanno Becker06811ce2017-05-03 15:10:34 +01001050 mbedtls_mpi_free( &T );
1051
1052#if !defined(MBEDTLS_RSA_NO_CRT)
1053 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1054#endif
1055
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001056 mbedtls_mpi_free( &C );
1057 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001058
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001061
1062 return( 0 );
1063}
1064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001066/**
1067 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1068 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001069 * \param dst buffer to mask
1070 * \param dlen length of destination buffer
1071 * \param src source of the mask generation
1072 * \param slen length of the source buffer
1073 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001074 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001075static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001079 unsigned char counter[4];
1080 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001081 unsigned int hlen;
1082 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001083 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001084
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001085 mbedtls_platform_memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
1086 mbedtls_platform_memset( counter, 0, 4 );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001087
Hanno Beckerd3827c72019-09-03 12:56:37 +01001088 hlen = mbedtls_md_get_size( mbedtls_md_get_handle( md_ctx ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001089
Simon Butcher02037452016-03-01 21:19:12 +00001090 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001091 p = dst;
1092
1093 while( dlen > 0 )
1094 {
1095 use_len = hlen;
1096 if( dlen < hlen )
1097 use_len = dlen;
1098
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001099 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1100 goto exit;
1101 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1102 goto exit;
1103 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1104 goto exit;
1105 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1106 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001107
1108 for( i = 0; i < use_len; ++i )
1109 *p++ ^= mask[i];
1110
1111 counter[3]++;
1112
1113 dlen -= use_len;
1114 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001115
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001116exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001117 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001118
1119 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001120}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001124/*
1125 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001128 int (*f_rng)(void *, unsigned char *, size_t),
1129 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001130 int mode,
1131 const unsigned char *label, size_t label_len,
1132 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001133 const unsigned char *input,
1134 unsigned char *output )
1135{
1136 size_t olen;
1137 int ret;
1138 unsigned char *p = output;
1139 unsigned int hlen;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001140 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001142
Hanno Beckerddeeed72018-12-13 18:07:00 +00001143 RSA_VALIDATE_RET( ctx != NULL );
1144 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1145 mode == MBEDTLS_RSA_PUBLIC );
1146 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001147 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001148 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1151 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001152
1153 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001157 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001159
1160 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001162
Simon Butcher02037452016-03-01 21:19:12 +00001163 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001164 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001166
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001167 mbedtls_platform_memset( output, 0, olen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001168
1169 *p++ = 0;
1170
Simon Butcher02037452016-03-01 21:19:12 +00001171 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001172 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001174
1175 p += hlen;
1176
Simon Butcher02037452016-03-01 21:19:12 +00001177 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001178 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1179 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001180 p += hlen;
1181 p += olen - 2 * hlen - 2 - ilen;
1182 *p++ = 1;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001183 mbedtls_platform_memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001186 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001187 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001188
Simon Butcher02037452016-03-01 21:19:12 +00001189 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001190 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1191 &md_ctx ) ) != 0 )
1192 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001193
Simon Butcher02037452016-03-01 21:19:12 +00001194 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001195 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1196 &md_ctx ) ) != 0 )
1197 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001198
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001199exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001201
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001202 if( ret != 0 )
1203 return( ret );
1204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 return( ( mode == MBEDTLS_RSA_PUBLIC )
1206 ? mbedtls_rsa_public( ctx, output, output )
1207 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001212/*
1213 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001216 int (*f_rng)(void *, unsigned char *, size_t),
1217 void *p_rng,
1218 int mode, size_t ilen,
1219 const unsigned char *input,
1220 unsigned char *output )
1221{
1222 size_t nb_pad, olen;
1223 int ret;
1224 unsigned char *p = output;
1225
Hanno Beckerddeeed72018-12-13 18:07:00 +00001226 RSA_VALIDATE_RET( ctx != NULL );
1227 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1228 mode == MBEDTLS_RSA_PUBLIC );
1229 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001230 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1233 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001234
Paul Bakkerb3869132013-02-28 17:21:01 +01001235 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001236
Simon Butcher02037452016-03-01 21:19:12 +00001237 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001238 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001240
1241 nb_pad = olen - 3 - ilen;
1242
1243 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001245 {
Hanno Beckerb86e6842018-12-18 14:46:04 +00001246 if( f_rng == NULL )
1247 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001250
1251 while( nb_pad-- > 0 )
1252 {
1253 int rng_dl = 100;
1254
1255 do {
1256 ret = f_rng( p_rng, p, 1 );
1257 } while( *p == 0 && --rng_dl && ret == 0 );
1258
Simon Butcher02037452016-03-01 21:19:12 +00001259 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001260 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001262
1263 p++;
1264 }
1265 }
1266 else
1267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001269
1270 while( nb_pad-- > 0 )
1271 *p++ = 0xFF;
1272 }
1273
1274 *p++ = 0;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001275 mbedtls_platform_memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 return( ( mode == MBEDTLS_RSA_PUBLIC )
1278 ? mbedtls_rsa_public( ctx, output, output )
1279 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001282
Paul Bakker5121ce52009-01-03 21:22:43 +00001283/*
1284 * Add the message padding, then do an RSA operation
1285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001287 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001288 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001289 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001290 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 unsigned char *output )
1292{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001293 RSA_VALIDATE_RET( ctx != NULL );
1294 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1295 mode == MBEDTLS_RSA_PUBLIC );
1296 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001297 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001298
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 switch( ctx->padding )
1300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_PKCS1_V15)
1302 case MBEDTLS_RSA_PKCS_V15:
1303 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001304 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001305#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#if defined(MBEDTLS_PKCS1_V21)
1308 case MBEDTLS_RSA_PKCS_V21:
1309 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001310 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001311#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001312
1313 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001316}
1317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001319/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001320 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001323 int (*f_rng)(void *, unsigned char *, size_t),
1324 void *p_rng,
1325 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001326 const unsigned char *label, size_t label_len,
1327 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001328 const unsigned char *input,
1329 unsigned char *output,
1330 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001331{
Paul Bakker23986e52011-04-24 08:57:21 +00001332 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001333 size_t ilen, i, pad_len;
1334 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1336 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001337 unsigned int hlen;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001338 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001340
Hanno Beckerddeeed72018-12-13 18:07:00 +00001341 RSA_VALIDATE_RET( ctx != NULL );
1342 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1343 mode == MBEDTLS_RSA_PUBLIC );
1344 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1345 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1346 RSA_VALIDATE_RET( input != NULL );
1347 RSA_VALIDATE_RET( olen != NULL );
1348
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001349 /*
1350 * Parameters sanity checks
1351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1353 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354
1355 ilen = ctx->len;
1356
Paul Bakker27fdf462011-06-09 13:55:13 +00001357 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001361 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001363
Janos Follathc17cda12016-02-11 11:08:18 +00001364 hlen = mbedtls_md_get_size( md_info );
1365
1366 // checking for integer underflow
1367 if( 2 * hlen + 2 > ilen )
1368 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1369
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001370 /*
1371 * RSA operation
1372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1374 ? mbedtls_rsa_public( ctx, input, buf )
1375 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
1377 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001378 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001379
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001380 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001381 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001384 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1385 {
1386 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001387 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001388 }
1389
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001390 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001391 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1392 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001393 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001394 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1395 &md_ctx ) ) != 0 )
1396 {
1397 mbedtls_md_free( &md_ctx );
1398 goto cleanup;
1399 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001402
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001403 /* Generate lHash */
1404 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1405 goto cleanup;
1406
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001407 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001408 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001409 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001411 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001413 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001414
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001415 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001416
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001417 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001418 for( i = 0; i < hlen; i++ )
1419 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001420
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001421 /* Get zero-padding len, but always read till end of buffer
1422 * (minus one, for the 01 byte) */
1423 pad_len = 0;
1424 pad_done = 0;
1425 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1426 {
1427 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001428 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001429 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001430
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001431 p += pad_len;
1432 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001433
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001434 /*
1435 * The only information "leaked" is whether the padding was correct or not
1436 * (eg, no data is copied if it was not correct). This meets the
1437 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1438 * the different error conditions.
1439 */
1440 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001441 {
1442 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1443 goto cleanup;
1444 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
Paul Bakker66d5d072014-06-17 16:39:18 +02001446 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001447 {
1448 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1449 goto cleanup;
1450 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001451
1452 *olen = ilen - (p - buf);
Teppo Järvelin91d79382019-10-02 09:09:31 +03001453 mbedtls_platform_memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001454 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001455
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001456cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001457 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1458 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001459
1460 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001465/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1466 *
1467 * \param value The value to analyze.
Gilles Peskine331d80e2018-10-04 18:32:29 +02001468 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001469 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001470static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001471{
1472 /* MSVC has a warning about unary minus on unsigned, but this is
1473 * well-defined and precisely what we want to do here */
1474#if defined(_MSC_VER)
1475#pragma warning( push )
1476#pragma warning( disable : 4146 )
1477#endif
1478 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1479#if defined(_MSC_VER)
1480#pragma warning( pop )
1481#endif
1482}
1483
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001484/** Check whether a size is out of bounds, without branches.
1485 *
1486 * This is equivalent to `size > max`, but is likely to be compiled to
1487 * to code using bitwise operation rather than a branch.
1488 *
1489 * \param size Size to check.
1490 * \param max Maximum desired value for \p size.
1491 * \return \c 0 if `size <= max`.
1492 * \return \c 1 if `size > max`.
1493 */
1494static unsigned size_greater_than( size_t size, size_t max )
1495{
1496 /* Return the sign bit (1 for negative) of (max - size). */
1497 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1498}
1499
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001500/** Choose between two integer values, without branches.
1501 *
Gilles Peskine331d80e2018-10-04 18:32:29 +02001502 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1503 * to code using bitwise operation rather than a branch.
1504 *
1505 * \param cond Condition to test.
1506 * \param if1 Value to use if \p cond is nonzero.
1507 * \param if0 Value to use if \p cond is zero.
1508 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001509 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001510static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001511{
Gilles Peskine331d80e2018-10-04 18:32:29 +02001512 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001513 return( ( mask & if1 ) | (~mask & if0 ) );
1514}
1515
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001516/** Shift some data towards the left inside a buffer without leaking
1517 * the length of the data through side channels.
1518 *
1519 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1520 * ```
1521 * memmove(start, start + offset, total - offset);
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001522 * mbedtls_platform_memset(start + offset, 0, total - offset);
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001523 * ```
1524 * but it strives to use a memory access pattern (and thus total timing)
1525 * that does not depend on \p offset. This timing independence comes at
1526 * the expense of performance.
1527 *
1528 * \param start Pointer to the start of the buffer.
1529 * \param total Total size of the buffer.
1530 * \param offset Offset from which to copy \p total - \p offset bytes.
1531 */
1532static void mem_move_to_left( void *start,
1533 size_t total,
1534 size_t offset )
1535{
1536 volatile unsigned char *buf = start;
1537 size_t i, n;
1538 if( total == 0 )
1539 return;
1540 for( i = 0; i < total; i++ )
1541 {
1542 unsigned no_op = size_greater_than( total - offset, i );
1543 /* The first `total - offset` passes are a no-op. The last
1544 * `offset` passes shift the data one byte to the left and
1545 * zero out the last byte. */
1546 for( n = 0; n < total - 1; n++ )
Gilles Peskine9b430702018-10-12 19:15:34 +02001547 {
1548 unsigned char current = buf[n];
1549 unsigned char next = buf[n+1];
1550 buf[n] = if_int( no_op, current, next );
1551 }
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001552 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1553 }
1554}
1555
Paul Bakkerb3869132013-02-28 17:21:01 +01001556/*
1557 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001560 int (*f_rng)(void *, unsigned char *, size_t),
1561 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001562 int mode, size_t *olen,
1563 const unsigned char *input,
1564 unsigned char *output,
Gilles Peskine5908dd42018-10-02 22:43:06 +02001565 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001566{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001567 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001568 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine85a74422018-10-05 14:50:21 +02001570 /* The following variables take sensitive values: their value must
1571 * not leak into the observable behavior of the function other than
1572 * the designated outputs (output, olen, return value). Otherwise
1573 * this would open the execution of the function to
1574 * side-channel-based variants of the Bleichenbacher padding oracle
1575 * attack. Potential side channels include overall timing, memory
1576 * access patterns (especially visible to an adversary who has access
1577 * to a shared memory cache), and branches (especially visible to
1578 * an adversary who has access to a shared code cache or to a shared
1579 * branch predictor). */
1580 size_t pad_count = 0;
1581 unsigned bad = 0;
1582 unsigned char pad_done = 0;
1583 size_t plaintext_size = 0;
1584 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001585
Hanno Beckerddeeed72018-12-13 18:07:00 +00001586 RSA_VALIDATE_RET( ctx != NULL );
1587 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1588 mode == MBEDTLS_RSA_PUBLIC );
1589 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1590 RSA_VALIDATE_RET( input != NULL );
1591 RSA_VALIDATE_RET( olen != NULL );
1592
1593 ilen = ctx->len;
1594 plaintext_max_size = ( output_max_len > ilen - 11 ?
1595 ilen - 11 :
1596 output_max_len );
1597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1599 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001600
Paul Bakkerb3869132013-02-28 17:21:01 +01001601 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1605 ? mbedtls_rsa_public( ctx, input, buf )
1606 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001607
1608 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001609 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001610
Gilles Peskine40b57f42018-10-05 15:06:12 +02001611 /* Check and get padding length in constant time and constant
1612 * memory trace. The first byte must be 0. */
1613 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001617 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1618 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001619 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001620
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001621 /* Read the whole buffer. Set pad_done to nonzero if we find
1622 * the 0x00 byte and remember the padding length in pad_count. */
1623 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001624 {
Gilles Peskine85a74422018-10-05 14:50:21 +02001625 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001626 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001627 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001628 }
1629 else
1630 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001631 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1632 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001633 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001634
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001635 /* Read the whole buffer. Set pad_done to nonzero if we find
1636 * the 0x00 byte and remember the padding length in pad_count.
1637 * If there's a non-0xff byte in the padding, the padding is bad. */
1638 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001639 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001640 pad_done |= if_int( buf[i], 0, 1 );
1641 pad_count += if_int( pad_done, 0, 1 );
1642 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001643 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001644 }
1645
Gilles Peskine40b57f42018-10-05 15:06:12 +02001646 /* If pad_done is still zero, there's no data, only unfinished padding. */
1647 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001648
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001649 /* There must be at least 8 bytes of padding. */
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001650 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001651
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001652 /* If the padding is valid, set plaintext_size to the number of
1653 * remaining bytes after stripping the padding. If the padding
1654 * is invalid, avoid leaking this fact through the size of the
1655 * output: use the maximum message size that fits in the output
1656 * buffer. Do it without branches to avoid leaking the padding
1657 * validity through timing. RSA keys are small enough that all the
1658 * size_t values involved fit in unsigned int. */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001659 plaintext_size = if_int( bad,
1660 (unsigned) plaintext_max_size,
Gilles Peskine85a74422018-10-05 14:50:21 +02001661 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001662
Gilles Peskine9265ff42018-10-04 19:13:43 +02001663 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001664 * buffer and to 1 otherwise. */
1665 output_too_large = size_greater_than( plaintext_size,
1666 plaintext_max_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Gilles Peskine9265ff42018-10-04 19:13:43 +02001668 /* Set ret without branches to avoid timing attacks. Return:
1669 * - INVALID_PADDING if the padding is bad (bad != 0).
1670 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1671 * plaintext does not fit in the output buffer.
1672 * - 0 if the padding is correct. */
Gilles Peskine48992472018-10-12 19:19:12 +02001673 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1674 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1675 0 ) );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001676
Gilles Peskine9265ff42018-10-04 19:13:43 +02001677 /* If the padding is bad or the plaintext is too large, zero the
1678 * data that we're about to copy to the output buffer.
1679 * We need to copy the same amount of data
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001680 * from the same buffer whether the padding is good or not to
1681 * avoid leaking the padding validity through overall timing or
1682 * through memory or cache access patterns. */
Gilles Peskine40b57f42018-10-05 15:06:12 +02001683 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001684 for( i = 11; i < ilen; i++ )
Gilles Peskine40b57f42018-10-05 15:06:12 +02001685 buf[i] &= ~bad;
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001686
Gilles Peskine9265ff42018-10-04 19:13:43 +02001687 /* If the plaintext is too large, truncate it to the buffer size.
1688 * Copy anyway to avoid revealing the length through timing, because
1689 * revealing the length is as bad as revealing the padding validity
1690 * for a Bleichenbacher attack. */
1691 plaintext_size = if_int( output_too_large,
1692 (unsigned) plaintext_max_size,
1693 (unsigned) plaintext_size );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001694
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001695 /* Move the plaintext to the leftmost position where it can start in
1696 * the working buffer, i.e. make it start plaintext_max_size from
1697 * the end of the buffer. Do this with a memory access trace that
1698 * does not depend on the plaintext size. After this move, the
1699 * starting location of the plaintext is no longer sensitive
1700 * information. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001701 mem_move_to_left( buf + ilen - plaintext_max_size,
1702 plaintext_max_size,
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001703 plaintext_max_size - plaintext_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001704
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001705 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskine9265ff42018-10-04 19:13:43 +02001706 * into the output buffer. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03001707 mbedtls_platform_memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001708
1709 /* Report the amount of data we copied to the output buffer. In case
1710 * of errors (bad padding or output too large), the value of *olen
1711 * when this function returns is not specified. Making it equivalent
1712 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001713 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001714
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001715cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001716 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001717
1718 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001719}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001721
1722/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001723 * Do an RSA operation, then remove the message padding
1724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001726 int (*f_rng)(void *, unsigned char *, size_t),
1727 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001728 int mode, size_t *olen,
1729 const unsigned char *input,
1730 unsigned char *output,
1731 size_t output_max_len)
1732{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001733 RSA_VALIDATE_RET( ctx != NULL );
1734 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1735 mode == MBEDTLS_RSA_PUBLIC );
1736 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1737 RSA_VALIDATE_RET( input != NULL );
1738 RSA_VALIDATE_RET( olen != NULL );
1739
Paul Bakkerb3869132013-02-28 17:21:01 +01001740 switch( ctx->padding )
1741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#if defined(MBEDTLS_PKCS1_V15)
1743 case MBEDTLS_RSA_PKCS_V15:
1744 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001745 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001746#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748#if defined(MBEDTLS_PKCS1_V21)
1749 case MBEDTLS_RSA_PKCS_V21:
1750 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001751 olen, input, output,
1752 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001753#endif
1754
1755 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001757 }
1758}
1759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001761/*
1762 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001765 int (*f_rng)(void *, unsigned char *, size_t),
1766 void *p_rng,
1767 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001769 unsigned int hashlen,
1770 const unsigned char *hash,
1771 unsigned char *sig )
1772{
1773 size_t olen;
1774 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001776 size_t slen, min_slen, hlen, offset = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001777 int ret;
1778 size_t msb;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001779 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 mbedtls_md_context_t md_ctx;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001781 RSA_VALIDATE_RET( ctx != NULL );
1782 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1783 mode == MBEDTLS_RSA_PUBLIC );
1784 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1785 hashlen == 0 ) ||
1786 hash != NULL );
1787 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1790 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001791
1792 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001794
1795 olen = ctx->len;
1796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001798 {
Simon Butcher02037452016-03-01 21:19:12 +00001799 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 md_info = mbedtls_md_info_from_type( md_alg );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001801 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001805 }
1806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001808 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001812
Jaeden Amero3725bb22018-09-07 19:12:36 +01001813 /* Calculate the largest possible salt length. Normally this is the hash
1814 * length, which is the maximum length the salt can have. If there is not
1815 * enough room, use the maximum salt length that fits. The constraint is
1816 * that the hash length plus the salt length plus 2 bytes must be at most
1817 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1818 * (PKCS#1 v2.2) §9.1.1 step 3. */
1819 min_slen = hlen - 2;
1820 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001822 else if( olen >= hlen + hlen + 2 )
1823 slen = hlen;
1824 else
1825 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001826
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001827 mbedtls_platform_memset( sig, 0, olen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001828
Simon Butcher02037452016-03-01 21:19:12 +00001829 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001830 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001832
Simon Butcher02037452016-03-01 21:19:12 +00001833 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001834 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001835 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001836 *p++ = 0x01;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001837 mbedtls_platform_memcpy( p, salt, slen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001838 p += slen;
1839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001841 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001842 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001843
Simon Butcher02037452016-03-01 21:19:12 +00001844 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001845 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1846 goto exit;
1847 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1848 goto exit;
1849 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1850 goto exit;
1851 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1852 goto exit;
1853 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1854 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001855
Simon Butcher02037452016-03-01 21:19:12 +00001856 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001857 if( msb % 8 == 0 )
1858 offset = 1;
1859
Simon Butcher02037452016-03-01 21:19:12 +00001860 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001861 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1862 &md_ctx ) ) != 0 )
1863 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001864
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001865 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001866 sig[0] &= 0xFF >> ( olen * 8 - msb );
1867
1868 p += hlen;
1869 *p++ = 0xBC;
1870
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001871 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001872
1873exit:
1874 mbedtls_md_free( &md_ctx );
1875
1876 if( ret != 0 )
1877 return( ret );
1878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 return( ( mode == MBEDTLS_RSA_PUBLIC )
1880 ? mbedtls_rsa_public( ctx, sig, sig )
1881 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001882}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001886/*
1887 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1888 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001889
1890/* Construct a PKCS v1.5 encoding of a hashed message
1891 *
1892 * This is used both for signature generation and verification.
1893 *
1894 * Parameters:
1895 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001896 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001897 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001898 * - hash: Buffer containing the hashed message or the raw data.
1899 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001900 * - dst: Buffer to hold the encoded message.
1901 *
1902 * Assumptions:
1903 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1904 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001905 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001906 *
1907 */
1908static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1909 unsigned int hashlen,
1910 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001911 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001912 unsigned char *dst )
1913{
1914 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001915 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001916 unsigned char *p = dst;
1917 const char *oid = NULL;
1918
1919 /* Are we signing hashed or raw data? */
1920 if( md_alg != MBEDTLS_MD_NONE )
1921 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001922 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
1923 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001924 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1925
1926 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1927 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1928
1929 hashlen = mbedtls_md_get_size( md_info );
1930
1931 /* Double-check that 8 + hashlen + oid_size can be used as a
1932 * 1-byte ASN.1 length encoding and that there's no overflow. */
1933 if( 8 + hashlen + oid_size >= 0x80 ||
1934 10 + hashlen < hashlen ||
1935 10 + hashlen + oid_size < 10 + hashlen )
1936 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1937
1938 /*
1939 * Static bounds check:
1940 * - Need 10 bytes for five tag-length pairs.
1941 * (Insist on 1-byte length encodings to protect against variants of
1942 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1943 * - Need hashlen bytes for hash
1944 * - Need oid_size bytes for hash alg OID.
1945 */
1946 if( nb_pad < 10 + hashlen + oid_size )
1947 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1948 nb_pad -= 10 + hashlen + oid_size;
1949 }
1950 else
1951 {
1952 if( nb_pad < hashlen )
1953 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1954
1955 nb_pad -= hashlen;
1956 }
1957
Hanno Becker2b2f8982017-09-27 17:10:03 +01001958 /* Need space for signature header and padding delimiter (3 bytes),
1959 * and 8 bytes for the minimal padding */
1960 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001961 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1962 nb_pad -= 3;
1963
1964 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001965 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001966
1967 /* Write signature header and padding */
1968 *p++ = 0;
1969 *p++ = MBEDTLS_RSA_SIGN;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001970 mbedtls_platform_memset( p, 0xFF, nb_pad );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001971 p += nb_pad;
1972 *p++ = 0;
1973
1974 /* Are we signing raw data? */
1975 if( md_alg == MBEDTLS_MD_NONE )
1976 {
Piotr Nowicki305a5ec2020-08-10 17:42:18 +02001977 if( mbedtls_platform_memcpy( p, hash, hashlen ) == p )
1978 {
1979 return( 0 );
1980 }
1981 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001982 }
1983
1984 /* Signing hashed data, add corresponding ASN.1 structure
1985 *
1986 * DigestInfo ::= SEQUENCE {
1987 * digestAlgorithm DigestAlgorithmIdentifier,
1988 * digest Digest }
1989 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1990 * Digest ::= OCTET STRING
1991 *
1992 * Schematic:
1993 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1994 * TAG-NULL + LEN [ NULL ] ]
1995 * TAG-OCTET + LEN [ HASH ] ]
1996 */
1997 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001998 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001999 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002000 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002001 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002002 *p++ = (unsigned char) oid_size;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002003 mbedtls_platform_memcpy( p, oid, oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002004 p += oid_size;
2005 *p++ = MBEDTLS_ASN1_NULL;
2006 *p++ = 0x00;
2007 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002008 *p++ = (unsigned char) hashlen;
Piotr Nowicki305a5ec2020-08-10 17:42:18 +02002009 if( mbedtls_platform_memcpy( p, hash, hashlen ) != p )
2010 {
2011 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2012 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002013 p += hashlen;
2014
2015 /* Just a sanity-check, should be automatic
2016 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002017 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002018 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002019 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002020 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2021 }
2022
2023 return( 0 );
2024}
2025
Paul Bakkerb3869132013-02-28 17:21:01 +01002026/*
2027 * Do an RSA operation to sign the message digest
2028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002030 int (*f_rng)(void *, unsigned char *, size_t),
2031 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002032 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002034 unsigned int hashlen,
2035 const unsigned char *hash,
2036 unsigned char *sig )
2037{
Piotr Nowicki305a5ec2020-08-10 17:42:18 +02002038 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002039 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002040
Hanno Beckerddeeed72018-12-13 18:07:00 +00002041 RSA_VALIDATE_RET( ctx != NULL );
2042 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2043 mode == MBEDTLS_RSA_PUBLIC );
2044 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2045 hashlen == 0 ) ||
2046 hash != NULL );
2047 RSA_VALIDATE_RET( sig != NULL );
2048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2050 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002051
Hanno Beckerfdf38032017-09-06 12:35:55 +01002052 /*
2053 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2054 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002055
Hanno Beckerfdf38032017-09-06 12:35:55 +01002056 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2057 ctx->len, sig ) ) != 0 )
2058 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002059
2060 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002061 * Call respective RSA primitive
2062 */
2063
2064 if( mode == MBEDTLS_RSA_PUBLIC )
2065 {
2066 /* Skip verification on a public key operation */
2067 return( mbedtls_rsa_public( ctx, sig, sig ) );
2068 }
2069
2070 /* Private key operation
2071 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002072 * In order to prevent Lenstra's attack, make the signature in a
2073 * temporary buffer and check it before returning it.
2074 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002075
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002076 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002077 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002078 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2079
Hanno Beckerfdf38032017-09-06 12:35:55 +01002080 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002081 if( verif == NULL )
2082 {
2083 mbedtls_free( sig_try );
2084 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2085 }
2086
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002087 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2088 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2089
Hanno Becker171a8f12017-09-06 12:32:16 +01002090 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002091 {
2092 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2093 goto cleanup;
2094 }
2095
Piotr Nowicki305a5ec2020-08-10 17:42:18 +02002096 if( mbedtls_platform_memcpy( sig, sig_try, ctx->len ) != sig )
2097 {
2098 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
2099 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002100
2101cleanup:
2102 mbedtls_free( sig_try );
2103 mbedtls_free( verif );
2104
2105 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002106}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002108
2109/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 * Do an RSA operation to sign the message digest
2111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002113 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002114 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002117 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002118 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 unsigned char *sig )
2120{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002121 RSA_VALIDATE_RET( ctx != NULL );
2122 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2123 mode == MBEDTLS_RSA_PUBLIC );
2124 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2125 hashlen == 0 ) ||
2126 hash != NULL );
2127 RSA_VALIDATE_RET( sig != NULL );
2128
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 switch( ctx->padding )
2130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131#if defined(MBEDTLS_PKCS1_V15)
2132 case MBEDTLS_RSA_PKCS_V15:
2133 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002134 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002135#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137#if defined(MBEDTLS_PKCS1_V21)
2138 case MBEDTLS_RSA_PKCS_V21:
2139 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002140 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002141#endif
2142
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002146}
2147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002149/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002150 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002153 int (*f_rng)(void *, unsigned char *, size_t),
2154 void *p_rng,
2155 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002157 unsigned int hashlen,
2158 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002160 int expected_salt_len,
2161 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002162{
Paul Bakker23986e52011-04-24 08:57:21 +00002163 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002164 size_t siglen;
2165 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002166 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002168 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002169 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002170 size_t observed_salt_len, msb;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01002171 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002173 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002174
Hanno Beckerddeeed72018-12-13 18:07:00 +00002175 RSA_VALIDATE_RET( ctx != NULL );
2176 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2177 mode == MBEDTLS_RSA_PUBLIC );
2178 RSA_VALIDATE_RET( sig != NULL );
2179 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2180 hashlen == 0 ) ||
2181 hash != NULL );
2182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002185
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 siglen = ctx->len;
2187
Paul Bakker27fdf462011-06-09 13:55:13 +00002188 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2192 ? mbedtls_rsa_public( ctx, sig, buf )
2193 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002194
2195 if( ret != 0 )
2196 return( ret );
2197
2198 p = buf;
2199
Paul Bakkerb3869132013-02-28 17:21:01 +01002200 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 {
Simon Butcher02037452016-03-01 21:19:12 +00002205 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 md_info = mbedtls_md_info_from_type( md_alg );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01002207 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002211 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01002214 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002218
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002219 mbedtls_platform_memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002220
Simon Butcher02037452016-03-01 21:19:12 +00002221 /*
2222 * Note: EMSA-PSS verification is over the length of N - 1 bits
2223 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002224 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002225
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002226 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2227 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2228
Simon Butcher02037452016-03-01 21:19:12 +00002229 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002230 if( msb % 8 == 0 )
2231 {
2232 p++;
2233 siglen -= 1;
2234 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002235
Gilles Peskine139108a2017-10-18 19:03:42 +02002236 if( siglen < hlen + 2 )
2237 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2238 hash_start = p + siglen - hlen - 1;
2239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002241 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002242 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002243
Jaeden Amero66954e12018-01-25 16:05:54 +00002244 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2245 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002246 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002247
Paul Bakkerb3869132013-02-28 17:21:01 +01002248 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002249
Gilles Peskine6a54b022017-10-17 19:02:13 +02002250 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002251 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002252
Gilles Peskine91048a32017-10-19 17:46:14 +02002253 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002254 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002255 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2256 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002257 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002258
Gilles Peskine6a54b022017-10-17 19:02:13 +02002259 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002262 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002263 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002264 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2265 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002266 }
2267
Simon Butcher02037452016-03-01 21:19:12 +00002268 /*
2269 * Generate H = Hash( M' )
2270 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002271 ret = mbedtls_md_starts( &md_ctx );
2272 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002273 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002274 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2275 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002276 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002277 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2278 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002279 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002280 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2281 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002282 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002283 ret = mbedtls_md_finish( &md_ctx, result );
2284 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002285 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002286
Piotr Nowickie3c4ee52020-06-23 12:59:56 +02002287 if( mbedtls_platform_memequal( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002288 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002289 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002290 goto exit;
2291 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002292
2293exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002295
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002296 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002297}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002298
2299/*
2300 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002303 int (*f_rng)(void *, unsigned char *, size_t),
2304 void *p_rng,
2305 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002307 unsigned int hashlen,
2308 const unsigned char *hash,
2309 const unsigned char *sig )
2310{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002311 mbedtls_md_type_t mgf1_hash_id;
2312 RSA_VALIDATE_RET( ctx != NULL );
2313 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2314 mode == MBEDTLS_RSA_PUBLIC );
2315 RSA_VALIDATE_RET( sig != NULL );
2316 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2317 hashlen == 0 ) ||
2318 hash != NULL );
2319
2320 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002322 : md_alg;
2323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002325 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002327 sig ) );
2328
2329}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002333/*
2334 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002337 int (*f_rng)(void *, unsigned char *, size_t),
2338 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002339 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002341 unsigned int hashlen,
2342 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002343 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002344{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002345 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002346 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002347 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002348
Hanno Beckerddeeed72018-12-13 18:07:00 +00002349 RSA_VALIDATE_RET( ctx != NULL );
2350 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2351 mode == MBEDTLS_RSA_PUBLIC );
2352 RSA_VALIDATE_RET( sig != NULL );
2353 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2354 hashlen == 0 ) ||
2355 hash != NULL );
2356
2357 sig_len = ctx->len;
2358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2360 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002361
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002362 /*
2363 * Prepare expected PKCS1 v1.5 encoding of hash.
2364 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002365
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002366 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2367 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2368 {
2369 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2370 goto cleanup;
2371 }
2372
2373 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2374 encoded_expected ) ) != 0 )
2375 goto cleanup;
2376
2377 /*
2378 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2379 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002382 ? mbedtls_rsa_public( ctx, sig, encoded )
2383 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002384 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002385 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002386
Simon Butcher02037452016-03-01 21:19:12 +00002387 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002388 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002389 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002390
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002391 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2392 sig_len ) ) != 0 )
2393 {
2394 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2395 goto cleanup;
2396 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002397
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002398cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002399
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002400 if( encoded != NULL )
2401 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002402 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002403 mbedtls_free( encoded );
2404 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002405
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002406 if( encoded_expected != NULL )
2407 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002408 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002409 mbedtls_free( encoded_expected );
2410 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002411
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002412 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
2416/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002417 * Do an RSA operation and check the message digest
2418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002420 int (*f_rng)(void *, unsigned char *, size_t),
2421 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002422 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002424 unsigned int hashlen,
2425 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002426 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002427{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002428 RSA_VALIDATE_RET( ctx != NULL );
2429 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2430 mode == MBEDTLS_RSA_PUBLIC );
2431 RSA_VALIDATE_RET( sig != NULL );
2432 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2433 hashlen == 0 ) ||
2434 hash != NULL );
2435
Paul Bakkerb3869132013-02-28 17:21:01 +01002436 switch( ctx->padding )
2437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438#if defined(MBEDTLS_PKCS1_V15)
2439 case MBEDTLS_RSA_PKCS_V15:
2440 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002441 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002442#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#if defined(MBEDTLS_PKCS1_V21)
2445 case MBEDTLS_RSA_PKCS_V21:
2446 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002447 hashlen, hash, sig );
2448#endif
2449
2450 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002452 }
2453}
2454
2455/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002456 * Copy the components of an RSA key
2457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002459{
2460 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002461 RSA_VALIDATE_RET( dst != NULL );
2462 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002463
2464 dst->ver = src->ver;
2465 dst->len = src->len;
2466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2468 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2471 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2472 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002473
2474#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2476 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2477 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2479 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002480#endif
2481
2482 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2485 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002486
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002487 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002488 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002489
2490cleanup:
2491 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002493
2494 return( ret );
2495}
2496
2497/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 * Free the components of an RSA key
2499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002501{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002502 if( ctx == NULL )
2503 return;
2504
irwir2239a862018-06-12 18:25:09 +03002505 mbedtls_mpi_free( &ctx->Vi );
2506 mbedtls_mpi_free( &ctx->Vf );
2507 mbedtls_mpi_free( &ctx->RN );
2508 mbedtls_mpi_free( &ctx->D );
2509 mbedtls_mpi_free( &ctx->Q );
2510 mbedtls_mpi_free( &ctx->P );
2511 mbedtls_mpi_free( &ctx->E );
2512 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002513
Hanno Becker33c30a02017-08-23 07:00:22 +01002514#if !defined(MBEDTLS_RSA_NO_CRT)
irwir2239a862018-06-12 18:25:09 +03002515 mbedtls_mpi_free( &ctx->RQ );
2516 mbedtls_mpi_free( &ctx->RP );
2517 mbedtls_mpi_free( &ctx->QP );
2518 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002519 mbedtls_mpi_free( &ctx->DP );
2520#endif /* MBEDTLS_RSA_NO_CRT */
2521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522#if defined(MBEDTLS_THREADING_C)
2523 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002524#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002525}
2526
Hanno Beckerab377312017-08-23 16:24:51 +01002527#endif /* !MBEDTLS_RSA_ALT */
2528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002531#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002532
2533/*
2534 * Example RSA-1024 keypair, for test purposes
2535 */
2536#define KEY_LEN 128
2537
2538#define RSA_N "9292758453063D803DD603D5E777D788" \
2539 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2540 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2541 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2542 "93A89813FBF3C4F8066D2D800F7C38A8" \
2543 "1AE31942917403FF4946B0A83D3D3E05" \
2544 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2545 "5E94BB77B07507233A0BC7BAC8F90F79"
2546
2547#define RSA_E "10001"
2548
2549#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2550 "66CA472BC44D253102F8B4A9D3BFA750" \
2551 "91386C0077937FE33FA3252D28855837" \
2552 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2553 "DF79C5CE07EE72C7F123142198164234" \
2554 "CABB724CF78B8173B9F880FC86322407" \
2555 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2556 "071513A1E85B5DFA031F21ECAE91A34D"
2557
2558#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2559 "2C01CAD19EA484A87EA4377637E75500" \
2560 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2561 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2562
2563#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2564 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2565 "910E4168387E3C30AA1E00C339A79508" \
2566 "8452DD96A9A5EA5D9DCA68DA636032AF"
2567
Paul Bakker5121ce52009-01-03 21:22:43 +00002568#define PT_LEN 24
2569#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2570 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002573static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002574{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002575#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002576 size_t i;
2577
Paul Bakker545570e2010-07-18 09:00:25 +00002578 if( rng_state != NULL )
2579 rng_state = NULL;
2580
Paul Bakkera3d195c2011-11-27 21:07:34 +00002581 for( i = 0; i < len; ++i )
2582 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002583#else
2584 if( rng_state != NULL )
2585 rng_state = NULL;
2586
2587 arc4random_buf( output, len );
2588#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002589
Paul Bakkera3d195c2011-11-27 21:07:34 +00002590 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002591}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002593
Paul Bakker5121ce52009-01-03 21:22:43 +00002594/*
2595 * Checkup routine
2596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002598{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002599 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002601 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 unsigned char rsa_plaintext[PT_LEN];
2604 unsigned char rsa_decrypted[PT_LEN];
2605 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002607 unsigned char sha1sum[20];
2608#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
Hanno Becker3a701162017-08-22 13:52:43 +01002610 mbedtls_mpi K;
2611
2612 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
Hanno Becker3a701162017-08-22 13:52:43 +01002615 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2616 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2617 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2618 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2619 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2620 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2621 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2622 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2623 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2624 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2625
Hanno Becker7f25f852017-10-10 16:56:22 +01002626 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
2628 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2632 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 {
2634 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
Hanno Becker5bc87292017-05-03 15:09:31 +01002637 ret = 1;
2638 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 }
2640
2641 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
Teppo Järvelinb5c46712019-10-04 13:35:55 +03002644 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Hanno Becker98838b02017-10-02 13:16:10 +01002646 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2647 PT_LEN, rsa_plaintext,
2648 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 {
2650 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Hanno Becker5bc87292017-05-03 15:09:31 +01002653 ret = 1;
2654 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656
2657 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Hanno Becker98838b02017-10-02 13:16:10 +01002660 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2661 &len, rsa_ciphertext, rsa_decrypted,
2662 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 {
2664 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
Hanno Becker5bc87292017-05-03 15:09:31 +01002667 ret = 1;
2668 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 }
2670
2671 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2672 {
2673 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Hanno Becker5bc87292017-05-03 15:09:31 +01002676 ret = 1;
2677 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 }
2679
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002680 if( verbose != 0 )
2681 mbedtls_printf( "passed\n" );
2682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002685 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002687 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002688 {
2689 if( verbose != 0 )
2690 mbedtls_printf( "failed\n" );
2691
2692 return( 1 );
2693 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002694
Hanno Becker98838b02017-10-02 13:16:10 +01002695 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2696 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2697 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 {
2699 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
Hanno Becker5bc87292017-05-03 15:09:31 +01002702 ret = 1;
2703 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 }
2705
2706 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Hanno Becker98838b02017-10-02 13:16:10 +01002709 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2710 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2711 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 {
2713 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Hanno Becker5bc87292017-05-03 15:09:31 +01002716 ret = 1;
2717 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 }
2719
2720 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002721 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002724 if( verbose != 0 )
2725 mbedtls_printf( "\n" );
2726
Paul Bakker3d8fb632014-04-17 12:42:41 +02002727cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002728 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 mbedtls_rsa_free( &rsa );
2730#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002731 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002733 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734}
2735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738#endif /* MBEDTLS_RSA_C */