blob: 4958cad3084d1b39ad86a10141834a8ed5053975 [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é-Gonnard750d3c72020-06-26 11:19:12 +0200779 mbedtls_mpi R;
780
781 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200782
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200783 if( ctx->Vf.p != NULL )
784 {
785 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
787 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
788 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
789 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200790
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200791 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200792 }
793
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200794 /* Unblinding value: Vf = random number, invertible mod N */
795 do {
796 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200797 {
798 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
799 goto cleanup;
800 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200803
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200804 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200805 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
806 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
807 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
808
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200809 /* At this point, Vi is invertible mod N if and only if both Vf and R
810 * are invertible mod N. If one of them isn't, we don't need to know
811 * which one, we just loop and choose new values for both of them.
812 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200813 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500814 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200815 goto cleanup;
816
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500817 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
818
819 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
820 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
821 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200822
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200823 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200824 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 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 +0200826
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200827
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200828cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200829 mbedtls_mpi_free( &R );
830
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200831 return( ret );
832}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200833
Paul Bakker5121ce52009-01-03 21:22:43 +0000834/*
Janos Follathe81102e2017-03-22 13:38:28 +0000835 * Exponent blinding supposed to prevent side-channel attacks using multiple
836 * traces of measurements to recover the RSA key. The more collisions are there,
837 * the more bits of the key can be recovered. See [3].
838 *
839 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
840 * observations on avarage.
841 *
842 * For example with 28 byte blinding to achieve 2 collisions the adversary has
843 * to make 2^112 observations on avarage.
844 *
845 * (With the currently (as of 2017 April) known best algorithms breaking 2048
846 * bit RSA requires approximately as much time as trying out 2^112 random keys.
847 * Thus in this sense with 28 byte blinding the security is not reduced by
848 * side-channel attacks like the one in [3])
849 *
850 * This countermeasure does not help if the key recovery is possible with a
851 * single trace.
852 */
853#define RSA_EXPONENT_BLINDING 28
854
855/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 * Do an RSA private key operation
857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200859 int (*f_rng)(void *, unsigned char *, size_t),
860 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000861 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 unsigned char *output )
863{
Janos Follath24eed8d2019-11-22 13:21:35 +0000864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000865 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100866
867 /* Temporary holding the result */
868 mbedtls_mpi T;
869
870 /* Temporaries holding P-1, Q-1 and the
871 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000872 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100873
874#if !defined(MBEDTLS_RSA_NO_CRT)
875 /* Temporaries holding the results mod p resp. mod q. */
876 mbedtls_mpi TP, TQ;
877
878 /* Temporaries holding the blinded exponents for
879 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000880 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100881
882 /* Pointers to actual exponents to be used - either the unblinded
883 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000884 mbedtls_mpi *DP = &ctx->DP;
885 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100886#else
887 /* Temporary holding the blinded exponent (if used). */
888 mbedtls_mpi D_blind;
889
890 /* Pointer to actual exponent to be used - either the unblinded
891 * or the blinded one, depending on the presence of a PRNG. */
892 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100893#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100894
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100895 /* Temporaries holding the initial input and the double
896 * checked result; should be the same in the end. */
897 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000898
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500899 RSA_VALIDATE_RET( ctx != NULL );
900 RSA_VALIDATE_RET( input != NULL );
901 RSA_VALIDATE_RET( output != NULL );
902
Hanno Beckerebd2c022017-10-12 10:54:53 +0100903 if( rsa_check_context( ctx, 1 /* private key checks */,
904 f_rng != NULL /* blinding y/n */ ) != 0 )
905 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100906 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100907 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100908
Hanno Becker06811ce2017-05-03 15:10:34 +0100909#if defined(MBEDTLS_THREADING_C)
910 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
911 return( ret );
912#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000913
Hanno Becker06811ce2017-05-03 15:10:34 +0100914 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100915 mbedtls_mpi_init( &T );
916
917 mbedtls_mpi_init( &P1 );
918 mbedtls_mpi_init( &Q1 );
919 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000920
Janos Follathf9203b42017-03-22 15:13:15 +0000921 if( f_rng != NULL )
922 {
Janos Follathe81102e2017-03-22 13:38:28 +0000923#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000924 mbedtls_mpi_init( &D_blind );
925#else
926 mbedtls_mpi_init( &DP_blind );
927 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000928#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000929 }
Janos Follathe81102e2017-03-22 13:38:28 +0000930
Hanno Becker06811ce2017-05-03 15:10:34 +0100931#if !defined(MBEDTLS_RSA_NO_CRT)
932 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200933#endif
934
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100935 mbedtls_mpi_init( &I );
936 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100937
938 /* End of MPI initialization */
939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
941 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000942 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200943 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
944 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000945 }
946
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100947 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100948
Paul Bakkerf451bac2013-08-30 15:37:02 +0200949 if( f_rng != NULL )
950 {
951 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200952 * Blinding
953 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200954 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200955 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
956 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000958
Janos Follathe81102e2017-03-22 13:38:28 +0000959 /*
960 * Exponent blinding
961 */
962 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
963 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
964
Janos Follathf9203b42017-03-22 15:13:15 +0000965#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000966 /*
967 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
968 */
969 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
970 f_rng, p_rng ) );
971 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
972 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
973 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
974
975 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000976#else
977 /*
978 * DP_blind = ( P - 1 ) * R + DP
979 */
980 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
981 f_rng, p_rng ) );
982 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
983 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
984 &ctx->DP ) );
985
986 DP = &DP_blind;
987
988 /*
989 * DQ_blind = ( Q - 1 ) * R + DQ
990 */
991 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
992 f_rng, p_rng ) );
993 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
994 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
995 &ctx->DQ ) );
996
997 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000998#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200999 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001002 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001003#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001004 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001005 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001007 * TP = input ^ dP mod P
1008 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001010
1011 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1012 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
1014 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001015 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001016 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001017 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1018 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1019 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
1021 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001022 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001024 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1025 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001027
Paul Bakkerf451bac2013-08-30 15:37:02 +02001028 if( f_rng != NULL )
1029 {
1030 /*
1031 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001032 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001033 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001034 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Hanno Becker2dec5e82017-10-03 07:49:52 +01001038 /* Verify the result to prevent glitching attacks. */
1039 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1040 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001041 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001042 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001043 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1044 goto cleanup;
1045 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001046
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
1050cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001052 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1053 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001054#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001055
Hanno Becker06811ce2017-05-03 15:10:34 +01001056 mbedtls_mpi_free( &P1 );
1057 mbedtls_mpi_free( &Q1 );
1058 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001059
1060 if( f_rng != NULL )
1061 {
Janos Follathe81102e2017-03-22 13:38:28 +00001062#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001063 mbedtls_mpi_free( &D_blind );
1064#else
1065 mbedtls_mpi_free( &DP_blind );
1066 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001067#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001068 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001069
Hanno Becker06811ce2017-05-03 15:10:34 +01001070 mbedtls_mpi_free( &T );
1071
1072#if !defined(MBEDTLS_RSA_NO_CRT)
1073 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1074#endif
1075
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001076 mbedtls_mpi_free( &C );
1077 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001078
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
1082 return( 0 );
1083}
1084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001086/**
1087 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1088 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001089 * \param dst buffer to mask
1090 * \param dlen length of destination buffer
1091 * \param src source of the mask generation
1092 * \param slen length of the source buffer
1093 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001094 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001095static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001099 unsigned char counter[4];
1100 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001101 unsigned int hlen;
1102 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001103 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001106 memset( counter, 0, 4 );
1107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001109
Simon Butcher02037452016-03-01 21:19:12 +00001110 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001111 p = dst;
1112
1113 while( dlen > 0 )
1114 {
1115 use_len = hlen;
1116 if( dlen < hlen )
1117 use_len = dlen;
1118
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001119 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1120 goto exit;
1121 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1122 goto exit;
1123 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1124 goto exit;
1125 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1126 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001127
1128 for( i = 0; i < use_len; ++i )
1129 *p++ ^= mask[i];
1130
1131 counter[3]++;
1132
1133 dlen -= use_len;
1134 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001135
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001136exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001137 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001138
1139 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001144/*
1145 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001148 int (*f_rng)(void *, unsigned char *, size_t),
1149 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001150 int mode,
1151 const unsigned char *label, size_t label_len,
1152 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001153 const unsigned char *input,
1154 unsigned char *output )
1155{
1156 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 unsigned char *p = output;
1159 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 const mbedtls_md_info_t *md_info;
1161 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001162
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001163 RSA_VALIDATE_RET( ctx != NULL );
1164 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1165 mode == MBEDTLS_RSA_PUBLIC );
1166 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001167 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001168 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1171 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001172
1173 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001177 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001179
1180 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001182
Simon Butcher02037452016-03-01 21:19:12 +00001183 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001184 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001186
1187 memset( output, 0, olen );
1188
1189 *p++ = 0;
1190
Simon Butcher02037452016-03-01 21:19:12 +00001191 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001192 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001194
1195 p += hlen;
1196
Simon Butcher02037452016-03-01 21:19:12 +00001197 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001198 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1199 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001200 p += hlen;
1201 p += olen - 2 * hlen - 2 - ilen;
1202 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001203 if( ilen != 0 )
1204 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001207 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001208 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001209
Simon Butcher02037452016-03-01 21:19:12 +00001210 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001211 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1212 &md_ctx ) ) != 0 )
1213 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001214
Simon Butcher02037452016-03-01 21:19:12 +00001215 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001216 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1217 &md_ctx ) ) != 0 )
1218 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001219
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001220exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001222
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001223 if( ret != 0 )
1224 return( ret );
1225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 return( ( mode == MBEDTLS_RSA_PUBLIC )
1227 ? mbedtls_rsa_public( ctx, output, output )
1228 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001233/*
1234 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001237 int (*f_rng)(void *, unsigned char *, size_t),
1238 void *p_rng,
1239 int mode, size_t ilen,
1240 const unsigned char *input,
1241 unsigned char *output )
1242{
1243 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001245 unsigned char *p = output;
1246
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001247 RSA_VALIDATE_RET( ctx != NULL );
1248 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1249 mode == MBEDTLS_RSA_PUBLIC );
1250 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001251 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001252
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001253 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001255
1256 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001257
Simon Butcher02037452016-03-01 21:19:12 +00001258 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001259 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001261
1262 nb_pad = olen - 3 - ilen;
1263
1264 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001266 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001267 if( f_rng == NULL )
1268 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001271
1272 while( nb_pad-- > 0 )
1273 {
1274 int rng_dl = 100;
1275
1276 do {
1277 ret = f_rng( p_rng, p, 1 );
1278 } while( *p == 0 && --rng_dl && ret == 0 );
1279
Simon Butcher02037452016-03-01 21:19:12 +00001280 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001281 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001283
1284 p++;
1285 }
1286 }
1287 else
1288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001290
1291 while( nb_pad-- > 0 )
1292 *p++ = 0xFF;
1293 }
1294
1295 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001296 if( ilen != 0 )
1297 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 return( ( mode == MBEDTLS_RSA_PUBLIC )
1300 ? mbedtls_rsa_public( ctx, output, output )
1301 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001304
Paul Bakker5121ce52009-01-03 21:22:43 +00001305/*
1306 * Add the message padding, then do an RSA operation
1307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001309 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001310 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001311 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001312 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 unsigned char *output )
1314{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001315 RSA_VALIDATE_RET( ctx != NULL );
1316 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1317 mode == MBEDTLS_RSA_PUBLIC );
1318 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001319 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001320
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 switch( ctx->padding )
1322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_PKCS1_V15)
1324 case MBEDTLS_RSA_PKCS_V15:
1325 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001326 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001327#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_PKCS1_V21)
1330 case MBEDTLS_RSA_PKCS_V21:
1331 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001332 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
1335 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001338}
1339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001341/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001342 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001345 int (*f_rng)(void *, unsigned char *, size_t),
1346 void *p_rng,
1347 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001348 const unsigned char *label, size_t label_len,
1349 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001350 const unsigned char *input,
1351 unsigned char *output,
1352 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001353{
Janos Follath24eed8d2019-11-22 13:21:35 +00001354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001355 size_t ilen, i, pad_len;
1356 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1358 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001359 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 const mbedtls_md_info_t *md_info;
1361 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001362
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001363 RSA_VALIDATE_RET( ctx != NULL );
1364 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1365 mode == MBEDTLS_RSA_PUBLIC );
1366 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1367 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1368 RSA_VALIDATE_RET( input != NULL );
1369 RSA_VALIDATE_RET( olen != NULL );
1370
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001371 /*
1372 * Parameters sanity checks
1373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1375 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
1377 ilen = ctx->len;
1378
Paul Bakker27fdf462011-06-09 13:55:13 +00001379 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001383 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001385
Janos Follathc17cda12016-02-11 11:08:18 +00001386 hlen = mbedtls_md_get_size( md_info );
1387
1388 // checking for integer underflow
1389 if( 2 * hlen + 2 > ilen )
1390 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1391
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001392 /*
1393 * RSA operation
1394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1396 ? mbedtls_rsa_public( ctx, input, buf )
1397 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
1399 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001400 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001402 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001403 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001406 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1407 {
1408 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001409 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001410 }
1411
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001412 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001413 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1414 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001415 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001416 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1417 &md_ctx ) ) != 0 )
1418 {
1419 mbedtls_md_free( &md_ctx );
1420 goto cleanup;
1421 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001424
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001425 /* Generate lHash */
1426 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1427 goto cleanup;
1428
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001429 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001430 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001431 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001433 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001435 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001436
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001437 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001438
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001439 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001440 for( i = 0; i < hlen; i++ )
1441 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001442
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001443 /* Get zero-padding len, but always read till end of buffer
1444 * (minus one, for the 01 byte) */
1445 pad_len = 0;
1446 pad_done = 0;
1447 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1448 {
1449 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001450 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001451 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001452
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001453 p += pad_len;
1454 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001455
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001456 /*
1457 * The only information "leaked" is whether the padding was correct or not
1458 * (eg, no data is copied if it was not correct). This meets the
1459 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1460 * the different error conditions.
1461 */
1462 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001463 {
1464 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1465 goto cleanup;
1466 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001467
Paul Bakker66d5d072014-06-17 16:39:18 +02001468 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001469 {
1470 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1471 goto cleanup;
1472 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001473
1474 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001475 if( *olen != 0 )
1476 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001477 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001478
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001479cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001480 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1481 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001482
1483 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001488/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1489 *
1490 * \param value The value to analyze.
1491 * \return Zero if \p value is zero, otherwise all-bits-one.
1492 */
1493static unsigned all_or_nothing_int( unsigned value )
1494{
1495 /* MSVC has a warning about unary minus on unsigned, but this is
1496 * well-defined and precisely what we want to do here */
1497#if defined(_MSC_VER)
1498#pragma warning( push )
1499#pragma warning( disable : 4146 )
1500#endif
1501 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1502#if defined(_MSC_VER)
1503#pragma warning( pop )
1504#endif
1505}
1506
1507/** Check whether a size is out of bounds, without branches.
1508 *
1509 * This is equivalent to `size > max`, but is likely to be compiled to
1510 * to code using bitwise operation rather than a branch.
1511 *
1512 * \param size Size to check.
1513 * \param max Maximum desired value for \p size.
1514 * \return \c 0 if `size <= max`.
1515 * \return \c 1 if `size > max`.
1516 */
1517static unsigned size_greater_than( size_t size, size_t max )
1518{
1519 /* Return the sign bit (1 for negative) of (max - size). */
1520 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1521}
1522
1523/** Choose between two integer values, without branches.
1524 *
1525 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1526 * to code using bitwise operation rather than a branch.
1527 *
1528 * \param cond Condition to test.
1529 * \param if1 Value to use if \p cond is nonzero.
1530 * \param if0 Value to use if \p cond is zero.
1531 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1532 */
1533static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
1534{
1535 unsigned mask = all_or_nothing_int( cond );
1536 return( ( mask & if1 ) | (~mask & if0 ) );
1537}
1538
1539/** Shift some data towards the left inside a buffer without leaking
1540 * the length of the data through side channels.
1541 *
1542 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1543 * ```
1544 * memmove(start, start + offset, total - offset);
1545 * memset(start + offset, 0, total - offset);
1546 * ```
1547 * but it strives to use a memory access pattern (and thus total timing)
1548 * that does not depend on \p offset. This timing independence comes at
1549 * the expense of performance.
1550 *
1551 * \param start Pointer to the start of the buffer.
1552 * \param total Total size of the buffer.
1553 * \param offset Offset from which to copy \p total - \p offset bytes.
1554 */
1555static void mem_move_to_left( void *start,
1556 size_t total,
1557 size_t offset )
1558{
1559 volatile unsigned char *buf = start;
1560 size_t i, n;
1561 if( total == 0 )
1562 return;
1563 for( i = 0; i < total; i++ )
1564 {
1565 unsigned no_op = size_greater_than( total - offset, i );
1566 /* The first `total - offset` passes are a no-op. The last
1567 * `offset` passes shift the data one byte to the left and
1568 * zero out the last byte. */
1569 for( n = 0; n < total - 1; n++ )
1570 {
1571 unsigned char current = buf[n];
1572 unsigned char next = buf[n+1];
1573 buf[n] = if_int( no_op, current, next );
1574 }
1575 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1576 }
1577}
1578
Paul Bakkerb3869132013-02-28 17:21:01 +01001579/*
1580 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001583 int (*f_rng)(void *, unsigned char *, size_t),
1584 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001585 int mode, size_t *olen,
1586 const unsigned char *input,
1587 unsigned char *output,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001588 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001589{
Janos Follath24eed8d2019-11-22 13:21:35 +00001590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001591 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001593 /* The following variables take sensitive values: their value must
1594 * not leak into the observable behavior of the function other than
1595 * the designated outputs (output, olen, return value). Otherwise
1596 * this would open the execution of the function to
1597 * side-channel-based variants of the Bleichenbacher padding oracle
1598 * attack. Potential side channels include overall timing, memory
1599 * access patterns (especially visible to an adversary who has access
1600 * to a shared memory cache), and branches (especially visible to
1601 * an adversary who has access to a shared code cache or to a shared
1602 * branch predictor). */
1603 size_t pad_count = 0;
1604 unsigned bad = 0;
1605 unsigned char pad_done = 0;
1606 size_t plaintext_size = 0;
1607 unsigned output_too_large;
1608
1609 RSA_VALIDATE_RET( ctx != NULL );
1610 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1611 mode == MBEDTLS_RSA_PUBLIC );
1612 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1613 RSA_VALIDATE_RET( input != NULL );
1614 RSA_VALIDATE_RET( olen != NULL );
1615
1616 ilen = ctx->len;
1617 plaintext_max_size = ( output_max_len > ilen - 11 ?
1618 ilen - 11 :
1619 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1622 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001623
Paul Bakkerb3869132013-02-28 17:21:01 +01001624 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1628 ? mbedtls_rsa_public( ctx, input, buf )
1629 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001630
1631 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001632 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001633
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001634 /* Check and get padding length in constant time and constant
1635 * memory trace. The first byte must be 0. */
1636 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001640 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1641 * where PS must be at least 8 nonzero bytes. */
1642 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001643
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001644 /* Read the whole buffer. Set pad_done to nonzero if we find
1645 * the 0x00 byte and remember the padding length in pad_count. */
1646 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001647 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001648 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001649 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001650 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001651 }
1652 else
1653 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001654 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1655 * where PS must be at least 8 bytes with the value 0xFF. */
1656 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001657
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001658 /* Read the whole buffer. Set pad_done to nonzero if we find
1659 * the 0x00 byte and remember the padding length in pad_count.
1660 * If there's a non-0xff byte in the padding, the padding is bad. */
1661 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001662 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001663 pad_done |= if_int( buf[i], 0, 1 );
1664 pad_count += if_int( pad_done, 0, 1 );
1665 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001667 }
1668
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001669 /* If pad_done is still zero, there's no data, only unfinished padding. */
1670 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001671
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001672 /* There must be at least 8 bytes of padding. */
1673 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001674
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001675 /* If the padding is valid, set plaintext_size to the number of
1676 * remaining bytes after stripping the padding. If the padding
1677 * is invalid, avoid leaking this fact through the size of the
1678 * output: use the maximum message size that fits in the output
1679 * buffer. Do it without branches to avoid leaking the padding
1680 * validity through timing. RSA keys are small enough that all the
1681 * size_t values involved fit in unsigned int. */
1682 plaintext_size = if_int( bad,
1683 (unsigned) plaintext_max_size,
1684 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001685
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001686 /* Set output_too_large to 0 if the plaintext fits in the output
1687 * buffer and to 1 otherwise. */
1688 output_too_large = size_greater_than( plaintext_size,
1689 plaintext_max_size );
1690
1691 /* Set ret without branches to avoid timing attacks. Return:
1692 * - INVALID_PADDING if the padding is bad (bad != 0).
1693 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1694 * plaintext does not fit in the output buffer.
1695 * - 0 if the padding is correct. */
1696 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1697 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1698 0 ) );
1699
1700 /* If the padding is bad or the plaintext is too large, zero the
1701 * data that we're about to copy to the output buffer.
1702 * We need to copy the same amount of data
1703 * from the same buffer whether the padding is good or not to
1704 * avoid leaking the padding validity through overall timing or
1705 * through memory or cache access patterns. */
1706 bad = all_or_nothing_int( bad | output_too_large );
1707 for( i = 11; i < ilen; i++ )
1708 buf[i] &= ~bad;
1709
1710 /* If the plaintext is too large, truncate it to the buffer size.
1711 * Copy anyway to avoid revealing the length through timing, because
1712 * revealing the length is as bad as revealing the padding validity
1713 * for a Bleichenbacher attack. */
1714 plaintext_size = if_int( output_too_large,
1715 (unsigned) plaintext_max_size,
1716 (unsigned) plaintext_size );
1717
1718 /* Move the plaintext to the leftmost position where it can start in
1719 * the working buffer, i.e. make it start plaintext_max_size from
1720 * the end of the buffer. Do this with a memory access trace that
1721 * does not depend on the plaintext size. After this move, the
1722 * starting location of the plaintext is no longer sensitive
1723 * information. */
1724 mem_move_to_left( buf + ilen - plaintext_max_size,
1725 plaintext_max_size,
1726 plaintext_max_size - plaintext_size );
1727
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001728 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1729 * buffer. If output_max_len is 0, then output may be an invalid pointer
1730 * and the result of memcpy() would be undefined; prevent undefined
1731 * behavior making sure to depend only on output_max_len (the size of the
1732 * user-provided output buffer), which is independent from plaintext
1733 * length, validity of padding, success of the decryption, and other
1734 * secrets. */
1735 if( output_max_len != 0 )
1736 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001737
1738 /* Report the amount of data we copied to the output buffer. In case
1739 * of errors (bad padding or output too large), the value of *olen
1740 * when this function returns is not specified. Making it equivalent
1741 * to the good case limits the risks of leaking the padding validity. */
1742 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001744cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001745 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001746
1747 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
1751/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001752 * Do an RSA operation, then remove the message padding
1753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001755 int (*f_rng)(void *, unsigned char *, size_t),
1756 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001757 int mode, size_t *olen,
1758 const unsigned char *input,
1759 unsigned char *output,
1760 size_t output_max_len)
1761{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001762 RSA_VALIDATE_RET( ctx != NULL );
1763 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1764 mode == MBEDTLS_RSA_PUBLIC );
1765 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1766 RSA_VALIDATE_RET( input != NULL );
1767 RSA_VALIDATE_RET( olen != NULL );
1768
Paul Bakkerb3869132013-02-28 17:21:01 +01001769 switch( ctx->padding )
1770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771#if defined(MBEDTLS_PKCS1_V15)
1772 case MBEDTLS_RSA_PKCS_V15:
1773 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001774 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001775#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777#if defined(MBEDTLS_PKCS1_V21)
1778 case MBEDTLS_RSA_PKCS_V21:
1779 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001780 olen, input, output,
1781 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001782#endif
1783
1784 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001786 }
1787}
1788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001790static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001791 int (*f_rng)(void *, unsigned char *, size_t),
1792 void *p_rng,
1793 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001795 unsigned int hashlen,
1796 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001797 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001798 unsigned char *sig )
1799{
1800 size_t olen;
1801 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001802 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001803 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001805 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 const mbedtls_md_info_t *md_info;
1807 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001808 RSA_VALIDATE_RET( ctx != NULL );
1809 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1810 mode == MBEDTLS_RSA_PUBLIC );
1811 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1812 hashlen == 0 ) ||
1813 hash != NULL );
1814 RSA_VALIDATE_RET( sig != NULL );
Cédric Meutera05cbec2020-04-25 15:02:34 +02001815 RSA_VALIDATE_RET( saltlen == MBEDTLS_RSA_SALT_LEN_ANY ||
1816 saltlen > 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1819 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001820
1821 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001823
1824 olen = ctx->len;
1825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001827 {
Simon Butcher02037452016-03-01 21:19:12 +00001828 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001830 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001834 }
1835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001837 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001841
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001842 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1843 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001844 /* Calculate the largest possible salt length, up to the hash size.
1845 * Normally this is the hash length, which is the maximum salt length
1846 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001847 * enough room, use the maximum salt length that fits. The constraint is
1848 * that the hash length plus the salt length plus 2 bytes must be at most
1849 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1850 * (PKCS#1 v2.2) §9.1.1 step 3. */
1851 min_slen = hlen - 2;
1852 if( olen < hlen + min_slen + 2 )
1853 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1854 else if( olen >= hlen + hlen + 2 )
1855 slen = hlen;
1856 else
1857 slen = olen - hlen - 2;
1858 }
Cédric Meuter2ee08502020-12-28 14:34:29 +01001859 else if ( (saltlen < 0) || ((size_t) saltlen > olen - hlen - 2) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001860 {
1861 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1862 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001863 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001864 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001865 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001866 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001867
1868 memset( sig, 0, olen );
1869
Simon Butcher02037452016-03-01 21:19:12 +00001870 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001871 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001872 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001873 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001874
1875 /* Generate salt of length slen in place in the encoded message */
1876 salt = p;
1877 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
1878 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
1879
Paul Bakkerb3869132013-02-28 17:21:01 +01001880 p += slen;
1881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001883 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001884 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001885
Simon Butcher02037452016-03-01 21:19:12 +00001886 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001887 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1888 goto exit;
1889 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1890 goto exit;
1891 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1892 goto exit;
1893 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1894 goto exit;
1895 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1896 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001897
Simon Butcher02037452016-03-01 21:19:12 +00001898 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001899 if( msb % 8 == 0 )
1900 offset = 1;
1901
Simon Butcher02037452016-03-01 21:19:12 +00001902 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001903 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1904 &md_ctx ) ) != 0 )
1905 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001906
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001907 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001908 sig[0] &= 0xFF >> ( olen * 8 - msb );
1909
1910 p += hlen;
1911 *p++ = 0xBC;
1912
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001913exit:
1914 mbedtls_md_free( &md_ctx );
1915
1916 if( ret != 0 )
1917 return( ret );
1918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 return( ( mode == MBEDTLS_RSA_PUBLIC )
1920 ? mbedtls_rsa_public( ctx, sig, sig )
1921 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001922}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001923
1924/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001925 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1926 * the option to pass in the salt length.
1927 */
1928int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1929 int (*f_rng)(void *, unsigned char *, size_t),
1930 void *p_rng,
1931 mbedtls_md_type_t md_alg,
1932 unsigned int hashlen,
1933 const unsigned char *hash,
1934 int saltlen,
1935 unsigned char *sig )
1936{
1937 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, md_alg,
1938 hashlen, hash, saltlen, sig );
1939}
1940
1941
1942/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001943 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1944 */
1945int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1946 int (*f_rng)(void *, unsigned char *, size_t),
1947 void *p_rng,
1948 int mode,
1949 mbedtls_md_type_t md_alg,
1950 unsigned int hashlen,
1951 const unsigned char *hash,
1952 unsigned char *sig )
1953{
Cédric Meuterf3fab332020-04-25 11:30:45 +02001954 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
1955 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001956}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001960/*
1961 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1962 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001963
1964/* Construct a PKCS v1.5 encoding of a hashed message
1965 *
1966 * This is used both for signature generation and verification.
1967 *
1968 * Parameters:
1969 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001970 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001971 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001972 * - hash: Buffer containing the hashed message or the raw data.
1973 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001974 * - dst: Buffer to hold the encoded message.
1975 *
1976 * Assumptions:
1977 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1978 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001979 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001980 *
1981 */
1982static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1983 unsigned int hashlen,
1984 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001985 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001986 unsigned char *dst )
1987{
1988 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001989 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001990 unsigned char *p = dst;
1991 const char *oid = NULL;
1992
1993 /* Are we signing hashed or raw data? */
1994 if( md_alg != MBEDTLS_MD_NONE )
1995 {
1996 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1997 if( md_info == NULL )
1998 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1999
2000 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
2001 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2002
2003 hashlen = mbedtls_md_get_size( md_info );
2004
2005 /* Double-check that 8 + hashlen + oid_size can be used as a
2006 * 1-byte ASN.1 length encoding and that there's no overflow. */
2007 if( 8 + hashlen + oid_size >= 0x80 ||
2008 10 + hashlen < hashlen ||
2009 10 + hashlen + oid_size < 10 + hashlen )
2010 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2011
2012 /*
2013 * Static bounds check:
2014 * - Need 10 bytes for five tag-length pairs.
2015 * (Insist on 1-byte length encodings to protect against variants of
2016 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2017 * - Need hashlen bytes for hash
2018 * - Need oid_size bytes for hash alg OID.
2019 */
2020 if( nb_pad < 10 + hashlen + oid_size )
2021 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2022 nb_pad -= 10 + hashlen + oid_size;
2023 }
2024 else
2025 {
2026 if( nb_pad < hashlen )
2027 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2028
2029 nb_pad -= hashlen;
2030 }
2031
Hanno Becker2b2f8982017-09-27 17:10:03 +01002032 /* Need space for signature header and padding delimiter (3 bytes),
2033 * and 8 bytes for the minimal padding */
2034 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002035 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2036 nb_pad -= 3;
2037
2038 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002039 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002040
2041 /* Write signature header and padding */
2042 *p++ = 0;
2043 *p++ = MBEDTLS_RSA_SIGN;
2044 memset( p, 0xFF, nb_pad );
2045 p += nb_pad;
2046 *p++ = 0;
2047
2048 /* Are we signing raw data? */
2049 if( md_alg == MBEDTLS_MD_NONE )
2050 {
2051 memcpy( p, hash, hashlen );
2052 return( 0 );
2053 }
2054
2055 /* Signing hashed data, add corresponding ASN.1 structure
2056 *
2057 * DigestInfo ::= SEQUENCE {
2058 * digestAlgorithm DigestAlgorithmIdentifier,
2059 * digest Digest }
2060 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2061 * Digest ::= OCTET STRING
2062 *
2063 * Schematic:
2064 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2065 * TAG-NULL + LEN [ NULL ] ]
2066 * TAG-OCTET + LEN [ HASH ] ]
2067 */
2068 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002069 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002070 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002071 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002072 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002073 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002074 memcpy( p, oid, oid_size );
2075 p += oid_size;
2076 *p++ = MBEDTLS_ASN1_NULL;
2077 *p++ = 0x00;
2078 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002079 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002080 memcpy( p, hash, hashlen );
2081 p += hashlen;
2082
2083 /* Just a sanity-check, should be automatic
2084 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002085 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002086 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002087 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002088 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2089 }
2090
2091 return( 0 );
2092}
2093
Paul Bakkerb3869132013-02-28 17:21:01 +01002094/*
2095 * Do an RSA operation to sign the message digest
2096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002098 int (*f_rng)(void *, unsigned char *, size_t),
2099 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002100 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002102 unsigned int hashlen,
2103 const unsigned char *hash,
2104 unsigned char *sig )
2105{
Janos Follath24eed8d2019-11-22 13:21:35 +00002106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002107 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002108
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002109 RSA_VALIDATE_RET( ctx != NULL );
2110 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2111 mode == MBEDTLS_RSA_PUBLIC );
2112 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2113 hashlen == 0 ) ||
2114 hash != NULL );
2115 RSA_VALIDATE_RET( sig != NULL );
2116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2118 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002119
Hanno Beckerfdf38032017-09-06 12:35:55 +01002120 /*
2121 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2122 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002123
Hanno Beckerfdf38032017-09-06 12:35:55 +01002124 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2125 ctx->len, sig ) ) != 0 )
2126 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002127
2128 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002129 * Call respective RSA primitive
2130 */
2131
2132 if( mode == MBEDTLS_RSA_PUBLIC )
2133 {
2134 /* Skip verification on a public key operation */
2135 return( mbedtls_rsa_public( ctx, sig, sig ) );
2136 }
2137
2138 /* Private key operation
2139 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002140 * In order to prevent Lenstra's attack, make the signature in a
2141 * temporary buffer and check it before returning it.
2142 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002143
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002144 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002145 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002146 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2147
Hanno Beckerfdf38032017-09-06 12:35:55 +01002148 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002149 if( verif == NULL )
2150 {
2151 mbedtls_free( sig_try );
2152 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2153 }
2154
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002155 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2156 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2157
Hanno Becker171a8f12017-09-06 12:32:16 +01002158 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002159 {
2160 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2161 goto cleanup;
2162 }
2163
2164 memcpy( sig, sig_try, ctx->len );
2165
2166cleanup:
2167 mbedtls_free( sig_try );
2168 mbedtls_free( verif );
2169
2170 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002173
2174/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 * Do an RSA operation to sign the message digest
2176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002178 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002179 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002182 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002183 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 unsigned char *sig )
2185{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002186 RSA_VALIDATE_RET( ctx != NULL );
2187 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2188 mode == MBEDTLS_RSA_PUBLIC );
2189 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2190 hashlen == 0 ) ||
2191 hash != NULL );
2192 RSA_VALIDATE_RET( sig != NULL );
2193
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 switch( ctx->padding )
2195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196#if defined(MBEDTLS_PKCS1_V15)
2197 case MBEDTLS_RSA_PKCS_V15:
2198 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002199 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002200#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202#if defined(MBEDTLS_PKCS1_V21)
2203 case MBEDTLS_RSA_PKCS_V21:
2204 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002205 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002206#endif
2207
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002211}
2212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002214/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002215 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002218 int (*f_rng)(void *, unsigned char *, size_t),
2219 void *p_rng,
2220 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002222 unsigned int hashlen,
2223 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002225 int expected_salt_len,
2226 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002227{
Janos Follath24eed8d2019-11-22 13:21:35 +00002228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002229 size_t siglen;
2230 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002231 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002233 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002234 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002235 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 const mbedtls_md_info_t *md_info;
2237 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002238 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002239
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002240 RSA_VALIDATE_RET( ctx != NULL );
2241 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2242 mode == MBEDTLS_RSA_PUBLIC );
2243 RSA_VALIDATE_RET( sig != NULL );
2244 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2245 hashlen == 0 ) ||
2246 hash != NULL );
2247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2249 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002250
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 siglen = ctx->len;
2252
Paul Bakker27fdf462011-06-09 13:55:13 +00002253 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2257 ? mbedtls_rsa_public( ctx, sig, buf )
2258 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002259
2260 if( ret != 0 )
2261 return( ret );
2262
2263 p = buf;
2264
Paul Bakkerb3869132013-02-28 17:21:01 +01002265 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 {
Simon Butcher02037452016-03-01 21:19:12 +00002270 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002272 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002276 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002279 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002283
Paul Bakkerb3869132013-02-28 17:21:01 +01002284 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002285
Simon Butcher02037452016-03-01 21:19:12 +00002286 /*
2287 * Note: EMSA-PSS verification is over the length of N - 1 bits
2288 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002289 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002290
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002291 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2292 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2293
Simon Butcher02037452016-03-01 21:19:12 +00002294 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002295 if( msb % 8 == 0 )
2296 {
2297 p++;
2298 siglen -= 1;
2299 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002300
Gilles Peskine139108a2017-10-18 19:03:42 +02002301 if( siglen < hlen + 2 )
2302 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2303 hash_start = p + siglen - hlen - 1;
2304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002306 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002307 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002308
Jaeden Amero66954e12018-01-25 16:05:54 +00002309 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2310 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002311 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002312
Paul Bakkerb3869132013-02-28 17:21:01 +01002313 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002314
Gilles Peskine6a54b022017-10-17 19:02:13 +02002315 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002316 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002317
Gilles Peskine91048a32017-10-19 17:46:14 +02002318 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002319 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002320 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2321 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002322 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002323
Gilles Peskine6a54b022017-10-17 19:02:13 +02002324 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002327 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002328 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002329 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2330 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002331 }
2332
Simon Butcher02037452016-03-01 21:19:12 +00002333 /*
2334 * Generate H = Hash( M' )
2335 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002336 ret = mbedtls_md_starts( &md_ctx );
2337 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002338 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002339 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2340 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002341 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002342 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2343 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002344 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002345 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2346 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002347 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002348 ret = mbedtls_md_finish( &md_ctx, result );
2349 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002350 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002351
Jaeden Amero66954e12018-01-25 16:05:54 +00002352 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002353 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002354 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002355 goto exit;
2356 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002357
2358exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002360
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002361 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002362}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002363
2364/*
2365 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002368 int (*f_rng)(void *, unsigned char *, size_t),
2369 void *p_rng,
2370 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002372 unsigned int hashlen,
2373 const unsigned char *hash,
2374 const unsigned char *sig )
2375{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002376 mbedtls_md_type_t mgf1_hash_id;
2377 RSA_VALIDATE_RET( ctx != NULL );
2378 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2379 mode == MBEDTLS_RSA_PUBLIC );
2380 RSA_VALIDATE_RET( sig != NULL );
2381 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2382 hashlen == 0 ) ||
2383 hash != NULL );
2384
2385 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002387 : md_alg;
2388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002390 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002392 sig ) );
2393
2394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002398/*
2399 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002402 int (*f_rng)(void *, unsigned char *, size_t),
2403 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002404 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002406 unsigned int hashlen,
2407 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002408 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002409{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002410 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002411 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002412 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002413
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002414 RSA_VALIDATE_RET( ctx != NULL );
2415 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2416 mode == MBEDTLS_RSA_PUBLIC );
2417 RSA_VALIDATE_RET( sig != NULL );
2418 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2419 hashlen == 0 ) ||
2420 hash != NULL );
2421
2422 sig_len = ctx->len;
2423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2425 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002426
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002427 /*
2428 * Prepare expected PKCS1 v1.5 encoding of hash.
2429 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002430
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002431 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2432 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2433 {
2434 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2435 goto cleanup;
2436 }
2437
2438 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2439 encoded_expected ) ) != 0 )
2440 goto cleanup;
2441
2442 /*
2443 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2444 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002447 ? mbedtls_rsa_public( ctx, sig, encoded )
2448 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002449 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002450 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002451
Simon Butcher02037452016-03-01 21:19:12 +00002452 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002453 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002454 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002455
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002456 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2457 sig_len ) ) != 0 )
2458 {
2459 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2460 goto cleanup;
2461 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002462
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002463cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002464
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002465 if( encoded != NULL )
2466 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002467 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002468 mbedtls_free( encoded );
2469 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002470
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002471 if( encoded_expected != NULL )
2472 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002473 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002474 mbedtls_free( encoded_expected );
2475 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002476
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002477 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002478}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
2481/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002482 * Do an RSA operation and check the message digest
2483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002485 int (*f_rng)(void *, unsigned char *, size_t),
2486 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002487 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002489 unsigned int hashlen,
2490 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002491 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002492{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002493 RSA_VALIDATE_RET( ctx != NULL );
2494 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2495 mode == MBEDTLS_RSA_PUBLIC );
2496 RSA_VALIDATE_RET( sig != NULL );
2497 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2498 hashlen == 0 ) ||
2499 hash != NULL );
2500
Paul Bakkerb3869132013-02-28 17:21:01 +01002501 switch( ctx->padding )
2502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#if defined(MBEDTLS_PKCS1_V15)
2504 case MBEDTLS_RSA_PKCS_V15:
2505 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002506 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002507#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#if defined(MBEDTLS_PKCS1_V21)
2510 case MBEDTLS_RSA_PKCS_V21:
2511 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002512 hashlen, hash, sig );
2513#endif
2514
2515 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002517 }
2518}
2519
2520/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002521 * Copy the components of an RSA key
2522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002524{
Janos Follath24eed8d2019-11-22 13:21:35 +00002525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002526 RSA_VALIDATE_RET( dst != NULL );
2527 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002528
2529 dst->ver = src->ver;
2530 dst->len = src->len;
2531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2533 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2536 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2537 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002538
2539#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2541 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2542 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2544 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002545#endif
2546
2547 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2550 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002551
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002552 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002553 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002554
2555cleanup:
2556 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002558
2559 return( ret );
2560}
2561
2562/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 * Free the components of an RSA key
2564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002566{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002567 if( ctx == NULL )
2568 return;
2569
2570 mbedtls_mpi_free( &ctx->Vi );
2571 mbedtls_mpi_free( &ctx->Vf );
2572 mbedtls_mpi_free( &ctx->RN );
2573 mbedtls_mpi_free( &ctx->D );
2574 mbedtls_mpi_free( &ctx->Q );
2575 mbedtls_mpi_free( &ctx->P );
2576 mbedtls_mpi_free( &ctx->E );
2577 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002578
Hanno Becker33c30a02017-08-23 07:00:22 +01002579#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002580 mbedtls_mpi_free( &ctx->RQ );
2581 mbedtls_mpi_free( &ctx->RP );
2582 mbedtls_mpi_free( &ctx->QP );
2583 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002584 mbedtls_mpi_free( &ctx->DP );
2585#endif /* MBEDTLS_RSA_NO_CRT */
2586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#if defined(MBEDTLS_THREADING_C)
2588 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002589#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002590}
2591
Hanno Beckerab377312017-08-23 16:24:51 +01002592#endif /* !MBEDTLS_RSA_ALT */
2593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002596#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
2598/*
2599 * Example RSA-1024 keypair, for test purposes
2600 */
2601#define KEY_LEN 128
2602
2603#define RSA_N "9292758453063D803DD603D5E777D788" \
2604 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2605 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2606 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2607 "93A89813FBF3C4F8066D2D800F7C38A8" \
2608 "1AE31942917403FF4946B0A83D3D3E05" \
2609 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2610 "5E94BB77B07507233A0BC7BAC8F90F79"
2611
2612#define RSA_E "10001"
2613
2614#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2615 "66CA472BC44D253102F8B4A9D3BFA750" \
2616 "91386C0077937FE33FA3252D28855837" \
2617 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2618 "DF79C5CE07EE72C7F123142198164234" \
2619 "CABB724CF78B8173B9F880FC86322407" \
2620 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2621 "071513A1E85B5DFA031F21ECAE91A34D"
2622
2623#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2624 "2C01CAD19EA484A87EA4377637E75500" \
2625 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2626 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2627
2628#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2629 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2630 "910E4168387E3C30AA1E00C339A79508" \
2631 "8452DD96A9A5EA5D9DCA68DA636032AF"
2632
Paul Bakker5121ce52009-01-03 21:22:43 +00002633#define PT_LEN 24
2634#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2635 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002638static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002639{
gufe44c2620da2020-08-03 17:56:50 +02002640#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002641 size_t i;
2642
Paul Bakker545570e2010-07-18 09:00:25 +00002643 if( rng_state != NULL )
2644 rng_state = NULL;
2645
Paul Bakkera3d195c2011-11-27 21:07:34 +00002646 for( i = 0; i < len; ++i )
2647 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002648#else
2649 if( rng_state != NULL )
2650 rng_state = NULL;
2651
2652 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002653#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002654
Paul Bakkera3d195c2011-11-27 21:07:34 +00002655 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002658
Paul Bakker5121ce52009-01-03 21:22:43 +00002659/*
2660 * Checkup routine
2661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002663{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002664 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002666 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 unsigned char rsa_plaintext[PT_LEN];
2669 unsigned char rsa_decrypted[PT_LEN];
2670 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002672 unsigned char sha1sum[20];
2673#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
Hanno Becker3a701162017-08-22 13:52:43 +01002675 mbedtls_mpi K;
2676
2677 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Hanno Becker3a701162017-08-22 13:52:43 +01002680 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2681 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2682 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2683 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2684 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2685 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2686 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2687 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2688 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2689 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2690
Hanno Becker7f25f852017-10-10 16:56:22 +01002691 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
2693 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2697 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 {
2699 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
Hanno Becker5bc87292017-05-03 15:09:31 +01002702 ret = 1;
2703 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 }
2705
2706 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
2709 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2710
Hanno Becker98838b02017-10-02 13:16:10 +01002711 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2712 PT_LEN, rsa_plaintext,
2713 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 {
2715 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Hanno Becker5bc87292017-05-03 15:09:31 +01002718 ret = 1;
2719 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 }
2721
2722 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
Hanno Becker98838b02017-10-02 13:16:10 +01002725 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2726 &len, rsa_ciphertext, rsa_decrypted,
2727 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 {
2729 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731
Hanno Becker5bc87292017-05-03 15:09:31 +01002732 ret = 1;
2733 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 }
2735
2736 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2737 {
2738 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Hanno Becker5bc87292017-05-03 15:09:31 +01002741 ret = 1;
2742 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 }
2744
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002745 if( verbose != 0 )
2746 mbedtls_printf( "passed\n" );
2747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002750 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002752 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002753 {
2754 if( verbose != 0 )
2755 mbedtls_printf( "failed\n" );
2756
2757 return( 1 );
2758 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
Hanno Becker98838b02017-10-02 13:16:10 +01002760 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2761 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2762 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 {
2764 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Hanno Becker5bc87292017-05-03 15:09:31 +01002767 ret = 1;
2768 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 }
2770
2771 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Hanno Becker98838b02017-10-02 13:16:10 +01002774 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2775 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2776 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 {
2778 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
Hanno Becker5bc87292017-05-03 15:09:31 +01002781 ret = 1;
2782 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002783 }
2784
2785 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002786 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002789 if( verbose != 0 )
2790 mbedtls_printf( "\n" );
2791
Paul Bakker3d8fb632014-04-17 12:42:41 +02002792cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002793 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794 mbedtls_rsa_free( &rsa );
2795#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002796 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002798 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002799}
2800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803#endif /* MBEDTLS_RSA_C */