blob: 20983c82ef1d7feb53ea4daa935e74009dd5935b [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
gufe44c2620da2020-08-03 17:56:50 +020052#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000053#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010058#else
Rich Evans00ab4702015-02-06 13:43:58 +000059#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020061#define mbedtls_calloc calloc
62#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010063#endif
64
Hanno Beckera565f542017-10-11 11:00:19 +010065#if !defined(MBEDTLS_RSA_ALT)
66
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050067/* Parameter validation macros */
68#define RSA_VALIDATE_RET( cond ) \
69 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
70#define RSA_VALIDATE( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE( cond )
72
Hanno Becker617c1ae2017-08-23 14:11:24 +010073int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
74 const mbedtls_mpi *N,
75 const mbedtls_mpi *P, const mbedtls_mpi *Q,
76 const mbedtls_mpi *D, const mbedtls_mpi *E )
77{
Janos Follath24eed8d2019-11-22 13:21:35 +000078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050079 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +010080
81 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
82 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
83 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
84 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
85 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
86 {
Chris Jonesb7d02e02021-04-01 17:40:03 +010087 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +010088 }
89
90 if( N != NULL )
91 ctx->len = mbedtls_mpi_size( &ctx->N );
92
93 return( 0 );
94}
95
96int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +010097 unsigned char const *N, size_t N_len,
98 unsigned char const *P, size_t P_len,
99 unsigned char const *Q, size_t Q_len,
100 unsigned char const *D, size_t D_len,
101 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100102{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000103 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500104 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100105
106 if( N != NULL )
107 {
108 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
109 ctx->len = mbedtls_mpi_size( &ctx->N );
110 }
111
112 if( P != NULL )
113 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
114
115 if( Q != NULL )
116 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
117
118 if( D != NULL )
119 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
120
121 if( E != NULL )
122 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
123
124cleanup:
125
126 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100127 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100128
129 return( 0 );
130}
131
Hanno Becker705fc682017-10-10 17:57:02 +0100132/*
133 * Checks whether the context fields are set in such a way
134 * that the RSA primitives will be able to execute without error.
135 * It does *not* make guarantees for consistency of the parameters.
136 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100137static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
138 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100139{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100140#if !defined(MBEDTLS_RSA_NO_CRT)
141 /* blinding_needed is only used for NO_CRT to decide whether
142 * P,Q need to be present or not. */
143 ((void) blinding_needed);
144#endif
145
Hanno Becker3a760a12018-01-05 08:14:49 +0000146 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
147 ctx->len > MBEDTLS_MPI_MAX_SIZE )
148 {
Hanno Becker705fc682017-10-10 17:57:02 +0100149 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000150 }
Hanno Becker705fc682017-10-10 17:57:02 +0100151
152 /*
153 * 1. Modular exponentiation needs positive, odd moduli.
154 */
155
156 /* Modular exponentiation wrt. N is always used for
157 * RSA public key operations. */
158 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
159 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
160 {
161 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
162 }
163
164#if !defined(MBEDTLS_RSA_NO_CRT)
165 /* Modular exponentiation for P and Q is only
166 * used for private key operations and if CRT
167 * is used. */
168 if( is_priv &&
169 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
170 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
171 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
172 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
173 {
174 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
175 }
176#endif /* !MBEDTLS_RSA_NO_CRT */
177
178 /*
179 * 2. Exponents must be positive
180 */
181
182 /* Always need E for public key operations */
183 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
185
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100186#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100187 /* For private key operations, use D or DP & DQ
188 * as (unblinded) exponents. */
189 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
190 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
191#else
192 if( is_priv &&
193 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
194 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
195 {
196 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
197 }
198#endif /* MBEDTLS_RSA_NO_CRT */
199
200 /* Blinding shouldn't make exponents negative either,
201 * so check that P, Q >= 1 if that hasn't yet been
202 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100203#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100204 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100205 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
206 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
207 {
208 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
209 }
210#endif
211
212 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100213 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100214#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100215 if( is_priv &&
216 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
217 {
218 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
219 }
220#endif
221
222 return( 0 );
223}
224
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100225int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100226{
227 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500228 int have_N, have_P, have_Q, have_D, have_E;
229#if !defined(MBEDTLS_RSA_NO_CRT)
230 int have_DP, have_DQ, have_QP;
231#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500232 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100233
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500234 RSA_VALIDATE_RET( ctx != NULL );
235
236 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
237 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
238 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
239 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
240 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500241
242#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500243 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
244 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
245 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500246#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100247
Hanno Becker617c1ae2017-08-23 14:11:24 +0100248 /*
249 * Check whether provided parameters are enough
250 * to deduce all others. The following incomplete
251 * parameter sets for private keys are supported:
252 *
253 * (1) P, Q missing.
254 * (2) D and potentially N missing.
255 *
256 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100257
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500258 n_missing = have_P && have_Q && have_D && have_E;
259 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
260 d_missing = have_P && have_Q && !have_D && have_E;
261 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100262
263 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500264 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100265
Hanno Becker617c1ae2017-08-23 14:11:24 +0100266 if( !is_priv && !is_pub )
267 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
268
269 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100270 * Step 1: Deduce N if P, Q are provided.
271 */
272
273 if( !have_N && have_P && have_Q )
274 {
275 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
276 &ctx->Q ) ) != 0 )
277 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100278 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100279 }
280
281 ctx->len = mbedtls_mpi_size( &ctx->N );
282 }
283
284 /*
285 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100286 */
287
288 if( pq_missing )
289 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100290 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100291 &ctx->P, &ctx->Q );
292 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100293 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100294
295 }
296 else if( d_missing )
297 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100298 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
299 &ctx->Q,
300 &ctx->E,
301 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100302 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100303 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100304 }
305 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100306
Hanno Becker617c1ae2017-08-23 14:11:24 +0100307 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100308 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100309 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100310 */
311
Hanno Becker23344b52017-08-23 07:43:27 +0100312#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500313 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100314 {
315 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
316 &ctx->DP, &ctx->DQ, &ctx->QP );
317 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100318 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100319 }
Hanno Becker23344b52017-08-23 07:43:27 +0100320#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100321
322 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100323 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324 */
325
Hanno Beckerebd2c022017-10-12 10:54:53 +0100326 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327}
328
Hanno Becker617c1ae2017-08-23 14:11:24 +0100329int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
330 unsigned char *N, size_t N_len,
331 unsigned char *P, size_t P_len,
332 unsigned char *Q, size_t Q_len,
333 unsigned char *D, size_t D_len,
334 unsigned char *E, size_t E_len )
335{
336 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500337 int is_priv;
338 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100339
340 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500341 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100342 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
343 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
344 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
345 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
346 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
347
348 if( !is_priv )
349 {
350 /* If we're trying to export private parameters for a public key,
351 * something must be wrong. */
352 if( P != NULL || Q != NULL || D != NULL )
353 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
354
355 }
356
357 if( N != NULL )
358 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
359
360 if( P != NULL )
361 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
362
363 if( Q != NULL )
364 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
365
366 if( D != NULL )
367 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
368
369 if( E != NULL )
370 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100371
372cleanup:
373
374 return( ret );
375}
376
Hanno Becker617c1ae2017-08-23 14:11:24 +0100377int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
378 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
379 mbedtls_mpi *D, mbedtls_mpi *E )
380{
Janos Follath24eed8d2019-11-22 13:21:35 +0000381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500382 int is_priv;
383 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100384
385 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500386 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100387 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
388 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
389 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
390 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
391 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
392
393 if( !is_priv )
394 {
395 /* If we're trying to export private parameters for a public key,
396 * something must be wrong. */
397 if( P != NULL || Q != NULL || D != NULL )
398 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
399
400 }
401
402 /* Export all requested core parameters. */
403
404 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
405 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
406 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
407 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
408 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
409 {
410 return( ret );
411 }
412
413 return( 0 );
414}
415
416/*
417 * Export CRT parameters
418 * This must also be implemented if CRT is not used, for being able to
419 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
420 * can be used in this case.
421 */
422int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
423 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
424{
Janos Follath24eed8d2019-11-22 13:21:35 +0000425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500426 int is_priv;
427 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100428
429 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500430 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100431 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
432 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
433 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
434 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
435 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
436
437 if( !is_priv )
438 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
439
Hanno Beckerdc95c892017-08-23 06:57:02 +0100440#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100441 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100442 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
443 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
444 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
445 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100446 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100447 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100448#else
449 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
450 DP, DQ, QP ) ) != 0 )
451 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100452 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100453 }
454#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100455
456 return( 0 );
457}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100458
Paul Bakker5121ce52009-01-03 21:22:43 +0000459/*
460 * Initialize an RSA context
461 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200462void mbedtls_rsa_init( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000463{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500464 RSA_VALIDATE( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
Ronald Cronc1905a12021-06-05 11:11:14 +0200468 ctx->padding = MBEDTLS_RSA_PKCS_V15;
469 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100472 /* Set ctx->ver to nonzero to indicate that the mutex has been
473 * initialized and will need to be freed. */
474 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200476#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000477}
478
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100479/*
480 * Set padding for an existing RSA context
481 */
Ronald Cronea7631b2021-06-03 18:51:59 +0200482int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
483 mbedtls_md_type_t hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100484{
Ronald Cron3a0375f2021-06-08 10:22:28 +0200485 switch( padding )
486 {
487#if defined(MBEDTLS_PKCS1_V15)
488 case MBEDTLS_RSA_PKCS_V15:
489 break;
490#endif
491
492#if defined(MBEDTLS_PKCS1_V21)
493 case MBEDTLS_RSA_PKCS_V21:
494 break;
495#endif
496 default:
497 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
498 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200499
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200500#if defined(MBEDTLS_PKCS1_V21)
Ronald Cronea7631b2021-06-03 18:51:59 +0200501 if( ( padding == MBEDTLS_RSA_PKCS_V21 ) &&
502 ( hash_id != MBEDTLS_MD_NONE ) )
503 {
504 const mbedtls_md_info_t *md_info;
505
506 md_info = mbedtls_md_info_from_type( hash_id );
507 if( md_info == NULL )
508 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
509 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200510#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500511
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100512 ctx->padding = padding;
513 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200514
515 return( 0 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100516}
517
Hanno Becker617c1ae2017-08-23 14:11:24 +0100518/*
519 * Get length in bytes of RSA modulus
520 */
521
522size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
523{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100524 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100525}
526
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
530/*
531 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800532 *
533 * This generation method follows the RSA key pair generation procedure of
534 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000537 int (*f_rng)(void *, unsigned char *, size_t),
538 void *p_rng,
539 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000540{
Janos Follath24eed8d2019-11-22 13:21:35 +0000541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800542 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100543 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500544 RSA_VALIDATE_RET( ctx != NULL );
545 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
Janos Follathb8fc1b02018-09-03 15:37:01 +0100547 /*
548 * If the modulus is 1024 bit long or shorter, then the security strength of
549 * the RSA algorithm is less than or equal to 80 bits and therefore an error
550 * rate of 2^-80 is sufficient.
551 */
552 if( nbits > 1024 )
553 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
554
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100555 mbedtls_mpi_init( &H );
556 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800557 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100559 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
560 {
561 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
562 goto cleanup;
563 }
564
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 /*
566 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800567 * 1. |P-Q| > 2^( nbits / 2 - 100 )
568 * 2. GCD( E, (P-1)*(Q-1) ) == 1
569 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
573 do
574 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100575 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
576 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
Janos Follathb8fc1b02018-09-03 15:37:01 +0100578 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
579 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800581 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
582 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
583 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 continue;
585
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800586 /* not required by any standards, but some users rely on the fact that P > Q */
587 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100588 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100589
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100590 /* Temporarily replace P,Q by P-1, Q-1 */
591 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
592 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
593 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800594
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800595 /* 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 +0200596 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800597 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
598 continue;
599
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800600 /* 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 -0800601 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
602 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
603 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
604
605 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
606 continue;
607
608 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800610 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100612 /* Restore P,Q */
613 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
614 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
615
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800616 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
617
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100618 ctx->len = mbedtls_mpi_size( &ctx->N );
619
Jethro Beekman97f95c92018-02-13 15:50:36 -0800620#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 * DP = D mod (P - 1)
623 * DQ = D mod (Q - 1)
624 * QP = Q^-1 mod P
625 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100626 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
627 &ctx->DP, &ctx->DQ, &ctx->QP ) );
628#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
Hanno Becker83aad1f2017-08-23 06:45:10 +0100630 /* Double-check */
631 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000632
633cleanup:
634
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100635 mbedtls_mpi_free( &H );
636 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800637 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
639 if( ret != 0 )
640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100642
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100643 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100644 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100645 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 }
647
Paul Bakker48377d92013-08-30 12:06:24 +0200648 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649}
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
653/*
654 * Check a public RSA key
655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500658 RSA_VALIDATE_RET( ctx != NULL );
659
Hanno Beckerebd2c022017-10-12 10:54:53 +0100660 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000662
Hanno Becker3a760a12018-01-05 08:14:49 +0000663 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100666 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
Hanno Becker705fc682017-10-10 17:57:02 +0100668 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
669 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100673 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675 return( 0 );
676}
677
678/*
Hanno Becker705fc682017-10-10 17:57:02 +0100679 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000682{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500683 RSA_VALIDATE_RET( ctx != NULL );
684
Hanno Becker705fc682017-10-10 17:57:02 +0100685 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100686 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 {
Hanno Becker98838b02017-10-02 13:16:10 +0100688 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 }
Paul Bakker48377d92013-08-30 12:06:24 +0200690
Hanno Becker98838b02017-10-02 13:16:10 +0100691 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100692 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100694 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000696
Hanno Beckerb269a852017-08-25 08:03:21 +0100697#if !defined(MBEDTLS_RSA_NO_CRT)
698 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
699 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
700 {
701 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
702 }
703#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000704
705 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000706}
707
708/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100709 * Check if contexts holding a public and private key match
710 */
Hanno Becker98838b02017-10-02 13:16:10 +0100711int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
712 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100713{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500714 RSA_VALIDATE_RET( pub != NULL );
715 RSA_VALIDATE_RET( prv != NULL );
716
Hanno Becker98838b02017-10-02 13:16:10 +0100717 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100721 }
722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
724 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100727 }
728
729 return( 0 );
730}
731
732/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 * Do an RSA public key operation
734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000736 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 unsigned char *output )
738{
Janos Follath24eed8d2019-11-22 13:21:35 +0000739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000740 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500742 RSA_VALIDATE_RET( ctx != NULL );
743 RSA_VALIDATE_RET( input != NULL );
744 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Hanno Beckerebd2c022017-10-12 10:54:53 +0100746 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100747 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200751#if defined(MBEDTLS_THREADING_C)
752 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
753 return( ret );
754#endif
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000759 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200760 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
761 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 }
763
764 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
766 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
768cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200770 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
771 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100772#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000775
776 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100777 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
779 return( 0 );
780}
781
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200782/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200783 * Generate or update blinding values, see section 10 of:
784 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200785 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200786 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200787 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200788static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200789 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
790{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200791 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200792 mbedtls_mpi R;
793
794 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200795
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200796 if( ctx->Vf.p != NULL )
797 {
798 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
800 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
801 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
802 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200803
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200804 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200805 }
806
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200807 /* Unblinding value: Vf = random number, invertible mod N */
808 do {
809 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200810 {
811 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
812 goto cleanup;
813 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 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 +0200816
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200817 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200818 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
819 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
820 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
821
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200822 /* At this point, Vi is invertible mod N if and only if both Vf and R
823 * are invertible mod N. If one of them isn't, we don't need to know
824 * which one, we just loop and choose new values for both of them.
825 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200826 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500827 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200828 goto cleanup;
829
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500830 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
831
832 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
833 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
834 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200835
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200836 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200837 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 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 +0200839
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200840
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200841cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200842 mbedtls_mpi_free( &R );
843
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200844 return( ret );
845}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200846
Paul Bakker5121ce52009-01-03 21:22:43 +0000847/*
Janos Follathe81102e2017-03-22 13:38:28 +0000848 * Exponent blinding supposed to prevent side-channel attacks using multiple
849 * traces of measurements to recover the RSA key. The more collisions are there,
850 * the more bits of the key can be recovered. See [3].
851 *
852 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800853 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000854 *
855 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800856 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000857 *
858 * (With the currently (as of 2017 April) known best algorithms breaking 2048
859 * bit RSA requires approximately as much time as trying out 2^112 random keys.
860 * Thus in this sense with 28 byte blinding the security is not reduced by
861 * side-channel attacks like the one in [3])
862 *
863 * This countermeasure does not help if the key recovery is possible with a
864 * single trace.
865 */
866#define RSA_EXPONENT_BLINDING 28
867
868/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 * Do an RSA private key operation
870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200872 int (*f_rng)(void *, unsigned char *, size_t),
873 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000874 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 unsigned char *output )
876{
Janos Follath24eed8d2019-11-22 13:21:35 +0000877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000878 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100879
880 /* Temporary holding the result */
881 mbedtls_mpi T;
882
883 /* Temporaries holding P-1, Q-1 and the
884 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000885 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100886
887#if !defined(MBEDTLS_RSA_NO_CRT)
888 /* Temporaries holding the results mod p resp. mod q. */
889 mbedtls_mpi TP, TQ;
890
891 /* Temporaries holding the blinded exponents for
892 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000893 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100894
895 /* Pointers to actual exponents to be used - either the unblinded
896 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000897 mbedtls_mpi *DP = &ctx->DP;
898 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100899#else
900 /* Temporary holding the blinded exponent (if used). */
901 mbedtls_mpi D_blind;
902
903 /* Pointer to actual exponent to be used - either the unblinded
904 * or the blinded one, depending on the presence of a PRNG. */
905 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100906#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100907
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100908 /* Temporaries holding the initial input and the double
909 * checked result; should be the same in the end. */
910 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500912 RSA_VALIDATE_RET( ctx != NULL );
913 RSA_VALIDATE_RET( input != NULL );
914 RSA_VALIDATE_RET( output != NULL );
915
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200916 if( f_rng == NULL )
917 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
918
919 if( rsa_check_context( ctx, 1 /* private key checks */,
920 1 /* blinding on */ ) != 0 )
Hanno Beckerebd2c022017-10-12 10:54:53 +0100921 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100922 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100923 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100924
Hanno Becker06811ce2017-05-03 15:10:34 +0100925#if defined(MBEDTLS_THREADING_C)
926 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
927 return( ret );
928#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000929
Hanno Becker06811ce2017-05-03 15:10:34 +0100930 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100931 mbedtls_mpi_init( &T );
932
933 mbedtls_mpi_init( &P1 );
934 mbedtls_mpi_init( &Q1 );
935 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000936
Janos Follathe81102e2017-03-22 13:38:28 +0000937#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200938 mbedtls_mpi_init( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +0000939#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200940 mbedtls_mpi_init( &DP_blind );
941 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000942#endif
943
Hanno Becker06811ce2017-05-03 15:10:34 +0100944#if !defined(MBEDTLS_RSA_NO_CRT)
945 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200946#endif
947
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100948 mbedtls_mpi_init( &I );
949 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100950
951 /* End of MPI initialization */
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
954 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200956 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
957 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000958 }
959
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100960 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100961
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200962 /*
963 * Blinding
964 * T = T * Vi mod N
965 */
966 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
967 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
968 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000969
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200970 /*
971 * Exponent blinding
972 */
973 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
974 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000975
Janos Follathf9203b42017-03-22 15:13:15 +0000976#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200977 /*
978 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
979 */
980 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
981 f_rng, p_rng ) );
982 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
983 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
984 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000985
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200986 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000987#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200988 /*
989 * DP_blind = ( P - 1 ) * R + DP
990 */
991 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
992 f_rng, p_rng ) );
993 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
994 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
995 &ctx->DP ) );
Janos Follathf9203b42017-03-22 15:13:15 +0000996
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200997 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000998
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200999 /*
1000 * DQ_blind = ( Q - 1 ) * R + DQ
1001 */
1002 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1003 f_rng, p_rng ) );
1004 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1005 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1006 &ctx->DQ ) );
Janos Follathf9203b42017-03-22 15:13:15 +00001007
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001008 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001009#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001012 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001013#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001014 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001015 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001016 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001017 * TP = input ^ dP mod P
1018 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001020
1021 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1022 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001023
1024 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001025 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001027 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1028 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1029 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
1031 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001032 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001034 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1035 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001037
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001038 /*
1039 * Unblind
1040 * T = T * Vf mod N
1041 */
1042 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
1043 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
Hanno Becker2dec5e82017-10-03 07:49:52 +01001045 /* Verify the result to prevent glitching attacks. */
1046 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1047 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001048 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001049 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001050 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1051 goto cleanup;
1052 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001053
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
1057cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001059 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1060 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001061#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001062
Hanno Becker06811ce2017-05-03 15:10:34 +01001063 mbedtls_mpi_free( &P1 );
1064 mbedtls_mpi_free( &Q1 );
1065 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001066
Janos Follathe81102e2017-03-22 13:38:28 +00001067#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001068 mbedtls_mpi_free( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +00001069#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001070 mbedtls_mpi_free( &DP_blind );
1071 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001072#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001073
Hanno Becker06811ce2017-05-03 15:10:34 +01001074 mbedtls_mpi_free( &T );
1075
1076#if !defined(MBEDTLS_RSA_NO_CRT)
1077 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1078#endif
1079
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001080 mbedtls_mpi_free( &C );
1081 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001082
Gilles Peskineae3741e2020-11-25 00:10:31 +01001083 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001084 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Gilles Peskineae3741e2020-11-25 00:10:31 +01001086 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001087}
1088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001090/**
1091 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1092 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001093 * \param dst buffer to mask
1094 * \param dlen length of destination buffer
1095 * \param src source of the mask generation
1096 * \param slen length of the source buffer
1097 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001098 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001099static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001101{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001103 unsigned char counter[4];
1104 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001105 unsigned int hlen;
1106 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001107 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001110 memset( counter, 0, 4 );
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001113
Simon Butcher02037452016-03-01 21:19:12 +00001114 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001115 p = dst;
1116
1117 while( dlen > 0 )
1118 {
1119 use_len = hlen;
1120 if( dlen < hlen )
1121 use_len = dlen;
1122
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001123 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1124 goto exit;
1125 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1126 goto exit;
1127 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1128 goto exit;
1129 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1130 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001131
1132 for( i = 0; i < use_len; ++i )
1133 *p++ ^= mask[i];
1134
1135 counter[3]++;
1136
1137 dlen -= use_len;
1138 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001139
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001140exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001141 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001142
1143 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001148/*
1149 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001152 int (*f_rng)(void *, unsigned char *, size_t),
1153 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001154 const unsigned char *label, size_t label_len,
1155 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001156 const unsigned char *input,
1157 unsigned char *output )
1158{
1159 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001161 unsigned char *p = output;
1162 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 const mbedtls_md_info_t *md_info;
1164 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001165
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001166 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001167 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001168 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001169 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1170
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001171 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001175 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001177
1178 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001180
Simon Butcher02037452016-03-01 21:19:12 +00001181 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001182 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001184
1185 memset( output, 0, olen );
1186
1187 *p++ = 0;
1188
Simon Butcher02037452016-03-01 21:19:12 +00001189 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001190 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001191 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001192
1193 p += hlen;
1194
Simon Butcher02037452016-03-01 21:19:12 +00001195 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001196 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1197 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001198 p += hlen;
1199 p += olen - 2 * hlen - 2 - ilen;
1200 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001201 if( ilen != 0 )
1202 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001205 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001206 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001207
Simon Butcher02037452016-03-01 21:19:12 +00001208 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001209 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1210 &md_ctx ) ) != 0 )
1211 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001212
Simon Butcher02037452016-03-01 21:19:12 +00001213 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001214 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1215 &md_ctx ) ) != 0 )
1216 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001217
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001218exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001220
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001221 if( ret != 0 )
1222 return( ret );
1223
Thomas Daubney141700f2021-05-13 19:06:10 +01001224 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001225}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001229/*
1230 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001233 int (*f_rng)(void *, unsigned char *, size_t),
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001234 void *p_rng, size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001235 const unsigned char *input,
1236 unsigned char *output )
1237{
1238 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001240 unsigned char *p = output;
1241
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001242 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001243 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001244 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001245
Paul Bakkerb3869132013-02-28 17:21:01 +01001246 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001247
Simon Butcher02037452016-03-01 21:19:12 +00001248 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001249 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001251
1252 nb_pad = olen - 3 - ilen;
1253
1254 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001255
1256 if( f_rng == NULL )
1257 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1258
1259 *p++ = MBEDTLS_RSA_CRYPT;
1260
1261 while( nb_pad-- > 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001262 {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001263 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001264
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001265 do {
1266 ret = f_rng( p_rng, p, 1 );
1267 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001268
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001269 /* Check if RNG failed to generate data */
1270 if( rng_dl == 0 || ret != 0 )
1271 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001272
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001273 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001274 }
1275
1276 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001277 if( ilen != 0 )
1278 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001279
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001280 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001281}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001283
Paul Bakker5121ce52009-01-03 21:22:43 +00001284/*
1285 * Add the message padding, then do an RSA operation
1286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001288 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001289 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +01001290 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001291 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 unsigned char *output )
1293{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001294 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001295 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001296 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001297
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 switch( ctx->padding )
1299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_PKCS1_V15)
1301 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney21772772021-05-13 17:30:32 +01001302 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001303 ilen, input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001304#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_PKCS1_V21)
1307 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney141700f2021-05-13 19:06:10 +01001308 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, NULL, 0,
Thomas Daubney21772772021-05-13 17:30:32 +01001309 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001310#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001311
1312 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001315}
1316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001318/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001319 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001322 int (*f_rng)(void *, unsigned char *, size_t),
1323 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001324 const unsigned char *label, size_t label_len,
1325 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001326 const unsigned char *input,
1327 unsigned char *output,
1328 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001329{
Janos Follath24eed8d2019-11-22 13:21:35 +00001330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001331 size_t ilen, i, pad_len;
1332 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1334 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001335 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 const mbedtls_md_info_t *md_info;
1337 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001338
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001339 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001340 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1341 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1342 RSA_VALIDATE_RET( input != NULL );
1343 RSA_VALIDATE_RET( olen != NULL );
1344
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001345 /*
1346 * Parameters sanity checks
1347 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001348 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
1351 ilen = ctx->len;
1352
Paul Bakker27fdf462011-06-09 13:55:13 +00001353 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001357 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001359
Janos Follathc17cda12016-02-11 11:08:18 +00001360 hlen = mbedtls_md_get_size( md_info );
1361
1362 // checking for integer underflow
1363 if( 2 * hlen + 2 > ilen )
1364 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1365
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001366 /*
1367 * RSA operation
1368 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001369 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
1371 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001372 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001374 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001375 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001378 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1379 {
1380 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001381 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001382 }
1383
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001384 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001385 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1386 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001387 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001388 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1389 &md_ctx ) ) != 0 )
1390 {
1391 mbedtls_md_free( &md_ctx );
1392 goto cleanup;
1393 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001396
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001397 /* Generate lHash */
1398 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1399 goto cleanup;
1400
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001401 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001402 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001403 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001405 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001407 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001408
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001409 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001410
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001411 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001412 for( i = 0; i < hlen; i++ )
1413 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001414
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001415 /* Get zero-padding len, but always read till end of buffer
1416 * (minus one, for the 01 byte) */
1417 pad_len = 0;
1418 pad_done = 0;
1419 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1420 {
1421 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001422 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001423 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001424
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001425 p += pad_len;
1426 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001427
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001428 /*
1429 * The only information "leaked" is whether the padding was correct or not
1430 * (eg, no data is copied if it was not correct). This meets the
1431 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1432 * the different error conditions.
1433 */
1434 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001435 {
1436 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1437 goto cleanup;
1438 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001439
Paul Bakker66d5d072014-06-17 16:39:18 +02001440 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001441 {
1442 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1443 goto cleanup;
1444 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
1446 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001447 if( *olen != 0 )
1448 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001449 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001450
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001451cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001452 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1453 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001454
1455 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001460/*
1461 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1462 */
1463int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
1464 int (*f_rng)(void *, unsigned char *, size_t),
1465 void *p_rng,
1466 size_t *olen,
1467 const unsigned char *input,
1468 unsigned char *output,
1469 size_t output_max_len )
1470{
1471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1472 size_t ilen;
1473 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1474
1475 RSA_VALIDATE_RET( ctx != NULL );
1476 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1477 RSA_VALIDATE_RET( input != NULL );
1478 RSA_VALIDATE_RET( olen != NULL );
1479
1480 ilen = ctx->len;
1481
1482 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1483 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1484
1485 if( ilen < 16 || ilen > sizeof( buf ) )
1486 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1487
1488 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
1489
1490 if( ret != 0 )
1491 goto cleanup;
1492
Gabor Mezei90437e32021-10-20 11:59:27 +02001493 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding( buf, ilen,
Gabor Mezei63bbba52021-10-18 16:17:57 +02001494 output, output_max_len, olen );
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001495
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001496cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001497 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001498
1499 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001500}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001502
1503/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001504 * Do an RSA operation, then remove the message padding
1505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001507 int (*f_rng)(void *, unsigned char *, size_t),
1508 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001509 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001510 const unsigned char *input,
1511 unsigned char *output,
1512 size_t output_max_len)
1513{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001514 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001515 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1516 RSA_VALIDATE_RET( input != NULL );
1517 RSA_VALIDATE_RET( olen != NULL );
1518
Paul Bakkerb3869132013-02-28 17:21:01 +01001519 switch( ctx->padding )
1520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#if defined(MBEDTLS_PKCS1_V15)
1522 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney34733082021-05-12 09:24:29 +01001523 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001524 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001525#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_PKCS1_V21)
1528 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001529 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001530 olen, input, output,
1531 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001532#endif
1533
1534 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001536 }
1537}
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001540static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001541 int (*f_rng)(void *, unsigned char *, size_t),
1542 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001544 unsigned int hashlen,
1545 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001546 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001547 unsigned char *sig )
1548{
1549 size_t olen;
1550 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001551 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001552 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001554 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 const mbedtls_md_info_t *md_info;
1556 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001557 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001558 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1559 hashlen == 0 ) ||
1560 hash != NULL );
1561 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001562
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001563 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1564 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1565
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001566 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001568
1569 olen = ctx->len;
1570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001572 {
Simon Butcher02037452016-03-01 21:19:12 +00001573 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001575 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001577
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001578 if( hashlen != mbedtls_md_get_size( md_info ) )
1579 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001580 }
1581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001583 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001587
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001588 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1589 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001590 /* Calculate the largest possible salt length, up to the hash size.
1591 * Normally this is the hash length, which is the maximum salt length
1592 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001593 * enough room, use the maximum salt length that fits. The constraint is
1594 * that the hash length plus the salt length plus 2 bytes must be at most
1595 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1596 * (PKCS#1 v2.2) §9.1.1 step 3. */
1597 min_slen = hlen - 2;
1598 if( olen < hlen + min_slen + 2 )
1599 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1600 else if( olen >= hlen + hlen + 2 )
1601 slen = hlen;
1602 else
1603 slen = olen - hlen - 2;
1604 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001605 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001608 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001609 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001610 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001611 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001612 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001613
1614 memset( sig, 0, olen );
1615
Simon Butcher02037452016-03-01 21:19:12 +00001616 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001617 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001618 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001619 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001620
1621 /* Generate salt of length slen in place in the encoded message */
1622 salt = p;
1623 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001624 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001625
Paul Bakkerb3869132013-02-28 17:21:01 +01001626 p += slen;
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001629 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001630 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001631
Simon Butcher02037452016-03-01 21:19:12 +00001632 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001633 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1634 goto exit;
1635 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1636 goto exit;
1637 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1638 goto exit;
1639 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1640 goto exit;
1641 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1642 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001643
Simon Butcher02037452016-03-01 21:19:12 +00001644 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001645 if( msb % 8 == 0 )
1646 offset = 1;
1647
Simon Butcher02037452016-03-01 21:19:12 +00001648 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001649 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1650 &md_ctx ) ) != 0 )
1651 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001652
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001653 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001654 sig[0] &= 0xFF >> ( olen * 8 - msb );
1655
1656 p += hlen;
1657 *p++ = 0xBC;
1658
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001659exit:
1660 mbedtls_md_free( &md_ctx );
1661
1662 if( ret != 0 )
1663 return( ret );
1664
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001665 return mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001666}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001667
1668/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001669 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1670 * the option to pass in the salt length.
1671 */
1672int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1673 int (*f_rng)(void *, unsigned char *, size_t),
1674 void *p_rng,
1675 mbedtls_md_type_t md_alg,
1676 unsigned int hashlen,
1677 const unsigned char *hash,
1678 int saltlen,
1679 unsigned char *sig )
1680{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001681 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001682 hashlen, hash, saltlen, sig );
1683}
1684
1685
1686/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001687 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1688 */
1689int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1690 int (*f_rng)(void *, unsigned char *, size_t),
1691 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001692 mbedtls_md_type_t md_alg,
1693 unsigned int hashlen,
1694 const unsigned char *hash,
1695 unsigned char *sig )
1696{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001697 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001698 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001699}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001703/*
1704 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1705 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001706
1707/* Construct a PKCS v1.5 encoding of a hashed message
1708 *
1709 * This is used both for signature generation and verification.
1710 *
1711 * Parameters:
1712 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001713 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001714 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001715 * - hash: Buffer containing the hashed message or the raw data.
1716 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001717 * - dst: Buffer to hold the encoded message.
1718 *
1719 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001720 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001721 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001722 *
1723 */
1724static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1725 unsigned int hashlen,
1726 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001727 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001728 unsigned char *dst )
1729{
1730 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001731 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001732 unsigned char *p = dst;
1733 const char *oid = NULL;
1734
1735 /* Are we signing hashed or raw data? */
1736 if( md_alg != MBEDTLS_MD_NONE )
1737 {
1738 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1739 if( md_info == NULL )
1740 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1741
1742 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1743 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1744
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001745 if( hashlen != mbedtls_md_get_size( md_info ) )
1746 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001747
1748 /* Double-check that 8 + hashlen + oid_size can be used as a
1749 * 1-byte ASN.1 length encoding and that there's no overflow. */
1750 if( 8 + hashlen + oid_size >= 0x80 ||
1751 10 + hashlen < hashlen ||
1752 10 + hashlen + oid_size < 10 + hashlen )
1753 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1754
1755 /*
1756 * Static bounds check:
1757 * - Need 10 bytes for five tag-length pairs.
1758 * (Insist on 1-byte length encodings to protect against variants of
1759 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1760 * - Need hashlen bytes for hash
1761 * - Need oid_size bytes for hash alg OID.
1762 */
1763 if( nb_pad < 10 + hashlen + oid_size )
1764 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1765 nb_pad -= 10 + hashlen + oid_size;
1766 }
1767 else
1768 {
1769 if( nb_pad < hashlen )
1770 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1771
1772 nb_pad -= hashlen;
1773 }
1774
Hanno Becker2b2f8982017-09-27 17:10:03 +01001775 /* Need space for signature header and padding delimiter (3 bytes),
1776 * and 8 bytes for the minimal padding */
1777 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001778 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1779 nb_pad -= 3;
1780
1781 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001782 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001783
1784 /* Write signature header and padding */
1785 *p++ = 0;
1786 *p++ = MBEDTLS_RSA_SIGN;
1787 memset( p, 0xFF, nb_pad );
1788 p += nb_pad;
1789 *p++ = 0;
1790
1791 /* Are we signing raw data? */
1792 if( md_alg == MBEDTLS_MD_NONE )
1793 {
1794 memcpy( p, hash, hashlen );
1795 return( 0 );
1796 }
1797
1798 /* Signing hashed data, add corresponding ASN.1 structure
1799 *
1800 * DigestInfo ::= SEQUENCE {
1801 * digestAlgorithm DigestAlgorithmIdentifier,
1802 * digest Digest }
1803 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1804 * Digest ::= OCTET STRING
1805 *
1806 * Schematic:
1807 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1808 * TAG-NULL + LEN [ NULL ] ]
1809 * TAG-OCTET + LEN [ HASH ] ]
1810 */
1811 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001812 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001813 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001814 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001815 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001816 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001817 memcpy( p, oid, oid_size );
1818 p += oid_size;
1819 *p++ = MBEDTLS_ASN1_NULL;
1820 *p++ = 0x00;
1821 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001822 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001823 memcpy( p, hash, hashlen );
1824 p += hashlen;
1825
1826 /* Just a sanity-check, should be automatic
1827 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001828 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001829 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001830 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001831 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1832 }
1833
1834 return( 0 );
1835}
1836
Paul Bakkerb3869132013-02-28 17:21:01 +01001837/*
1838 * Do an RSA operation to sign the message digest
1839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001841 int (*f_rng)(void *, unsigned char *, size_t),
1842 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001844 unsigned int hashlen,
1845 const unsigned char *hash,
1846 unsigned char *sig )
1847{
Janos Follath24eed8d2019-11-22 13:21:35 +00001848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001849 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001850
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001851 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001852 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1853 hashlen == 0 ) ||
1854 hash != NULL );
1855 RSA_VALIDATE_RET( sig != NULL );
1856
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001857 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1858 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1859
Hanno Beckerfdf38032017-09-06 12:35:55 +01001860 /*
1861 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1862 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001863
Hanno Beckerfdf38032017-09-06 12:35:55 +01001864 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1865 ctx->len, sig ) ) != 0 )
1866 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001867
Hanno Beckerfdf38032017-09-06 12:35:55 +01001868 /* Private key operation
1869 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001870 * In order to prevent Lenstra's attack, make the signature in a
1871 * temporary buffer and check it before returning it.
1872 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001873
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001874 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001875 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001876 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1877
Hanno Beckerfdf38032017-09-06 12:35:55 +01001878 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001879 if( verif == NULL )
1880 {
1881 mbedtls_free( sig_try );
1882 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1883 }
1884
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001885 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1886 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1887
Gabor Mezei90437e32021-10-20 11:59:27 +02001888 if( mbedtls_ct_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001889 {
1890 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1891 goto cleanup;
1892 }
1893
1894 memcpy( sig, sig_try, ctx->len );
1895
1896cleanup:
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001897 mbedtls_platform_zeroize( sig_try, ctx->len );
1898 mbedtls_platform_zeroize( verif, ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001899 mbedtls_free( sig_try );
1900 mbedtls_free( verif );
1901
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001902 if( ret != 0 )
1903 memset( sig, '!', ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001904 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001905}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001907
1908/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 * Do an RSA operation to sign the message digest
1910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001912 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001913 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001915 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001916 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 unsigned char *sig )
1918{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001919 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001920 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1921 hashlen == 0 ) ||
1922 hash != NULL );
1923 RSA_VALIDATE_RET( sig != NULL );
1924
Paul Bakker5121ce52009-01-03 21:22:43 +00001925 switch( ctx->padding )
1926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927#if defined(MBEDTLS_PKCS1_V15)
1928 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney52654982021-05-18 16:54:00 +01001929 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng,
Thomas Daubney140184d2021-05-18 16:04:07 +01001930 md_alg, hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001931#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933#if defined(MBEDTLS_PKCS1_V21)
1934 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyde9fdc42021-05-18 17:10:04 +01001935 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
1936 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001937#endif
1938
Paul Bakker5121ce52009-01-03 21:22:43 +00001939 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001942}
1943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001945/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001946 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001950 unsigned int hashlen,
1951 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001953 int expected_salt_len,
1954 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001955{
Janos Follath24eed8d2019-11-22 13:21:35 +00001956 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001957 size_t siglen;
1958 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001959 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001961 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001962 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001963 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 const mbedtls_md_info_t *md_info;
1965 mbedtls_md_context_t md_ctx;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001966 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = {0};
Paul Bakkerb3869132013-02-28 17:21:01 +01001967
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001968 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001969 RSA_VALIDATE_RET( sig != NULL );
1970 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1971 hashlen == 0 ) ||
1972 hash != NULL );
1973
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 siglen = ctx->len;
1975
Paul Bakker27fdf462011-06-09 13:55:13 +00001976 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001978
Thomas Daubney782a7f52021-05-19 12:27:35 +01001979 ret = mbedtls_rsa_public( ctx, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001980
1981 if( ret != 0 )
1982 return( ret );
1983
1984 p = buf;
1985
Paul Bakkerb3869132013-02-28 17:21:01 +01001986 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 {
Simon Butcher02037452016-03-01 21:19:12 +00001991 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001993 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001995
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001996 if( hashlen != mbedtls_md_get_size( md_info ) )
1997 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001998 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002001 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002005
Paul Bakkerb3869132013-02-28 17:21:01 +01002006 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002007
Simon Butcher02037452016-03-01 21:19:12 +00002008 /*
2009 * Note: EMSA-PSS verification is over the length of N - 1 bits
2010 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002011 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002012
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002013 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2014 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2015
Simon Butcher02037452016-03-01 21:19:12 +00002016 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002017 if( msb % 8 == 0 )
2018 {
2019 p++;
2020 siglen -= 1;
2021 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002022
Gilles Peskine139108a2017-10-18 19:03:42 +02002023 if( siglen < hlen + 2 )
2024 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2025 hash_start = p + siglen - hlen - 1;
2026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002028 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002029 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002030
Jaeden Amero66954e12018-01-25 16:05:54 +00002031 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2032 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002033 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002034
Paul Bakkerb3869132013-02-28 17:21:01 +01002035 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002036
Gilles Peskine6a54b022017-10-17 19:02:13 +02002037 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002038 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002039
Gilles Peskine91048a32017-10-19 17:46:14 +02002040 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002041 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002042 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2043 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002044 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002045
Gilles Peskine6a54b022017-10-17 19:02:13 +02002046 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002049 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002050 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002051 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2052 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002053 }
2054
Simon Butcher02037452016-03-01 21:19:12 +00002055 /*
2056 * Generate H = Hash( M' )
2057 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002058 ret = mbedtls_md_starts( &md_ctx );
2059 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002060 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002061 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2062 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002063 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002064 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2065 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002066 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002067 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2068 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002069 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002070 ret = mbedtls_md_finish( &md_ctx, result );
2071 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002072 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002073
Jaeden Amero66954e12018-01-25 16:05:54 +00002074 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002075 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002076 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002077 goto exit;
2078 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002079
2080exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002082
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002083 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002084}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002085
2086/*
2087 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002091 unsigned int hashlen,
2092 const unsigned char *hash,
2093 const unsigned char *sig )
2094{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002095 mbedtls_md_type_t mgf1_hash_id;
2096 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002097 RSA_VALIDATE_RET( sig != NULL );
2098 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2099 hashlen == 0 ) ||
2100 hash != NULL );
2101
2102 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002104 : md_alg;
2105
Thomas Daubney9e65f792021-05-19 12:18:58 +01002106 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx,
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002107 md_alg, hashlen, hash,
2108 mgf1_hash_id,
2109 MBEDTLS_RSA_SALT_LEN_ANY,
2110 sig ) );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002111
2112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002116/*
2117 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002121 unsigned int hashlen,
2122 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002123 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002124{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002125 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002126 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002127 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002128
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002129 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002130 RSA_VALIDATE_RET( sig != NULL );
2131 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2132 hashlen == 0 ) ||
2133 hash != NULL );
2134
2135 sig_len = ctx->len;
2136
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002137 /*
2138 * Prepare expected PKCS1 v1.5 encoding of hash.
2139 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002140
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002141 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2142 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2143 {
2144 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2145 goto cleanup;
2146 }
2147
2148 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2149 encoded_expected ) ) != 0 )
2150 goto cleanup;
2151
2152 /*
2153 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2154 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002155
Thomas Daubney41e4ce42021-05-19 15:10:05 +01002156 ret = mbedtls_rsa_public( ctx, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002157 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002158 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002159
Simon Butcher02037452016-03-01 21:19:12 +00002160 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002161 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002162 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002163
Gabor Mezei90437e32021-10-20 11:59:27 +02002164 if( ( ret = mbedtls_ct_memcmp( encoded, encoded_expected,
gabor-mezei-arm46025642021-07-19 15:19:19 +02002165 sig_len ) ) != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002166 {
2167 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2168 goto cleanup;
2169 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002170
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002171cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002172
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002173 if( encoded != NULL )
2174 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002175 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002176 mbedtls_free( encoded );
2177 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002178
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002179 if( encoded_expected != NULL )
2180 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002181 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002182 mbedtls_free( encoded_expected );
2183 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002184
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002185 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002188
2189/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002190 * Do an RSA operation and check the message digest
2191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002194 unsigned int hashlen,
2195 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002196 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002197{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002198 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002199 RSA_VALIDATE_RET( sig != NULL );
2200 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2201 hashlen == 0 ) ||
2202 hash != NULL );
2203
Paul Bakkerb3869132013-02-28 17:21:01 +01002204 switch( ctx->padding )
2205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206#if defined(MBEDTLS_PKCS1_V15)
2207 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney2e126252021-05-19 11:48:53 +01002208 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, md_alg,
2209 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002210#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#if defined(MBEDTLS_PKCS1_V21)
2213 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002214 return mbedtls_rsa_rsassa_pss_verify( ctx, md_alg,
2215 hashlen, hash, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01002216#endif
2217
2218 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002220 }
2221}
2222
2223/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002224 * Copy the components of an RSA key
2225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002227{
Janos Follath24eed8d2019-11-22 13:21:35 +00002228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002229 RSA_VALIDATE_RET( dst != NULL );
2230 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002231
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002232 dst->len = src->len;
2233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2235 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2238 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2239 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002240
2241#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2243 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2244 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2246 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002247#endif
2248
2249 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2252 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002253
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002254 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002255 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002256
2257cleanup:
2258 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002260
2261 return( ret );
2262}
2263
2264/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 * Free the components of an RSA key
2266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002268{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002269 if( ctx == NULL )
2270 return;
2271
2272 mbedtls_mpi_free( &ctx->Vi );
2273 mbedtls_mpi_free( &ctx->Vf );
2274 mbedtls_mpi_free( &ctx->RN );
2275 mbedtls_mpi_free( &ctx->D );
2276 mbedtls_mpi_free( &ctx->Q );
2277 mbedtls_mpi_free( &ctx->P );
2278 mbedtls_mpi_free( &ctx->E );
2279 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002280
Hanno Becker33c30a02017-08-23 07:00:22 +01002281#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002282 mbedtls_mpi_free( &ctx->RQ );
2283 mbedtls_mpi_free( &ctx->RP );
2284 mbedtls_mpi_free( &ctx->QP );
2285 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002286 mbedtls_mpi_free( &ctx->DP );
2287#endif /* MBEDTLS_RSA_NO_CRT */
2288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002290 /* Free the mutex, but only if it hasn't been freed already. */
2291 if( ctx->ver != 0 )
2292 {
2293 mbedtls_mutex_free( &ctx->mutex );
2294 ctx->ver = 0;
2295 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002297}
2298
Hanno Beckerab377312017-08-23 16:24:51 +01002299#endif /* !MBEDTLS_RSA_ALT */
2300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002302
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002303#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002304
2305/*
2306 * Example RSA-1024 keypair, for test purposes
2307 */
2308#define KEY_LEN 128
2309
2310#define RSA_N "9292758453063D803DD603D5E777D788" \
2311 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2312 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2313 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2314 "93A89813FBF3C4F8066D2D800F7C38A8" \
2315 "1AE31942917403FF4946B0A83D3D3E05" \
2316 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2317 "5E94BB77B07507233A0BC7BAC8F90F79"
2318
2319#define RSA_E "10001"
2320
2321#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2322 "66CA472BC44D253102F8B4A9D3BFA750" \
2323 "91386C0077937FE33FA3252D28855837" \
2324 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2325 "DF79C5CE07EE72C7F123142198164234" \
2326 "CABB724CF78B8173B9F880FC86322407" \
2327 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2328 "071513A1E85B5DFA031F21ECAE91A34D"
2329
2330#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2331 "2C01CAD19EA484A87EA4377637E75500" \
2332 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2333 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2334
2335#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2336 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2337 "910E4168387E3C30AA1E00C339A79508" \
2338 "8452DD96A9A5EA5D9DCA68DA636032AF"
2339
Paul Bakker5121ce52009-01-03 21:22:43 +00002340#define PT_LEN 24
2341#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2342 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002345static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002346{
gufe44c2620da2020-08-03 17:56:50 +02002347#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002348 size_t i;
2349
Paul Bakker545570e2010-07-18 09:00:25 +00002350 if( rng_state != NULL )
2351 rng_state = NULL;
2352
Paul Bakkera3d195c2011-11-27 21:07:34 +00002353 for( i = 0; i < len; ++i )
2354 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002355#else
2356 if( rng_state != NULL )
2357 rng_state = NULL;
2358
2359 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002360#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002361
Paul Bakkera3d195c2011-11-27 21:07:34 +00002362 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002365
Paul Bakker5121ce52009-01-03 21:22:43 +00002366/*
2367 * Checkup routine
2368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002370{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002371 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002373 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 unsigned char rsa_plaintext[PT_LEN];
2376 unsigned char rsa_decrypted[PT_LEN];
2377 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002379 unsigned char sha1sum[20];
2380#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
Hanno Becker3a701162017-08-22 13:52:43 +01002382 mbedtls_mpi K;
2383
2384 mbedtls_mpi_init( &K );
Ronald Cronc1905a12021-06-05 11:11:14 +02002385 mbedtls_rsa_init( &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
Hanno Becker3a701162017-08-22 13:52:43 +01002387 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2388 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2389 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2390 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2391 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2392 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2393 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2394 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2395 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2396 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2397
Hanno Becker7f25f852017-10-10 16:56:22 +01002398 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
2400 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2404 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 {
2406 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002408
Hanno Becker5bc87292017-05-03 15:09:31 +01002409 ret = 1;
2410 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 }
2412
2413 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
2416 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2417
Thomas Daubney21772772021-05-13 17:30:32 +01002418 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002419 PT_LEN, rsa_plaintext,
2420 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 {
2422 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002424
Hanno Becker5bc87292017-05-03 15:09:31 +01002425 ret = 1;
2426 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 }
2428
2429 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002431
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002432 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002433 &len, rsa_ciphertext, rsa_decrypted,
2434 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 {
2436 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
Hanno Becker5bc87292017-05-03 15:09:31 +01002439 ret = 1;
2440 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 }
2442
2443 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2444 {
2445 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002447
Hanno Becker5bc87292017-05-03 15:09:31 +01002448 ret = 1;
2449 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 }
2451
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002452 if( verbose != 0 )
2453 mbedtls_printf( "passed\n" );
2454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002457 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
TRodziewicz26371e42021-06-08 16:45:41 +02002459 if( mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002460 {
2461 if( verbose != 0 )
2462 mbedtls_printf( "failed\n" );
2463
2464 return( 1 );
2465 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002466
Hanno Becker98838b02017-10-02 13:16:10 +01002467 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002468 MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002469 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 {
2471 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Hanno Becker5bc87292017-05-03 15:09:31 +01002474 ret = 1;
2475 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 }
2477
2478 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002481 if( mbedtls_rsa_pkcs1_verify( &rsa, MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002482 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 {
2484 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Hanno Becker5bc87292017-05-03 15:09:31 +01002487 ret = 1;
2488 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 }
2490
2491 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002492 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002494
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002495 if( verbose != 0 )
2496 mbedtls_printf( "\n" );
2497
Paul Bakker3d8fb632014-04-17 12:42:41 +02002498cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002499 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 mbedtls_rsa_free( &rsa );
2501#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002502 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002504 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505}
2506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#endif /* MBEDTLS_RSA_C */