blob: 502fe8654e16fa70e28def770d9de804f3e4277b [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"
Chris Jones66a4cd42021-03-09 16:04:12 +000043#include "rsa_alt_helpers.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 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100105 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100106 }
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 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100145 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100146
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 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100296 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100297 }
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 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100311 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100312
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 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100321 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100322 }
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 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100336 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100337 }
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 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100464 return( MBEDTLS_ERROR_ADD( 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 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100470 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100471 }
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)
Gilles Peskineeb940592021-02-01 17:57:41 +0100493 /* Set ctx->ver to nonzero to indicate that the mutex has been
494 * initialized and will need to be freed. */
495 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000498}
499
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100500/*
501 * Set padding for an existing RSA context
502 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500503void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
504 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100505{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500506 RSA_VALIDATE( ctx != NULL );
507 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
508 padding == MBEDTLS_RSA_PKCS_V21 );
509
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100510 ctx->padding = padding;
511 ctx->hash_id = hash_id;
512}
513
Hanno Becker617c1ae2017-08-23 14:11:24 +0100514/*
515 * Get length in bytes of RSA modulus
516 */
517
518size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
519{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100520 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100521}
522
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526/*
527 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800528 *
529 * This generation method follows the RSA key pair generation procedure of
530 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000533 int (*f_rng)(void *, unsigned char *, size_t),
534 void *p_rng,
535 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000536{
Janos Follath24eed8d2019-11-22 13:21:35 +0000537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800538 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100539 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500540 RSA_VALIDATE_RET( ctx != NULL );
541 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
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
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100555 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
556 {
557 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
558 goto cleanup;
559 }
560
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 /*
562 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800563 * 1. |P-Q| > 2^( nbits / 2 - 100 )
564 * 2. GCD( E, (P-1)*(Q-1) ) == 1
565 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 do
570 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100571 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
572 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Janos Follathb8fc1b02018-09-03 15:37:01 +0100574 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
575 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800577 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
578 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
579 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 continue;
581
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800582 /* not required by any standards, but some users rely on the fact that P > Q */
583 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100584 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100585
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100586 /* Temporarily replace P,Q by P-1, Q-1 */
587 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
588 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
589 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800590
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800591 /* 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 +0200592 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800593 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
594 continue;
595
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800596 /* 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 -0800597 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
598 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
599 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
600
601 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
602 continue;
603
604 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800606 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100608 /* Restore P,Q */
609 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
610 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
611
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800612 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
613
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100614 ctx->len = mbedtls_mpi_size( &ctx->N );
615
Jethro Beekman97f95c92018-02-13 15:50:36 -0800616#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 * DP = D mod (P - 1)
619 * DQ = D mod (Q - 1)
620 * QP = Q^-1 mod P
621 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100622 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
623 &ctx->DP, &ctx->DQ, &ctx->QP ) );
624#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
Hanno Becker83aad1f2017-08-23 06:45:10 +0100626 /* Double-check */
627 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
629cleanup:
630
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100631 mbedtls_mpi_free( &H );
632 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800633 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
635 if( ret != 0 )
636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100638
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100639 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100640 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100641 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 }
643
Paul Bakker48377d92013-08-30 12:06:24 +0200644 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645}
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
649/*
650 * Check a public RSA key
651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000653{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500654 RSA_VALIDATE_RET( ctx != NULL );
655
Hanno Beckerebd2c022017-10-12 10:54:53 +0100656 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000658
Hanno Becker3a760a12018-01-05 08:14:49 +0000659 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100662 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Hanno Becker705fc682017-10-10 17:57:02 +0100664 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
665 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100669 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
671 return( 0 );
672}
673
674/*
Hanno Becker705fc682017-10-10 17:57:02 +0100675 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000678{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500679 RSA_VALIDATE_RET( ctx != NULL );
680
Hanno Becker705fc682017-10-10 17:57:02 +0100681 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100682 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 {
Hanno Becker98838b02017-10-02 13:16:10 +0100684 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 }
Paul Bakker48377d92013-08-30 12:06:24 +0200686
Hanno Becker98838b02017-10-02 13:16:10 +0100687 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100688 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100690 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000692
Hanno Beckerb269a852017-08-25 08:03:21 +0100693#if !defined(MBEDTLS_RSA_NO_CRT)
694 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
695 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
696 {
697 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
698 }
699#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000700
701 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000702}
703
704/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100705 * Check if contexts holding a public and private key match
706 */
Hanno Becker98838b02017-10-02 13:16:10 +0100707int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
708 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100709{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500710 RSA_VALIDATE_RET( pub != NULL );
711 RSA_VALIDATE_RET( prv != NULL );
712
Hanno Becker98838b02017-10-02 13:16:10 +0100713 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100717 }
718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
720 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100723 }
724
725 return( 0 );
726}
727
728/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 * Do an RSA public key operation
730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000732 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 unsigned char *output )
734{
Janos Follath24eed8d2019-11-22 13:21:35 +0000735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000736 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500738 RSA_VALIDATE_RET( ctx != NULL );
739 RSA_VALIDATE_RET( input != NULL );
740 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000741
Hanno Beckerebd2c022017-10-12 10:54:53 +0100742 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100743 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200747#if defined(MBEDTLS_THREADING_C)
748 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
749 return( ret );
750#endif
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000755 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200756 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
757 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000758 }
759
760 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
762 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
764cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200766 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
767 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100768#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
772 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100773 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000774
775 return( 0 );
776}
777
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200778/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200779 * Generate or update blinding values, see section 10 of:
780 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200781 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200782 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200783 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200784static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200785 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
786{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200787 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200788 mbedtls_mpi R;
789
790 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200791
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200792 if( ctx->Vf.p != NULL )
793 {
794 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
796 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
797 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
798 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200799
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200800 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200801 }
802
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200803 /* Unblinding value: Vf = random number, invertible mod N */
804 do {
805 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200806 {
807 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
808 goto cleanup;
809 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 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 +0200812
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200813 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200814 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
815 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
816 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
817
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200818 /* At this point, Vi is invertible mod N if and only if both Vf and R
819 * are invertible mod N. If one of them isn't, we don't need to know
820 * which one, we just loop and choose new values for both of them.
821 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200822 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500823 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200824 goto cleanup;
825
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500826 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
827
828 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
829 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
830 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200831
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200832 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200833 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 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 +0200835
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200836
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200837cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200838 mbedtls_mpi_free( &R );
839
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200840 return( ret );
841}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200842
Paul Bakker5121ce52009-01-03 21:22:43 +0000843/*
Janos Follathe81102e2017-03-22 13:38:28 +0000844 * Exponent blinding supposed to prevent side-channel attacks using multiple
845 * traces of measurements to recover the RSA key. The more collisions are there,
846 * the more bits of the key can be recovered. See [3].
847 *
848 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
849 * observations on avarage.
850 *
851 * For example with 28 byte blinding to achieve 2 collisions the adversary has
852 * to make 2^112 observations on avarage.
853 *
854 * (With the currently (as of 2017 April) known best algorithms breaking 2048
855 * bit RSA requires approximately as much time as trying out 2^112 random keys.
856 * Thus in this sense with 28 byte blinding the security is not reduced by
857 * side-channel attacks like the one in [3])
858 *
859 * This countermeasure does not help if the key recovery is possible with a
860 * single trace.
861 */
862#define RSA_EXPONENT_BLINDING 28
863
864/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000865 * Do an RSA private key operation
866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200868 int (*f_rng)(void *, unsigned char *, size_t),
869 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000870 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 unsigned char *output )
872{
Janos Follath24eed8d2019-11-22 13:21:35 +0000873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000874 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100875
876 /* Temporary holding the result */
877 mbedtls_mpi T;
878
879 /* Temporaries holding P-1, Q-1 and the
880 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000881 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100882
883#if !defined(MBEDTLS_RSA_NO_CRT)
884 /* Temporaries holding the results mod p resp. mod q. */
885 mbedtls_mpi TP, TQ;
886
887 /* Temporaries holding the blinded exponents for
888 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000889 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100890
891 /* Pointers to actual exponents to be used - either the unblinded
892 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000893 mbedtls_mpi *DP = &ctx->DP;
894 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100895#else
896 /* Temporary holding the blinded exponent (if used). */
897 mbedtls_mpi D_blind;
898
899 /* Pointer to actual exponent to be used - either the unblinded
900 * or the blinded one, depending on the presence of a PRNG. */
901 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100902#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100903
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100904 /* Temporaries holding the initial input and the double
905 * checked result; should be the same in the end. */
906 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000907
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500908 RSA_VALIDATE_RET( ctx != NULL );
909 RSA_VALIDATE_RET( input != NULL );
910 RSA_VALIDATE_RET( output != NULL );
911
Hanno Beckerebd2c022017-10-12 10:54:53 +0100912 if( rsa_check_context( ctx, 1 /* private key checks */,
913 f_rng != NULL /* blinding y/n */ ) != 0 )
914 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100915 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100916 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100917
Hanno Becker06811ce2017-05-03 15:10:34 +0100918#if defined(MBEDTLS_THREADING_C)
919 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
920 return( ret );
921#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000922
Hanno Becker06811ce2017-05-03 15:10:34 +0100923 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100924 mbedtls_mpi_init( &T );
925
926 mbedtls_mpi_init( &P1 );
927 mbedtls_mpi_init( &Q1 );
928 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000929
Janos Follathf9203b42017-03-22 15:13:15 +0000930 if( f_rng != NULL )
931 {
Janos Follathe81102e2017-03-22 13:38:28 +0000932#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000933 mbedtls_mpi_init( &D_blind );
934#else
935 mbedtls_mpi_init( &DP_blind );
936 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000937#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000938 }
Janos Follathe81102e2017-03-22 13:38:28 +0000939
Hanno Becker06811ce2017-05-03 15:10:34 +0100940#if !defined(MBEDTLS_RSA_NO_CRT)
941 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200942#endif
943
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100944 mbedtls_mpi_init( &I );
945 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100946
947 /* End of MPI initialization */
948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
950 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200952 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
953 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 }
955
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100956 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100957
Paul Bakkerf451bac2013-08-30 15:37:02 +0200958 if( f_rng != NULL )
959 {
960 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200961 * Blinding
962 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200963 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200964 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
965 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000967
Janos Follathe81102e2017-03-22 13:38:28 +0000968 /*
969 * Exponent blinding
970 */
971 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
972 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
973
Janos Follathf9203b42017-03-22 15:13:15 +0000974#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000975 /*
976 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
977 */
978 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
979 f_rng, p_rng ) );
980 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
981 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
982 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
983
984 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000985#else
986 /*
987 * DP_blind = ( P - 1 ) * R + DP
988 */
989 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
990 f_rng, p_rng ) );
991 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
992 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
993 &ctx->DP ) );
994
995 DP = &DP_blind;
996
997 /*
998 * DQ_blind = ( Q - 1 ) * R + DQ
999 */
1000 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1001 f_rng, p_rng ) );
1002 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1003 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1004 &ctx->DQ ) );
1005
1006 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001007#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001008 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001011 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001012#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001013 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001014 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001016 * TP = input ^ dP mod P
1017 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001019
1020 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1021 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
1023 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001024 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001025 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001026 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1027 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1028 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
1030 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001031 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001032 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001033 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1034 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001036
Paul Bakkerf451bac2013-08-30 15:37:02 +02001037 if( f_rng != NULL )
1038 {
1039 /*
1040 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001041 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001042 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001043 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001045 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
Hanno Becker2dec5e82017-10-03 07:49:52 +01001047 /* Verify the result to prevent glitching attacks. */
1048 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1049 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001050 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001051 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001052 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1053 goto cleanup;
1054 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001055
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
1059cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001061 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1062 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001063#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001064
Hanno Becker06811ce2017-05-03 15:10:34 +01001065 mbedtls_mpi_free( &P1 );
1066 mbedtls_mpi_free( &Q1 );
1067 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001068
1069 if( f_rng != NULL )
1070 {
Janos Follathe81102e2017-03-22 13:38:28 +00001071#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001072 mbedtls_mpi_free( &D_blind );
1073#else
1074 mbedtls_mpi_free( &DP_blind );
1075 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001076#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001077 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Hanno Becker06811ce2017-05-03 15:10:34 +01001079 mbedtls_mpi_free( &T );
1080
1081#if !defined(MBEDTLS_RSA_NO_CRT)
1082 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1083#endif
1084
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001085 mbedtls_mpi_free( &C );
1086 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001087
Gilles Peskineae3741e2020-11-25 00:10:31 +01001088 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001089 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Gilles Peskineae3741e2020-11-25 00:10:31 +01001091 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001092}
1093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001095/**
1096 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1097 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001098 * \param dst buffer to mask
1099 * \param dlen length of destination buffer
1100 * \param src source of the mask generation
1101 * \param slen length of the source buffer
1102 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001103 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001104static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001108 unsigned char counter[4];
1109 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001110 unsigned int hlen;
1111 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001112 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001115 memset( counter, 0, 4 );
1116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001118
Simon Butcher02037452016-03-01 21:19:12 +00001119 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001120 p = dst;
1121
1122 while( dlen > 0 )
1123 {
1124 use_len = hlen;
1125 if( dlen < hlen )
1126 use_len = dlen;
1127
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001128 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1129 goto exit;
1130 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1131 goto exit;
1132 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1133 goto exit;
1134 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1135 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001136
1137 for( i = 0; i < use_len; ++i )
1138 *p++ ^= mask[i];
1139
1140 counter[3]++;
1141
1142 dlen -= use_len;
1143 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001144
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001145exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001146 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001147
1148 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001153/*
1154 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001157 int (*f_rng)(void *, unsigned char *, size_t),
1158 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001159 int mode,
1160 const unsigned char *label, size_t label_len,
1161 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001162 const unsigned char *input,
1163 unsigned char *output )
1164{
1165 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001167 unsigned char *p = output;
1168 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 const mbedtls_md_info_t *md_info;
1170 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001171
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001172 RSA_VALIDATE_RET( ctx != NULL );
1173 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1174 mode == MBEDTLS_RSA_PUBLIC );
1175 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001176 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001177 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1180 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001181
1182 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001186 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001188
1189 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001191
Simon Butcher02037452016-03-01 21:19:12 +00001192 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001193 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001195
1196 memset( output, 0, olen );
1197
1198 *p++ = 0;
1199
Simon Butcher02037452016-03-01 21:19:12 +00001200 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001201 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001202 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001203
1204 p += hlen;
1205
Simon Butcher02037452016-03-01 21:19:12 +00001206 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001207 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1208 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001209 p += hlen;
1210 p += olen - 2 * hlen - 2 - ilen;
1211 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001212 if( ilen != 0 )
1213 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001216 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001217 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001218
Simon Butcher02037452016-03-01 21:19:12 +00001219 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001220 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1221 &md_ctx ) ) != 0 )
1222 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001223
Simon Butcher02037452016-03-01 21:19:12 +00001224 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001225 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1226 &md_ctx ) ) != 0 )
1227 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001228
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001229exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001231
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001232 if( ret != 0 )
1233 return( ret );
1234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 return( ( mode == MBEDTLS_RSA_PUBLIC )
1236 ? mbedtls_rsa_public( ctx, output, output )
1237 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001242/*
1243 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001246 int (*f_rng)(void *, unsigned char *, size_t),
1247 void *p_rng,
1248 int mode, size_t ilen,
1249 const unsigned char *input,
1250 unsigned char *output )
1251{
1252 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001254 unsigned char *p = output;
1255
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001256 RSA_VALIDATE_RET( ctx != NULL );
1257 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1258 mode == MBEDTLS_RSA_PUBLIC );
1259 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001260 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001261
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001262 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001264
1265 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001266
Simon Butcher02037452016-03-01 21:19:12 +00001267 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001268 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001270
1271 nb_pad = olen - 3 - ilen;
1272
1273 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001275 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001276 if( f_rng == NULL )
1277 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001280
1281 while( nb_pad-- > 0 )
1282 {
1283 int rng_dl = 100;
1284
1285 do {
1286 ret = f_rng( p_rng, p, 1 );
1287 } while( *p == 0 && --rng_dl && ret == 0 );
1288
Simon Butcher02037452016-03-01 21:19:12 +00001289 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001290 if( rng_dl == 0 || ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001291 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001292
1293 p++;
1294 }
1295 }
1296 else
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001299
1300 while( nb_pad-- > 0 )
1301 *p++ = 0xFF;
1302 }
1303
1304 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001305 if( ilen != 0 )
1306 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 return( ( mode == MBEDTLS_RSA_PUBLIC )
1309 ? mbedtls_rsa_public( ctx, output, output )
1310 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001311}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001313
Paul Bakker5121ce52009-01-03 21:22:43 +00001314/*
1315 * Add the message padding, then do an RSA operation
1316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001318 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001319 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001320 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001321 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 unsigned char *output )
1323{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001324 RSA_VALIDATE_RET( ctx != NULL );
1325 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1326 mode == MBEDTLS_RSA_PUBLIC );
1327 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001328 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001329
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 switch( ctx->padding )
1331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#if defined(MBEDTLS_PKCS1_V15)
1333 case MBEDTLS_RSA_PKCS_V15:
1334 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001335 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001336#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338#if defined(MBEDTLS_PKCS1_V21)
1339 case MBEDTLS_RSA_PKCS_V21:
1340 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001341 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001342#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
1344 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001347}
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001350/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001351 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001354 int (*f_rng)(void *, unsigned char *, size_t),
1355 void *p_rng,
1356 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001357 const unsigned char *label, size_t label_len,
1358 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001359 const unsigned char *input,
1360 unsigned char *output,
1361 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001362{
Janos Follath24eed8d2019-11-22 13:21:35 +00001363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001364 size_t ilen, i, pad_len;
1365 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1367 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001368 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 const mbedtls_md_info_t *md_info;
1370 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001371
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001372 RSA_VALIDATE_RET( ctx != NULL );
1373 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1374 mode == MBEDTLS_RSA_PUBLIC );
1375 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1376 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1377 RSA_VALIDATE_RET( input != NULL );
1378 RSA_VALIDATE_RET( olen != NULL );
1379
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001380 /*
1381 * Parameters sanity checks
1382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1384 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385
1386 ilen = ctx->len;
1387
Paul Bakker27fdf462011-06-09 13:55:13 +00001388 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001392 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001394
Janos Follathc17cda12016-02-11 11:08:18 +00001395 hlen = mbedtls_md_get_size( md_info );
1396
1397 // checking for integer underflow
1398 if( 2 * hlen + 2 > ilen )
1399 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1400
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001401 /*
1402 * RSA operation
1403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1405 ? mbedtls_rsa_public( ctx, input, buf )
1406 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
1408 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001409 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001411 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001412 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001415 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1416 {
1417 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001418 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001419 }
1420
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001421 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001422 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1423 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001424 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001425 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1426 &md_ctx ) ) != 0 )
1427 {
1428 mbedtls_md_free( &md_ctx );
1429 goto cleanup;
1430 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001433
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001434 /* Generate lHash */
1435 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1436 goto cleanup;
1437
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001438 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001439 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001440 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001442 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001443
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001444 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001446 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001447
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001448 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001449 for( i = 0; i < hlen; i++ )
1450 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001451
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001452 /* Get zero-padding len, but always read till end of buffer
1453 * (minus one, for the 01 byte) */
1454 pad_len = 0;
1455 pad_done = 0;
1456 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1457 {
1458 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001459 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001460 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001461
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001462 p += pad_len;
1463 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001464
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001465 /*
1466 * The only information "leaked" is whether the padding was correct or not
1467 * (eg, no data is copied if it was not correct). This meets the
1468 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1469 * the different error conditions.
1470 */
1471 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001472 {
1473 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1474 goto cleanup;
1475 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001476
Paul Bakker66d5d072014-06-17 16:39:18 +02001477 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001478 {
1479 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1480 goto cleanup;
1481 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001482
1483 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001484 if( *olen != 0 )
1485 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001486 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001487
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001488cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001489 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1490 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001491
1492 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001493}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001497/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1498 *
1499 * \param value The value to analyze.
1500 * \return Zero if \p value is zero, otherwise all-bits-one.
1501 */
1502static unsigned all_or_nothing_int( unsigned value )
1503{
1504 /* MSVC has a warning about unary minus on unsigned, but this is
1505 * well-defined and precisely what we want to do here */
1506#if defined(_MSC_VER)
1507#pragma warning( push )
1508#pragma warning( disable : 4146 )
1509#endif
1510 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1511#if defined(_MSC_VER)
1512#pragma warning( pop )
1513#endif
1514}
1515
1516/** Check whether a size is out of bounds, without branches.
1517 *
1518 * This is equivalent to `size > max`, but is likely to be compiled to
1519 * to code using bitwise operation rather than a branch.
1520 *
1521 * \param size Size to check.
1522 * \param max Maximum desired value for \p size.
1523 * \return \c 0 if `size <= max`.
1524 * \return \c 1 if `size > max`.
1525 */
1526static unsigned size_greater_than( size_t size, size_t max )
1527{
1528 /* Return the sign bit (1 for negative) of (max - size). */
1529 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1530}
1531
1532/** Choose between two integer values, without branches.
1533 *
1534 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1535 * to code using bitwise operation rather than a branch.
1536 *
1537 * \param cond Condition to test.
1538 * \param if1 Value to use if \p cond is nonzero.
1539 * \param if0 Value to use if \p cond is zero.
1540 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1541 */
1542static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
1543{
1544 unsigned mask = all_or_nothing_int( cond );
1545 return( ( mask & if1 ) | (~mask & if0 ) );
1546}
1547
1548/** Shift some data towards the left inside a buffer without leaking
1549 * the length of the data through side channels.
1550 *
1551 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1552 * ```
1553 * memmove(start, start + offset, total - offset);
1554 * memset(start + offset, 0, total - offset);
1555 * ```
1556 * but it strives to use a memory access pattern (and thus total timing)
1557 * that does not depend on \p offset. This timing independence comes at
1558 * the expense of performance.
1559 *
1560 * \param start Pointer to the start of the buffer.
1561 * \param total Total size of the buffer.
1562 * \param offset Offset from which to copy \p total - \p offset bytes.
1563 */
1564static void mem_move_to_left( void *start,
1565 size_t total,
1566 size_t offset )
1567{
1568 volatile unsigned char *buf = start;
1569 size_t i, n;
1570 if( total == 0 )
1571 return;
1572 for( i = 0; i < total; i++ )
1573 {
1574 unsigned no_op = size_greater_than( total - offset, i );
1575 /* The first `total - offset` passes are a no-op. The last
1576 * `offset` passes shift the data one byte to the left and
1577 * zero out the last byte. */
1578 for( n = 0; n < total - 1; n++ )
1579 {
1580 unsigned char current = buf[n];
1581 unsigned char next = buf[n+1];
1582 buf[n] = if_int( no_op, current, next );
1583 }
1584 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1585 }
1586}
1587
Paul Bakkerb3869132013-02-28 17:21:01 +01001588/*
1589 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001592 int (*f_rng)(void *, unsigned char *, size_t),
1593 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001594 int mode, size_t *olen,
1595 const unsigned char *input,
1596 unsigned char *output,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001597 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001598{
Janos Follath24eed8d2019-11-22 13:21:35 +00001599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001600 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001602 /* The following variables take sensitive values: their value must
1603 * not leak into the observable behavior of the function other than
1604 * the designated outputs (output, olen, return value). Otherwise
1605 * this would open the execution of the function to
1606 * side-channel-based variants of the Bleichenbacher padding oracle
1607 * attack. Potential side channels include overall timing, memory
1608 * access patterns (especially visible to an adversary who has access
1609 * to a shared memory cache), and branches (especially visible to
1610 * an adversary who has access to a shared code cache or to a shared
1611 * branch predictor). */
1612 size_t pad_count = 0;
1613 unsigned bad = 0;
1614 unsigned char pad_done = 0;
1615 size_t plaintext_size = 0;
1616 unsigned output_too_large;
1617
1618 RSA_VALIDATE_RET( ctx != NULL );
1619 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1620 mode == MBEDTLS_RSA_PUBLIC );
1621 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1622 RSA_VALIDATE_RET( input != NULL );
1623 RSA_VALIDATE_RET( olen != NULL );
1624
1625 ilen = ctx->len;
1626 plaintext_max_size = ( output_max_len > ilen - 11 ?
1627 ilen - 11 :
1628 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1631 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001632
Paul Bakkerb3869132013-02-28 17:21:01 +01001633 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1637 ? mbedtls_rsa_public( ctx, input, buf )
1638 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001639
1640 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001641 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001642
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001643 /* Check and get padding length in constant time and constant
1644 * memory trace. The first byte must be 0. */
1645 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001648 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001649 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1650 * where PS must be at least 8 nonzero bytes. */
1651 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001652
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001653 /* Read the whole buffer. Set pad_done to nonzero if we find
1654 * the 0x00 byte and remember the padding length in pad_count. */
1655 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001656 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001657 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001658 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001659 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001660 }
1661 else
1662 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001663 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1664 * where PS must be at least 8 bytes with the value 0xFF. */
1665 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001666
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001667 /* Read the whole buffer. Set pad_done to nonzero if we find
1668 * the 0x00 byte and remember the padding length in pad_count.
1669 * If there's a non-0xff byte in the padding, the padding is bad. */
1670 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001671 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001672 pad_done |= if_int( buf[i], 0, 1 );
1673 pad_count += if_int( pad_done, 0, 1 );
1674 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001676 }
1677
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001678 /* If pad_done is still zero, there's no data, only unfinished padding. */
1679 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001680
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001681 /* There must be at least 8 bytes of padding. */
1682 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001683
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001684 /* If the padding is valid, set plaintext_size to the number of
1685 * remaining bytes after stripping the padding. If the padding
1686 * is invalid, avoid leaking this fact through the size of the
1687 * output: use the maximum message size that fits in the output
1688 * buffer. Do it without branches to avoid leaking the padding
1689 * validity through timing. RSA keys are small enough that all the
1690 * size_t values involved fit in unsigned int. */
1691 plaintext_size = if_int( bad,
1692 (unsigned) plaintext_max_size,
1693 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001694
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001695 /* Set output_too_large to 0 if the plaintext fits in the output
1696 * buffer and to 1 otherwise. */
1697 output_too_large = size_greater_than( plaintext_size,
1698 plaintext_max_size );
1699
1700 /* Set ret without branches to avoid timing attacks. Return:
1701 * - INVALID_PADDING if the padding is bad (bad != 0).
1702 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1703 * plaintext does not fit in the output buffer.
1704 * - 0 if the padding is correct. */
1705 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1706 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1707 0 ) );
1708
1709 /* If the padding is bad or the plaintext is too large, zero the
1710 * data that we're about to copy to the output buffer.
1711 * We need to copy the same amount of data
1712 * from the same buffer whether the padding is good or not to
1713 * avoid leaking the padding validity through overall timing or
1714 * through memory or cache access patterns. */
1715 bad = all_or_nothing_int( bad | output_too_large );
1716 for( i = 11; i < ilen; i++ )
1717 buf[i] &= ~bad;
1718
1719 /* If the plaintext is too large, truncate it to the buffer size.
1720 * Copy anyway to avoid revealing the length through timing, because
1721 * revealing the length is as bad as revealing the padding validity
1722 * for a Bleichenbacher attack. */
1723 plaintext_size = if_int( output_too_large,
1724 (unsigned) plaintext_max_size,
1725 (unsigned) plaintext_size );
1726
1727 /* Move the plaintext to the leftmost position where it can start in
1728 * the working buffer, i.e. make it start plaintext_max_size from
1729 * the end of the buffer. Do this with a memory access trace that
1730 * does not depend on the plaintext size. After this move, the
1731 * starting location of the plaintext is no longer sensitive
1732 * information. */
1733 mem_move_to_left( buf + ilen - plaintext_max_size,
1734 plaintext_max_size,
1735 plaintext_max_size - plaintext_size );
1736
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001737 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1738 * buffer. If output_max_len is 0, then output may be an invalid pointer
1739 * and the result of memcpy() would be undefined; prevent undefined
1740 * behavior making sure to depend only on output_max_len (the size of the
1741 * user-provided output buffer), which is independent from plaintext
1742 * length, validity of padding, success of the decryption, and other
1743 * secrets. */
1744 if( output_max_len != 0 )
1745 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001746
1747 /* Report the amount of data we copied to the output buffer. In case
1748 * of errors (bad padding or output too large), the value of *olen
1749 * when this function returns is not specified. Making it equivalent
1750 * to the good case limits the risks of leaking the padding validity. */
1751 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001752
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001753cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001754 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001755
1756 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001759
1760/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001761 * Do an RSA operation, then remove the message padding
1762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001764 int (*f_rng)(void *, unsigned char *, size_t),
1765 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001766 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001767 const unsigned char *input,
1768 unsigned char *output,
1769 size_t output_max_len)
1770{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001771 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001772 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1773 RSA_VALIDATE_RET( input != NULL );
1774 RSA_VALIDATE_RET( olen != NULL );
1775
Paul Bakkerb3869132013-02-28 17:21:01 +01001776 switch( ctx->padding )
1777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778#if defined(MBEDTLS_PKCS1_V15)
1779 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001780 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001781 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001782#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784#if defined(MBEDTLS_PKCS1_V21)
1785 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001786 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001787 olen, input, output,
1788 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001789#endif
1790
1791 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001793 }
1794}
1795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001797static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001798 int (*f_rng)(void *, unsigned char *, size_t),
1799 void *p_rng,
1800 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001802 unsigned int hashlen,
1803 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001804 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001805 unsigned char *sig )
1806{
1807 size_t olen;
1808 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001809 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001810 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001812 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 const mbedtls_md_info_t *md_info;
1814 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001815 RSA_VALIDATE_RET( ctx != NULL );
1816 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1817 mode == MBEDTLS_RSA_PUBLIC );
1818 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1819 hashlen == 0 ) ||
1820 hash != NULL );
1821 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1824 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001825
1826 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001828
1829 olen = ctx->len;
1830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001832 {
Simon Butcher02037452016-03-01 21:19:12 +00001833 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001835 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001839 }
1840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001842 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001846
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001847 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1848 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001849 /* Calculate the largest possible salt length, up to the hash size.
1850 * Normally this is the hash length, which is the maximum salt length
1851 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001852 * enough room, use the maximum salt length that fits. The constraint is
1853 * that the hash length plus the salt length plus 2 bytes must be at most
1854 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1855 * (PKCS#1 v2.2) §9.1.1 step 3. */
1856 min_slen = hlen - 2;
1857 if( olen < hlen + min_slen + 2 )
1858 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1859 else if( olen >= hlen + hlen + 2 )
1860 slen = hlen;
1861 else
1862 slen = olen - hlen - 2;
1863 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001864 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001867 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001868 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001869 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001870 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001871 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001872
1873 memset( sig, 0, olen );
1874
Simon Butcher02037452016-03-01 21:19:12 +00001875 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001876 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001877 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001878 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001879
1880 /* Generate salt of length slen in place in the encoded message */
1881 salt = p;
1882 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001883 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001884
Paul Bakkerb3869132013-02-28 17:21:01 +01001885 p += slen;
1886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001888 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001889 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001890
Simon Butcher02037452016-03-01 21:19:12 +00001891 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001892 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1893 goto exit;
1894 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1895 goto exit;
1896 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1897 goto exit;
1898 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1899 goto exit;
1900 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1901 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001902
Simon Butcher02037452016-03-01 21:19:12 +00001903 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001904 if( msb % 8 == 0 )
1905 offset = 1;
1906
Simon Butcher02037452016-03-01 21:19:12 +00001907 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001908 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1909 &md_ctx ) ) != 0 )
1910 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001911
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001912 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001913 sig[0] &= 0xFF >> ( olen * 8 - msb );
1914
1915 p += hlen;
1916 *p++ = 0xBC;
1917
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001918exit:
1919 mbedtls_md_free( &md_ctx );
1920
1921 if( ret != 0 )
1922 return( ret );
1923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 return( ( mode == MBEDTLS_RSA_PUBLIC )
1925 ? mbedtls_rsa_public( ctx, sig, sig )
1926 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001927}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001928
1929/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001930 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1931 * the option to pass in the salt length.
1932 */
1933int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1934 int (*f_rng)(void *, unsigned char *, size_t),
1935 void *p_rng,
1936 mbedtls_md_type_t md_alg,
1937 unsigned int hashlen,
1938 const unsigned char *hash,
1939 int saltlen,
1940 unsigned char *sig )
1941{
1942 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, md_alg,
1943 hashlen, hash, saltlen, sig );
1944}
1945
1946
1947/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001948 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1949 */
1950int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1951 int (*f_rng)(void *, unsigned char *, size_t),
1952 void *p_rng,
1953 int mode,
1954 mbedtls_md_type_t md_alg,
1955 unsigned int hashlen,
1956 const unsigned char *hash,
1957 unsigned char *sig )
1958{
Cédric Meuterf3fab332020-04-25 11:30:45 +02001959 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
1960 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001965/*
1966 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1967 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001968
1969/* Construct a PKCS v1.5 encoding of a hashed message
1970 *
1971 * This is used both for signature generation and verification.
1972 *
1973 * Parameters:
1974 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001975 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001976 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001977 * - hash: Buffer containing the hashed message or the raw data.
1978 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001979 * - dst: Buffer to hold the encoded message.
1980 *
1981 * Assumptions:
1982 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1983 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001984 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001985 *
1986 */
1987static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1988 unsigned int hashlen,
1989 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001990 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001991 unsigned char *dst )
1992{
1993 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001994 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001995 unsigned char *p = dst;
1996 const char *oid = NULL;
1997
1998 /* Are we signing hashed or raw data? */
1999 if( md_alg != MBEDTLS_MD_NONE )
2000 {
2001 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
2002 if( md_info == NULL )
2003 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2004
2005 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
2006 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2007
2008 hashlen = mbedtls_md_get_size( md_info );
2009
2010 /* Double-check that 8 + hashlen + oid_size can be used as a
2011 * 1-byte ASN.1 length encoding and that there's no overflow. */
2012 if( 8 + hashlen + oid_size >= 0x80 ||
2013 10 + hashlen < hashlen ||
2014 10 + hashlen + oid_size < 10 + hashlen )
2015 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2016
2017 /*
2018 * Static bounds check:
2019 * - Need 10 bytes for five tag-length pairs.
2020 * (Insist on 1-byte length encodings to protect against variants of
2021 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2022 * - Need hashlen bytes for hash
2023 * - Need oid_size bytes for hash alg OID.
2024 */
2025 if( nb_pad < 10 + hashlen + oid_size )
2026 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2027 nb_pad -= 10 + hashlen + oid_size;
2028 }
2029 else
2030 {
2031 if( nb_pad < hashlen )
2032 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2033
2034 nb_pad -= hashlen;
2035 }
2036
Hanno Becker2b2f8982017-09-27 17:10:03 +01002037 /* Need space for signature header and padding delimiter (3 bytes),
2038 * and 8 bytes for the minimal padding */
2039 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002040 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2041 nb_pad -= 3;
2042
2043 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002044 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002045
2046 /* Write signature header and padding */
2047 *p++ = 0;
2048 *p++ = MBEDTLS_RSA_SIGN;
2049 memset( p, 0xFF, nb_pad );
2050 p += nb_pad;
2051 *p++ = 0;
2052
2053 /* Are we signing raw data? */
2054 if( md_alg == MBEDTLS_MD_NONE )
2055 {
2056 memcpy( p, hash, hashlen );
2057 return( 0 );
2058 }
2059
2060 /* Signing hashed data, add corresponding ASN.1 structure
2061 *
2062 * DigestInfo ::= SEQUENCE {
2063 * digestAlgorithm DigestAlgorithmIdentifier,
2064 * digest Digest }
2065 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2066 * Digest ::= OCTET STRING
2067 *
2068 * Schematic:
2069 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2070 * TAG-NULL + LEN [ NULL ] ]
2071 * TAG-OCTET + LEN [ HASH ] ]
2072 */
2073 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002074 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002075 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002076 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002077 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002078 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002079 memcpy( p, oid, oid_size );
2080 p += oid_size;
2081 *p++ = MBEDTLS_ASN1_NULL;
2082 *p++ = 0x00;
2083 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002084 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002085 memcpy( p, hash, hashlen );
2086 p += hashlen;
2087
2088 /* Just a sanity-check, should be automatic
2089 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002090 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002091 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002092 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002093 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2094 }
2095
2096 return( 0 );
2097}
2098
Paul Bakkerb3869132013-02-28 17:21:01 +01002099/*
2100 * Do an RSA operation to sign the message digest
2101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002103 int (*f_rng)(void *, unsigned char *, size_t),
2104 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002105 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002107 unsigned int hashlen,
2108 const unsigned char *hash,
2109 unsigned char *sig )
2110{
Janos Follath24eed8d2019-11-22 13:21:35 +00002111 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002112 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002113
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002114 RSA_VALIDATE_RET( ctx != NULL );
2115 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2116 mode == MBEDTLS_RSA_PUBLIC );
2117 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2118 hashlen == 0 ) ||
2119 hash != NULL );
2120 RSA_VALIDATE_RET( sig != NULL );
2121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2123 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002124
Hanno Beckerfdf38032017-09-06 12:35:55 +01002125 /*
2126 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2127 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002128
Hanno Beckerfdf38032017-09-06 12:35:55 +01002129 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2130 ctx->len, sig ) ) != 0 )
2131 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002132
2133 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002134 * Call respective RSA primitive
2135 */
2136
2137 if( mode == MBEDTLS_RSA_PUBLIC )
2138 {
2139 /* Skip verification on a public key operation */
2140 return( mbedtls_rsa_public( ctx, sig, sig ) );
2141 }
2142
2143 /* Private key operation
2144 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002145 * In order to prevent Lenstra's attack, make the signature in a
2146 * temporary buffer and check it before returning it.
2147 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002148
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002149 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002150 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002151 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2152
Hanno Beckerfdf38032017-09-06 12:35:55 +01002153 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002154 if( verif == NULL )
2155 {
2156 mbedtls_free( sig_try );
2157 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2158 }
2159
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002160 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2161 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2162
Hanno Becker171a8f12017-09-06 12:32:16 +01002163 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002164 {
2165 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2166 goto cleanup;
2167 }
2168
2169 memcpy( sig, sig_try, ctx->len );
2170
2171cleanup:
2172 mbedtls_free( sig_try );
2173 mbedtls_free( verif );
2174
2175 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002178
2179/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 * Do an RSA operation to sign the message digest
2181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002183 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002184 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002187 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002188 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 unsigned char *sig )
2190{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002191 RSA_VALIDATE_RET( ctx != NULL );
2192 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2193 mode == MBEDTLS_RSA_PUBLIC );
2194 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2195 hashlen == 0 ) ||
2196 hash != NULL );
2197 RSA_VALIDATE_RET( sig != NULL );
2198
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 switch( ctx->padding )
2200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201#if defined(MBEDTLS_PKCS1_V15)
2202 case MBEDTLS_RSA_PKCS_V15:
2203 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002204 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002205#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#if defined(MBEDTLS_PKCS1_V21)
2208 case MBEDTLS_RSA_PKCS_V21:
2209 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002210 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002211#endif
2212
Paul Bakker5121ce52009-01-03 21:22:43 +00002213 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002216}
2217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002219/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002220 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002223 int (*f_rng)(void *, unsigned char *, size_t),
2224 void *p_rng,
2225 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002227 unsigned int hashlen,
2228 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002230 int expected_salt_len,
2231 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002232{
Janos Follath24eed8d2019-11-22 13:21:35 +00002233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002234 size_t siglen;
2235 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002236 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002238 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002239 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002240 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 const mbedtls_md_info_t *md_info;
2242 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002243 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002244
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002245 RSA_VALIDATE_RET( ctx != NULL );
2246 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2247 mode == MBEDTLS_RSA_PUBLIC );
2248 RSA_VALIDATE_RET( sig != NULL );
2249 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2250 hashlen == 0 ) ||
2251 hash != NULL );
2252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2254 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002255
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 siglen = ctx->len;
2257
Paul Bakker27fdf462011-06-09 13:55:13 +00002258 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2262 ? mbedtls_rsa_public( ctx, sig, buf )
2263 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002264
2265 if( ret != 0 )
2266 return( ret );
2267
2268 p = buf;
2269
Paul Bakkerb3869132013-02-28 17:21:01 +01002270 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 {
Simon Butcher02037452016-03-01 21:19:12 +00002275 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002277 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002281 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002284 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002288
Paul Bakkerb3869132013-02-28 17:21:01 +01002289 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002290
Simon Butcher02037452016-03-01 21:19:12 +00002291 /*
2292 * Note: EMSA-PSS verification is over the length of N - 1 bits
2293 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002294 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002295
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002296 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2297 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2298
Simon Butcher02037452016-03-01 21:19:12 +00002299 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002300 if( msb % 8 == 0 )
2301 {
2302 p++;
2303 siglen -= 1;
2304 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002305
Gilles Peskine139108a2017-10-18 19:03:42 +02002306 if( siglen < hlen + 2 )
2307 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2308 hash_start = p + siglen - hlen - 1;
2309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002311 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002312 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002313
Jaeden Amero66954e12018-01-25 16:05:54 +00002314 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2315 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002316 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002317
Paul Bakkerb3869132013-02-28 17:21:01 +01002318 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002319
Gilles Peskine6a54b022017-10-17 19:02:13 +02002320 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002321 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002322
Gilles Peskine91048a32017-10-19 17:46:14 +02002323 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002324 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002325 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2326 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002327 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002328
Gilles Peskine6a54b022017-10-17 19:02:13 +02002329 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002332 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002333 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002334 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2335 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002336 }
2337
Simon Butcher02037452016-03-01 21:19:12 +00002338 /*
2339 * Generate H = Hash( M' )
2340 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002341 ret = mbedtls_md_starts( &md_ctx );
2342 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002343 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002344 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2345 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002346 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002347 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2348 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002349 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002350 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2351 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002352 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002353 ret = mbedtls_md_finish( &md_ctx, result );
2354 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002355 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002356
Jaeden Amero66954e12018-01-25 16:05:54 +00002357 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002358 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002359 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002360 goto exit;
2361 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002362
2363exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002365
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002366 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002367}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002368
2369/*
2370 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002373 int (*f_rng)(void *, unsigned char *, size_t),
2374 void *p_rng,
2375 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002377 unsigned int hashlen,
2378 const unsigned char *hash,
2379 const unsigned char *sig )
2380{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002381 mbedtls_md_type_t mgf1_hash_id;
2382 RSA_VALIDATE_RET( ctx != NULL );
2383 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2384 mode == MBEDTLS_RSA_PUBLIC );
2385 RSA_VALIDATE_RET( sig != NULL );
2386 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2387 hashlen == 0 ) ||
2388 hash != NULL );
2389
2390 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002392 : md_alg;
2393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002395 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002397 sig ) );
2398
2399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002403/*
2404 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002407 int (*f_rng)(void *, unsigned char *, size_t),
2408 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002409 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002411 unsigned int hashlen,
2412 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002413 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002414{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002415 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002416 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002417 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002418
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002419 RSA_VALIDATE_RET( ctx != NULL );
2420 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2421 mode == MBEDTLS_RSA_PUBLIC );
2422 RSA_VALIDATE_RET( sig != NULL );
2423 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2424 hashlen == 0 ) ||
2425 hash != NULL );
2426
2427 sig_len = ctx->len;
2428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2430 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002431
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002432 /*
2433 * Prepare expected PKCS1 v1.5 encoding of hash.
2434 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002435
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002436 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2437 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2438 {
2439 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2440 goto cleanup;
2441 }
2442
2443 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2444 encoded_expected ) ) != 0 )
2445 goto cleanup;
2446
2447 /*
2448 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2449 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002452 ? mbedtls_rsa_public( ctx, sig, encoded )
2453 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002454 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002455 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002456
Simon Butcher02037452016-03-01 21:19:12 +00002457 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002458 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002459 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002460
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002461 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2462 sig_len ) ) != 0 )
2463 {
2464 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2465 goto cleanup;
2466 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002467
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002468cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002469
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002470 if( encoded != NULL )
2471 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002472 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002473 mbedtls_free( encoded );
2474 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002475
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002476 if( encoded_expected != NULL )
2477 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002478 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002479 mbedtls_free( encoded_expected );
2480 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002481
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002482 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
2486/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002487 * Do an RSA operation and check the message digest
2488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002490 int (*f_rng)(void *, unsigned char *, size_t),
2491 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002492 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002494 unsigned int hashlen,
2495 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002496 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002497{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002498 RSA_VALIDATE_RET( ctx != NULL );
2499 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2500 mode == MBEDTLS_RSA_PUBLIC );
2501 RSA_VALIDATE_RET( sig != NULL );
2502 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2503 hashlen == 0 ) ||
2504 hash != NULL );
2505
Paul Bakkerb3869132013-02-28 17:21:01 +01002506 switch( ctx->padding )
2507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#if defined(MBEDTLS_PKCS1_V15)
2509 case MBEDTLS_RSA_PKCS_V15:
2510 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002511 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002512#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#if defined(MBEDTLS_PKCS1_V21)
2515 case MBEDTLS_RSA_PKCS_V21:
2516 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002517 hashlen, hash, sig );
2518#endif
2519
2520 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002522 }
2523}
2524
2525/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002526 * Copy the components of an RSA key
2527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002529{
Janos Follath24eed8d2019-11-22 13:21:35 +00002530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002531 RSA_VALIDATE_RET( dst != NULL );
2532 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002533
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002534 dst->len = src->len;
2535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2537 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2540 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2541 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002542
2543#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2545 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2546 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2548 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002549#endif
2550
2551 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2554 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002555
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002556 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002557 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002558
2559cleanup:
2560 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002562
2563 return( ret );
2564}
2565
2566/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 * Free the components of an RSA key
2568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002570{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002571 if( ctx == NULL )
2572 return;
2573
2574 mbedtls_mpi_free( &ctx->Vi );
2575 mbedtls_mpi_free( &ctx->Vf );
2576 mbedtls_mpi_free( &ctx->RN );
2577 mbedtls_mpi_free( &ctx->D );
2578 mbedtls_mpi_free( &ctx->Q );
2579 mbedtls_mpi_free( &ctx->P );
2580 mbedtls_mpi_free( &ctx->E );
2581 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002582
Hanno Becker33c30a02017-08-23 07:00:22 +01002583#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002584 mbedtls_mpi_free( &ctx->RQ );
2585 mbedtls_mpi_free( &ctx->RP );
2586 mbedtls_mpi_free( &ctx->QP );
2587 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002588 mbedtls_mpi_free( &ctx->DP );
2589#endif /* MBEDTLS_RSA_NO_CRT */
2590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002592 /* Free the mutex, but only if it hasn't been freed already. */
2593 if( ctx->ver != 0 )
2594 {
2595 mbedtls_mutex_free( &ctx->mutex );
2596 ctx->ver = 0;
2597 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002598#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002599}
2600
Hanno Beckerab377312017-08-23 16:24:51 +01002601#endif /* !MBEDTLS_RSA_ALT */
2602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002605#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
2607/*
2608 * Example RSA-1024 keypair, for test purposes
2609 */
2610#define KEY_LEN 128
2611
2612#define RSA_N "9292758453063D803DD603D5E777D788" \
2613 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2614 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2615 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2616 "93A89813FBF3C4F8066D2D800F7C38A8" \
2617 "1AE31942917403FF4946B0A83D3D3E05" \
2618 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2619 "5E94BB77B07507233A0BC7BAC8F90F79"
2620
2621#define RSA_E "10001"
2622
2623#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2624 "66CA472BC44D253102F8B4A9D3BFA750" \
2625 "91386C0077937FE33FA3252D28855837" \
2626 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2627 "DF79C5CE07EE72C7F123142198164234" \
2628 "CABB724CF78B8173B9F880FC86322407" \
2629 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2630 "071513A1E85B5DFA031F21ECAE91A34D"
2631
2632#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2633 "2C01CAD19EA484A87EA4377637E75500" \
2634 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2635 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2636
2637#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2638 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2639 "910E4168387E3C30AA1E00C339A79508" \
2640 "8452DD96A9A5EA5D9DCA68DA636032AF"
2641
Paul Bakker5121ce52009-01-03 21:22:43 +00002642#define PT_LEN 24
2643#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2644 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002647static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002648{
gufe44c2620da2020-08-03 17:56:50 +02002649#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002650 size_t i;
2651
Paul Bakker545570e2010-07-18 09:00:25 +00002652 if( rng_state != NULL )
2653 rng_state = NULL;
2654
Paul Bakkera3d195c2011-11-27 21:07:34 +00002655 for( i = 0; i < len; ++i )
2656 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002657#else
2658 if( rng_state != NULL )
2659 rng_state = NULL;
2660
2661 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002662#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002663
Paul Bakkera3d195c2011-11-27 21:07:34 +00002664 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002667
Paul Bakker5121ce52009-01-03 21:22:43 +00002668/*
2669 * Checkup routine
2670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002672{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002673 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002675 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 unsigned char rsa_plaintext[PT_LEN];
2678 unsigned char rsa_decrypted[PT_LEN];
2679 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002681 unsigned char sha1sum[20];
2682#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
Hanno Becker3a701162017-08-22 13:52:43 +01002684 mbedtls_mpi K;
2685
2686 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Hanno Becker3a701162017-08-22 13:52:43 +01002689 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2690 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2691 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2692 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2693 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2694 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2695 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2696 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2697 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2698 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2699
Hanno Becker7f25f852017-10-10 16:56:22 +01002700 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
2702 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2706 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 {
2708 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Hanno Becker5bc87292017-05-03 15:09:31 +01002711 ret = 1;
2712 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 }
2714
2715 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
2718 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2719
Hanno Becker98838b02017-10-02 13:16:10 +01002720 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2721 PT_LEN, rsa_plaintext,
2722 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 {
2724 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726
Hanno Becker5bc87292017-05-03 15:09:31 +01002727 ret = 1;
2728 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 }
2730
2731 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002734 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002735 &len, rsa_ciphertext, rsa_decrypted,
2736 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 {
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
2745 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2746 {
2747 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002749
Hanno Becker5bc87292017-05-03 15:09:31 +01002750 ret = 1;
2751 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 }
2753
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002754 if( verbose != 0 )
2755 mbedtls_printf( "passed\n" );
2756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002759 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002760
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002761 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002762 {
2763 if( verbose != 0 )
2764 mbedtls_printf( "failed\n" );
2765
2766 return( 1 );
2767 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002768
Hanno Becker98838b02017-10-02 13:16:10 +01002769 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2770 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2771 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 {
2773 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
Hanno Becker5bc87292017-05-03 15:09:31 +01002776 ret = 1;
2777 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 }
2779
2780 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002782
Hanno Becker98838b02017-10-02 13:16:10 +01002783 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2784 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2785 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 {
2787 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Hanno Becker5bc87292017-05-03 15:09:31 +01002790 ret = 1;
2791 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002792 }
2793
2794 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002795 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002797
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002798 if( verbose != 0 )
2799 mbedtls_printf( "\n" );
2800
Paul Bakker3d8fb632014-04-17 12:42:41 +02002801cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002802 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 mbedtls_rsa_free( &rsa );
2804#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002805 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002807 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002808}
2809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812#endif /* MBEDTLS_RSA_C */