blob: ef262d9dd5936d0224ed778ba39358cf3843c7a0 [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"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020047#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020048#include "mbedtls/constant_time.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
gufe44c2620da2020-08-03 17:56:50 +020056#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#else
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020065#define mbedtls_calloc calloc
66#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#endif
68
Neil Armstrongea761962022-02-21 10:42:29 +010069#if defined(MBEDTLS_USE_PSA_CRYPTO)
70#include "mbedtls/pk.h"
71#endif
72
73#if defined(MBEDTLS_USE_PSA_CRYPTO)
74int mbedtls_pk_rsa_psa_err_translate( psa_status_t status )
75{
76 switch( status )
77 {
78 case PSA_ERROR_NOT_PERMITTED:
79 case PSA_ERROR_INVALID_ARGUMENT:
80 case PSA_ERROR_INVALID_HANDLE:
81 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
82 case PSA_ERROR_BUFFER_TOO_SMALL:
83 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
84 case PSA_ERROR_INSUFFICIENT_ENTROPY:
85 return( MBEDTLS_ERR_RSA_RNG_FAILED );
86 case PSA_ERROR_INVALID_SIGNATURE:
87 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
88 case PSA_ERROR_INVALID_PADDING:
89 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
90 default:
91 return( mbedtls_pk_psa_err_translate( status ) );
92 }
93}
94#endif
95
Hanno Beckera565f542017-10-11 11:00:19 +010096#if !defined(MBEDTLS_RSA_ALT)
97
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050098/* Parameter validation macros */
99#define RSA_VALIDATE_RET( cond ) \
100 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
101#define RSA_VALIDATE( cond ) \
102 MBEDTLS_INTERNAL_VALIDATE( cond )
103
Hanno Becker617c1ae2017-08-23 14:11:24 +0100104int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
105 const mbedtls_mpi *N,
106 const mbedtls_mpi *P, const mbedtls_mpi *Q,
107 const mbedtls_mpi *D, const mbedtls_mpi *E )
108{
Janos Follath24eed8d2019-11-22 13:21:35 +0000109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100111
112 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
113 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
114 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
115 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
116 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
117 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100118 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100119 }
120
121 if( N != NULL )
122 ctx->len = mbedtls_mpi_size( &ctx->N );
123
124 return( 0 );
125}
126
127int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100128 unsigned char const *N, size_t N_len,
129 unsigned char const *P, size_t P_len,
130 unsigned char const *Q, size_t Q_len,
131 unsigned char const *D, size_t D_len,
132 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100133{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000134 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500135 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100136
137 if( N != NULL )
138 {
139 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
140 ctx->len = mbedtls_mpi_size( &ctx->N );
141 }
142
143 if( P != NULL )
144 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
145
146 if( Q != NULL )
147 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
148
149 if( D != NULL )
150 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
151
152 if( E != NULL )
153 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
154
155cleanup:
156
157 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100158 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100159
160 return( 0 );
161}
162
Hanno Becker705fc682017-10-10 17:57:02 +0100163/*
164 * Checks whether the context fields are set in such a way
165 * that the RSA primitives will be able to execute without error.
166 * It does *not* make guarantees for consistency of the parameters.
167 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100168static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
169 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100170{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100171#if !defined(MBEDTLS_RSA_NO_CRT)
172 /* blinding_needed is only used for NO_CRT to decide whether
173 * P,Q need to be present or not. */
174 ((void) blinding_needed);
175#endif
176
Hanno Becker3a760a12018-01-05 08:14:49 +0000177 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
178 ctx->len > MBEDTLS_MPI_MAX_SIZE )
179 {
Hanno Becker705fc682017-10-10 17:57:02 +0100180 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000181 }
Hanno Becker705fc682017-10-10 17:57:02 +0100182
183 /*
184 * 1. Modular exponentiation needs positive, odd moduli.
185 */
186
187 /* Modular exponentiation wrt. N is always used for
188 * RSA public key operations. */
189 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
190 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
191 {
192 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
193 }
194
195#if !defined(MBEDTLS_RSA_NO_CRT)
196 /* Modular exponentiation for P and Q is only
197 * used for private key operations and if CRT
198 * is used. */
199 if( is_priv &&
200 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
201 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
202 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
203 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
204 {
205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
206 }
207#endif /* !MBEDTLS_RSA_NO_CRT */
208
209 /*
210 * 2. Exponents must be positive
211 */
212
213 /* Always need E for public key operations */
214 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
215 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
216
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100217#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100218 /* For private key operations, use D or DP & DQ
219 * as (unblinded) exponents. */
220 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
221 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
222#else
223 if( is_priv &&
224 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
225 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
226 {
227 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
228 }
229#endif /* MBEDTLS_RSA_NO_CRT */
230
231 /* Blinding shouldn't make exponents negative either,
232 * so check that P, Q >= 1 if that hasn't yet been
233 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100234#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100235 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100236 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
237 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
238 {
239 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
240 }
241#endif
242
243 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100244 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100245#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100246 if( is_priv &&
247 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
248 {
249 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
250 }
251#endif
252
253 return( 0 );
254}
255
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100256int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100257{
258 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500259 int have_N, have_P, have_Q, have_D, have_E;
260#if !defined(MBEDTLS_RSA_NO_CRT)
261 int have_DP, have_DQ, have_QP;
262#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500263 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100264
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500265 RSA_VALIDATE_RET( ctx != NULL );
266
267 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
268 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
269 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
270 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
271 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500272
273#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500274 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
275 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
276 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500277#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100278
Hanno Becker617c1ae2017-08-23 14:11:24 +0100279 /*
280 * Check whether provided parameters are enough
281 * to deduce all others. The following incomplete
282 * parameter sets for private keys are supported:
283 *
284 * (1) P, Q missing.
285 * (2) D and potentially N missing.
286 *
287 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100288
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500289 n_missing = have_P && have_Q && have_D && have_E;
290 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
291 d_missing = have_P && have_Q && !have_D && have_E;
292 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100293
294 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500295 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100296
Hanno Becker617c1ae2017-08-23 14:11:24 +0100297 if( !is_priv && !is_pub )
298 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
299
300 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100301 * Step 1: Deduce N if P, Q are provided.
302 */
303
304 if( !have_N && have_P && have_Q )
305 {
306 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
307 &ctx->Q ) ) != 0 )
308 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100309 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100310 }
311
312 ctx->len = mbedtls_mpi_size( &ctx->N );
313 }
314
315 /*
316 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100317 */
318
319 if( pq_missing )
320 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100321 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100322 &ctx->P, &ctx->Q );
323 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100324 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100325
326 }
327 else if( d_missing )
328 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100329 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
330 &ctx->Q,
331 &ctx->E,
332 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100333 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100334 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100335 }
336 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100337
Hanno Becker617c1ae2017-08-23 14:11:24 +0100338 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100339 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100340 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100341 */
342
Hanno Becker23344b52017-08-23 07:43:27 +0100343#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500344 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100345 {
346 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
347 &ctx->DP, &ctx->DQ, &ctx->QP );
348 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100349 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100350 }
Hanno Becker23344b52017-08-23 07:43:27 +0100351#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100352
353 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100354 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100355 */
356
Hanno Beckerebd2c022017-10-12 10:54:53 +0100357 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100358}
359
Hanno Becker617c1ae2017-08-23 14:11:24 +0100360int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
361 unsigned char *N, size_t N_len,
362 unsigned char *P, size_t P_len,
363 unsigned char *Q, size_t Q_len,
364 unsigned char *D, size_t D_len,
365 unsigned char *E, size_t E_len )
366{
367 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500368 int is_priv;
369 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100370
371 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500372 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100373 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
374 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
375 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
376 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
377 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
378
379 if( !is_priv )
380 {
381 /* If we're trying to export private parameters for a public key,
382 * something must be wrong. */
383 if( P != NULL || Q != NULL || D != NULL )
384 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
385
386 }
387
388 if( N != NULL )
389 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
390
391 if( P != NULL )
392 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
393
394 if( Q != NULL )
395 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
396
397 if( D != NULL )
398 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
399
400 if( E != NULL )
401 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100402
403cleanup:
404
405 return( ret );
406}
407
Hanno Becker617c1ae2017-08-23 14:11:24 +0100408int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
409 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
410 mbedtls_mpi *D, mbedtls_mpi *E )
411{
Janos Follath24eed8d2019-11-22 13:21:35 +0000412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500413 int is_priv;
414 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100415
416 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500417 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100418 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
419 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
420 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
421 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
422 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
423
424 if( !is_priv )
425 {
426 /* If we're trying to export private parameters for a public key,
427 * something must be wrong. */
428 if( P != NULL || Q != NULL || D != NULL )
429 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
430
431 }
432
433 /* Export all requested core parameters. */
434
435 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
436 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
437 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
438 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
439 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
440 {
441 return( ret );
442 }
443
444 return( 0 );
445}
446
447/*
448 * Export CRT parameters
449 * This must also be implemented if CRT is not used, for being able to
450 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
451 * can be used in this case.
452 */
453int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
454 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
455{
Janos Follath24eed8d2019-11-22 13:21:35 +0000456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500457 int is_priv;
458 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100459
460 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500461 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100462 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
463 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
464 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
465 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
466 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
467
468 if( !is_priv )
469 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
470
Hanno Beckerdc95c892017-08-23 06:57:02 +0100471#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100472 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100473 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
474 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
475 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
476 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100477 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100478 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100479#else
480 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
481 DP, DQ, QP ) ) != 0 )
482 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100483 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100484 }
485#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100486
487 return( 0 );
488}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100489
Paul Bakker5121ce52009-01-03 21:22:43 +0000490/*
491 * Initialize an RSA context
492 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200493void mbedtls_rsa_init( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000494{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500495 RSA_VALIDATE( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
Ronald Cronc1905a12021-06-05 11:11:14 +0200499 ctx->padding = MBEDTLS_RSA_PKCS_V15;
500 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100503 /* Set ctx->ver to nonzero to indicate that the mutex has been
504 * initialized and will need to be freed. */
505 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200507#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000508}
509
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100510/*
511 * Set padding for an existing RSA context
512 */
Ronald Cronea7631b2021-06-03 18:51:59 +0200513int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
514 mbedtls_md_type_t hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100515{
Ronald Cron3a0375f2021-06-08 10:22:28 +0200516 switch( padding )
517 {
518#if defined(MBEDTLS_PKCS1_V15)
519 case MBEDTLS_RSA_PKCS_V15:
520 break;
521#endif
522
523#if defined(MBEDTLS_PKCS1_V21)
524 case MBEDTLS_RSA_PKCS_V21:
525 break;
526#endif
527 default:
528 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
529 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200530
531 if( ( padding == MBEDTLS_RSA_PKCS_V21 ) &&
532 ( hash_id != MBEDTLS_MD_NONE ) )
533 {
534 const mbedtls_md_info_t *md_info;
535
536 md_info = mbedtls_md_info_from_type( hash_id );
537 if( md_info == NULL )
538 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
539 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500540
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100541 ctx->padding = padding;
542 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200543
544 return( 0 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100545}
546
Hanno Becker617c1ae2017-08-23 14:11:24 +0100547/*
548 * Get length in bytes of RSA modulus
549 */
550
551size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
552{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100553 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100554}
555
556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559/*
560 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800561 *
562 * This generation method follows the RSA key pair generation procedure of
563 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000566 int (*f_rng)(void *, unsigned char *, size_t),
567 void *p_rng,
568 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000569{
Janos Follath24eed8d2019-11-22 13:21:35 +0000570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800571 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100572 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500573 RSA_VALIDATE_RET( ctx != NULL );
574 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Janos Follathb8fc1b02018-09-03 15:37:01 +0100576 /*
577 * If the modulus is 1024 bit long or shorter, then the security strength of
578 * the RSA algorithm is less than or equal to 80 bits and therefore an error
579 * rate of 2^-80 is sufficient.
580 */
581 if( nbits > 1024 )
582 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
583
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100584 mbedtls_mpi_init( &H );
585 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800586 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100588 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
589 {
590 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
591 goto cleanup;
592 }
593
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 /*
595 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800596 * 1. |P-Q| > 2^( nbits / 2 - 100 )
597 * 2. GCD( E, (P-1)*(Q-1) ) == 1
598 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602 do
603 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100604 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
605 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Janos Follathb8fc1b02018-09-03 15:37:01 +0100607 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
608 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800610 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
611 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
612 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 continue;
614
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800615 /* not required by any standards, but some users rely on the fact that P > Q */
616 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100617 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100618
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100619 /* Temporarily replace P,Q by P-1, Q-1 */
620 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
621 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
622 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800623
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800624 /* 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 +0200625 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800626 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
627 continue;
628
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800629 /* 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 -0800630 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
631 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
632 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
633
634 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
635 continue;
636
637 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800639 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100641 /* Restore P,Q */
642 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
643 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
644
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800645 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
646
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100647 ctx->len = mbedtls_mpi_size( &ctx->N );
648
Jethro Beekman97f95c92018-02-13 15:50:36 -0800649#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 * DP = D mod (P - 1)
652 * DQ = D mod (Q - 1)
653 * QP = Q^-1 mod P
654 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100655 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
656 &ctx->DP, &ctx->DQ, &ctx->QP ) );
657#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000658
Hanno Becker83aad1f2017-08-23 06:45:10 +0100659 /* Double-check */
660 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
662cleanup:
663
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100664 mbedtls_mpi_free( &H );
665 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800666 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
668 if( ret != 0 )
669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100671
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100672 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100673 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100674 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 }
676
Paul Bakker48377d92013-08-30 12:06:24 +0200677 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000678}
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000681
682/*
683 * Check a public RSA key
684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000686{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500687 RSA_VALIDATE_RET( ctx != NULL );
688
Hanno Beckerebd2c022017-10-12 10:54:53 +0100689 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000691
Hanno Becker3a760a12018-01-05 08:14:49 +0000692 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100695 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000696
Hanno Becker705fc682017-10-10 17:57:02 +0100697 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
698 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100702 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
704 return( 0 );
705}
706
707/*
Hanno Becker705fc682017-10-10 17:57:02 +0100708 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000711{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500712 RSA_VALIDATE_RET( ctx != NULL );
713
Hanno Becker705fc682017-10-10 17:57:02 +0100714 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100715 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000716 {
Hanno Becker98838b02017-10-02 13:16:10 +0100717 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000718 }
Paul Bakker48377d92013-08-30 12:06:24 +0200719
Hanno Becker98838b02017-10-02 13:16:10 +0100720 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100721 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100723 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000725
Hanno Beckerb269a852017-08-25 08:03:21 +0100726#if !defined(MBEDTLS_RSA_NO_CRT)
727 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
728 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
729 {
730 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
731 }
732#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000733
734 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000735}
736
737/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100738 * Check if contexts holding a public and private key match
739 */
Hanno Becker98838b02017-10-02 13:16:10 +0100740int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
741 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100742{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500743 RSA_VALIDATE_RET( pub != NULL );
744 RSA_VALIDATE_RET( prv != NULL );
745
Hanno Becker98838b02017-10-02 13:16:10 +0100746 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100750 }
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
753 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100756 }
757
758 return( 0 );
759}
760
761/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 * Do an RSA public key operation
763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000765 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000766 unsigned char *output )
767{
Janos Follath24eed8d2019-11-22 13:21:35 +0000768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000769 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500771 RSA_VALIDATE_RET( ctx != NULL );
772 RSA_VALIDATE_RET( input != NULL );
773 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000774
Hanno Beckerebd2c022017-10-12 10:54:53 +0100775 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100776 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200780#if defined(MBEDTLS_THREADING_C)
781 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
782 return( ret );
783#endif
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000788 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200789 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
790 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000791 }
792
793 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
795 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000796
797cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200799 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
800 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100801#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000804
805 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100806 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000807
808 return( 0 );
809}
810
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200811/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200812 * Generate or update blinding values, see section 10 of:
813 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200814 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200815 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200816 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200817static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200818 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
819{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200820 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200821 mbedtls_mpi R;
822
823 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200824
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200825 if( ctx->Vf.p != NULL )
826 {
827 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
829 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
830 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
831 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200832
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200833 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200834 }
835
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200836 /* Unblinding value: Vf = random number, invertible mod N */
837 do {
838 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200839 {
840 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
841 goto cleanup;
842 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 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 +0200845
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200846 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200847 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
848 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
849 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
850
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200851 /* At this point, Vi is invertible mod N if and only if both Vf and R
852 * are invertible mod N. If one of them isn't, we don't need to know
853 * which one, we just loop and choose new values for both of them.
854 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200855 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500856 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200857 goto cleanup;
858
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500859 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
860
861 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
862 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
863 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200864
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200865 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200866 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 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 +0200868
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200869
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200870cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200871 mbedtls_mpi_free( &R );
872
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200873 return( ret );
874}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200875
Paul Bakker5121ce52009-01-03 21:22:43 +0000876/*
Janos Follathe81102e2017-03-22 13:38:28 +0000877 * Exponent blinding supposed to prevent side-channel attacks using multiple
878 * traces of measurements to recover the RSA key. The more collisions are there,
879 * the more bits of the key can be recovered. See [3].
880 *
881 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
882 * observations on avarage.
883 *
884 * For example with 28 byte blinding to achieve 2 collisions the adversary has
885 * to make 2^112 observations on avarage.
886 *
887 * (With the currently (as of 2017 April) known best algorithms breaking 2048
888 * bit RSA requires approximately as much time as trying out 2^112 random keys.
889 * Thus in this sense with 28 byte blinding the security is not reduced by
890 * side-channel attacks like the one in [3])
891 *
892 * This countermeasure does not help if the key recovery is possible with a
893 * single trace.
894 */
895#define RSA_EXPONENT_BLINDING 28
896
897/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000898 * Do an RSA private key operation
899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200901 int (*f_rng)(void *, unsigned char *, size_t),
902 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000903 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000904 unsigned char *output )
905{
Janos Follath24eed8d2019-11-22 13:21:35 +0000906 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000907 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100908
909 /* Temporary holding the result */
910 mbedtls_mpi T;
911
912 /* Temporaries holding P-1, Q-1 and the
913 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000914 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100915
916#if !defined(MBEDTLS_RSA_NO_CRT)
917 /* Temporaries holding the results mod p resp. mod q. */
918 mbedtls_mpi TP, TQ;
919
920 /* Temporaries holding the blinded exponents for
921 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000922 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100923
924 /* Pointers to actual exponents to be used - either the unblinded
925 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000926 mbedtls_mpi *DP = &ctx->DP;
927 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100928#else
929 /* Temporary holding the blinded exponent (if used). */
930 mbedtls_mpi D_blind;
931
932 /* Pointer to actual exponent to be used - either the unblinded
933 * or the blinded one, depending on the presence of a PRNG. */
934 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100935#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100936
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100937 /* Temporaries holding the initial input and the double
938 * checked result; should be the same in the end. */
939 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000940
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500941 RSA_VALIDATE_RET( ctx != NULL );
942 RSA_VALIDATE_RET( input != NULL );
943 RSA_VALIDATE_RET( output != NULL );
944
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200945 if( f_rng == NULL )
946 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
947
948 if( rsa_check_context( ctx, 1 /* private key checks */,
949 1 /* blinding on */ ) != 0 )
Hanno Beckerebd2c022017-10-12 10:54:53 +0100950 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100951 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100952 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100953
Hanno Becker06811ce2017-05-03 15:10:34 +0100954#if defined(MBEDTLS_THREADING_C)
955 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
956 return( ret );
957#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000958
Hanno Becker06811ce2017-05-03 15:10:34 +0100959 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100960 mbedtls_mpi_init( &T );
961
962 mbedtls_mpi_init( &P1 );
963 mbedtls_mpi_init( &Q1 );
964 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000965
Janos Follathe81102e2017-03-22 13:38:28 +0000966#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200967 mbedtls_mpi_init( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +0000968#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200969 mbedtls_mpi_init( &DP_blind );
970 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000971#endif
972
Hanno Becker06811ce2017-05-03 15:10:34 +0100973#if !defined(MBEDTLS_RSA_NO_CRT)
974 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200975#endif
976
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100977 mbedtls_mpi_init( &I );
978 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100979
980 /* End of MPI initialization */
981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
983 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200985 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
986 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000987 }
988
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100989 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100990
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200991 /*
992 * Blinding
993 * T = T * Vi mod N
994 */
995 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
996 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
997 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000998
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200999 /*
1000 * Exponent blinding
1001 */
1002 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1003 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001004
Janos Follathf9203b42017-03-22 15:13:15 +00001005#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001006 /*
1007 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1008 */
1009 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1010 f_rng, p_rng ) );
1011 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1012 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1013 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001014
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001015 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001016#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001017 /*
1018 * DP_blind = ( P - 1 ) * R + DP
1019 */
1020 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1021 f_rng, p_rng ) );
1022 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1023 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1024 &ctx->DP ) );
Janos Follathf9203b42017-03-22 15:13:15 +00001025
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001026 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001027
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001028 /*
1029 * DQ_blind = ( Q - 1 ) * R + DQ
1030 */
1031 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1032 f_rng, p_rng ) );
1033 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1034 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1035 &ctx->DQ ) );
Janos Follathf9203b42017-03-22 15:13:15 +00001036
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001037 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001038#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001041 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001042#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001043 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001044 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001045 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001046 * TP = input ^ dP mod P
1047 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001048 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001049
1050 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1051 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
1053 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001054 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001056 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1057 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1058 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
1060 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001061 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001063 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1064 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001066
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001067 /*
1068 * Unblind
1069 * T = T * Vf mod N
1070 */
1071 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
1072 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073
Hanno Becker2dec5e82017-10-03 07:49:52 +01001074 /* Verify the result to prevent glitching attacks. */
1075 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1076 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001077 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001078 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001079 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1080 goto cleanup;
1081 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001082
Paul Bakker5121ce52009-01-03 21:22:43 +00001083 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
1086cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001088 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1089 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001090#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001091
Hanno Becker06811ce2017-05-03 15:10:34 +01001092 mbedtls_mpi_free( &P1 );
1093 mbedtls_mpi_free( &Q1 );
1094 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001095
Janos Follathe81102e2017-03-22 13:38:28 +00001096#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001097 mbedtls_mpi_free( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +00001098#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001099 mbedtls_mpi_free( &DP_blind );
1100 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001101#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
Hanno Becker06811ce2017-05-03 15:10:34 +01001103 mbedtls_mpi_free( &T );
1104
1105#if !defined(MBEDTLS_RSA_NO_CRT)
1106 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1107#endif
1108
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001109 mbedtls_mpi_free( &C );
1110 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001111
Gilles Peskineae3741e2020-11-25 00:10:31 +01001112 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001113 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
Gilles Peskineae3741e2020-11-25 00:10:31 +01001115 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001116}
1117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001119/**
1120 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1121 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001122 * \param dst buffer to mask
1123 * \param dlen length of destination buffer
1124 * \param src source of the mask generation
1125 * \param slen length of the source buffer
1126 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001127 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001128static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001130{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001132 unsigned char counter[4];
1133 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001134 unsigned int hlen;
1135 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001136 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001139 memset( counter, 0, 4 );
1140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001142
Simon Butcher02037452016-03-01 21:19:12 +00001143 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001144 p = dst;
1145
1146 while( dlen > 0 )
1147 {
1148 use_len = hlen;
1149 if( dlen < hlen )
1150 use_len = dlen;
1151
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001152 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1153 goto exit;
1154 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1155 goto exit;
1156 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1157 goto exit;
1158 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1159 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001160
1161 for( i = 0; i < use_len; ++i )
1162 *p++ ^= mask[i];
1163
1164 counter[3]++;
1165
1166 dlen -= use_len;
1167 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001168
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001169exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001170 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001171
1172 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001177/*
1178 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001181 int (*f_rng)(void *, unsigned char *, size_t),
1182 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001183 const unsigned char *label, size_t label_len,
1184 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001185 const unsigned char *input,
1186 unsigned char *output )
1187{
1188 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001190 unsigned char *p = output;
1191 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 const mbedtls_md_info_t *md_info;
1193 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001194
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001195 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001196 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001197 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001198 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1199
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001200 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001204 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001206
1207 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001209
Simon Butcher02037452016-03-01 21:19:12 +00001210 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001211 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001213
1214 memset( output, 0, olen );
1215
1216 *p++ = 0;
1217
Simon Butcher02037452016-03-01 21:19:12 +00001218 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001219 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001220 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001221
1222 p += hlen;
1223
Simon Butcher02037452016-03-01 21:19:12 +00001224 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001225 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1226 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001227 p += hlen;
1228 p += olen - 2 * hlen - 2 - ilen;
1229 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001230 if( ilen != 0 )
1231 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001234 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001235 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001236
Simon Butcher02037452016-03-01 21:19:12 +00001237 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001238 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1239 &md_ctx ) ) != 0 )
1240 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001241
Simon Butcher02037452016-03-01 21:19:12 +00001242 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001243 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1244 &md_ctx ) ) != 0 )
1245 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001246
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001247exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001249
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001250 if( ret != 0 )
1251 return( ret );
1252
Thomas Daubney141700f2021-05-13 19:06:10 +01001253 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001258/*
1259 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001262 int (*f_rng)(void *, unsigned char *, size_t),
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001263 void *p_rng, size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001264 const unsigned char *input,
1265 unsigned char *output )
1266{
1267 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001269 unsigned char *p = output;
1270
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001271 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001272 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001273 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001274
Paul Bakkerb3869132013-02-28 17:21:01 +01001275 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001276
Simon Butcher02037452016-03-01 21:19:12 +00001277 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001278 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001280
1281 nb_pad = olen - 3 - ilen;
1282
1283 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001284
1285 if( f_rng == NULL )
1286 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1287
1288 *p++ = MBEDTLS_RSA_CRYPT;
1289
1290 while( nb_pad-- > 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001291 {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001292 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001293
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001294 do {
1295 ret = f_rng( p_rng, p, 1 );
1296 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001297
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001298 /* Check if RNG failed to generate data */
1299 if( rng_dl == 0 || ret != 0 )
1300 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001301
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001302 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001303 }
1304
1305 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001306 if( ilen != 0 )
1307 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001308
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001309 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001310}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001312
Paul Bakker5121ce52009-01-03 21:22:43 +00001313/*
1314 * Add the message padding, then do an RSA operation
1315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001317 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001318 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +01001319 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001320 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 unsigned char *output )
1322{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001323 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001324 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001325 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001326
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 switch( ctx->padding )
1328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_PKCS1_V15)
1330 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney21772772021-05-13 17:30:32 +01001331 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001332 ilen, input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#if defined(MBEDTLS_PKCS1_V21)
1336 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney141700f2021-05-13 19:06:10 +01001337 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, NULL, 0,
Thomas Daubney21772772021-05-13 17:30:32 +01001338 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001339#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
1341 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001344}
1345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001347/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001348 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001351 int (*f_rng)(void *, unsigned char *, size_t),
1352 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001353 const unsigned char *label, size_t label_len,
1354 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001355 const unsigned char *input,
1356 unsigned char *output,
1357 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001358{
Janos Follath24eed8d2019-11-22 13:21:35 +00001359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001360 size_t ilen, i, pad_len;
1361 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1363 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001364 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 const mbedtls_md_info_t *md_info;
1366 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001367
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001368 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001369 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1370 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1371 RSA_VALIDATE_RET( input != NULL );
1372 RSA_VALIDATE_RET( olen != NULL );
1373
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001374 /*
1375 * Parameters sanity checks
1376 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001377 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379
1380 ilen = ctx->len;
1381
Paul Bakker27fdf462011-06-09 13:55:13 +00001382 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001386 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001388
Janos Follathc17cda12016-02-11 11:08:18 +00001389 hlen = mbedtls_md_get_size( md_info );
1390
1391 // checking for integer underflow
1392 if( 2 * hlen + 2 > ilen )
1393 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1394
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001395 /*
1396 * RSA operation
1397 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001398 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
1400 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001401 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001403 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001404 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001407 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1408 {
1409 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001410 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001411 }
1412
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001413 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001414 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1415 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001416 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001417 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1418 &md_ctx ) ) != 0 )
1419 {
1420 mbedtls_md_free( &md_ctx );
1421 goto cleanup;
1422 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001425
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001426 /* Generate lHash */
1427 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1428 goto cleanup;
1429
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001430 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001431 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001432 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001434 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001436 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001437
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001438 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001439
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001440 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001441 for( i = 0; i < hlen; i++ )
1442 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001443
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001444 /* Get zero-padding len, but always read till end of buffer
1445 * (minus one, for the 01 byte) */
1446 pad_len = 0;
1447 pad_done = 0;
1448 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1449 {
1450 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001451 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001452 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001453
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001454 p += pad_len;
1455 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001456
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001457 /*
1458 * The only information "leaked" is whether the padding was correct or not
1459 * (eg, no data is copied if it was not correct). This meets the
1460 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1461 * the different error conditions.
1462 */
1463 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001464 {
1465 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1466 goto cleanup;
1467 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001468
Paul Bakker66d5d072014-06-17 16:39:18 +02001469 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001470 {
1471 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1472 goto cleanup;
1473 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001474
1475 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001476 if( *olen != 0 )
1477 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001478 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001479
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001480cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001481 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1482 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001483
1484 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001489/*
1490 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1491 */
1492int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
1493 int (*f_rng)(void *, unsigned char *, size_t),
1494 void *p_rng,
1495 size_t *olen,
1496 const unsigned char *input,
1497 unsigned char *output,
1498 size_t output_max_len )
1499{
1500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1501 size_t ilen;
1502 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1503
1504 RSA_VALIDATE_RET( ctx != NULL );
1505 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1506 RSA_VALIDATE_RET( input != NULL );
1507 RSA_VALIDATE_RET( olen != NULL );
1508
1509 ilen = ctx->len;
1510
1511 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1512 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1513
1514 if( ilen < 16 || ilen > sizeof( buf ) )
1515 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1516
1517 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
1518
1519 if( ret != 0 )
1520 goto cleanup;
1521
Gabor Mezei90437e32021-10-20 11:59:27 +02001522 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding( buf, ilen,
Gabor Mezei63bbba52021-10-18 16:17:57 +02001523 output, output_max_len, olen );
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001524
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001525cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001526 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001527
1528 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001529}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001531
1532/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001533 * Do an RSA operation, then remove the message padding
1534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001536 int (*f_rng)(void *, unsigned char *, size_t),
1537 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001538 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001539 const unsigned char *input,
1540 unsigned char *output,
1541 size_t output_max_len)
1542{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001543 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001544 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1545 RSA_VALIDATE_RET( input != NULL );
1546 RSA_VALIDATE_RET( olen != NULL );
1547
Paul Bakkerb3869132013-02-28 17:21:01 +01001548 switch( ctx->padding )
1549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#if defined(MBEDTLS_PKCS1_V15)
1551 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney34733082021-05-12 09:24:29 +01001552 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001553 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001554#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556#if defined(MBEDTLS_PKCS1_V21)
1557 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001558 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001559 olen, input, output,
1560 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001561#endif
1562
1563 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001565 }
1566}
1567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001569static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001570 int (*f_rng)(void *, unsigned char *, size_t),
1571 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001573 unsigned int hashlen,
1574 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001575 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001576 unsigned char *sig )
1577{
1578 size_t olen;
1579 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001580 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001581 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001583 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 const mbedtls_md_info_t *md_info;
1585 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001586 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001587 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1588 hashlen == 0 ) ||
1589 hash != NULL );
1590 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001591
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001592 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1593 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1594
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001595 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001597
1598 olen = ctx->len;
1599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001601 {
Simon Butcher02037452016-03-01 21:19:12 +00001602 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001604 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001606
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001607 if( hashlen != mbedtls_md_get_size( md_info ) )
1608 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001609 }
1610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001612 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001616
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001617 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1618 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001619 /* Calculate the largest possible salt length, up to the hash size.
1620 * Normally this is the hash length, which is the maximum salt length
1621 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001622 * enough room, use the maximum salt length that fits. The constraint is
1623 * that the hash length plus the salt length plus 2 bytes must be at most
1624 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1625 * (PKCS#1 v2.2) §9.1.1 step 3. */
1626 min_slen = hlen - 2;
1627 if( olen < hlen + min_slen + 2 )
1628 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1629 else if( olen >= hlen + hlen + 2 )
1630 slen = hlen;
1631 else
1632 slen = olen - hlen - 2;
1633 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001634 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001637 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001638 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001639 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001640 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001641 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001642
1643 memset( sig, 0, olen );
1644
Simon Butcher02037452016-03-01 21:19:12 +00001645 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001646 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001647 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001648 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001649
1650 /* Generate salt of length slen in place in the encoded message */
1651 salt = p;
1652 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001653 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001654
Paul Bakkerb3869132013-02-28 17:21:01 +01001655 p += slen;
1656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001658 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001659 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001660
Simon Butcher02037452016-03-01 21:19:12 +00001661 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001662 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1663 goto exit;
1664 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1665 goto exit;
1666 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1667 goto exit;
1668 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1669 goto exit;
1670 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1671 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001672
Simon Butcher02037452016-03-01 21:19:12 +00001673 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001674 if( msb % 8 == 0 )
1675 offset = 1;
1676
Simon Butcher02037452016-03-01 21:19:12 +00001677 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001678 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1679 &md_ctx ) ) != 0 )
1680 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001681
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001682 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001683 sig[0] &= 0xFF >> ( olen * 8 - msb );
1684
1685 p += hlen;
1686 *p++ = 0xBC;
1687
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001688exit:
1689 mbedtls_md_free( &md_ctx );
1690
1691 if( ret != 0 )
1692 return( ret );
1693
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001694 return mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001695}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001696
1697/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001698 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1699 * the option to pass in the salt length.
1700 */
1701int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1702 int (*f_rng)(void *, unsigned char *, size_t),
1703 void *p_rng,
1704 mbedtls_md_type_t md_alg,
1705 unsigned int hashlen,
1706 const unsigned char *hash,
1707 int saltlen,
1708 unsigned char *sig )
1709{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001710 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001711 hashlen, hash, saltlen, sig );
1712}
1713
1714
1715/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001716 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1717 */
1718int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1719 int (*f_rng)(void *, unsigned char *, size_t),
1720 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001721 mbedtls_md_type_t md_alg,
1722 unsigned int hashlen,
1723 const unsigned char *hash,
1724 unsigned char *sig )
1725{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001726 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001727 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001728}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001732/*
1733 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1734 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001735
1736/* Construct a PKCS v1.5 encoding of a hashed message
1737 *
1738 * This is used both for signature generation and verification.
1739 *
1740 * Parameters:
1741 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001742 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001743 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001744 * - hash: Buffer containing the hashed message or the raw data.
1745 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001746 * - dst: Buffer to hold the encoded message.
1747 *
1748 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001749 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001750 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001751 *
1752 */
1753static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1754 unsigned int hashlen,
1755 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001756 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001757 unsigned char *dst )
1758{
1759 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001760 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001761 unsigned char *p = dst;
1762 const char *oid = NULL;
1763
1764 /* Are we signing hashed or raw data? */
1765 if( md_alg != MBEDTLS_MD_NONE )
1766 {
1767 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1768 if( md_info == NULL )
1769 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1770
1771 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1772 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1773
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001774 if( hashlen != mbedtls_md_get_size( md_info ) )
1775 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001776
1777 /* Double-check that 8 + hashlen + oid_size can be used as a
1778 * 1-byte ASN.1 length encoding and that there's no overflow. */
1779 if( 8 + hashlen + oid_size >= 0x80 ||
1780 10 + hashlen < hashlen ||
1781 10 + hashlen + oid_size < 10 + hashlen )
1782 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1783
1784 /*
1785 * Static bounds check:
1786 * - Need 10 bytes for five tag-length pairs.
1787 * (Insist on 1-byte length encodings to protect against variants of
1788 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1789 * - Need hashlen bytes for hash
1790 * - Need oid_size bytes for hash alg OID.
1791 */
1792 if( nb_pad < 10 + hashlen + oid_size )
1793 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1794 nb_pad -= 10 + hashlen + oid_size;
1795 }
1796 else
1797 {
1798 if( nb_pad < hashlen )
1799 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1800
1801 nb_pad -= hashlen;
1802 }
1803
Hanno Becker2b2f8982017-09-27 17:10:03 +01001804 /* Need space for signature header and padding delimiter (3 bytes),
1805 * and 8 bytes for the minimal padding */
1806 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001807 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1808 nb_pad -= 3;
1809
1810 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001811 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001812
1813 /* Write signature header and padding */
1814 *p++ = 0;
1815 *p++ = MBEDTLS_RSA_SIGN;
1816 memset( p, 0xFF, nb_pad );
1817 p += nb_pad;
1818 *p++ = 0;
1819
1820 /* Are we signing raw data? */
1821 if( md_alg == MBEDTLS_MD_NONE )
1822 {
1823 memcpy( p, hash, hashlen );
1824 return( 0 );
1825 }
1826
1827 /* Signing hashed data, add corresponding ASN.1 structure
1828 *
1829 * DigestInfo ::= SEQUENCE {
1830 * digestAlgorithm DigestAlgorithmIdentifier,
1831 * digest Digest }
1832 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1833 * Digest ::= OCTET STRING
1834 *
1835 * Schematic:
1836 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1837 * TAG-NULL + LEN [ NULL ] ]
1838 * TAG-OCTET + LEN [ HASH ] ]
1839 */
1840 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001841 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001842 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001843 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001844 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001845 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001846 memcpy( p, oid, oid_size );
1847 p += oid_size;
1848 *p++ = MBEDTLS_ASN1_NULL;
1849 *p++ = 0x00;
1850 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001851 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001852 memcpy( p, hash, hashlen );
1853 p += hashlen;
1854
1855 /* Just a sanity-check, should be automatic
1856 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001857 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001858 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001859 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001860 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1861 }
1862
1863 return( 0 );
1864}
1865
Paul Bakkerb3869132013-02-28 17:21:01 +01001866/*
1867 * Do an RSA operation to sign the message digest
1868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001870 int (*f_rng)(void *, unsigned char *, size_t),
1871 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001873 unsigned int hashlen,
1874 const unsigned char *hash,
1875 unsigned char *sig )
1876{
Janos Follath24eed8d2019-11-22 13:21:35 +00001877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001878 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001879
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001880 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001881 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1882 hashlen == 0 ) ||
1883 hash != NULL );
1884 RSA_VALIDATE_RET( sig != NULL );
1885
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001886 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1887 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1888
Hanno Beckerfdf38032017-09-06 12:35:55 +01001889 /*
1890 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1891 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001892
Hanno Beckerfdf38032017-09-06 12:35:55 +01001893 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1894 ctx->len, sig ) ) != 0 )
1895 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001896
Hanno Beckerfdf38032017-09-06 12:35:55 +01001897 /* Private key operation
1898 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001899 * In order to prevent Lenstra's attack, make the signature in a
1900 * temporary buffer and check it before returning it.
1901 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001902
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001903 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001904 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001905 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1906
Hanno Beckerfdf38032017-09-06 12:35:55 +01001907 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001908 if( verif == NULL )
1909 {
1910 mbedtls_free( sig_try );
1911 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1912 }
1913
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001914 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1915 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1916
Gabor Mezei90437e32021-10-20 11:59:27 +02001917 if( mbedtls_ct_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001918 {
1919 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1920 goto cleanup;
1921 }
1922
1923 memcpy( sig, sig_try, ctx->len );
1924
1925cleanup:
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001926 mbedtls_platform_zeroize( sig_try, ctx->len );
1927 mbedtls_platform_zeroize( verif, ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001928 mbedtls_free( sig_try );
1929 mbedtls_free( verif );
1930
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001931 if( ret != 0 )
1932 memset( sig, '!', ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001933 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001936
1937/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 * Do an RSA operation to sign the message digest
1939 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001941 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001942 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001944 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001945 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001946 unsigned char *sig )
1947{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001948 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001949 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1950 hashlen == 0 ) ||
1951 hash != NULL );
1952 RSA_VALIDATE_RET( sig != NULL );
1953
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 switch( ctx->padding )
1955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#if defined(MBEDTLS_PKCS1_V15)
1957 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney52654982021-05-18 16:54:00 +01001958 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng,
Thomas Daubney140184d2021-05-18 16:04:07 +01001959 md_alg, hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001960#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962#if defined(MBEDTLS_PKCS1_V21)
1963 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyde9fdc42021-05-18 17:10:04 +01001964 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
1965 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001966#endif
1967
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001971}
1972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001974/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001975 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001979 unsigned int hashlen,
1980 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001982 int expected_salt_len,
1983 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001984{
Janos Follath24eed8d2019-11-22 13:21:35 +00001985 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001986 size_t siglen;
1987 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001988 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001990 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001991 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001992 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 const mbedtls_md_info_t *md_info;
1994 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001995 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001996
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001997 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001998 RSA_VALIDATE_RET( sig != NULL );
1999 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2000 hashlen == 0 ) ||
2001 hash != NULL );
2002
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 siglen = ctx->len;
2004
Paul Bakker27fdf462011-06-09 13:55:13 +00002005 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002007
Thomas Daubney782a7f52021-05-19 12:27:35 +01002008 ret = mbedtls_rsa_public( ctx, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002009
2010 if( ret != 0 )
2011 return( ret );
2012
2013 p = buf;
2014
Paul Bakkerb3869132013-02-28 17:21:01 +01002015 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 {
Simon Butcher02037452016-03-01 21:19:12 +00002020 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002022 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002024
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002025 if( hashlen != mbedtls_md_get_size( md_info ) )
2026 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002027 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002030 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002034
Paul Bakkerb3869132013-02-28 17:21:01 +01002035 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002036
Simon Butcher02037452016-03-01 21:19:12 +00002037 /*
2038 * Note: EMSA-PSS verification is over the length of N - 1 bits
2039 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002040 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002041
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002042 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2043 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2044
Simon Butcher02037452016-03-01 21:19:12 +00002045 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002046 if( msb % 8 == 0 )
2047 {
2048 p++;
2049 siglen -= 1;
2050 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002051
Gilles Peskine139108a2017-10-18 19:03:42 +02002052 if( siglen < hlen + 2 )
2053 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2054 hash_start = p + siglen - hlen - 1;
2055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002057 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002058 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002059
Jaeden Amero66954e12018-01-25 16:05:54 +00002060 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2061 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002062 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002063
Paul Bakkerb3869132013-02-28 17:21:01 +01002064 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002065
Gilles Peskine6a54b022017-10-17 19:02:13 +02002066 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002067 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002068
Gilles Peskine91048a32017-10-19 17:46:14 +02002069 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002070 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002071 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2072 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002073 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002074
Gilles Peskine6a54b022017-10-17 19:02:13 +02002075 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002078 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002079 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002080 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2081 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002082 }
2083
Simon Butcher02037452016-03-01 21:19:12 +00002084 /*
2085 * Generate H = Hash( M' )
2086 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002087 ret = mbedtls_md_starts( &md_ctx );
2088 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002089 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002090 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2091 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002092 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002093 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2094 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002095 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002096 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2097 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002098 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002099 ret = mbedtls_md_finish( &md_ctx, result );
2100 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002101 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002102
Jaeden Amero66954e12018-01-25 16:05:54 +00002103 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002104 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002105 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002106 goto exit;
2107 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002108
2109exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002111
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002112 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002113}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002114
2115/*
2116 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002120 unsigned int hashlen,
2121 const unsigned char *hash,
2122 const unsigned char *sig )
2123{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002124 mbedtls_md_type_t mgf1_hash_id;
2125 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002126 RSA_VALIDATE_RET( sig != NULL );
2127 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2128 hashlen == 0 ) ||
2129 hash != NULL );
2130
2131 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002133 : md_alg;
2134
Thomas Daubney9e65f792021-05-19 12:18:58 +01002135 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx,
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002136 md_alg, hashlen, hash,
2137 mgf1_hash_id,
2138 MBEDTLS_RSA_SALT_LEN_ANY,
2139 sig ) );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002140
2141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002145/*
2146 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002150 unsigned int hashlen,
2151 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002152 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002153{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002154 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002155 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002156 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002157
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002158 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002159 RSA_VALIDATE_RET( sig != NULL );
2160 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2161 hashlen == 0 ) ||
2162 hash != NULL );
2163
2164 sig_len = ctx->len;
2165
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002166 /*
2167 * Prepare expected PKCS1 v1.5 encoding of hash.
2168 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002169
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002170 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2171 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2172 {
2173 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2174 goto cleanup;
2175 }
2176
2177 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2178 encoded_expected ) ) != 0 )
2179 goto cleanup;
2180
2181 /*
2182 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2183 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002184
Thomas Daubney41e4ce42021-05-19 15:10:05 +01002185 ret = mbedtls_rsa_public( ctx, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002186 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002187 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002188
Simon Butcher02037452016-03-01 21:19:12 +00002189 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002190 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002191 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002192
Gabor Mezei90437e32021-10-20 11:59:27 +02002193 if( ( ret = mbedtls_ct_memcmp( encoded, encoded_expected,
gabor-mezei-arm46025642021-07-19 15:19:19 +02002194 sig_len ) ) != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002195 {
2196 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2197 goto cleanup;
2198 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002199
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002200cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002201
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002202 if( encoded != NULL )
2203 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002204 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002205 mbedtls_free( encoded );
2206 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002207
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002208 if( encoded_expected != NULL )
2209 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002210 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002211 mbedtls_free( encoded_expected );
2212 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002213
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002214 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002217
2218/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002219 * Do an RSA operation and check the message digest
2220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002223 unsigned int hashlen,
2224 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002225 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002226{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002227 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002228 RSA_VALIDATE_RET( sig != NULL );
2229 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2230 hashlen == 0 ) ||
2231 hash != NULL );
2232
Paul Bakkerb3869132013-02-28 17:21:01 +01002233 switch( ctx->padding )
2234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235#if defined(MBEDTLS_PKCS1_V15)
2236 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney2e126252021-05-19 11:48:53 +01002237 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, md_alg,
2238 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002239#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241#if defined(MBEDTLS_PKCS1_V21)
2242 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002243 return mbedtls_rsa_rsassa_pss_verify( ctx, md_alg,
2244 hashlen, hash, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01002245#endif
2246
2247 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002249 }
2250}
2251
2252/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002253 * Copy the components of an RSA key
2254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002256{
Janos Follath24eed8d2019-11-22 13:21:35 +00002257 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002258 RSA_VALIDATE_RET( dst != NULL );
2259 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002260
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002261 dst->len = src->len;
2262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2264 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2267 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2268 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002269
2270#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2272 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2273 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2275 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002276#endif
2277
2278 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2281 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002282
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002283 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002284 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002285
2286cleanup:
2287 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002289
2290 return( ret );
2291}
2292
2293/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 * Free the components of an RSA key
2295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002297{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002298 if( ctx == NULL )
2299 return;
2300
2301 mbedtls_mpi_free( &ctx->Vi );
2302 mbedtls_mpi_free( &ctx->Vf );
2303 mbedtls_mpi_free( &ctx->RN );
2304 mbedtls_mpi_free( &ctx->D );
2305 mbedtls_mpi_free( &ctx->Q );
2306 mbedtls_mpi_free( &ctx->P );
2307 mbedtls_mpi_free( &ctx->E );
2308 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002309
Hanno Becker33c30a02017-08-23 07:00:22 +01002310#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002311 mbedtls_mpi_free( &ctx->RQ );
2312 mbedtls_mpi_free( &ctx->RP );
2313 mbedtls_mpi_free( &ctx->QP );
2314 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002315 mbedtls_mpi_free( &ctx->DP );
2316#endif /* MBEDTLS_RSA_NO_CRT */
2317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002319 /* Free the mutex, but only if it hasn't been freed already. */
2320 if( ctx->ver != 0 )
2321 {
2322 mbedtls_mutex_free( &ctx->mutex );
2323 ctx->ver = 0;
2324 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002325#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002326}
2327
Hanno Beckerab377312017-08-23 16:24:51 +01002328#endif /* !MBEDTLS_RSA_ALT */
2329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002332#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002333
2334/*
2335 * Example RSA-1024 keypair, for test purposes
2336 */
2337#define KEY_LEN 128
2338
2339#define RSA_N "9292758453063D803DD603D5E777D788" \
2340 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2341 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2342 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2343 "93A89813FBF3C4F8066D2D800F7C38A8" \
2344 "1AE31942917403FF4946B0A83D3D3E05" \
2345 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2346 "5E94BB77B07507233A0BC7BAC8F90F79"
2347
2348#define RSA_E "10001"
2349
2350#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2351 "66CA472BC44D253102F8B4A9D3BFA750" \
2352 "91386C0077937FE33FA3252D28855837" \
2353 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2354 "DF79C5CE07EE72C7F123142198164234" \
2355 "CABB724CF78B8173B9F880FC86322407" \
2356 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2357 "071513A1E85B5DFA031F21ECAE91A34D"
2358
2359#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2360 "2C01CAD19EA484A87EA4377637E75500" \
2361 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2362 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2363
2364#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2365 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2366 "910E4168387E3C30AA1E00C339A79508" \
2367 "8452DD96A9A5EA5D9DCA68DA636032AF"
2368
Paul Bakker5121ce52009-01-03 21:22:43 +00002369#define PT_LEN 24
2370#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2371 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002374static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002375{
gufe44c2620da2020-08-03 17:56:50 +02002376#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002377 size_t i;
2378
Paul Bakker545570e2010-07-18 09:00:25 +00002379 if( rng_state != NULL )
2380 rng_state = NULL;
2381
Paul Bakkera3d195c2011-11-27 21:07:34 +00002382 for( i = 0; i < len; ++i )
2383 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002384#else
2385 if( rng_state != NULL )
2386 rng_state = NULL;
2387
2388 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002389#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002390
Paul Bakkera3d195c2011-11-27 21:07:34 +00002391 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002392}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002394
Paul Bakker5121ce52009-01-03 21:22:43 +00002395/*
2396 * Checkup routine
2397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002399{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002400 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002402 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 unsigned char rsa_plaintext[PT_LEN];
2405 unsigned char rsa_decrypted[PT_LEN];
2406 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002408 unsigned char sha1sum[20];
2409#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002410
Hanno Becker3a701162017-08-22 13:52:43 +01002411 mbedtls_mpi K;
2412
2413 mbedtls_mpi_init( &K );
Ronald Cronc1905a12021-06-05 11:11:14 +02002414 mbedtls_rsa_init( &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
Hanno Becker3a701162017-08-22 13:52:43 +01002416 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2417 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2418 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2419 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2420 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2421 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2422 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2423 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2424 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2425 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2426
Hanno Becker7f25f852017-10-10 16:56:22 +01002427 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
2429 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2433 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 {
2435 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437
Hanno Becker5bc87292017-05-03 15:09:31 +01002438 ret = 1;
2439 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 }
2441
2442 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2446
Thomas Daubney21772772021-05-13 17:30:32 +01002447 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002448 PT_LEN, rsa_plaintext,
2449 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 {
2451 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
Hanno Becker5bc87292017-05-03 15:09:31 +01002454 ret = 1;
2455 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 }
2457
2458 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002461 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002462 &len, rsa_ciphertext, rsa_decrypted,
2463 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 {
2465 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002467
Hanno Becker5bc87292017-05-03 15:09:31 +01002468 ret = 1;
2469 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 }
2471
2472 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2473 {
2474 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
Hanno Becker5bc87292017-05-03 15:09:31 +01002477 ret = 1;
2478 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 }
2480
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002481 if( verbose != 0 )
2482 mbedtls_printf( "passed\n" );
2483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002486 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
TRodziewicz26371e42021-06-08 16:45:41 +02002488 if( mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002489 {
2490 if( verbose != 0 )
2491 mbedtls_printf( "failed\n" );
2492
2493 return( 1 );
2494 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
Hanno Becker98838b02017-10-02 13:16:10 +01002496 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002497 MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002498 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 {
2500 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Hanno Becker5bc87292017-05-03 15:09:31 +01002503 ret = 1;
2504 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 }
2506
2507 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002510 if( mbedtls_rsa_pkcs1_verify( &rsa, MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002511 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 {
2513 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Hanno Becker5bc87292017-05-03 15:09:31 +01002516 ret = 1;
2517 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 }
2519
2520 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002521 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002524 if( verbose != 0 )
2525 mbedtls_printf( "\n" );
2526
Paul Bakker3d8fb632014-04-17 12:42:41 +02002527cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002528 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 mbedtls_rsa_free( &rsa );
2530#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002531 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002533 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002534}
2535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538#endif /* MBEDTLS_RSA_C */