blob: e95768f1ddc8858ff2c3e900b0e9e5c4cf41db65 [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
500 if( ( padding == MBEDTLS_RSA_PKCS_V21 ) &&
501 ( hash_id != MBEDTLS_MD_NONE ) )
502 {
503 const mbedtls_md_info_t *md_info;
504
505 md_info = mbedtls_md_info_from_type( hash_id );
506 if( md_info == NULL )
507 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
508 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500509
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100510 ctx->padding = padding;
511 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200512
513 return( 0 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100514}
515
Hanno Becker617c1ae2017-08-23 14:11:24 +0100516/*
517 * Get length in bytes of RSA modulus
518 */
519
520size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
521{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100522 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100523}
524
525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528/*
529 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800530 *
531 * This generation method follows the RSA key pair generation procedure of
532 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000535 int (*f_rng)(void *, unsigned char *, size_t),
536 void *p_rng,
537 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000538{
Janos Follath24eed8d2019-11-22 13:21:35 +0000539 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800540 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100541 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500542 RSA_VALIDATE_RET( ctx != NULL );
543 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Janos Follathb8fc1b02018-09-03 15:37:01 +0100545 /*
546 * If the modulus is 1024 bit long or shorter, then the security strength of
547 * the RSA algorithm is less than or equal to 80 bits and therefore an error
548 * rate of 2^-80 is sufficient.
549 */
550 if( nbits > 1024 )
551 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
552
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100553 mbedtls_mpi_init( &H );
554 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800555 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100557 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
558 {
559 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
560 goto cleanup;
561 }
562
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 /*
564 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800565 * 1. |P-Q| > 2^( nbits / 2 - 100 )
566 * 2. GCD( E, (P-1)*(Q-1) ) == 1
567 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571 do
572 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100573 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
574 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Janos Follathb8fc1b02018-09-03 15:37:01 +0100576 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
577 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800579 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
580 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
581 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 continue;
583
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800584 /* not required by any standards, but some users rely on the fact that P > Q */
585 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100586 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100587
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100588 /* Temporarily replace P,Q by P-1, Q-1 */
589 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
590 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
591 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800592
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800593 /* 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 +0200594 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800595 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
596 continue;
597
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800598 /* 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 -0800599 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
600 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
601 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
602
603 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
604 continue;
605
606 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800608 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100610 /* Restore P,Q */
611 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
612 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
613
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800614 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
615
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100616 ctx->len = mbedtls_mpi_size( &ctx->N );
617
Jethro Beekman97f95c92018-02-13 15:50:36 -0800618#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 * DP = D mod (P - 1)
621 * DQ = D mod (Q - 1)
622 * QP = Q^-1 mod P
623 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100624 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
625 &ctx->DP, &ctx->DQ, &ctx->QP ) );
626#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
Hanno Becker83aad1f2017-08-23 06:45:10 +0100628 /* Double-check */
629 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
631cleanup:
632
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100633 mbedtls_mpi_free( &H );
634 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800635 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
637 if( ret != 0 )
638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100640
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100641 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100642 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100643 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 }
645
Paul Bakker48377d92013-08-30 12:06:24 +0200646 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000647}
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
651/*
652 * Check a public RSA key
653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000655{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500656 RSA_VALIDATE_RET( ctx != NULL );
657
Hanno Beckerebd2c022017-10-12 10:54:53 +0100658 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000660
Hanno Becker3a760a12018-01-05 08:14:49 +0000661 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100664 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
Hanno Becker705fc682017-10-10 17:57:02 +0100666 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
667 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100671 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 return( 0 );
674}
675
676/*
Hanno Becker705fc682017-10-10 17:57:02 +0100677 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000680{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500681 RSA_VALIDATE_RET( ctx != NULL );
682
Hanno Becker705fc682017-10-10 17:57:02 +0100683 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100684 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 {
Hanno Becker98838b02017-10-02 13:16:10 +0100686 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 }
Paul Bakker48377d92013-08-30 12:06:24 +0200688
Hanno Becker98838b02017-10-02 13:16:10 +0100689 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100690 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100692 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000694
Hanno Beckerb269a852017-08-25 08:03:21 +0100695#if !defined(MBEDTLS_RSA_NO_CRT)
696 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
697 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
698 {
699 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
700 }
701#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000702
703 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000704}
705
706/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100707 * Check if contexts holding a public and private key match
708 */
Hanno Becker98838b02017-10-02 13:16:10 +0100709int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
710 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100711{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500712 RSA_VALIDATE_RET( pub != NULL );
713 RSA_VALIDATE_RET( prv != NULL );
714
Hanno Becker98838b02017-10-02 13:16:10 +0100715 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100719 }
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
722 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100725 }
726
727 return( 0 );
728}
729
730/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000731 * Do an RSA public key operation
732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000734 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 unsigned char *output )
736{
Janos Follath24eed8d2019-11-22 13:21:35 +0000737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000738 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500740 RSA_VALIDATE_RET( ctx != NULL );
741 RSA_VALIDATE_RET( input != NULL );
742 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
Hanno Beckerebd2c022017-10-12 10:54:53 +0100744 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100745 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000748
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200749#if defined(MBEDTLS_THREADING_C)
750 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
751 return( ret );
752#endif
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000757 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200758 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
759 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 }
761
762 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
764 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
766cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200768 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
769 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100770#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
774 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100775 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000776
777 return( 0 );
778}
779
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200780/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200781 * Generate or update blinding values, see section 10 of:
782 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200783 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200784 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200785 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200786static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200787 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
788{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200789 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200790 mbedtls_mpi R;
791
792 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200793
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200794 if( ctx->Vf.p != NULL )
795 {
796 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
798 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
799 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
800 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200801
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200802 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200803 }
804
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200805 /* Unblinding value: Vf = random number, invertible mod N */
806 do {
807 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200808 {
809 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
810 goto cleanup;
811 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 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 +0200814
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200815 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200816 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
817 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
818 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
819
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200820 /* At this point, Vi is invertible mod N if and only if both Vf and R
821 * are invertible mod N. If one of them isn't, we don't need to know
822 * which one, we just loop and choose new values for both of them.
823 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200824 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500825 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200826 goto cleanup;
827
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500828 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
829
830 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
831 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
832 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200833
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200834 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200835 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 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 +0200837
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200838
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200839cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200840 mbedtls_mpi_free( &R );
841
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200842 return( ret );
843}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200844
Paul Bakker5121ce52009-01-03 21:22:43 +0000845/*
Janos Follathe81102e2017-03-22 13:38:28 +0000846 * Exponent blinding supposed to prevent side-channel attacks using multiple
847 * traces of measurements to recover the RSA key. The more collisions are there,
848 * the more bits of the key can be recovered. See [3].
849 *
850 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800851 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000852 *
853 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800854 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000855 *
856 * (With the currently (as of 2017 April) known best algorithms breaking 2048
857 * bit RSA requires approximately as much time as trying out 2^112 random keys.
858 * Thus in this sense with 28 byte blinding the security is not reduced by
859 * side-channel attacks like the one in [3])
860 *
861 * This countermeasure does not help if the key recovery is possible with a
862 * single trace.
863 */
864#define RSA_EXPONENT_BLINDING 28
865
866/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 * Do an RSA private key operation
868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200870 int (*f_rng)(void *, unsigned char *, size_t),
871 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000872 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 unsigned char *output )
874{
Janos Follath24eed8d2019-11-22 13:21:35 +0000875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000876 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100877
878 /* Temporary holding the result */
879 mbedtls_mpi T;
880
881 /* Temporaries holding P-1, Q-1 and the
882 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000883 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100884
885#if !defined(MBEDTLS_RSA_NO_CRT)
886 /* Temporaries holding the results mod p resp. mod q. */
887 mbedtls_mpi TP, TQ;
888
889 /* Temporaries holding the blinded exponents for
890 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000891 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100892
893 /* Pointers to actual exponents to be used - either the unblinded
894 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000895 mbedtls_mpi *DP = &ctx->DP;
896 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100897#else
898 /* Temporary holding the blinded exponent (if used). */
899 mbedtls_mpi D_blind;
900
901 /* Pointer to actual exponent to be used - either the unblinded
902 * or the blinded one, depending on the presence of a PRNG. */
903 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100904#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100905
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100906 /* Temporaries holding the initial input and the double
907 * checked result; should be the same in the end. */
908 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000909
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500910 RSA_VALIDATE_RET( ctx != NULL );
911 RSA_VALIDATE_RET( input != NULL );
912 RSA_VALIDATE_RET( output != NULL );
913
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200914 if( f_rng == NULL )
915 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
916
917 if( rsa_check_context( ctx, 1 /* private key checks */,
918 1 /* blinding on */ ) != 0 )
Hanno Beckerebd2c022017-10-12 10:54:53 +0100919 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100920 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100921 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100922
Hanno Becker06811ce2017-05-03 15:10:34 +0100923#if defined(MBEDTLS_THREADING_C)
924 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
925 return( ret );
926#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000927
Hanno Becker06811ce2017-05-03 15:10:34 +0100928 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100929 mbedtls_mpi_init( &T );
930
931 mbedtls_mpi_init( &P1 );
932 mbedtls_mpi_init( &Q1 );
933 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000934
Janos Follathe81102e2017-03-22 13:38:28 +0000935#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200936 mbedtls_mpi_init( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +0000937#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200938 mbedtls_mpi_init( &DP_blind );
939 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000940#endif
941
Hanno Becker06811ce2017-05-03 15:10:34 +0100942#if !defined(MBEDTLS_RSA_NO_CRT)
943 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200944#endif
945
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100946 mbedtls_mpi_init( &I );
947 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100948
949 /* End of MPI initialization */
950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
952 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000953 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200954 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
955 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 }
957
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100958 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100959
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200960 /*
961 * Blinding
962 * T = T * Vi mod N
963 */
964 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
965 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
966 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000967
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200968 /*
969 * Exponent blinding
970 */
971 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
972 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000973
Janos Follathf9203b42017-03-22 15:13:15 +0000974#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200975 /*
976 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
977 */
978 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
979 f_rng, p_rng ) );
980 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
981 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
982 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000983
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200984 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000985#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200986 /*
987 * DP_blind = ( P - 1 ) * R + DP
988 */
989 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
990 f_rng, p_rng ) );
991 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
992 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
993 &ctx->DP ) );
Janos Follathf9203b42017-03-22 15:13:15 +0000994
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200995 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000996
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200997 /*
998 * DQ_blind = ( Q - 1 ) * R + DQ
999 */
1000 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1001 f_rng, p_rng ) );
1002 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1003 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1004 &ctx->DQ ) );
Janos Follathf9203b42017-03-22 15:13:15 +00001005
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001006 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001007#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001010 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001011#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001012 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001013 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001015 * TP = input ^ dP mod P
1016 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001017 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001018
1019 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1020 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
1022 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001023 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001024 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001025 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1026 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1027 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
1029 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001030 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001032 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1033 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001035
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001036 /*
1037 * Unblind
1038 * T = T * Vf mod N
1039 */
1040 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
1041 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
Hanno Becker2dec5e82017-10-03 07:49:52 +01001043 /* Verify the result to prevent glitching attacks. */
1044 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1045 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001046 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001047 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001048 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1049 goto cleanup;
1050 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001051
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
1055cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001057 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1058 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001059#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001060
Hanno Becker06811ce2017-05-03 15:10:34 +01001061 mbedtls_mpi_free( &P1 );
1062 mbedtls_mpi_free( &Q1 );
1063 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001064
Janos Follathe81102e2017-03-22 13:38:28 +00001065#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001066 mbedtls_mpi_free( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +00001067#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001068 mbedtls_mpi_free( &DP_blind );
1069 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001070#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001071
Hanno Becker06811ce2017-05-03 15:10:34 +01001072 mbedtls_mpi_free( &T );
1073
1074#if !defined(MBEDTLS_RSA_NO_CRT)
1075 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1076#endif
1077
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001078 mbedtls_mpi_free( &C );
1079 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001080
Gilles Peskineae3741e2020-11-25 00:10:31 +01001081 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001082 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001083
Gilles Peskineae3741e2020-11-25 00:10:31 +01001084 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001085}
1086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001088/**
1089 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1090 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001091 * \param dst buffer to mask
1092 * \param dlen length of destination buffer
1093 * \param src source of the mask generation
1094 * \param slen length of the source buffer
1095 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001096 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001097static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001101 unsigned char counter[4];
1102 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001103 unsigned int hlen;
1104 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001105 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001108 memset( counter, 0, 4 );
1109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001111
Simon Butcher02037452016-03-01 21:19:12 +00001112 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001113 p = dst;
1114
1115 while( dlen > 0 )
1116 {
1117 use_len = hlen;
1118 if( dlen < hlen )
1119 use_len = dlen;
1120
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001121 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1122 goto exit;
1123 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1124 goto exit;
1125 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1126 goto exit;
1127 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1128 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001129
1130 for( i = 0; i < use_len; ++i )
1131 *p++ ^= mask[i];
1132
1133 counter[3]++;
1134
1135 dlen -= use_len;
1136 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001137
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001138exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001139 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001140
1141 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001146/*
1147 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001150 int (*f_rng)(void *, unsigned char *, size_t),
1151 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001152 const unsigned char *label, size_t label_len,
1153 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001154 const unsigned char *input,
1155 unsigned char *output )
1156{
1157 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001159 unsigned char *p = output;
1160 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 const mbedtls_md_info_t *md_info;
1162 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001163
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001164 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001165 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001166 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001167 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1168
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001169 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001173 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001175
1176 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001178
Simon Butcher02037452016-03-01 21:19:12 +00001179 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001180 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001182
1183 memset( output, 0, olen );
1184
1185 *p++ = 0;
1186
Simon Butcher02037452016-03-01 21:19:12 +00001187 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001188 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001189 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001190
1191 p += hlen;
1192
Simon Butcher02037452016-03-01 21:19:12 +00001193 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001194 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1195 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001196 p += hlen;
1197 p += olen - 2 * hlen - 2 - ilen;
1198 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001199 if( ilen != 0 )
1200 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001203 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001204 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001205
Simon Butcher02037452016-03-01 21:19:12 +00001206 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001207 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1208 &md_ctx ) ) != 0 )
1209 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001210
Simon Butcher02037452016-03-01 21:19:12 +00001211 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001212 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1213 &md_ctx ) ) != 0 )
1214 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001215
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001216exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001218
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001219 if( ret != 0 )
1220 return( ret );
1221
Thomas Daubney141700f2021-05-13 19:06:10 +01001222 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001223}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001227/*
1228 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001231 int (*f_rng)(void *, unsigned char *, size_t),
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001232 void *p_rng, size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001233 const unsigned char *input,
1234 unsigned char *output )
1235{
1236 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001238 unsigned char *p = output;
1239
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001240 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001241 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001242 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001243
Paul Bakkerb3869132013-02-28 17:21:01 +01001244 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001245
Simon Butcher02037452016-03-01 21:19:12 +00001246 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001247 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001249
1250 nb_pad = olen - 3 - ilen;
1251
1252 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001253
1254 if( f_rng == NULL )
1255 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1256
1257 *p++ = MBEDTLS_RSA_CRYPT;
1258
1259 while( nb_pad-- > 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001260 {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001261 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001262
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001263 do {
1264 ret = f_rng( p_rng, p, 1 );
1265 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001266
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001267 /* Check if RNG failed to generate data */
1268 if( rng_dl == 0 || ret != 0 )
1269 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001270
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001271 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001272 }
1273
1274 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001275 if( ilen != 0 )
1276 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001277
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001278 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001281
Paul Bakker5121ce52009-01-03 21:22:43 +00001282/*
1283 * Add the message padding, then do an RSA operation
1284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001286 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001287 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +01001288 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001289 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 unsigned char *output )
1291{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001292 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001293 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001294 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001295
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 switch( ctx->padding )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#if defined(MBEDTLS_PKCS1_V15)
1299 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney21772772021-05-13 17:30:32 +01001300 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001301 ilen, input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001302#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#if defined(MBEDTLS_PKCS1_V21)
1305 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney141700f2021-05-13 19:06:10 +01001306 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, NULL, 0,
Thomas Daubney21772772021-05-13 17:30:32 +01001307 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001308#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
1310 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001313}
1314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001316/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001317 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001320 int (*f_rng)(void *, unsigned char *, size_t),
1321 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001322 const unsigned char *label, size_t label_len,
1323 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001324 const unsigned char *input,
1325 unsigned char *output,
1326 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001327{
Janos Follath24eed8d2019-11-22 13:21:35 +00001328 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001329 size_t ilen, i, pad_len;
1330 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1332 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001333 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 const mbedtls_md_info_t *md_info;
1335 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001336
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001337 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001338 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1339 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1340 RSA_VALIDATE_RET( input != NULL );
1341 RSA_VALIDATE_RET( olen != NULL );
1342
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001343 /*
1344 * Parameters sanity checks
1345 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001346 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
1349 ilen = ctx->len;
1350
Paul Bakker27fdf462011-06-09 13:55:13 +00001351 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001355 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001357
Janos Follathc17cda12016-02-11 11:08:18 +00001358 hlen = mbedtls_md_get_size( md_info );
1359
1360 // checking for integer underflow
1361 if( 2 * hlen + 2 > ilen )
1362 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1363
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001364 /*
1365 * RSA operation
1366 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001367 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001368
1369 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001370 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001372 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001373 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001376 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1377 {
1378 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001379 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001380 }
1381
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001382 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001383 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1384 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001385 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001386 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1387 &md_ctx ) ) != 0 )
1388 {
1389 mbedtls_md_free( &md_ctx );
1390 goto cleanup;
1391 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001394
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001395 /* Generate lHash */
1396 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1397 goto cleanup;
1398
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001399 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001400 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001401 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001403 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001405 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001406
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001407 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001408
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001409 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001410 for( i = 0; i < hlen; i++ )
1411 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001412
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001413 /* Get zero-padding len, but always read till end of buffer
1414 * (minus one, for the 01 byte) */
1415 pad_len = 0;
1416 pad_done = 0;
1417 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1418 {
1419 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001420 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001421 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001422
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001423 p += pad_len;
1424 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001425
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001426 /*
1427 * The only information "leaked" is whether the padding was correct or not
1428 * (eg, no data is copied if it was not correct). This meets the
1429 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1430 * the different error conditions.
1431 */
1432 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001433 {
1434 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1435 goto cleanup;
1436 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001437
Paul Bakker66d5d072014-06-17 16:39:18 +02001438 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001439 {
1440 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1441 goto cleanup;
1442 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001443
1444 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001445 if( *olen != 0 )
1446 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001447 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001448
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001449cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001450 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1451 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001452
1453 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001458/*
1459 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1460 */
1461int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
1462 int (*f_rng)(void *, unsigned char *, size_t),
1463 void *p_rng,
1464 size_t *olen,
1465 const unsigned char *input,
1466 unsigned char *output,
1467 size_t output_max_len )
1468{
1469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1470 size_t ilen;
1471 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1472
1473 RSA_VALIDATE_RET( ctx != NULL );
1474 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1475 RSA_VALIDATE_RET( input != NULL );
1476 RSA_VALIDATE_RET( olen != NULL );
1477
1478 ilen = ctx->len;
1479
1480 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1481 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1482
1483 if( ilen < 16 || ilen > sizeof( buf ) )
1484 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1485
1486 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
1487
1488 if( ret != 0 )
1489 goto cleanup;
1490
Gabor Mezei90437e32021-10-20 11:59:27 +02001491 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding( buf, ilen,
Gabor Mezei63bbba52021-10-18 16:17:57 +02001492 output, output_max_len, olen );
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001493
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001494cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001495 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001496
1497 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001500
1501/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001502 * Do an RSA operation, then remove the message padding
1503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001505 int (*f_rng)(void *, unsigned char *, size_t),
1506 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001507 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001508 const unsigned char *input,
1509 unsigned char *output,
1510 size_t output_max_len)
1511{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001512 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001513 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1514 RSA_VALIDATE_RET( input != NULL );
1515 RSA_VALIDATE_RET( olen != NULL );
1516
Paul Bakkerb3869132013-02-28 17:21:01 +01001517 switch( ctx->padding )
1518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#if defined(MBEDTLS_PKCS1_V15)
1520 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney34733082021-05-12 09:24:29 +01001521 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001522 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001523#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_PKCS1_V21)
1526 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001527 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001528 olen, input, output,
1529 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001530#endif
1531
1532 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001534 }
1535}
1536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001538static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001539 int (*f_rng)(void *, unsigned char *, size_t),
1540 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001542 unsigned int hashlen,
1543 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001544 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001545 unsigned char *sig )
1546{
1547 size_t olen;
1548 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001549 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001550 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001552 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 const mbedtls_md_info_t *md_info;
1554 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001555 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001556 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1557 hashlen == 0 ) ||
1558 hash != NULL );
1559 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001560
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001561 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1562 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1563
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001564 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001566
1567 olen = ctx->len;
1568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001570 {
Simon Butcher02037452016-03-01 21:19:12 +00001571 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001573 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001575
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001576 if( hashlen != mbedtls_md_get_size( md_info ) )
1577 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001578 }
1579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001581 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001585
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001586 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1587 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001588 /* Calculate the largest possible salt length, up to the hash size.
1589 * Normally this is the hash length, which is the maximum salt length
1590 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001591 * enough room, use the maximum salt length that fits. The constraint is
1592 * that the hash length plus the salt length plus 2 bytes must be at most
1593 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1594 * (PKCS#1 v2.2) §9.1.1 step 3. */
1595 min_slen = hlen - 2;
1596 if( olen < hlen + min_slen + 2 )
1597 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1598 else if( olen >= hlen + hlen + 2 )
1599 slen = hlen;
1600 else
1601 slen = olen - hlen - 2;
1602 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001603 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001606 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001607 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001608 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001609 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001610 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001611
1612 memset( sig, 0, olen );
1613
Simon Butcher02037452016-03-01 21:19:12 +00001614 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001615 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001616 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001617 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001618
1619 /* Generate salt of length slen in place in the encoded message */
1620 salt = p;
1621 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001622 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001623
Paul Bakkerb3869132013-02-28 17:21:01 +01001624 p += slen;
1625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001627 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001628 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001629
Simon Butcher02037452016-03-01 21:19:12 +00001630 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001631 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1632 goto exit;
1633 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1634 goto exit;
1635 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1636 goto exit;
1637 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1638 goto exit;
1639 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1640 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001641
Simon Butcher02037452016-03-01 21:19:12 +00001642 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001643 if( msb % 8 == 0 )
1644 offset = 1;
1645
Simon Butcher02037452016-03-01 21:19:12 +00001646 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001647 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1648 &md_ctx ) ) != 0 )
1649 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001650
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001651 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001652 sig[0] &= 0xFF >> ( olen * 8 - msb );
1653
1654 p += hlen;
1655 *p++ = 0xBC;
1656
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001657exit:
1658 mbedtls_md_free( &md_ctx );
1659
1660 if( ret != 0 )
1661 return( ret );
1662
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001663 return mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001664}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001665
1666/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001667 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1668 * the option to pass in the salt length.
1669 */
1670int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1671 int (*f_rng)(void *, unsigned char *, size_t),
1672 void *p_rng,
1673 mbedtls_md_type_t md_alg,
1674 unsigned int hashlen,
1675 const unsigned char *hash,
1676 int saltlen,
1677 unsigned char *sig )
1678{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001679 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001680 hashlen, hash, saltlen, sig );
1681}
1682
1683
1684/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001685 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1686 */
1687int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1688 int (*f_rng)(void *, unsigned char *, size_t),
1689 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001690 mbedtls_md_type_t md_alg,
1691 unsigned int hashlen,
1692 const unsigned char *hash,
1693 unsigned char *sig )
1694{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001695 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001696 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001701/*
1702 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1703 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001704
1705/* Construct a PKCS v1.5 encoding of a hashed message
1706 *
1707 * This is used both for signature generation and verification.
1708 *
1709 * Parameters:
1710 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001711 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001712 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001713 * - hash: Buffer containing the hashed message or the raw data.
1714 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001715 * - dst: Buffer to hold the encoded message.
1716 *
1717 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001718 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001719 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001720 *
1721 */
1722static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1723 unsigned int hashlen,
1724 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001725 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001726 unsigned char *dst )
1727{
1728 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001729 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001730 unsigned char *p = dst;
1731 const char *oid = NULL;
1732
1733 /* Are we signing hashed or raw data? */
1734 if( md_alg != MBEDTLS_MD_NONE )
1735 {
1736 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1737 if( md_info == NULL )
1738 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1739
1740 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1741 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1742
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001743 if( hashlen != mbedtls_md_get_size( md_info ) )
1744 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001745
1746 /* Double-check that 8 + hashlen + oid_size can be used as a
1747 * 1-byte ASN.1 length encoding and that there's no overflow. */
1748 if( 8 + hashlen + oid_size >= 0x80 ||
1749 10 + hashlen < hashlen ||
1750 10 + hashlen + oid_size < 10 + hashlen )
1751 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1752
1753 /*
1754 * Static bounds check:
1755 * - Need 10 bytes for five tag-length pairs.
1756 * (Insist on 1-byte length encodings to protect against variants of
1757 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1758 * - Need hashlen bytes for hash
1759 * - Need oid_size bytes for hash alg OID.
1760 */
1761 if( nb_pad < 10 + hashlen + oid_size )
1762 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1763 nb_pad -= 10 + hashlen + oid_size;
1764 }
1765 else
1766 {
1767 if( nb_pad < hashlen )
1768 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1769
1770 nb_pad -= hashlen;
1771 }
1772
Hanno Becker2b2f8982017-09-27 17:10:03 +01001773 /* Need space for signature header and padding delimiter (3 bytes),
1774 * and 8 bytes for the minimal padding */
1775 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001776 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1777 nb_pad -= 3;
1778
1779 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001780 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001781
1782 /* Write signature header and padding */
1783 *p++ = 0;
1784 *p++ = MBEDTLS_RSA_SIGN;
1785 memset( p, 0xFF, nb_pad );
1786 p += nb_pad;
1787 *p++ = 0;
1788
1789 /* Are we signing raw data? */
1790 if( md_alg == MBEDTLS_MD_NONE )
1791 {
1792 memcpy( p, hash, hashlen );
1793 return( 0 );
1794 }
1795
1796 /* Signing hashed data, add corresponding ASN.1 structure
1797 *
1798 * DigestInfo ::= SEQUENCE {
1799 * digestAlgorithm DigestAlgorithmIdentifier,
1800 * digest Digest }
1801 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1802 * Digest ::= OCTET STRING
1803 *
1804 * Schematic:
1805 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1806 * TAG-NULL + LEN [ NULL ] ]
1807 * TAG-OCTET + LEN [ HASH ] ]
1808 */
1809 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001810 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001811 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001812 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001813 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001814 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001815 memcpy( p, oid, oid_size );
1816 p += oid_size;
1817 *p++ = MBEDTLS_ASN1_NULL;
1818 *p++ = 0x00;
1819 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001820 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001821 memcpy( p, hash, hashlen );
1822 p += hashlen;
1823
1824 /* Just a sanity-check, should be automatic
1825 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001826 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001827 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001828 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001829 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1830 }
1831
1832 return( 0 );
1833}
1834
Paul Bakkerb3869132013-02-28 17:21:01 +01001835/*
1836 * Do an RSA operation to sign the message digest
1837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001839 int (*f_rng)(void *, unsigned char *, size_t),
1840 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001842 unsigned int hashlen,
1843 const unsigned char *hash,
1844 unsigned char *sig )
1845{
Janos Follath24eed8d2019-11-22 13:21:35 +00001846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001847 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001848
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001849 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001850 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1851 hashlen == 0 ) ||
1852 hash != NULL );
1853 RSA_VALIDATE_RET( sig != NULL );
1854
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001855 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1856 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1857
Hanno Beckerfdf38032017-09-06 12:35:55 +01001858 /*
1859 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1860 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001861
Hanno Beckerfdf38032017-09-06 12:35:55 +01001862 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1863 ctx->len, sig ) ) != 0 )
1864 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001865
Hanno Beckerfdf38032017-09-06 12:35:55 +01001866 /* Private key operation
1867 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001868 * In order to prevent Lenstra's attack, make the signature in a
1869 * temporary buffer and check it before returning it.
1870 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001871
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001872 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001873 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001874 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1875
Hanno Beckerfdf38032017-09-06 12:35:55 +01001876 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001877 if( verif == NULL )
1878 {
1879 mbedtls_free( sig_try );
1880 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1881 }
1882
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001883 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1884 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1885
Gabor Mezei90437e32021-10-20 11:59:27 +02001886 if( mbedtls_ct_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001887 {
1888 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1889 goto cleanup;
1890 }
1891
1892 memcpy( sig, sig_try, ctx->len );
1893
1894cleanup:
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001895 mbedtls_platform_zeroize( sig_try, ctx->len );
1896 mbedtls_platform_zeroize( verif, ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001897 mbedtls_free( sig_try );
1898 mbedtls_free( verif );
1899
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001900 if( ret != 0 )
1901 memset( sig, '!', ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001902 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001905
1906/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 * Do an RSA operation to sign the message digest
1908 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001910 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001911 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001913 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001914 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 unsigned char *sig )
1916{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001917 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001918 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1919 hashlen == 0 ) ||
1920 hash != NULL );
1921 RSA_VALIDATE_RET( sig != NULL );
1922
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 switch( ctx->padding )
1924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925#if defined(MBEDTLS_PKCS1_V15)
1926 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney52654982021-05-18 16:54:00 +01001927 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng,
Thomas Daubney140184d2021-05-18 16:04:07 +01001928 md_alg, hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001929#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#if defined(MBEDTLS_PKCS1_V21)
1932 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyde9fdc42021-05-18 17:10:04 +01001933 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
1934 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001935#endif
1936
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001939 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001940}
1941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001943/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001944 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001948 unsigned int hashlen,
1949 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001951 int expected_salt_len,
1952 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001953{
Janos Follath24eed8d2019-11-22 13:21:35 +00001954 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001955 size_t siglen;
1956 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001957 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001959 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001960 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001961 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 const mbedtls_md_info_t *md_info;
1963 mbedtls_md_context_t md_ctx;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001964 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = {0};
Paul Bakkerb3869132013-02-28 17:21:01 +01001965
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001966 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001967 RSA_VALIDATE_RET( sig != NULL );
1968 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1969 hashlen == 0 ) ||
1970 hash != NULL );
1971
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 siglen = ctx->len;
1973
Paul Bakker27fdf462011-06-09 13:55:13 +00001974 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001976
Thomas Daubney782a7f52021-05-19 12:27:35 +01001977 ret = mbedtls_rsa_public( ctx, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001978
1979 if( ret != 0 )
1980 return( ret );
1981
1982 p = buf;
1983
Paul Bakkerb3869132013-02-28 17:21:01 +01001984 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 {
Simon Butcher02037452016-03-01 21:19:12 +00001989 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001991 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001993
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001994 if( hashlen != mbedtls_md_get_size( md_info ) )
1995 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001996 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001999 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002003
Paul Bakkerb3869132013-02-28 17:21:01 +01002004 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002005
Simon Butcher02037452016-03-01 21:19:12 +00002006 /*
2007 * Note: EMSA-PSS verification is over the length of N - 1 bits
2008 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002009 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002010
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002011 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2012 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2013
Simon Butcher02037452016-03-01 21:19:12 +00002014 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002015 if( msb % 8 == 0 )
2016 {
2017 p++;
2018 siglen -= 1;
2019 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002020
Gilles Peskine139108a2017-10-18 19:03:42 +02002021 if( siglen < hlen + 2 )
2022 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2023 hash_start = p + siglen - hlen - 1;
2024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002026 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002027 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002028
Jaeden Amero66954e12018-01-25 16:05:54 +00002029 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2030 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002031 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002032
Paul Bakkerb3869132013-02-28 17:21:01 +01002033 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002034
Gilles Peskine6a54b022017-10-17 19:02:13 +02002035 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002036 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002037
Gilles Peskine91048a32017-10-19 17:46:14 +02002038 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002039 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002040 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2041 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002042 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002043
Gilles Peskine6a54b022017-10-17 19:02:13 +02002044 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002047 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002048 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002049 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2050 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002051 }
2052
Simon Butcher02037452016-03-01 21:19:12 +00002053 /*
2054 * Generate H = Hash( M' )
2055 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002056 ret = mbedtls_md_starts( &md_ctx );
2057 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002058 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002059 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2060 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002061 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002062 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2063 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002064 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002065 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2066 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002067 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002068 ret = mbedtls_md_finish( &md_ctx, result );
2069 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002070 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002071
Jaeden Amero66954e12018-01-25 16:05:54 +00002072 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002073 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002074 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002075 goto exit;
2076 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002077
2078exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002080
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002081 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002082}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002083
2084/*
2085 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002089 unsigned int hashlen,
2090 const unsigned char *hash,
2091 const unsigned char *sig )
2092{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002093 mbedtls_md_type_t mgf1_hash_id;
2094 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002095 RSA_VALIDATE_RET( sig != NULL );
2096 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2097 hashlen == 0 ) ||
2098 hash != NULL );
2099
2100 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002102 : md_alg;
2103
Thomas Daubney9e65f792021-05-19 12:18:58 +01002104 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx,
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002105 md_alg, hashlen, hash,
2106 mgf1_hash_id,
2107 MBEDTLS_RSA_SALT_LEN_ANY,
2108 sig ) );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002109
2110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002114/*
2115 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002119 unsigned int hashlen,
2120 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002121 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002122{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002123 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002124 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002125 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002126
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002127 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002128 RSA_VALIDATE_RET( sig != NULL );
2129 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2130 hashlen == 0 ) ||
2131 hash != NULL );
2132
2133 sig_len = ctx->len;
2134
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002135 /*
2136 * Prepare expected PKCS1 v1.5 encoding of hash.
2137 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002138
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002139 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2140 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2141 {
2142 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2143 goto cleanup;
2144 }
2145
2146 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2147 encoded_expected ) ) != 0 )
2148 goto cleanup;
2149
2150 /*
2151 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2152 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002153
Thomas Daubney41e4ce42021-05-19 15:10:05 +01002154 ret = mbedtls_rsa_public( ctx, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002155 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002156 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002157
Simon Butcher02037452016-03-01 21:19:12 +00002158 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002159 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002160 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002161
Gabor Mezei90437e32021-10-20 11:59:27 +02002162 if( ( ret = mbedtls_ct_memcmp( encoded, encoded_expected,
gabor-mezei-arm46025642021-07-19 15:19:19 +02002163 sig_len ) ) != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002164 {
2165 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2166 goto cleanup;
2167 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002168
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002169cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002170
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002171 if( encoded != NULL )
2172 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002173 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002174 mbedtls_free( encoded );
2175 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002176
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002177 if( encoded_expected != NULL )
2178 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002179 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002180 mbedtls_free( encoded_expected );
2181 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002182
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002183 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002184}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
2187/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002188 * Do an RSA operation and check the message digest
2189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002192 unsigned int hashlen,
2193 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002194 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002195{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002196 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002197 RSA_VALIDATE_RET( sig != NULL );
2198 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2199 hashlen == 0 ) ||
2200 hash != NULL );
2201
Paul Bakkerb3869132013-02-28 17:21:01 +01002202 switch( ctx->padding )
2203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204#if defined(MBEDTLS_PKCS1_V15)
2205 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney2e126252021-05-19 11:48:53 +01002206 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, md_alg,
2207 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002208#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210#if defined(MBEDTLS_PKCS1_V21)
2211 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002212 return mbedtls_rsa_rsassa_pss_verify( ctx, md_alg,
2213 hashlen, hash, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01002214#endif
2215
2216 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002218 }
2219}
2220
2221/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002222 * Copy the components of an RSA key
2223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002225{
Janos Follath24eed8d2019-11-22 13:21:35 +00002226 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002227 RSA_VALIDATE_RET( dst != NULL );
2228 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002229
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002230 dst->len = src->len;
2231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2233 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2236 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2237 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002238
2239#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2241 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2242 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2244 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002245#endif
2246
2247 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2250 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002251
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002252 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002253 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002254
2255cleanup:
2256 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002258
2259 return( ret );
2260}
2261
2262/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 * Free the components of an RSA key
2264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002266{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002267 if( ctx == NULL )
2268 return;
2269
2270 mbedtls_mpi_free( &ctx->Vi );
2271 mbedtls_mpi_free( &ctx->Vf );
2272 mbedtls_mpi_free( &ctx->RN );
2273 mbedtls_mpi_free( &ctx->D );
2274 mbedtls_mpi_free( &ctx->Q );
2275 mbedtls_mpi_free( &ctx->P );
2276 mbedtls_mpi_free( &ctx->E );
2277 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002278
Hanno Becker33c30a02017-08-23 07:00:22 +01002279#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002280 mbedtls_mpi_free( &ctx->RQ );
2281 mbedtls_mpi_free( &ctx->RP );
2282 mbedtls_mpi_free( &ctx->QP );
2283 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002284 mbedtls_mpi_free( &ctx->DP );
2285#endif /* MBEDTLS_RSA_NO_CRT */
2286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002288 /* Free the mutex, but only if it hasn't been freed already. */
2289 if( ctx->ver != 0 )
2290 {
2291 mbedtls_mutex_free( &ctx->mutex );
2292 ctx->ver = 0;
2293 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002294#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002295}
2296
Hanno Beckerab377312017-08-23 16:24:51 +01002297#endif /* !MBEDTLS_RSA_ALT */
2298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002301#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002302
2303/*
2304 * Example RSA-1024 keypair, for test purposes
2305 */
2306#define KEY_LEN 128
2307
2308#define RSA_N "9292758453063D803DD603D5E777D788" \
2309 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2310 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2311 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2312 "93A89813FBF3C4F8066D2D800F7C38A8" \
2313 "1AE31942917403FF4946B0A83D3D3E05" \
2314 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2315 "5E94BB77B07507233A0BC7BAC8F90F79"
2316
2317#define RSA_E "10001"
2318
2319#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2320 "66CA472BC44D253102F8B4A9D3BFA750" \
2321 "91386C0077937FE33FA3252D28855837" \
2322 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2323 "DF79C5CE07EE72C7F123142198164234" \
2324 "CABB724CF78B8173B9F880FC86322407" \
2325 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2326 "071513A1E85B5DFA031F21ECAE91A34D"
2327
2328#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2329 "2C01CAD19EA484A87EA4377637E75500" \
2330 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2331 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2332
2333#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2334 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2335 "910E4168387E3C30AA1E00C339A79508" \
2336 "8452DD96A9A5EA5D9DCA68DA636032AF"
2337
Paul Bakker5121ce52009-01-03 21:22:43 +00002338#define PT_LEN 24
2339#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2340 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002343static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002344{
gufe44c2620da2020-08-03 17:56:50 +02002345#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002346 size_t i;
2347
Paul Bakker545570e2010-07-18 09:00:25 +00002348 if( rng_state != NULL )
2349 rng_state = NULL;
2350
Paul Bakkera3d195c2011-11-27 21:07:34 +00002351 for( i = 0; i < len; ++i )
2352 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002353#else
2354 if( rng_state != NULL )
2355 rng_state = NULL;
2356
2357 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002358#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002359
Paul Bakkera3d195c2011-11-27 21:07:34 +00002360 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002363
Paul Bakker5121ce52009-01-03 21:22:43 +00002364/*
2365 * Checkup routine
2366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002368{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002369 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002371 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 unsigned char rsa_plaintext[PT_LEN];
2374 unsigned char rsa_decrypted[PT_LEN];
2375 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002377 unsigned char sha1sum[20];
2378#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002379
Hanno Becker3a701162017-08-22 13:52:43 +01002380 mbedtls_mpi K;
2381
2382 mbedtls_mpi_init( &K );
Ronald Cronc1905a12021-06-05 11:11:14 +02002383 mbedtls_rsa_init( &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +00002384
Hanno Becker3a701162017-08-22 13:52:43 +01002385 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2386 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2387 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2388 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2389 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2390 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2391 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2392 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2393 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2394 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2395
Hanno Becker7f25f852017-10-10 16:56:22 +01002396 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
2398 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2402 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 {
2404 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Hanno Becker5bc87292017-05-03 15:09:31 +01002407 ret = 1;
2408 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 }
2410
2411 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
2414 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2415
Thomas Daubney21772772021-05-13 17:30:32 +01002416 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002417 PT_LEN, rsa_plaintext,
2418 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 {
2420 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
Hanno Becker5bc87292017-05-03 15:09:31 +01002423 ret = 1;
2424 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 }
2426
2427 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002430 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002431 &len, rsa_ciphertext, rsa_decrypted,
2432 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 {
2434 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
Hanno Becker5bc87292017-05-03 15:09:31 +01002437 ret = 1;
2438 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 }
2440
2441 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2442 {
2443 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Hanno Becker5bc87292017-05-03 15:09:31 +01002446 ret = 1;
2447 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 }
2449
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002450 if( verbose != 0 )
2451 mbedtls_printf( "passed\n" );
2452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002455 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
TRodziewicz26371e42021-06-08 16:45:41 +02002457 if( mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002458 {
2459 if( verbose != 0 )
2460 mbedtls_printf( "failed\n" );
2461
2462 return( 1 );
2463 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
Hanno Becker98838b02017-10-02 13:16:10 +01002465 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002466 MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002467 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 {
2469 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
Hanno Becker5bc87292017-05-03 15:09:31 +01002472 ret = 1;
2473 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 }
2475
2476 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002479 if( mbedtls_rsa_pkcs1_verify( &rsa, MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002480 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 {
2482 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002484
Hanno Becker5bc87292017-05-03 15:09:31 +01002485 ret = 1;
2486 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 }
2488
2489 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002490 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002493 if( verbose != 0 )
2494 mbedtls_printf( "\n" );
2495
Paul Bakker3d8fb632014-04-17 12:42:41 +02002496cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002497 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 mbedtls_rsa_free( &rsa );
2499#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002500 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002502 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002503}
2504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#endif /* MBEDTLS_RSA_C */