blob: accc5b29b8180cbc63470590429135c026b98dc8 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 Bakker5121ce52009-01-03 21:22:43 +000018 */
Hanno Becker74716312017-10-02 10:00:37 +010019
Paul Bakker5121ce52009-01-03 21:22:43 +000020/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000021 * The following sources were referenced in the design of this implementation
22 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000023 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000024 * [1] A method for obtaining digital signatures and public-key cryptosystems
25 * R Rivest, A Shamir, and L Adleman
26 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
27 *
28 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
29 * Menezes, van Oorschot and Vanstone
30 *
Janos Follathe81102e2017-03-22 13:38:28 +000031 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
32 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
33 * Stefan Mangard
34 * https://arxiv.org/abs/1702.08719v2
35 *
Paul Bakker5121ce52009-01-03 21:22:43 +000036 */
37
Gilles Peskinedb09ef62020-06-03 01:43:33 +020038#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010043#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000046#include "mbedtls/error.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000047
Rich Evans00ab4702015-02-06 13:43:58 +000048#include <string.h>
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
gufe44c2620da2020-08-03 17:56:50 +020054#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010060#else
Rich Evans00ab4702015-02-06 13:43:58 +000061#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020063#define mbedtls_calloc calloc
64#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010065#endif
66
Hanno Beckera565f542017-10-11 11:00:19 +010067#if !defined(MBEDTLS_RSA_ALT)
68
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050069/* Parameter validation macros */
70#define RSA_VALIDATE_RET( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
72#define RSA_VALIDATE( cond ) \
73 MBEDTLS_INTERNAL_VALIDATE( cond )
74
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010075#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +010076/* constant-time buffer comparison */
77static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
78{
79 size_t i;
80 const unsigned char *A = (const unsigned char *) a;
81 const unsigned char *B = (const unsigned char *) b;
82 unsigned char diff = 0;
83
84 for( i = 0; i < n; i++ )
85 diff |= A[i] ^ B[i];
86
87 return( diff );
88}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010089#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +010090
Hanno Becker617c1ae2017-08-23 14:11:24 +010091int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
92 const mbedtls_mpi *N,
93 const mbedtls_mpi *P, const mbedtls_mpi *Q,
94 const mbedtls_mpi *D, const mbedtls_mpi *E )
95{
Janos Follath24eed8d2019-11-22 13:21:35 +000096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +010098
99 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
100 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
101 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
102 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
103 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
104 {
105 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
106 }
107
108 if( N != NULL )
109 ctx->len = mbedtls_mpi_size( &ctx->N );
110
111 return( 0 );
112}
113
114int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100115 unsigned char const *N, size_t N_len,
116 unsigned char const *P, size_t P_len,
117 unsigned char const *Q, size_t Q_len,
118 unsigned char const *D, size_t D_len,
119 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100120{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000121 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500122 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100123
124 if( N != NULL )
125 {
126 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
127 ctx->len = mbedtls_mpi_size( &ctx->N );
128 }
129
130 if( P != NULL )
131 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
132
133 if( Q != NULL )
134 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
135
136 if( D != NULL )
137 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
138
139 if( E != NULL )
140 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
141
142cleanup:
143
144 if( ret != 0 )
145 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
146
147 return( 0 );
148}
149
Hanno Becker705fc682017-10-10 17:57:02 +0100150/*
151 * Checks whether the context fields are set in such a way
152 * that the RSA primitives will be able to execute without error.
153 * It does *not* make guarantees for consistency of the parameters.
154 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100155static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
156 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100157{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100158#if !defined(MBEDTLS_RSA_NO_CRT)
159 /* blinding_needed is only used for NO_CRT to decide whether
160 * P,Q need to be present or not. */
161 ((void) blinding_needed);
162#endif
163
Hanno Becker3a760a12018-01-05 08:14:49 +0000164 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
165 ctx->len > MBEDTLS_MPI_MAX_SIZE )
166 {
Hanno Becker705fc682017-10-10 17:57:02 +0100167 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000168 }
Hanno Becker705fc682017-10-10 17:57:02 +0100169
170 /*
171 * 1. Modular exponentiation needs positive, odd moduli.
172 */
173
174 /* Modular exponentiation wrt. N is always used for
175 * RSA public key operations. */
176 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
177 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
178 {
179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
180 }
181
182#if !defined(MBEDTLS_RSA_NO_CRT)
183 /* Modular exponentiation for P and Q is only
184 * used for private key operations and if CRT
185 * is used. */
186 if( is_priv &&
187 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
188 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
189 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
190 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
191 {
192 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
193 }
194#endif /* !MBEDTLS_RSA_NO_CRT */
195
196 /*
197 * 2. Exponents must be positive
198 */
199
200 /* Always need E for public key operations */
201 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
202 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
203
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100204#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100205 /* For private key operations, use D or DP & DQ
206 * as (unblinded) exponents. */
207 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
208 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
209#else
210 if( is_priv &&
211 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
212 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
213 {
214 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
215 }
216#endif /* MBEDTLS_RSA_NO_CRT */
217
218 /* Blinding shouldn't make exponents negative either,
219 * so check that P, Q >= 1 if that hasn't yet been
220 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100221#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100222 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100223 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
224 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
225 {
226 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
227 }
228#endif
229
230 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100231 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100232#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100233 if( is_priv &&
234 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
235 {
236 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
237 }
238#endif
239
240 return( 0 );
241}
242
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100243int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100244{
245 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500246 int have_N, have_P, have_Q, have_D, have_E;
247#if !defined(MBEDTLS_RSA_NO_CRT)
248 int have_DP, have_DQ, have_QP;
249#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500250 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100251
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500252 RSA_VALIDATE_RET( ctx != NULL );
253
254 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
255 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
256 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
257 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
258 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500259
260#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500261 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
262 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
263 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500264#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100265
Hanno Becker617c1ae2017-08-23 14:11:24 +0100266 /*
267 * Check whether provided parameters are enough
268 * to deduce all others. The following incomplete
269 * parameter sets for private keys are supported:
270 *
271 * (1) P, Q missing.
272 * (2) D and potentially N missing.
273 *
274 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100275
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500276 n_missing = have_P && have_Q && have_D && have_E;
277 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
278 d_missing = have_P && have_Q && !have_D && have_E;
279 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100280
281 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100283
Hanno Becker617c1ae2017-08-23 14:11:24 +0100284 if( !is_priv && !is_pub )
285 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
286
287 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100288 * Step 1: Deduce N if P, Q are provided.
289 */
290
291 if( !have_N && have_P && have_Q )
292 {
293 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
294 &ctx->Q ) ) != 0 )
295 {
296 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
297 }
298
299 ctx->len = mbedtls_mpi_size( &ctx->N );
300 }
301
302 /*
303 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100304 */
305
306 if( pq_missing )
307 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100308 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100309 &ctx->P, &ctx->Q );
310 if( ret != 0 )
311 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
312
313 }
314 else if( d_missing )
315 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100316 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
317 &ctx->Q,
318 &ctx->E,
319 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100320 {
321 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
322 }
323 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324
Hanno Becker617c1ae2017-08-23 14:11:24 +0100325 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100326 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100327 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100328 */
329
Hanno Becker23344b52017-08-23 07:43:27 +0100330#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500331 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100332 {
333 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
334 &ctx->DP, &ctx->DQ, &ctx->QP );
335 if( ret != 0 )
336 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
337 }
Hanno Becker23344b52017-08-23 07:43:27 +0100338#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100339
340 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100341 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100342 */
343
Hanno Beckerebd2c022017-10-12 10:54:53 +0100344 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100345}
346
Hanno Becker617c1ae2017-08-23 14:11:24 +0100347int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
348 unsigned char *N, size_t N_len,
349 unsigned char *P, size_t P_len,
350 unsigned char *Q, size_t Q_len,
351 unsigned char *D, size_t D_len,
352 unsigned char *E, size_t E_len )
353{
354 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500355 int is_priv;
356 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100357
358 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500359 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100360 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
361 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
362 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
363 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
364 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
365
366 if( !is_priv )
367 {
368 /* If we're trying to export private parameters for a public key,
369 * something must be wrong. */
370 if( P != NULL || Q != NULL || D != NULL )
371 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
372
373 }
374
375 if( N != NULL )
376 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
377
378 if( P != NULL )
379 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
380
381 if( Q != NULL )
382 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
383
384 if( D != NULL )
385 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
386
387 if( E != NULL )
388 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100389
390cleanup:
391
392 return( ret );
393}
394
Hanno Becker617c1ae2017-08-23 14:11:24 +0100395int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
396 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
397 mbedtls_mpi *D, mbedtls_mpi *E )
398{
Janos Follath24eed8d2019-11-22 13:21:35 +0000399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500400 int is_priv;
401 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100402
403 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500404 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100405 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
406 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
407 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
408 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
409 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
410
411 if( !is_priv )
412 {
413 /* If we're trying to export private parameters for a public key,
414 * something must be wrong. */
415 if( P != NULL || Q != NULL || D != NULL )
416 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
417
418 }
419
420 /* Export all requested core parameters. */
421
422 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
423 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
424 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
425 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
426 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
427 {
428 return( ret );
429 }
430
431 return( 0 );
432}
433
434/*
435 * Export CRT parameters
436 * This must also be implemented if CRT is not used, for being able to
437 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
438 * can be used in this case.
439 */
440int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
441 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
442{
Janos Follath24eed8d2019-11-22 13:21:35 +0000443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500444 int is_priv;
445 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100446
447 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500448 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100449 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
450 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
451 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
452 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
453 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
454
455 if( !is_priv )
456 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
457
Hanno Beckerdc95c892017-08-23 06:57:02 +0100458#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100459 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100460 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
461 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
462 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
463 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100464 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100465 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100466#else
467 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
468 DP, DQ, QP ) ) != 0 )
469 {
470 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
471 }
472#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100473
474 return( 0 );
475}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100476
Paul Bakker5121ce52009-01-03 21:22:43 +0000477/*
478 * Initialize an RSA context
479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000482 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000483{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500484 RSA_VALIDATE( ctx != NULL );
485 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
486 padding == MBEDTLS_RSA_PKCS_V21 );
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#if defined(MBEDTLS_THREADING_C)
493 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200494#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000495}
496
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100497/*
498 * Set padding for an existing RSA context
499 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500500void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
501 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100502{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500503 RSA_VALIDATE( ctx != NULL );
504 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
505 padding == MBEDTLS_RSA_PKCS_V21 );
506
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100507 ctx->padding = padding;
508 ctx->hash_id = hash_id;
509}
510
Hanno Becker617c1ae2017-08-23 14:11:24 +0100511/*
512 * Get length in bytes of RSA modulus
513 */
514
515size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
516{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100517 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100518}
519
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523/*
524 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800525 *
526 * This generation method follows the RSA key pair generation procedure of
527 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000530 int (*f_rng)(void *, unsigned char *, size_t),
531 void *p_rng,
532 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000533{
Janos Follath24eed8d2019-11-22 13:21:35 +0000534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800535 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100536 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500537 RSA_VALIDATE_RET( ctx != NULL );
538 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500540 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
Janos Follathef441782016-09-21 13:18:12 +0100541 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
542
Janos Follathb8fc1b02018-09-03 15:37:01 +0100543 /*
544 * If the modulus is 1024 bit long or shorter, then the security strength of
545 * the RSA algorithm is less than or equal to 80 bits and therefore an error
546 * rate of 2^-80 is sufficient.
547 */
548 if( nbits > 1024 )
549 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
550
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100551 mbedtls_mpi_init( &H );
552 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800553 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555 /*
556 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800557 * 1. |P-Q| > 2^( nbits / 2 - 100 )
558 * 2. GCD( E, (P-1)*(Q-1) ) == 1
559 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
563 do
564 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100565 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
566 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Janos Follathb8fc1b02018-09-03 15:37:01 +0100568 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
569 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800571 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
572 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
573 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 continue;
575
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800576 /* not required by any standards, but some users rely on the fact that P > Q */
577 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100578 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100579
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100580 /* Temporarily replace P,Q by P-1, Q-1 */
581 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
582 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
583 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800584
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800585 /* 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 +0200586 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800587 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
588 continue;
589
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800590 /* 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 -0800591 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
592 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
593 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
594
595 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
596 continue;
597
598 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800600 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100602 /* Restore P,Q */
603 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
604 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
605
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800606 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
607
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100608 ctx->len = mbedtls_mpi_size( &ctx->N );
609
Jethro Beekman97f95c92018-02-13 15:50:36 -0800610#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 * DP = D mod (P - 1)
613 * DQ = D mod (Q - 1)
614 * QP = Q^-1 mod P
615 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100616 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
617 &ctx->DP, &ctx->DQ, &ctx->QP ) );
618#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
Hanno Becker83aad1f2017-08-23 06:45:10 +0100620 /* Double-check */
621 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
623cleanup:
624
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100625 mbedtls_mpi_free( &H );
626 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800627 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
629 if( ret != 0 )
630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_rsa_free( ctx );
632 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 }
634
Paul Bakker48377d92013-08-30 12:06:24 +0200635 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636}
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640/*
641 * Check a public RSA key
642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000644{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500645 RSA_VALIDATE_RET( ctx != NULL );
646
Hanno Beckerebd2c022017-10-12 10:54:53 +0100647 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000649
Hanno Becker3a760a12018-01-05 08:14:49 +0000650 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100653 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Hanno Becker705fc682017-10-10 17:57:02 +0100655 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
656 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100660 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
662 return( 0 );
663}
664
665/*
Hanno Becker705fc682017-10-10 17:57:02 +0100666 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000669{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500670 RSA_VALIDATE_RET( ctx != NULL );
671
Hanno Becker705fc682017-10-10 17:57:02 +0100672 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100673 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000674 {
Hanno Becker98838b02017-10-02 13:16:10 +0100675 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 }
Paul Bakker48377d92013-08-30 12:06:24 +0200677
Hanno Becker98838b02017-10-02 13:16:10 +0100678 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100679 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100681 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000683
Hanno Beckerb269a852017-08-25 08:03:21 +0100684#if !defined(MBEDTLS_RSA_NO_CRT)
685 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
686 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
687 {
688 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
689 }
690#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000691
692 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000693}
694
695/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696 * Check if contexts holding a public and private key match
697 */
Hanno Becker98838b02017-10-02 13:16:10 +0100698int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
699 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100700{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500701 RSA_VALIDATE_RET( pub != NULL );
702 RSA_VALIDATE_RET( prv != NULL );
703
Hanno Becker98838b02017-10-02 13:16:10 +0100704 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708 }
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
711 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100714 }
715
716 return( 0 );
717}
718
719/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 * Do an RSA public key operation
721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000723 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000724 unsigned char *output )
725{
Janos Follath24eed8d2019-11-22 13:21:35 +0000726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000727 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500729 RSA_VALIDATE_RET( ctx != NULL );
730 RSA_VALIDATE_RET( input != NULL );
731 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
Hanno Beckerebd2c022017-10-12 10:54:53 +0100733 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100734 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200738#if defined(MBEDTLS_THREADING_C)
739 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
740 return( ret );
741#endif
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000746 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200747 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
748 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 }
750
751 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
753 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
755cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200757 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
758 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100759#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
763 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
766 return( 0 );
767}
768
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200769/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200770 * Generate or update blinding values, see section 10 of:
771 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200772 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200773 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200774 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200775static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200776 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
777{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200778 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200779
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200780 if( ctx->Vf.p != NULL )
781 {
782 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
784 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
785 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
786 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200787
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200788 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200789 }
790
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200791 /* Unblinding value: Vf = random number, invertible mod N */
792 do {
793 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
797 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
798 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200799
800 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
802 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 +0200803
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200804
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200805cleanup:
806 return( ret );
807}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200808
Paul Bakker5121ce52009-01-03 21:22:43 +0000809/*
Janos Follathe81102e2017-03-22 13:38:28 +0000810 * Exponent blinding supposed to prevent side-channel attacks using multiple
811 * traces of measurements to recover the RSA key. The more collisions are there,
812 * the more bits of the key can be recovered. See [3].
813 *
814 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
815 * observations on avarage.
816 *
817 * For example with 28 byte blinding to achieve 2 collisions the adversary has
818 * to make 2^112 observations on avarage.
819 *
820 * (With the currently (as of 2017 April) known best algorithms breaking 2048
821 * bit RSA requires approximately as much time as trying out 2^112 random keys.
822 * Thus in this sense with 28 byte blinding the security is not reduced by
823 * side-channel attacks like the one in [3])
824 *
825 * This countermeasure does not help if the key recovery is possible with a
826 * single trace.
827 */
828#define RSA_EXPONENT_BLINDING 28
829
830/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000831 * Do an RSA private key operation
832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200834 int (*f_rng)(void *, unsigned char *, size_t),
835 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000836 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 unsigned char *output )
838{
Janos Follath24eed8d2019-11-22 13:21:35 +0000839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000840 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100841
842 /* Temporary holding the result */
843 mbedtls_mpi T;
844
845 /* Temporaries holding P-1, Q-1 and the
846 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000847 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100848
849#if !defined(MBEDTLS_RSA_NO_CRT)
850 /* Temporaries holding the results mod p resp. mod q. */
851 mbedtls_mpi TP, TQ;
852
853 /* Temporaries holding the blinded exponents for
854 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000855 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100856
857 /* Pointers to actual exponents to be used - either the unblinded
858 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000859 mbedtls_mpi *DP = &ctx->DP;
860 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100861#else
862 /* Temporary holding the blinded exponent (if used). */
863 mbedtls_mpi D_blind;
864
865 /* Pointer to actual exponent to be used - either the unblinded
866 * or the blinded one, depending on the presence of a PRNG. */
867 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100868#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100869
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100870 /* Temporaries holding the initial input and the double
871 * checked result; should be the same in the end. */
872 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000873
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500874 RSA_VALIDATE_RET( ctx != NULL );
875 RSA_VALIDATE_RET( input != NULL );
876 RSA_VALIDATE_RET( output != NULL );
877
Hanno Beckerebd2c022017-10-12 10:54:53 +0100878 if( rsa_check_context( ctx, 1 /* private key checks */,
879 f_rng != NULL /* blinding y/n */ ) != 0 )
880 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100881 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100882 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100883
Hanno Becker06811ce2017-05-03 15:10:34 +0100884#if defined(MBEDTLS_THREADING_C)
885 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
886 return( ret );
887#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000888
Hanno Becker06811ce2017-05-03 15:10:34 +0100889 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100890 mbedtls_mpi_init( &T );
891
892 mbedtls_mpi_init( &P1 );
893 mbedtls_mpi_init( &Q1 );
894 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000895
Janos Follathf9203b42017-03-22 15:13:15 +0000896 if( f_rng != NULL )
897 {
Janos Follathe81102e2017-03-22 13:38:28 +0000898#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000899 mbedtls_mpi_init( &D_blind );
900#else
901 mbedtls_mpi_init( &DP_blind );
902 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000903#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000904 }
Janos Follathe81102e2017-03-22 13:38:28 +0000905
Hanno Becker06811ce2017-05-03 15:10:34 +0100906#if !defined(MBEDTLS_RSA_NO_CRT)
907 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200908#endif
909
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100910 mbedtls_mpi_init( &I );
911 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100912
913 /* End of MPI initialization */
914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
916 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200918 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
919 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 }
921
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100922 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100923
Paul Bakkerf451bac2013-08-30 15:37:02 +0200924 if( f_rng != NULL )
925 {
926 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200927 * Blinding
928 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200929 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200930 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
931 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000933
Janos Follathe81102e2017-03-22 13:38:28 +0000934 /*
935 * Exponent blinding
936 */
937 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
938 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
939
Janos Follathf9203b42017-03-22 15:13:15 +0000940#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000941 /*
942 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
943 */
944 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
945 f_rng, p_rng ) );
946 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
947 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
948 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
949
950 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000951#else
952 /*
953 * DP_blind = ( P - 1 ) * R + DP
954 */
955 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
956 f_rng, p_rng ) );
957 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
958 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
959 &ctx->DP ) );
960
961 DP = &DP_blind;
962
963 /*
964 * DQ_blind = ( Q - 1 ) * R + DQ
965 */
966 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
967 f_rng, p_rng ) );
968 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
969 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
970 &ctx->DQ ) );
971
972 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000973#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200974 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000977 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100978#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200979 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000980 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000981 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100982 * TP = input ^ dP mod P
983 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100985
986 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
987 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000988
989 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100990 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100992 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
993 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
994 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
996 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100997 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100999 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1000 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001002
Paul Bakkerf451bac2013-08-30 15:37:02 +02001003 if( f_rng != NULL )
1004 {
1005 /*
1006 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001007 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001008 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001009 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
Hanno Becker2dec5e82017-10-03 07:49:52 +01001013 /* Verify the result to prevent glitching attacks. */
1014 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1015 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001016 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001017 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001018 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1019 goto cleanup;
1020 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001021
Paul Bakker5121ce52009-01-03 21:22:43 +00001022 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
1025cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001027 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1028 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001029#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001030
Hanno Becker06811ce2017-05-03 15:10:34 +01001031 mbedtls_mpi_free( &P1 );
1032 mbedtls_mpi_free( &Q1 );
1033 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001034
1035 if( f_rng != NULL )
1036 {
Janos Follathe81102e2017-03-22 13:38:28 +00001037#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001038 mbedtls_mpi_free( &D_blind );
1039#else
1040 mbedtls_mpi_free( &DP_blind );
1041 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001042#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001043 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
Hanno Becker06811ce2017-05-03 15:10:34 +01001045 mbedtls_mpi_free( &T );
1046
1047#if !defined(MBEDTLS_RSA_NO_CRT)
1048 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1049#endif
1050
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001051 mbedtls_mpi_free( &C );
1052 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001053
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
1057 return( 0 );
1058}
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001061/**
1062 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1063 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001064 * \param dst buffer to mask
1065 * \param dlen length of destination buffer
1066 * \param src source of the mask generation
1067 * \param slen length of the source buffer
1068 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001069 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001070static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001072{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001074 unsigned char counter[4];
1075 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001076 unsigned int hlen;
1077 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001078 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001081 memset( counter, 0, 4 );
1082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001084
Simon Butcher02037452016-03-01 21:19:12 +00001085 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001086 p = dst;
1087
1088 while( dlen > 0 )
1089 {
1090 use_len = hlen;
1091 if( dlen < hlen )
1092 use_len = dlen;
1093
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001094 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1095 goto exit;
1096 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1097 goto exit;
1098 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1099 goto exit;
1100 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1101 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001102
1103 for( i = 0; i < use_len; ++i )
1104 *p++ ^= mask[i];
1105
1106 counter[3]++;
1107
1108 dlen -= use_len;
1109 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001110
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001111exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001112 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001113
1114 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001115}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001119/*
1120 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001123 int (*f_rng)(void *, unsigned char *, size_t),
1124 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001125 int mode,
1126 const unsigned char *label, size_t label_len,
1127 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001128 const unsigned char *input,
1129 unsigned char *output )
1130{
1131 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001133 unsigned char *p = output;
1134 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 const mbedtls_md_info_t *md_info;
1136 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001137
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001138 RSA_VALIDATE_RET( ctx != NULL );
1139 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1140 mode == MBEDTLS_RSA_PUBLIC );
1141 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001142 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001143 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1146 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001147
1148 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001152 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001154
1155 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001157
Simon Butcher02037452016-03-01 21:19:12 +00001158 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001159 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001161
1162 memset( output, 0, olen );
1163
1164 *p++ = 0;
1165
Simon Butcher02037452016-03-01 21:19:12 +00001166 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001167 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001169
1170 p += hlen;
1171
Simon Butcher02037452016-03-01 21:19:12 +00001172 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001173 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1174 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001175 p += hlen;
1176 p += olen - 2 * hlen - 2 - ilen;
1177 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001178 if( ilen != 0 )
1179 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001182 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001183 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001184
Simon Butcher02037452016-03-01 21:19:12 +00001185 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001186 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1187 &md_ctx ) ) != 0 )
1188 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001189
Simon Butcher02037452016-03-01 21:19:12 +00001190 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001191 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1192 &md_ctx ) ) != 0 )
1193 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001194
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001195exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001197
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001198 if( ret != 0 )
1199 return( ret );
1200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 return( ( mode == MBEDTLS_RSA_PUBLIC )
1202 ? mbedtls_rsa_public( ctx, output, output )
1203 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001208/*
1209 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001212 int (*f_rng)(void *, unsigned char *, size_t),
1213 void *p_rng,
1214 int mode, size_t ilen,
1215 const unsigned char *input,
1216 unsigned char *output )
1217{
1218 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001220 unsigned char *p = output;
1221
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001222 RSA_VALIDATE_RET( ctx != NULL );
1223 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1224 mode == MBEDTLS_RSA_PUBLIC );
1225 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001226 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001227
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001228 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001230
1231 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001232
Simon Butcher02037452016-03-01 21:19:12 +00001233 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001234 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001236
1237 nb_pad = olen - 3 - ilen;
1238
1239 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001241 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001242 if( f_rng == NULL )
1243 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001246
1247 while( nb_pad-- > 0 )
1248 {
1249 int rng_dl = 100;
1250
1251 do {
1252 ret = f_rng( p_rng, p, 1 );
1253 } while( *p == 0 && --rng_dl && ret == 0 );
1254
Simon Butcher02037452016-03-01 21:19:12 +00001255 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001256 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001258
1259 p++;
1260 }
1261 }
1262 else
1263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001265
1266 while( nb_pad-- > 0 )
1267 *p++ = 0xFF;
1268 }
1269
1270 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001271 if( ilen != 0 )
1272 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 return( ( mode == MBEDTLS_RSA_PUBLIC )
1275 ? mbedtls_rsa_public( ctx, output, output )
1276 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001277}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001279
Paul Bakker5121ce52009-01-03 21:22:43 +00001280/*
1281 * Add the message padding, then do an RSA operation
1282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001284 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001285 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001286 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001287 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 unsigned char *output )
1289{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001290 RSA_VALIDATE_RET( ctx != NULL );
1291 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1292 mode == MBEDTLS_RSA_PUBLIC );
1293 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001294 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001295
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 switch( ctx->padding )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#if defined(MBEDTLS_PKCS1_V15)
1299 case MBEDTLS_RSA_PKCS_V15:
1300 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001301 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001302#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#if defined(MBEDTLS_PKCS1_V21)
1305 case MBEDTLS_RSA_PKCS_V21:
1306 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001307 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001308#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
1310 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001313}
1314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001316/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001317 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001320 int (*f_rng)(void *, unsigned char *, size_t),
1321 void *p_rng,
1322 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001323 const unsigned char *label, size_t label_len,
1324 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001325 const unsigned char *input,
1326 unsigned char *output,
1327 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001328{
Janos Follath24eed8d2019-11-22 13:21:35 +00001329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001330 size_t ilen, i, pad_len;
1331 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1333 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001334 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 const mbedtls_md_info_t *md_info;
1336 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001337
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001338 RSA_VALIDATE_RET( ctx != NULL );
1339 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1340 mode == MBEDTLS_RSA_PUBLIC );
1341 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1342 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1343 RSA_VALIDATE_RET( input != NULL );
1344 RSA_VALIDATE_RET( olen != NULL );
1345
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001346 /*
1347 * Parameters sanity checks
1348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1350 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001351
1352 ilen = ctx->len;
1353
Paul Bakker27fdf462011-06-09 13:55:13 +00001354 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001358 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001360
Janos Follathc17cda12016-02-11 11:08:18 +00001361 hlen = mbedtls_md_get_size( md_info );
1362
1363 // checking for integer underflow
1364 if( 2 * hlen + 2 > ilen )
1365 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1366
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001367 /*
1368 * RSA operation
1369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1371 ? mbedtls_rsa_public( ctx, input, buf )
1372 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
1374 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001375 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001377 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001378 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001381 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1382 {
1383 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001384 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001385 }
1386
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001387 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001388 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1389 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001390 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001391 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1392 &md_ctx ) ) != 0 )
1393 {
1394 mbedtls_md_free( &md_ctx );
1395 goto cleanup;
1396 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001399
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001400 /* Generate lHash */
1401 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1402 goto cleanup;
1403
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001404 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001405 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001406 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001408 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001410 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001411
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001412 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001413
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001414 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001415 for( i = 0; i < hlen; i++ )
1416 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001417
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001418 /* Get zero-padding len, but always read till end of buffer
1419 * (minus one, for the 01 byte) */
1420 pad_len = 0;
1421 pad_done = 0;
1422 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1423 {
1424 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001425 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001426 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001427
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001428 p += pad_len;
1429 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001430
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001431 /*
1432 * The only information "leaked" is whether the padding was correct or not
1433 * (eg, no data is copied if it was not correct). This meets the
1434 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1435 * the different error conditions.
1436 */
1437 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001438 {
1439 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1440 goto cleanup;
1441 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001442
Paul Bakker66d5d072014-06-17 16:39:18 +02001443 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001444 {
1445 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1446 goto cleanup;
1447 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001448
1449 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001450 if( *olen != 0 )
1451 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001452 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001453
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001454cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001455 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1456 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001457
1458 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001459}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001463/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1464 *
1465 * \param value The value to analyze.
1466 * \return Zero if \p value is zero, otherwise all-bits-one.
1467 */
1468static unsigned all_or_nothing_int( unsigned value )
1469{
1470 /* MSVC has a warning about unary minus on unsigned, but this is
1471 * well-defined and precisely what we want to do here */
1472#if defined(_MSC_VER)
1473#pragma warning( push )
1474#pragma warning( disable : 4146 )
1475#endif
1476 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1477#if defined(_MSC_VER)
1478#pragma warning( pop )
1479#endif
1480}
1481
1482/** Check whether a size is out of bounds, without branches.
1483 *
1484 * This is equivalent to `size > max`, but is likely to be compiled to
1485 * to code using bitwise operation rather than a branch.
1486 *
1487 * \param size Size to check.
1488 * \param max Maximum desired value for \p size.
1489 * \return \c 0 if `size <= max`.
1490 * \return \c 1 if `size > max`.
1491 */
1492static unsigned size_greater_than( size_t size, size_t max )
1493{
1494 /* Return the sign bit (1 for negative) of (max - size). */
1495 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1496}
1497
1498/** Choose between two integer values, without branches.
1499 *
1500 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1501 * to code using bitwise operation rather than a branch.
1502 *
1503 * \param cond Condition to test.
1504 * \param if1 Value to use if \p cond is nonzero.
1505 * \param if0 Value to use if \p cond is zero.
1506 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1507 */
1508static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
1509{
1510 unsigned mask = all_or_nothing_int( cond );
1511 return( ( mask & if1 ) | (~mask & if0 ) );
1512}
1513
1514/** Shift some data towards the left inside a buffer without leaking
1515 * the length of the data through side channels.
1516 *
1517 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1518 * ```
1519 * memmove(start, start + offset, total - offset);
1520 * memset(start + offset, 0, total - offset);
1521 * ```
1522 * but it strives to use a memory access pattern (and thus total timing)
1523 * that does not depend on \p offset. This timing independence comes at
1524 * the expense of performance.
1525 *
1526 * \param start Pointer to the start of the buffer.
1527 * \param total Total size of the buffer.
1528 * \param offset Offset from which to copy \p total - \p offset bytes.
1529 */
1530static void mem_move_to_left( void *start,
1531 size_t total,
1532 size_t offset )
1533{
1534 volatile unsigned char *buf = start;
1535 size_t i, n;
1536 if( total == 0 )
1537 return;
1538 for( i = 0; i < total; i++ )
1539 {
1540 unsigned no_op = size_greater_than( total - offset, i );
1541 /* The first `total - offset` passes are a no-op. The last
1542 * `offset` passes shift the data one byte to the left and
1543 * zero out the last byte. */
1544 for( n = 0; n < total - 1; n++ )
1545 {
1546 unsigned char current = buf[n];
1547 unsigned char next = buf[n+1];
1548 buf[n] = if_int( no_op, current, next );
1549 }
1550 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1551 }
1552}
1553
Paul Bakkerb3869132013-02-28 17:21:01 +01001554/*
1555 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001558 int (*f_rng)(void *, unsigned char *, size_t),
1559 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001560 int mode, size_t *olen,
1561 const unsigned char *input,
1562 unsigned char *output,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001563 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001564{
Janos Follath24eed8d2019-11-22 13:21:35 +00001565 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001566 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001568 /* The following variables take sensitive values: their value must
1569 * not leak into the observable behavior of the function other than
1570 * the designated outputs (output, olen, return value). Otherwise
1571 * this would open the execution of the function to
1572 * side-channel-based variants of the Bleichenbacher padding oracle
1573 * attack. Potential side channels include overall timing, memory
1574 * access patterns (especially visible to an adversary who has access
1575 * to a shared memory cache), and branches (especially visible to
1576 * an adversary who has access to a shared code cache or to a shared
1577 * branch predictor). */
1578 size_t pad_count = 0;
1579 unsigned bad = 0;
1580 unsigned char pad_done = 0;
1581 size_t plaintext_size = 0;
1582 unsigned output_too_large;
1583
1584 RSA_VALIDATE_RET( ctx != NULL );
1585 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1586 mode == MBEDTLS_RSA_PUBLIC );
1587 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1588 RSA_VALIDATE_RET( input != NULL );
1589 RSA_VALIDATE_RET( olen != NULL );
1590
1591 ilen = ctx->len;
1592 plaintext_max_size = ( output_max_len > ilen - 11 ?
1593 ilen - 11 :
1594 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1597 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001598
Paul Bakkerb3869132013-02-28 17:21:01 +01001599 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1603 ? mbedtls_rsa_public( ctx, input, buf )
1604 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001605
1606 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001607 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001608
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001609 /* Check and get padding length in constant time and constant
1610 * memory trace. The first byte must be 0. */
1611 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001614 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001615 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1616 * where PS must be at least 8 nonzero bytes. */
1617 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001618
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001619 /* Read the whole buffer. Set pad_done to nonzero if we find
1620 * the 0x00 byte and remember the padding length in pad_count. */
1621 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001622 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001623 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001624 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001625 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001626 }
1627 else
1628 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001629 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1630 * where PS must be at least 8 bytes with the value 0xFF. */
1631 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001632
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001633 /* Read the whole buffer. Set pad_done to nonzero if we find
1634 * the 0x00 byte and remember the padding length in pad_count.
1635 * If there's a non-0xff byte in the padding, the padding is bad. */
1636 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001637 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001638 pad_done |= if_int( buf[i], 0, 1 );
1639 pad_count += if_int( pad_done, 0, 1 );
1640 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001641 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001642 }
1643
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001644 /* If pad_done is still zero, there's no data, only unfinished padding. */
1645 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001646
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001647 /* There must be at least 8 bytes of padding. */
1648 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001649
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001650 /* If the padding is valid, set plaintext_size to the number of
1651 * remaining bytes after stripping the padding. If the padding
1652 * is invalid, avoid leaking this fact through the size of the
1653 * output: use the maximum message size that fits in the output
1654 * buffer. Do it without branches to avoid leaking the padding
1655 * validity through timing. RSA keys are small enough that all the
1656 * size_t values involved fit in unsigned int. */
1657 plaintext_size = if_int( bad,
1658 (unsigned) plaintext_max_size,
1659 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001660
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001661 /* Set output_too_large to 0 if the plaintext fits in the output
1662 * buffer and to 1 otherwise. */
1663 output_too_large = size_greater_than( plaintext_size,
1664 plaintext_max_size );
1665
1666 /* Set ret without branches to avoid timing attacks. Return:
1667 * - INVALID_PADDING if the padding is bad (bad != 0).
1668 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1669 * plaintext does not fit in the output buffer.
1670 * - 0 if the padding is correct. */
1671 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1672 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1673 0 ) );
1674
1675 /* If the padding is bad or the plaintext is too large, zero the
1676 * data that we're about to copy to the output buffer.
1677 * We need to copy the same amount of data
1678 * from the same buffer whether the padding is good or not to
1679 * avoid leaking the padding validity through overall timing or
1680 * through memory or cache access patterns. */
1681 bad = all_or_nothing_int( bad | output_too_large );
1682 for( i = 11; i < ilen; i++ )
1683 buf[i] &= ~bad;
1684
1685 /* If the plaintext is too large, truncate it to the buffer size.
1686 * Copy anyway to avoid revealing the length through timing, because
1687 * revealing the length is as bad as revealing the padding validity
1688 * for a Bleichenbacher attack. */
1689 plaintext_size = if_int( output_too_large,
1690 (unsigned) plaintext_max_size,
1691 (unsigned) plaintext_size );
1692
1693 /* Move the plaintext to the leftmost position where it can start in
1694 * the working buffer, i.e. make it start plaintext_max_size from
1695 * the end of the buffer. Do this with a memory access trace that
1696 * does not depend on the plaintext size. After this move, the
1697 * starting location of the plaintext is no longer sensitive
1698 * information. */
1699 mem_move_to_left( buf + ilen - plaintext_max_size,
1700 plaintext_max_size,
1701 plaintext_max_size - plaintext_size );
1702
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001703 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1704 * buffer. If output_max_len is 0, then output may be an invalid pointer
1705 * and the result of memcpy() would be undefined; prevent undefined
1706 * behavior making sure to depend only on output_max_len (the size of the
1707 * user-provided output buffer), which is independent from plaintext
1708 * length, validity of padding, success of the decryption, and other
1709 * secrets. */
1710 if( output_max_len != 0 )
1711 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001712
1713 /* Report the amount of data we copied to the output buffer. In case
1714 * of errors (bad padding or output too large), the value of *olen
1715 * when this function returns is not specified. Making it equivalent
1716 * to the good case limits the risks of leaking the padding validity. */
1717 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001718
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001719cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001720 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001721
1722 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001723}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001725
1726/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001727 * Do an RSA operation, then remove the message padding
1728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001730 int (*f_rng)(void *, unsigned char *, size_t),
1731 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001732 int mode, size_t *olen,
1733 const unsigned char *input,
1734 unsigned char *output,
1735 size_t output_max_len)
1736{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001737 RSA_VALIDATE_RET( ctx != NULL );
1738 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1739 mode == MBEDTLS_RSA_PUBLIC );
1740 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1741 RSA_VALIDATE_RET( input != NULL );
1742 RSA_VALIDATE_RET( olen != NULL );
1743
Paul Bakkerb3869132013-02-28 17:21:01 +01001744 switch( ctx->padding )
1745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#if defined(MBEDTLS_PKCS1_V15)
1747 case MBEDTLS_RSA_PKCS_V15:
1748 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001749 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001750#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752#if defined(MBEDTLS_PKCS1_V21)
1753 case MBEDTLS_RSA_PKCS_V21:
1754 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001755 olen, input, output,
1756 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001757#endif
1758
1759 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001761 }
1762}
1763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001765/*
1766 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001769 int (*f_rng)(void *, unsigned char *, size_t),
1770 void *p_rng,
1771 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001773 unsigned int hashlen,
1774 const unsigned char *hash,
1775 unsigned char *sig )
1776{
1777 size_t olen;
1778 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001780 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001781 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001782 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 const mbedtls_md_info_t *md_info;
1784 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001785 RSA_VALIDATE_RET( ctx != NULL );
1786 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1787 mode == MBEDTLS_RSA_PUBLIC );
1788 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1789 hashlen == 0 ) ||
1790 hash != NULL );
1791 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1794 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001795
1796 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001798
1799 olen = ctx->len;
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001802 {
Simon Butcher02037452016-03-01 21:19:12 +00001803 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001805 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001809 }
1810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001812 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001816
Jaeden Amero3725bb22018-09-07 19:12:36 +01001817 /* Calculate the largest possible salt length. Normally this is the hash
1818 * length, which is the maximum length the salt can have. If there is not
1819 * enough room, use the maximum salt length that fits. The constraint is
1820 * that the hash length plus the salt length plus 2 bytes must be at most
1821 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1822 * (PKCS#1 v2.2) §9.1.1 step 3. */
1823 min_slen = hlen - 2;
1824 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001826 else if( olen >= hlen + hlen + 2 )
1827 slen = hlen;
1828 else
1829 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001830
1831 memset( sig, 0, olen );
1832
Simon Butcher02037452016-03-01 21:19:12 +00001833 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001834 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001836
Simon Butcher02037452016-03-01 21:19:12 +00001837 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001838 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001839 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001840 *p++ = 0x01;
1841 memcpy( p, salt, slen );
1842 p += slen;
1843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001845 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001846 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001847
Simon Butcher02037452016-03-01 21:19:12 +00001848 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001849 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1850 goto exit;
1851 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1852 goto exit;
1853 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1854 goto exit;
1855 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1856 goto exit;
1857 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1858 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001859
Simon Butcher02037452016-03-01 21:19:12 +00001860 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001861 if( msb % 8 == 0 )
1862 offset = 1;
1863
Simon Butcher02037452016-03-01 21:19:12 +00001864 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001865 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1866 &md_ctx ) ) != 0 )
1867 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001868
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001869 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001870 sig[0] &= 0xFF >> ( olen * 8 - msb );
1871
1872 p += hlen;
1873 *p++ = 0xBC;
1874
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001875 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001876
1877exit:
1878 mbedtls_md_free( &md_ctx );
1879
1880 if( ret != 0 )
1881 return( ret );
1882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 return( ( mode == MBEDTLS_RSA_PUBLIC )
1884 ? mbedtls_rsa_public( ctx, sig, sig )
1885 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001886}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001890/*
1891 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1892 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001893
1894/* Construct a PKCS v1.5 encoding of a hashed message
1895 *
1896 * This is used both for signature generation and verification.
1897 *
1898 * Parameters:
1899 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001900 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001901 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001902 * - hash: Buffer containing the hashed message or the raw data.
1903 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001904 * - dst: Buffer to hold the encoded message.
1905 *
1906 * Assumptions:
1907 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1908 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001909 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001910 *
1911 */
1912static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1913 unsigned int hashlen,
1914 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001915 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001916 unsigned char *dst )
1917{
1918 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001919 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001920 unsigned char *p = dst;
1921 const char *oid = NULL;
1922
1923 /* Are we signing hashed or raw data? */
1924 if( md_alg != MBEDTLS_MD_NONE )
1925 {
1926 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1927 if( md_info == NULL )
1928 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1929
1930 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1931 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1932
1933 hashlen = mbedtls_md_get_size( md_info );
1934
1935 /* Double-check that 8 + hashlen + oid_size can be used as a
1936 * 1-byte ASN.1 length encoding and that there's no overflow. */
1937 if( 8 + hashlen + oid_size >= 0x80 ||
1938 10 + hashlen < hashlen ||
1939 10 + hashlen + oid_size < 10 + hashlen )
1940 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1941
1942 /*
1943 * Static bounds check:
1944 * - Need 10 bytes for five tag-length pairs.
1945 * (Insist on 1-byte length encodings to protect against variants of
1946 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1947 * - Need hashlen bytes for hash
1948 * - Need oid_size bytes for hash alg OID.
1949 */
1950 if( nb_pad < 10 + hashlen + oid_size )
1951 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1952 nb_pad -= 10 + hashlen + oid_size;
1953 }
1954 else
1955 {
1956 if( nb_pad < hashlen )
1957 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1958
1959 nb_pad -= hashlen;
1960 }
1961
Hanno Becker2b2f8982017-09-27 17:10:03 +01001962 /* Need space for signature header and padding delimiter (3 bytes),
1963 * and 8 bytes for the minimal padding */
1964 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001965 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1966 nb_pad -= 3;
1967
1968 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001969 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001970
1971 /* Write signature header and padding */
1972 *p++ = 0;
1973 *p++ = MBEDTLS_RSA_SIGN;
1974 memset( p, 0xFF, nb_pad );
1975 p += nb_pad;
1976 *p++ = 0;
1977
1978 /* Are we signing raw data? */
1979 if( md_alg == MBEDTLS_MD_NONE )
1980 {
1981 memcpy( p, hash, hashlen );
1982 return( 0 );
1983 }
1984
1985 /* Signing hashed data, add corresponding ASN.1 structure
1986 *
1987 * DigestInfo ::= SEQUENCE {
1988 * digestAlgorithm DigestAlgorithmIdentifier,
1989 * digest Digest }
1990 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1991 * Digest ::= OCTET STRING
1992 *
1993 * Schematic:
1994 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1995 * TAG-NULL + LEN [ NULL ] ]
1996 * TAG-OCTET + LEN [ HASH ] ]
1997 */
1998 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001999 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002000 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002001 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002002 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002003 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002004 memcpy( p, oid, oid_size );
2005 p += oid_size;
2006 *p++ = MBEDTLS_ASN1_NULL;
2007 *p++ = 0x00;
2008 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002009 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002010 memcpy( p, hash, hashlen );
2011 p += hashlen;
2012
2013 /* Just a sanity-check, should be automatic
2014 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002015 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002016 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002017 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002018 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2019 }
2020
2021 return( 0 );
2022}
2023
Paul Bakkerb3869132013-02-28 17:21:01 +01002024/*
2025 * Do an RSA operation to sign the message digest
2026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002028 int (*f_rng)(void *, unsigned char *, size_t),
2029 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002030 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002032 unsigned int hashlen,
2033 const unsigned char *hash,
2034 unsigned char *sig )
2035{
Janos Follath24eed8d2019-11-22 13:21:35 +00002036 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002037 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002038
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002039 RSA_VALIDATE_RET( ctx != NULL );
2040 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2041 mode == MBEDTLS_RSA_PUBLIC );
2042 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2043 hashlen == 0 ) ||
2044 hash != NULL );
2045 RSA_VALIDATE_RET( sig != NULL );
2046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2048 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002049
Hanno Beckerfdf38032017-09-06 12:35:55 +01002050 /*
2051 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2052 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002053
Hanno Beckerfdf38032017-09-06 12:35:55 +01002054 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2055 ctx->len, sig ) ) != 0 )
2056 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002057
2058 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002059 * Call respective RSA primitive
2060 */
2061
2062 if( mode == MBEDTLS_RSA_PUBLIC )
2063 {
2064 /* Skip verification on a public key operation */
2065 return( mbedtls_rsa_public( ctx, sig, sig ) );
2066 }
2067
2068 /* Private key operation
2069 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002070 * In order to prevent Lenstra's attack, make the signature in a
2071 * temporary buffer and check it before returning it.
2072 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002073
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002074 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002075 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002076 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2077
Hanno Beckerfdf38032017-09-06 12:35:55 +01002078 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002079 if( verif == NULL )
2080 {
2081 mbedtls_free( sig_try );
2082 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2083 }
2084
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002085 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2086 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2087
Hanno Becker171a8f12017-09-06 12:32:16 +01002088 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002089 {
2090 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2091 goto cleanup;
2092 }
2093
2094 memcpy( sig, sig_try, ctx->len );
2095
2096cleanup:
2097 mbedtls_free( sig_try );
2098 mbedtls_free( verif );
2099
2100 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002101}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002103
2104/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 * Do an RSA operation to sign the message digest
2106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002108 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002109 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002112 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002113 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 unsigned char *sig )
2115{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002116 RSA_VALIDATE_RET( ctx != NULL );
2117 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2118 mode == MBEDTLS_RSA_PUBLIC );
2119 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2120 hashlen == 0 ) ||
2121 hash != NULL );
2122 RSA_VALIDATE_RET( sig != NULL );
2123
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 switch( ctx->padding )
2125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126#if defined(MBEDTLS_PKCS1_V15)
2127 case MBEDTLS_RSA_PKCS_V15:
2128 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002129 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002130#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132#if defined(MBEDTLS_PKCS1_V21)
2133 case MBEDTLS_RSA_PKCS_V21:
2134 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002135 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002136#endif
2137
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002141}
2142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002144/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002145 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002148 int (*f_rng)(void *, unsigned char *, size_t),
2149 void *p_rng,
2150 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002152 unsigned int hashlen,
2153 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002155 int expected_salt_len,
2156 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002157{
Janos Follath24eed8d2019-11-22 13:21:35 +00002158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002159 size_t siglen;
2160 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002161 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002163 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002164 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002165 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 const mbedtls_md_info_t *md_info;
2167 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002168 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002169
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002170 RSA_VALIDATE_RET( ctx != NULL );
2171 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2172 mode == MBEDTLS_RSA_PUBLIC );
2173 RSA_VALIDATE_RET( sig != NULL );
2174 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2175 hashlen == 0 ) ||
2176 hash != NULL );
2177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002180
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 siglen = ctx->len;
2182
Paul Bakker27fdf462011-06-09 13:55:13 +00002183 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2187 ? mbedtls_rsa_public( ctx, sig, buf )
2188 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002189
2190 if( ret != 0 )
2191 return( ret );
2192
2193 p = buf;
2194
Paul Bakkerb3869132013-02-28 17:21:01 +01002195 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 {
Simon Butcher02037452016-03-01 21:19:12 +00002200 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002202 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002206 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002209 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002213
Paul Bakkerb3869132013-02-28 17:21:01 +01002214 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002215
Simon Butcher02037452016-03-01 21:19:12 +00002216 /*
2217 * Note: EMSA-PSS verification is over the length of N - 1 bits
2218 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002219 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002220
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002221 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2222 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2223
Simon Butcher02037452016-03-01 21:19:12 +00002224 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002225 if( msb % 8 == 0 )
2226 {
2227 p++;
2228 siglen -= 1;
2229 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002230
Gilles Peskine139108a2017-10-18 19:03:42 +02002231 if( siglen < hlen + 2 )
2232 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2233 hash_start = p + siglen - hlen - 1;
2234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002236 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002237 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002238
Jaeden Amero66954e12018-01-25 16:05:54 +00002239 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2240 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002241 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002242
Paul Bakkerb3869132013-02-28 17:21:01 +01002243 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002244
Gilles Peskine6a54b022017-10-17 19:02:13 +02002245 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002246 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002247
Gilles Peskine91048a32017-10-19 17:46:14 +02002248 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002249 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002250 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2251 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002252 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002253
Gilles Peskine6a54b022017-10-17 19:02:13 +02002254 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002257 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002258 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002259 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2260 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002261 }
2262
Simon Butcher02037452016-03-01 21:19:12 +00002263 /*
2264 * Generate H = Hash( M' )
2265 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002266 ret = mbedtls_md_starts( &md_ctx );
2267 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002268 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002269 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2270 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002271 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002272 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2273 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002274 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002275 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2276 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002277 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002278 ret = mbedtls_md_finish( &md_ctx, result );
2279 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002280 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002281
Jaeden Amero66954e12018-01-25 16:05:54 +00002282 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002283 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002284 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002285 goto exit;
2286 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002287
2288exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002290
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002291 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002292}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002293
2294/*
2295 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002298 int (*f_rng)(void *, unsigned char *, size_t),
2299 void *p_rng,
2300 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002302 unsigned int hashlen,
2303 const unsigned char *hash,
2304 const unsigned char *sig )
2305{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002306 mbedtls_md_type_t mgf1_hash_id;
2307 RSA_VALIDATE_RET( ctx != NULL );
2308 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2309 mode == MBEDTLS_RSA_PUBLIC );
2310 RSA_VALIDATE_RET( sig != NULL );
2311 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2312 hashlen == 0 ) ||
2313 hash != NULL );
2314
2315 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002317 : md_alg;
2318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002320 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002322 sig ) );
2323
2324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002328/*
2329 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002332 int (*f_rng)(void *, unsigned char *, size_t),
2333 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002334 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002336 unsigned int hashlen,
2337 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002338 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002339{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002340 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002341 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002342 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002343
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002344 RSA_VALIDATE_RET( ctx != NULL );
2345 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2346 mode == MBEDTLS_RSA_PUBLIC );
2347 RSA_VALIDATE_RET( sig != NULL );
2348 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2349 hashlen == 0 ) ||
2350 hash != NULL );
2351
2352 sig_len = ctx->len;
2353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2355 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002356
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002357 /*
2358 * Prepare expected PKCS1 v1.5 encoding of hash.
2359 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002360
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002361 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2362 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2363 {
2364 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2365 goto cleanup;
2366 }
2367
2368 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2369 encoded_expected ) ) != 0 )
2370 goto cleanup;
2371
2372 /*
2373 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2374 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002377 ? mbedtls_rsa_public( ctx, sig, encoded )
2378 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002379 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002380 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002381
Simon Butcher02037452016-03-01 21:19:12 +00002382 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002383 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002384 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002385
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002386 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2387 sig_len ) ) != 0 )
2388 {
2389 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2390 goto cleanup;
2391 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002392
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002393cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002394
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002395 if( encoded != NULL )
2396 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002397 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002398 mbedtls_free( encoded );
2399 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002400
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002401 if( encoded_expected != NULL )
2402 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002403 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002404 mbedtls_free( encoded_expected );
2405 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002406
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002407 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002410
2411/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002412 * Do an RSA operation and check the message digest
2413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002415 int (*f_rng)(void *, unsigned char *, size_t),
2416 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002417 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002419 unsigned int hashlen,
2420 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002421 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002422{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002423 RSA_VALIDATE_RET( ctx != NULL );
2424 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2425 mode == MBEDTLS_RSA_PUBLIC );
2426 RSA_VALIDATE_RET( sig != NULL );
2427 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2428 hashlen == 0 ) ||
2429 hash != NULL );
2430
Paul Bakkerb3869132013-02-28 17:21:01 +01002431 switch( ctx->padding )
2432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433#if defined(MBEDTLS_PKCS1_V15)
2434 case MBEDTLS_RSA_PKCS_V15:
2435 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002436 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002437#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439#if defined(MBEDTLS_PKCS1_V21)
2440 case MBEDTLS_RSA_PKCS_V21:
2441 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002442 hashlen, hash, sig );
2443#endif
2444
2445 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002447 }
2448}
2449
2450/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002451 * Copy the components of an RSA key
2452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002454{
Janos Follath24eed8d2019-11-22 13:21:35 +00002455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002456 RSA_VALIDATE_RET( dst != NULL );
2457 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002458
2459 dst->ver = src->ver;
2460 dst->len = src->len;
2461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2463 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2466 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2467 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002468
2469#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2471 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2472 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2474 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002475#endif
2476
2477 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2480 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002481
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002482 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002483 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002484
2485cleanup:
2486 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002488
2489 return( ret );
2490}
2491
2492/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 * Free the components of an RSA key
2494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002496{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002497 if( ctx == NULL )
2498 return;
2499
2500 mbedtls_mpi_free( &ctx->Vi );
2501 mbedtls_mpi_free( &ctx->Vf );
2502 mbedtls_mpi_free( &ctx->RN );
2503 mbedtls_mpi_free( &ctx->D );
2504 mbedtls_mpi_free( &ctx->Q );
2505 mbedtls_mpi_free( &ctx->P );
2506 mbedtls_mpi_free( &ctx->E );
2507 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002508
Hanno Becker33c30a02017-08-23 07:00:22 +01002509#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002510 mbedtls_mpi_free( &ctx->RQ );
2511 mbedtls_mpi_free( &ctx->RP );
2512 mbedtls_mpi_free( &ctx->QP );
2513 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002514 mbedtls_mpi_free( &ctx->DP );
2515#endif /* MBEDTLS_RSA_NO_CRT */
2516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517#if defined(MBEDTLS_THREADING_C)
2518 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002519#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002520}
2521
Hanno Beckerab377312017-08-23 16:24:51 +01002522#endif /* !MBEDTLS_RSA_ALT */
2523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002525
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002526#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
2528/*
2529 * Example RSA-1024 keypair, for test purposes
2530 */
2531#define KEY_LEN 128
2532
2533#define RSA_N "9292758453063D803DD603D5E777D788" \
2534 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2535 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2536 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2537 "93A89813FBF3C4F8066D2D800F7C38A8" \
2538 "1AE31942917403FF4946B0A83D3D3E05" \
2539 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2540 "5E94BB77B07507233A0BC7BAC8F90F79"
2541
2542#define RSA_E "10001"
2543
2544#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2545 "66CA472BC44D253102F8B4A9D3BFA750" \
2546 "91386C0077937FE33FA3252D28855837" \
2547 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2548 "DF79C5CE07EE72C7F123142198164234" \
2549 "CABB724CF78B8173B9F880FC86322407" \
2550 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2551 "071513A1E85B5DFA031F21ECAE91A34D"
2552
2553#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2554 "2C01CAD19EA484A87EA4377637E75500" \
2555 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2556 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2557
2558#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2559 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2560 "910E4168387E3C30AA1E00C339A79508" \
2561 "8452DD96A9A5EA5D9DCA68DA636032AF"
2562
Paul Bakker5121ce52009-01-03 21:22:43 +00002563#define PT_LEN 24
2564#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2565 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002568static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002569{
gufe44c2620da2020-08-03 17:56:50 +02002570#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002571 size_t i;
2572
Paul Bakker545570e2010-07-18 09:00:25 +00002573 if( rng_state != NULL )
2574 rng_state = NULL;
2575
Paul Bakkera3d195c2011-11-27 21:07:34 +00002576 for( i = 0; i < len; ++i )
2577 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002578#else
2579 if( rng_state != NULL )
2580 rng_state = NULL;
2581
2582 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002583#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002584
Paul Bakkera3d195c2011-11-27 21:07:34 +00002585 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002586}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002588
Paul Bakker5121ce52009-01-03 21:22:43 +00002589/*
2590 * Checkup routine
2591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002593{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002594 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002596 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 unsigned char rsa_plaintext[PT_LEN];
2599 unsigned char rsa_decrypted[PT_LEN];
2600 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002602 unsigned char sha1sum[20];
2603#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Hanno Becker3a701162017-08-22 13:52:43 +01002605 mbedtls_mpi K;
2606
2607 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
Hanno Becker3a701162017-08-22 13:52:43 +01002610 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2611 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2612 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2613 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2614 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2615 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2616 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2617 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2618 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2619 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2620
Hanno Becker7f25f852017-10-10 16:56:22 +01002621 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
2623 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2627 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 {
2629 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Hanno Becker5bc87292017-05-03 15:09:31 +01002632 ret = 1;
2633 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 }
2635
2636 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
2639 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2640
Hanno Becker98838b02017-10-02 13:16:10 +01002641 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2642 PT_LEN, rsa_plaintext,
2643 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 {
2645 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Hanno Becker5bc87292017-05-03 15:09:31 +01002648 ret = 1;
2649 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 }
2651
2652 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Hanno Becker98838b02017-10-02 13:16:10 +01002655 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2656 &len, rsa_ciphertext, rsa_decrypted,
2657 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002658 {
2659 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Hanno Becker5bc87292017-05-03 15:09:31 +01002662 ret = 1;
2663 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 }
2665
2666 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2667 {
2668 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Hanno Becker5bc87292017-05-03 15:09:31 +01002671 ret = 1;
2672 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002673 }
2674
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002675 if( verbose != 0 )
2676 mbedtls_printf( "passed\n" );
2677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002680 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002682 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002683 {
2684 if( verbose != 0 )
2685 mbedtls_printf( "failed\n" );
2686
2687 return( 1 );
2688 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
Hanno Becker98838b02017-10-02 13:16:10 +01002690 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2691 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2692 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 {
2694 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Hanno Becker5bc87292017-05-03 15:09:31 +01002697 ret = 1;
2698 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
2700
2701 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
Hanno Becker98838b02017-10-02 13:16:10 +01002704 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2705 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2706 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 {
2708 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Hanno Becker5bc87292017-05-03 15:09:31 +01002711 ret = 1;
2712 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 }
2714
2715 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002716 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002719 if( verbose != 0 )
2720 mbedtls_printf( "\n" );
2721
Paul Bakker3d8fb632014-04-17 12:42:41 +02002722cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002723 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 mbedtls_rsa_free( &rsa );
2725#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002726 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002728 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729}
2730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733#endif /* MBEDTLS_RSA_C */