blob: ae9e68b91ac89c953e627cecff95ae028b51e601 [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"
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020049#include "hash_info.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
gufe44c2620da2020-08-03 17:56:50 +020053#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000054#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000055#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +020057/* We use MD first if it's available (for compatibility reasons)
58 * and "fall back" to PSA otherwise (which needs psa_crypto_init()). */
59#if defined(MBEDTLS_PKCS1_V21)
Przemek Stekiel40afdd22022-09-06 13:08:28 +020060#if !defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +020061#include "psa/crypto.h"
62#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +020063#endif /* MBEDTLS_MD_C */
64#endif /* MBEDTLS_PKCS1_V21 */
65
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010067
Hanno Beckera565f542017-10-11 11:00:19 +010068#if !defined(MBEDTLS_RSA_ALT)
69
Hanno Becker617c1ae2017-08-23 14:11:24 +010070int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
71 const mbedtls_mpi *N,
72 const mbedtls_mpi *P, const mbedtls_mpi *Q,
73 const mbedtls_mpi *D, const mbedtls_mpi *E )
74{
Janos Follath24eed8d2019-11-22 13:21:35 +000075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +010076
77 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
78 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
79 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
80 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
81 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
82 {
Chris Jonesb7d02e02021-04-01 17:40:03 +010083 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +010084 }
85
86 if( N != NULL )
87 ctx->len = mbedtls_mpi_size( &ctx->N );
88
89 return( 0 );
90}
91
92int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +010093 unsigned char const *N, size_t N_len,
94 unsigned char const *P, size_t P_len,
95 unsigned char const *Q, size_t Q_len,
96 unsigned char const *D, size_t D_len,
97 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +010098{
Hanno Beckerd4d60572018-01-10 07:12:01 +000099 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100100
101 if( N != NULL )
102 {
103 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
104 ctx->len = mbedtls_mpi_size( &ctx->N );
105 }
106
107 if( P != NULL )
108 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
109
110 if( Q != NULL )
111 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
112
113 if( D != NULL )
114 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
115
116 if( E != NULL )
117 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
118
119cleanup:
120
121 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100122 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100123
124 return( 0 );
125}
126
Hanno Becker705fc682017-10-10 17:57:02 +0100127/*
128 * Checks whether the context fields are set in such a way
129 * that the RSA primitives will be able to execute without error.
130 * It does *not* make guarantees for consistency of the parameters.
131 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100132static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
133 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100134{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100135#if !defined(MBEDTLS_RSA_NO_CRT)
136 /* blinding_needed is only used for NO_CRT to decide whether
137 * P,Q need to be present or not. */
138 ((void) blinding_needed);
139#endif
140
Hanno Becker3a760a12018-01-05 08:14:49 +0000141 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
142 ctx->len > MBEDTLS_MPI_MAX_SIZE )
143 {
Hanno Becker705fc682017-10-10 17:57:02 +0100144 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000145 }
Hanno Becker705fc682017-10-10 17:57:02 +0100146
147 /*
148 * 1. Modular exponentiation needs positive, odd moduli.
149 */
150
151 /* Modular exponentiation wrt. N is always used for
152 * RSA public key operations. */
153 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
154 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
155 {
156 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
157 }
158
159#if !defined(MBEDTLS_RSA_NO_CRT)
160 /* Modular exponentiation for P and Q is only
161 * used for private key operations and if CRT
162 * is used. */
163 if( is_priv &&
164 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
165 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
166 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
167 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
168 {
169 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
170 }
171#endif /* !MBEDTLS_RSA_NO_CRT */
172
173 /*
174 * 2. Exponents must be positive
175 */
176
177 /* Always need E for public key operations */
178 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
180
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100181#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100182 /* For private key operations, use D or DP & DQ
183 * as (unblinded) exponents. */
184 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
185 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
186#else
187 if( is_priv &&
188 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
189 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
190 {
191 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
192 }
193#endif /* MBEDTLS_RSA_NO_CRT */
194
195 /* Blinding shouldn't make exponents negative either,
196 * so check that P, Q >= 1 if that hasn't yet been
197 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100198#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100199 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100200 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
201 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
202 {
203 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
204 }
205#endif
206
207 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100208 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100209#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100210 if( is_priv &&
211 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
212 {
213 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
214 }
215#endif
216
217 return( 0 );
218}
219
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100220int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100221{
222 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500223 int have_N, have_P, have_Q, have_D, have_E;
224#if !defined(MBEDTLS_RSA_NO_CRT)
225 int have_DP, have_DQ, have_QP;
226#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100228
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500229 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
230 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
231 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
232 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
233 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500234
235#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500236 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
237 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
238 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500239#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100240
Hanno Becker617c1ae2017-08-23 14:11:24 +0100241 /*
242 * Check whether provided parameters are enough
243 * to deduce all others. The following incomplete
244 * parameter sets for private keys are supported:
245 *
246 * (1) P, Q missing.
247 * (2) D and potentially N missing.
248 *
249 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100250
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500251 n_missing = have_P && have_Q && have_D && have_E;
252 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
253 d_missing = have_P && have_Q && !have_D && have_E;
254 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100255
256 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500257 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100258
Hanno Becker617c1ae2017-08-23 14:11:24 +0100259 if( !is_priv && !is_pub )
260 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
261
262 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100263 * Step 1: Deduce N if P, Q are provided.
264 */
265
266 if( !have_N && have_P && have_Q )
267 {
268 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
269 &ctx->Q ) ) != 0 )
270 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100271 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100272 }
273
274 ctx->len = mbedtls_mpi_size( &ctx->N );
275 }
276
277 /*
278 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100279 */
280
281 if( pq_missing )
282 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100283 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100284 &ctx->P, &ctx->Q );
285 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100286 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100287
288 }
289 else if( d_missing )
290 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100291 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
292 &ctx->Q,
293 &ctx->E,
294 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100295 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100296 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100297 }
298 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100299
Hanno Becker617c1ae2017-08-23 14:11:24 +0100300 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100301 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100302 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100303 */
304
Hanno Becker23344b52017-08-23 07:43:27 +0100305#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500306 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100307 {
308 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
309 &ctx->DP, &ctx->DQ, &ctx->QP );
310 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100311 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100312 }
Hanno Becker23344b52017-08-23 07:43:27 +0100313#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100314
315 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100316 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100317 */
318
Hanno Beckerebd2c022017-10-12 10:54:53 +0100319 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100320}
321
Hanno Becker617c1ae2017-08-23 14:11:24 +0100322int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
323 unsigned char *N, size_t N_len,
324 unsigned char *P, size_t P_len,
325 unsigned char *Q, size_t Q_len,
326 unsigned char *D, size_t D_len,
327 unsigned char *E, size_t E_len )
328{
329 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500330 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100331
332 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500333 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100334 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
335 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
336 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
337 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
338 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
339
340 if( !is_priv )
341 {
342 /* If we're trying to export private parameters for a public key,
343 * something must be wrong. */
344 if( P != NULL || Q != NULL || D != NULL )
345 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
346
347 }
348
349 if( N != NULL )
350 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
351
352 if( P != NULL )
353 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
354
355 if( Q != NULL )
356 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
357
358 if( D != NULL )
359 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
360
361 if( E != NULL )
362 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100363
364cleanup:
365
366 return( ret );
367}
368
Hanno Becker617c1ae2017-08-23 14:11:24 +0100369int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
370 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
371 mbedtls_mpi *D, mbedtls_mpi *E )
372{
Janos Follath24eed8d2019-11-22 13:21:35 +0000373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500374 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100375
376 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500377 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100378 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
379 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
380 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
381 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
382 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
383
384 if( !is_priv )
385 {
386 /* If we're trying to export private parameters for a public key,
387 * something must be wrong. */
388 if( P != NULL || Q != NULL || D != NULL )
389 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
390
391 }
392
393 /* Export all requested core parameters. */
394
395 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
396 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
397 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
398 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
399 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
400 {
401 return( ret );
402 }
403
404 return( 0 );
405}
406
407/*
408 * Export CRT parameters
409 * This must also be implemented if CRT is not used, for being able to
410 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
411 * can be used in this case.
412 */
413int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
414 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
415{
Janos Follath24eed8d2019-11-22 13:21:35 +0000416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500417 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100418
419 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500420 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100421 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
422 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
423 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
424 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
425 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
426
427 if( !is_priv )
428 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
429
Hanno Beckerdc95c892017-08-23 06:57:02 +0100430#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100431 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100432 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
433 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
434 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
435 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100436 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100437 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100438#else
439 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
440 DP, DQ, QP ) ) != 0 )
441 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100442 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100443 }
444#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100445
446 return( 0 );
447}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100448
Paul Bakker5121ce52009-01-03 21:22:43 +0000449/*
450 * Initialize an RSA context
451 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200452void mbedtls_rsa_init( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
Ronald Cronc1905a12021-06-05 11:11:14 +0200456 ctx->padding = MBEDTLS_RSA_PKCS_V15;
457 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100460 /* Set ctx->ver to nonzero to indicate that the mutex has been
461 * initialized and will need to be freed. */
462 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200464#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000465}
466
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100467/*
468 * Set padding for an existing RSA context
469 */
Ronald Cronea7631b2021-06-03 18:51:59 +0200470int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
471 mbedtls_md_type_t hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100472{
Ronald Cron3a0375f2021-06-08 10:22:28 +0200473 switch( padding )
474 {
475#if defined(MBEDTLS_PKCS1_V15)
476 case MBEDTLS_RSA_PKCS_V15:
477 break;
478#endif
479
480#if defined(MBEDTLS_PKCS1_V21)
481 case MBEDTLS_RSA_PKCS_V21:
482 break;
483#endif
484 default:
485 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
486 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200487
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200488#if defined(MBEDTLS_PKCS1_V21)
Ronald Cronea7631b2021-06-03 18:51:59 +0200489 if( ( padding == MBEDTLS_RSA_PKCS_V21 ) &&
490 ( hash_id != MBEDTLS_MD_NONE ) )
491 {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200492 /* Just make sure this hash is supported in this build. */
Przemek Stekiel712bb9c2022-07-29 11:12:00 +0200493 if( mbedtls_hash_info_psa_from_md( hash_id ) == PSA_ALG_NONE )
Ronald Cronea7631b2021-06-03 18:51:59 +0200494 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
495 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200496#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500497
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100498 ctx->padding = padding;
499 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200500
501 return( 0 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100502}
503
Hanno Becker617c1ae2017-08-23 14:11:24 +0100504/*
505 * Get length in bytes of RSA modulus
506 */
507
508size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
509{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100510 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100511}
512
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
516/*
517 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800518 *
519 * This generation method follows the RSA key pair generation procedure of
520 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000523 int (*f_rng)(void *, unsigned char *, size_t),
524 void *p_rng,
525 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000526{
Janos Follath24eed8d2019-11-22 13:21:35 +0000527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800528 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100529 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
Janos Follathb8fc1b02018-09-03 15:37:01 +0100531 /*
532 * If the modulus is 1024 bit long or shorter, then the security strength of
533 * the RSA algorithm is less than or equal to 80 bits and therefore an error
534 * rate of 2^-80 is sufficient.
535 */
536 if( nbits > 1024 )
537 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
538
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100539 mbedtls_mpi_init( &H );
540 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800541 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100543 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
544 {
545 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
546 goto cleanup;
547 }
548
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 /*
550 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800551 * 1. |P-Q| > 2^( nbits / 2 - 100 )
552 * 2. GCD( E, (P-1)*(Q-1) ) == 1
553 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557 do
558 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100559 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
560 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
Janos Follathb8fc1b02018-09-03 15:37:01 +0100562 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
563 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800565 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
566 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
567 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 continue;
569
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800570 /* not required by any standards, but some users rely on the fact that P > Q */
571 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100572 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100573
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100574 /* Temporarily replace P,Q by P-1, Q-1 */
575 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
576 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
577 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800578
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800579 /* 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 +0200580 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800581 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
582 continue;
583
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800584 /* 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 -0800585 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
586 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
587 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
588
589 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
590 continue;
591
592 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800594 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100596 /* Restore P,Q */
597 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
598 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
599
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800600 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
601
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100602 ctx->len = mbedtls_mpi_size( &ctx->N );
603
Jethro Beekman97f95c92018-02-13 15:50:36 -0800604#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 * DP = D mod (P - 1)
607 * DQ = D mod (Q - 1)
608 * QP = Q^-1 mod P
609 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100610 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
611 &ctx->DP, &ctx->DQ, &ctx->QP ) );
612#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
Hanno Becker83aad1f2017-08-23 06:45:10 +0100614 /* Double-check */
615 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
617cleanup:
618
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100619 mbedtls_mpi_free( &H );
620 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800621 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
623 if( ret != 0 )
624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100626
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100627 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100628 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100629 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 }
631
Paul Bakker48377d92013-08-30 12:06:24 +0200632 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633}
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
637/*
638 * Check a public RSA key
639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000641{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100642 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000644
Hanno Becker3a760a12018-01-05 08:14:49 +0000645 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100648 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
Hanno Becker705fc682017-10-10 17:57:02 +0100650 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
651 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100655 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
657 return( 0 );
658}
659
660/*
Hanno Becker705fc682017-10-10 17:57:02 +0100661 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000664{
Hanno Becker705fc682017-10-10 17:57:02 +0100665 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100666 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 {
Hanno Becker98838b02017-10-02 13:16:10 +0100668 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 }
Paul Bakker48377d92013-08-30 12:06:24 +0200670
Hanno Becker98838b02017-10-02 13:16:10 +0100671 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100672 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100674 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000676
Hanno Beckerb269a852017-08-25 08:03:21 +0100677#if !defined(MBEDTLS_RSA_NO_CRT)
678 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
679 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
680 {
681 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
682 }
683#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000684
685 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000686}
687
688/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100689 * Check if contexts holding a public and private key match
690 */
Hanno Becker98838b02017-10-02 13:16:10 +0100691int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
692 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100693{
Hanno Becker98838b02017-10-02 13:16:10 +0100694 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100698 }
699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
701 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100704 }
705
706 return( 0 );
707}
708
709/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000710 * Do an RSA public key operation
711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000713 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 unsigned char *output )
715{
Janos Follath24eed8d2019-11-22 13:21:35 +0000716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000717 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000719
Hanno Beckerebd2c022017-10-12 10:54:53 +0100720 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100721 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200725#if defined(MBEDTLS_THREADING_C)
726 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
727 return( ret );
728#endif
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200734 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
735 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000736 }
737
738 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
740 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000741
742cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200744 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
745 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100746#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
750 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100751 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
753 return( 0 );
754}
755
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200756/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200757 * Generate or update blinding values, see section 10 of:
758 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200759 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200760 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200761 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200762static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200763 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
764{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200765 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200766 mbedtls_mpi R;
767
768 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200769
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200770 if( ctx->Vf.p != NULL )
771 {
772 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
774 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
775 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
776 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200777
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200778 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200779 }
780
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200781 /* Unblinding value: Vf = random number, invertible mod N */
782 do {
783 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200784 {
785 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
786 goto cleanup;
787 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 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 +0200790
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200791 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200792 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
793 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
794 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
795
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200796 /* At this point, Vi is invertible mod N if and only if both Vf and R
797 * are invertible mod N. If one of them isn't, we don't need to know
798 * which one, we just loop and choose new values for both of them.
799 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200800 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500801 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200802 goto cleanup;
803
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500804 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
805
806 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
807 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
808 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200809
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200810 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200811 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 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 +0200813
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200814
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200815cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200816 mbedtls_mpi_free( &R );
817
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200818 return( ret );
819}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200820
Paul Bakker5121ce52009-01-03 21:22:43 +0000821/*
Janos Follathe81102e2017-03-22 13:38:28 +0000822 * Exponent blinding supposed to prevent side-channel attacks using multiple
823 * traces of measurements to recover the RSA key. The more collisions are there,
824 * the more bits of the key can be recovered. See [3].
825 *
826 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800827 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000828 *
829 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800830 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000831 *
832 * (With the currently (as of 2017 April) known best algorithms breaking 2048
833 * bit RSA requires approximately as much time as trying out 2^112 random keys.
834 * Thus in this sense with 28 byte blinding the security is not reduced by
835 * side-channel attacks like the one in [3])
836 *
837 * This countermeasure does not help if the key recovery is possible with a
838 * single trace.
839 */
840#define RSA_EXPONENT_BLINDING 28
841
842/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000843 * Do an RSA private key operation
844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200846 int (*f_rng)(void *, unsigned char *, size_t),
847 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000848 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 unsigned char *output )
850{
Janos Follath24eed8d2019-11-22 13:21:35 +0000851 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000852 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100853
854 /* Temporary holding the result */
855 mbedtls_mpi T;
856
857 /* Temporaries holding P-1, Q-1 and the
858 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000859 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100860
861#if !defined(MBEDTLS_RSA_NO_CRT)
862 /* Temporaries holding the results mod p resp. mod q. */
863 mbedtls_mpi TP, TQ;
864
865 /* Temporaries holding the blinded exponents for
866 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000867 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100868
869 /* Pointers to actual exponents to be used - either the unblinded
870 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000871 mbedtls_mpi *DP = &ctx->DP;
872 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100873#else
874 /* Temporary holding the blinded exponent (if used). */
875 mbedtls_mpi D_blind;
876
877 /* Pointer to actual exponent to be used - either the unblinded
878 * or the blinded one, depending on the presence of a PRNG. */
879 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100880#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100881
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100882 /* Temporaries holding the initial input and the double
883 * checked result; should be the same in the end. */
884 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000885
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200886 if( f_rng == NULL )
887 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
888
889 if( rsa_check_context( ctx, 1 /* private key checks */,
890 1 /* blinding on */ ) != 0 )
Hanno Beckerebd2c022017-10-12 10:54:53 +0100891 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100892 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100893 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100894
Hanno Becker06811ce2017-05-03 15:10:34 +0100895#if defined(MBEDTLS_THREADING_C)
896 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
897 return( ret );
898#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000899
Hanno Becker06811ce2017-05-03 15:10:34 +0100900 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100901 mbedtls_mpi_init( &T );
902
903 mbedtls_mpi_init( &P1 );
904 mbedtls_mpi_init( &Q1 );
905 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000906
Janos Follathe81102e2017-03-22 13:38:28 +0000907#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200908 mbedtls_mpi_init( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +0000909#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200910 mbedtls_mpi_init( &DP_blind );
911 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000912#endif
913
Hanno Becker06811ce2017-05-03 15:10:34 +0100914#if !defined(MBEDTLS_RSA_NO_CRT)
915 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200916#endif
917
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100918 mbedtls_mpi_init( &I );
919 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100920
921 /* End of MPI initialization */
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
924 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200926 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
927 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 }
929
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100930 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100931
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200932 /*
933 * Blinding
934 * T = T * Vi mod N
935 */
936 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
937 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
938 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000939
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200940 /*
941 * Exponent blinding
942 */
943 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
944 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000945
Janos Follathf9203b42017-03-22 15:13:15 +0000946#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200947 /*
948 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
949 */
950 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
951 f_rng, p_rng ) );
952 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
953 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
954 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000955
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200956 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000957#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200958 /*
959 * DP_blind = ( P - 1 ) * R + DP
960 */
961 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
962 f_rng, p_rng ) );
963 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
964 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
965 &ctx->DP ) );
Janos Follathf9203b42017-03-22 15:13:15 +0000966
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200967 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000968
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200969 /*
970 * DQ_blind = ( Q - 1 ) * R + DQ
971 */
972 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
973 f_rng, p_rng ) );
974 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
975 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
976 &ctx->DQ ) );
Janos Follathf9203b42017-03-22 15:13:15 +0000977
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200978 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000979#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000982 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100983#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200984 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000985 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100987 * TP = input ^ dP mod P
988 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100990
991 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
992 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
994 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100995 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100997 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
998 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
999 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
1001 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001002 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001004 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1005 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001007
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001008 /*
1009 * Unblind
1010 * T = T * Vf mod N
1011 */
1012 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
1013 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
Hanno Becker2dec5e82017-10-03 07:49:52 +01001015 /* Verify the result to prevent glitching attacks. */
1016 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1017 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001018 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001019 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001020 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1021 goto cleanup;
1022 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001023
Paul Bakker5121ce52009-01-03 21:22:43 +00001024 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
1027cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001029 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1030 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001031#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001032
Hanno Becker06811ce2017-05-03 15:10:34 +01001033 mbedtls_mpi_free( &P1 );
1034 mbedtls_mpi_free( &Q1 );
1035 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001036
Janos Follathe81102e2017-03-22 13:38:28 +00001037#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001038 mbedtls_mpi_free( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +00001039#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001040 mbedtls_mpi_free( &DP_blind );
1041 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
Hanno Becker06811ce2017-05-03 15:10:34 +01001044 mbedtls_mpi_free( &T );
1045
1046#if !defined(MBEDTLS_RSA_NO_CRT)
1047 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1048#endif
1049
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001050 mbedtls_mpi_free( &C );
1051 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001052
Gilles Peskineae3741e2020-11-25 00:10:31 +01001053 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001054 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
Gilles Peskineae3741e2020-11-25 00:10:31 +01001056 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001057}
1058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001060/**
1061 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1062 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001063 * \param dst buffer to mask
1064 * \param dlen length of destination buffer
1065 * \param src source of the mask generation
1066 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001067 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001068 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001069static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001070 size_t slen, mbedtls_md_type_t md_alg )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001071{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001072 unsigned char counter[4];
1073 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001074 unsigned int hlen;
1075 size_t i, use_len;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001076 unsigned char mask[MBEDTLS_HASH_MAX_SIZE];
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001077#if defined(MBEDTLS_MD_C)
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001078 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001079 const mbedtls_md_info_t *md_info;
1080 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001081
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001082 mbedtls_md_init( &md_ctx );
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001083 md_info = mbedtls_md_info_from_type( md_alg );
1084 if( md_info == NULL )
1085 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1086
1087 mbedtls_md_init( &md_ctx );
1088 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1089 goto exit;
1090
1091 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001092#else
1093 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
1094 psa_algorithm_t alg = mbedtls_psa_translate_md( md_alg );
1095 psa_status_t status = PSA_SUCCESS;
1096 size_t out_len;
1097
1098 hlen = PSA_HASH_LENGTH( alg );
1099#endif
1100
1101 memset( mask, 0, sizeof( mask ) );
1102 memset( counter, 0, 4 );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001103
Simon Butcher02037452016-03-01 21:19:12 +00001104 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001105 p = dst;
1106
1107 while( dlen > 0 )
1108 {
1109 use_len = hlen;
1110 if( dlen < hlen )
1111 use_len = dlen;
1112
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001113#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001114 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001115 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001116 if( ( ret = mbedtls_md_update( &md_ctx, src, slen ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001117 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001118 if( ( ret = mbedtls_md_update( &md_ctx, counter, 4 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001119 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001120 if( ( ret = mbedtls_md_finish( &md_ctx, mask ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001121 goto exit;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001122#else
1123 if( ( status = psa_hash_setup( &op, alg ) ) != PSA_SUCCESS )
1124 goto exit;
1125 if( ( status = psa_hash_update( &op, src, slen ) ) != PSA_SUCCESS )
1126 goto exit;
1127 if( ( status = psa_hash_update( &op, counter, 4 ) ) != PSA_SUCCESS )
1128 goto exit;
1129 status = psa_hash_finish( &op, mask, sizeof( mask ), &out_len );
1130 if( status != PSA_SUCCESS )
1131 goto exit;
1132#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001133
1134 for( i = 0; i < use_len; ++i )
1135 *p++ ^= mask[i];
1136
1137 counter[3]++;
1138
1139 dlen -= use_len;
1140 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001141
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001142exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001143 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001144#if defined(MBEDTLS_MD_C)
1145 mbedtls_md_free( &md_ctx );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001146
1147 return( ret );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001148#else
1149 psa_hash_abort( &op );
1150
Przemek Stekiel2aae0402022-07-29 11:20:07 +02001151 return( mbedtls_md_error_from_psa( status ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001152#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001153}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001154
1155/**
1156 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1157 *
1158 * \param hash the input hash
1159 * \param hlen length of the input hash
1160 * \param salt the input salt
1161 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001162 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001163 * \param md_alg message digest to use
1164 */
1165static int hash_mprime( const unsigned char *hash, size_t hlen,
1166 const unsigned char *salt, size_t slen,
1167 unsigned char *out, mbedtls_md_type_t md_alg )
1168{
1169 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001170
1171#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001172 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001174
1175 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1176 if( md_info == NULL )
1177 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1178
1179 mbedtls_md_init( &md_ctx );
1180 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1181 goto exit;
1182 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1183 goto exit;
1184 if( ( ret = mbedtls_md_update( &md_ctx, zeros, sizeof( zeros ) ) ) != 0 )
1185 goto exit;
1186 if( ( ret = mbedtls_md_update( &md_ctx, hash, hlen ) ) != 0 )
1187 goto exit;
1188 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1189 goto exit;
1190 if( ( ret = mbedtls_md_finish( &md_ctx, out ) ) != 0 )
1191 goto exit;
1192
1193exit:
1194 mbedtls_md_free( &md_ctx );
1195
1196 return( ret );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001197#else
1198 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
1199 psa_algorithm_t alg = mbedtls_psa_translate_md( md_alg );
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001200 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED ;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001201 size_t out_size = PSA_HASH_LENGTH( alg );
1202 size_t out_len;
1203
1204 if( ( status = psa_hash_setup( &op, alg ) ) != PSA_SUCCESS )
1205 goto exit;
1206 if( ( status = psa_hash_update( &op, zeros, sizeof( zeros ) ) ) != PSA_SUCCESS )
1207 goto exit;
1208 if( ( status = psa_hash_update( &op, hash, hlen ) ) != PSA_SUCCESS )
1209 goto exit;
1210 if( ( status = psa_hash_update( &op, salt, slen ) ) != PSA_SUCCESS )
1211 goto exit;
1212 status = psa_hash_finish( &op, out, out_size, &out_len );
1213 if( status != PSA_SUCCESS )
1214 goto exit;
1215
1216exit:
1217 psa_hash_abort( &op );
1218
Przemek Stekiel2aae0402022-07-29 11:20:07 +02001219 return( mbedtls_md_error_from_psa( status ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001220#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001221}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001222
1223/**
1224 * Compute a hash.
1225 *
1226 * \param md_alg algorithm to use
1227 * \param input input message to hash
1228 * \param ilen input length
1229 * \param output the output buffer - must be large enough for \p md_alg
1230 */
1231static int compute_hash( mbedtls_md_type_t md_alg,
1232 const unsigned char *input, size_t ilen,
1233 unsigned char *output )
1234{
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001235#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001236 const mbedtls_md_info_t *md_info;
1237
1238 md_info = mbedtls_md_info_from_type( md_alg );
1239 if( md_info == NULL )
1240 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1241
1242 return( mbedtls_md( md_info, input, ilen, output ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001243#else
1244 psa_algorithm_t alg = mbedtls_psa_translate_md( md_alg );
1245 psa_status_t status;
1246 size_t out_size = PSA_HASH_LENGTH( alg );
1247 size_t out_len;
1248
1249 status = psa_hash_compute( alg, input, ilen, output, out_size, &out_len );
1250
Przemek Stekiel2aae0402022-07-29 11:20:07 +02001251 return( mbedtls_md_error_from_psa( status ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001252#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001257/*
1258 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001261 int (*f_rng)(void *, unsigned char *, size_t),
1262 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001263 const unsigned char *label, size_t label_len,
1264 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001265 const unsigned char *input,
1266 unsigned char *output )
1267{
1268 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001270 unsigned char *p = output;
1271 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001272
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001273 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001275
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001276 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1277 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001279
1280 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001281
Simon Butcher02037452016-03-01 21:19:12 +00001282 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001283 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001285
1286 memset( output, 0, olen );
1287
1288 *p++ = 0;
1289
Simon Butcher02037452016-03-01 21:19:12 +00001290 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001291 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001292 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001293
1294 p += hlen;
1295
Simon Butcher02037452016-03-01 21:19:12 +00001296 /* Construct DB */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001297 ret = compute_hash( (mbedtls_md_type_t) ctx->hash_id, label, label_len, p );
1298 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001299 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001300 p += hlen;
1301 p += olen - 2 * hlen - 2 - ilen;
1302 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001303 if( ilen != 0 )
1304 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001305
Simon Butcher02037452016-03-01 21:19:12 +00001306 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001307 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001308 ctx->hash_id ) ) != 0 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02001309 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001310
Simon Butcher02037452016-03-01 21:19:12 +00001311 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001312 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001313 ctx->hash_id ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001314 return( ret );
1315
Thomas Daubney141700f2021-05-13 19:06:10 +01001316 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001317}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001321/*
1322 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001325 int (*f_rng)(void *, unsigned char *, size_t),
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001326 void *p_rng, size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001327 const unsigned char *input,
1328 unsigned char *output )
1329{
1330 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001332 unsigned char *p = output;
1333
Paul Bakkerb3869132013-02-28 17:21:01 +01001334 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001335
Simon Butcher02037452016-03-01 21:19:12 +00001336 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001337 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001339
1340 nb_pad = olen - 3 - ilen;
1341
1342 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001343
1344 if( f_rng == NULL )
1345 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1346
1347 *p++ = MBEDTLS_RSA_CRYPT;
1348
1349 while( nb_pad-- > 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001350 {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001351 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001352
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001353 do {
1354 ret = f_rng( p_rng, p, 1 );
1355 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001356
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001357 /* Check if RNG failed to generate data */
1358 if( rng_dl == 0 || ret != 0 )
1359 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001360
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001361 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001362 }
1363
1364 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001365 if( ilen != 0 )
1366 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001367
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001368 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001371
Paul Bakker5121ce52009-01-03 21:22:43 +00001372/*
1373 * Add the message padding, then do an RSA operation
1374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001376 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001377 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +01001378 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001379 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 unsigned char *output )
1381{
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 switch( ctx->padding )
1383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#if defined(MBEDTLS_PKCS1_V15)
1385 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney21772772021-05-13 17:30:32 +01001386 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001387 ilen, input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001388#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_PKCS1_V21)
1391 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney141700f2021-05-13 19:06:10 +01001392 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, NULL, 0,
Thomas Daubney21772772021-05-13 17:30:32 +01001393 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001394#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001395
1396 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001399}
1400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001402/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001403 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001406 int (*f_rng)(void *, unsigned char *, size_t),
1407 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001408 const unsigned char *label, size_t label_len,
1409 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001410 const unsigned char *input,
1411 unsigned char *output,
1412 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001413{
Janos Follath24eed8d2019-11-22 13:21:35 +00001414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001415 size_t ilen, i, pad_len;
1416 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001418 unsigned char lhash[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001419 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001420
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001421 /*
1422 * Parameters sanity checks
1423 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001424 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426
1427 ilen = ctx->len;
1428
Paul Bakker27fdf462011-06-09 13:55:13 +00001429 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001432 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1433 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001435
Janos Follathc17cda12016-02-11 11:08:18 +00001436 // checking for integer underflow
1437 if( 2 * hlen + 2 > ilen )
1438 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1439
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001440 /*
1441 * RSA operation
1442 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001443 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001444
1445 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001446 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001447
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001448 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001449 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001450 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001451 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001452 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001453 ctx->hash_id ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001454 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001455 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001456 ctx->hash_id ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001457 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001458 goto cleanup;
1459 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001460
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001461 /* Generate lHash */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001462 ret = compute_hash( (mbedtls_md_type_t) ctx->hash_id,
1463 label, label_len, lhash );
1464 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001465 goto cleanup;
1466
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001467 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001468 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001469 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001471 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001473 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001474
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001475 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001476
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001477 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001478 for( i = 0; i < hlen; i++ )
1479 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001480
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001481 /* Get zero-padding len, but always read till end of buffer
1482 * (minus one, for the 01 byte) */
1483 pad_len = 0;
1484 pad_done = 0;
1485 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1486 {
1487 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001488 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001489 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001490
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001491 p += pad_len;
1492 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001493
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001494 /*
1495 * The only information "leaked" is whether the padding was correct or not
1496 * (eg, no data is copied if it was not correct). This meets the
1497 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1498 * the different error conditions.
1499 */
1500 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001501 {
1502 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1503 goto cleanup;
1504 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001505
Paul Bakker66d5d072014-06-17 16:39:18 +02001506 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001507 {
1508 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1509 goto cleanup;
1510 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001511
1512 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001513 if( *olen != 0 )
1514 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001515 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001516
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001517cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001518 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1519 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001520
1521 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001522}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001526/*
1527 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1528 */
1529int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
1530 int (*f_rng)(void *, unsigned char *, size_t),
1531 void *p_rng,
1532 size_t *olen,
1533 const unsigned char *input,
1534 unsigned char *output,
1535 size_t output_max_len )
1536{
1537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1538 size_t ilen;
1539 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1540
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001541 ilen = ctx->len;
1542
1543 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1544 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1545
1546 if( ilen < 16 || ilen > sizeof( buf ) )
1547 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1548
1549 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
1550
1551 if( ret != 0 )
1552 goto cleanup;
1553
Gabor Mezei90437e32021-10-20 11:59:27 +02001554 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding( buf, ilen,
Gabor Mezei63bbba52021-10-18 16:17:57 +02001555 output, output_max_len, olen );
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001556
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001557cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001558 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001559
1560 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001563
1564/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001565 * Do an RSA operation, then remove the message padding
1566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001568 int (*f_rng)(void *, unsigned char *, size_t),
1569 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001570 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001571 const unsigned char *input,
1572 unsigned char *output,
1573 size_t output_max_len)
1574{
1575 switch( ctx->padding )
1576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#if defined(MBEDTLS_PKCS1_V15)
1578 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney34733082021-05-12 09:24:29 +01001579 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001580 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001581#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_PKCS1_V21)
1584 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001585 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001586 olen, input, output,
1587 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001588#endif
1589
1590 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001592 }
1593}
1594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001596static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001597 int (*f_rng)(void *, unsigned char *, size_t),
1598 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001600 unsigned int hashlen,
1601 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001602 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001603 unsigned char *sig )
1604{
1605 size_t olen;
1606 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001607 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001608 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001610 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001611
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001612 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
1613 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01001614
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001615 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1616 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1617
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001618 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001620
1621 olen = ctx->len;
1622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001624 {
Simon Butcher02037452016-03-01 21:19:12 +00001625 /* Gather length of hash to sign */
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001626 size_t exp_hashlen = mbedtls_hash_info_get_size( md_alg );
1627 if( exp_hashlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001629
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001630 if( hashlen != exp_hashlen )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001631 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001632 }
1633
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001634 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1635 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001637
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001638 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1639 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001640 /* Calculate the largest possible salt length, up to the hash size.
1641 * Normally this is the hash length, which is the maximum salt length
1642 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001643 * enough room, use the maximum salt length that fits. The constraint is
1644 * that the hash length plus the salt length plus 2 bytes must be at most
1645 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1646 * (PKCS#1 v2.2) §9.1.1 step 3. */
1647 min_slen = hlen - 2;
1648 if( olen < hlen + min_slen + 2 )
1649 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1650 else if( olen >= hlen + hlen + 2 )
1651 slen = hlen;
1652 else
1653 slen = olen - hlen - 2;
1654 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001655 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001658 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001659 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001660 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001661 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001662 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001663
1664 memset( sig, 0, olen );
1665
Simon Butcher02037452016-03-01 21:19:12 +00001666 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001667 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001668 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001669 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001670
1671 /* Generate salt of length slen in place in the encoded message */
1672 salt = p;
1673 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001674 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001675
Paul Bakkerb3869132013-02-28 17:21:01 +01001676 p += slen;
1677
Simon Butcher02037452016-03-01 21:19:12 +00001678 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001679 ret = hash_mprime( hash, hashlen, salt, slen, p, ctx->hash_id );
1680 if( ret != 0 )
1681 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001682
Simon Butcher02037452016-03-01 21:19:12 +00001683 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001684 if( msb % 8 == 0 )
1685 offset = 1;
1686
Simon Butcher02037452016-03-01 21:19:12 +00001687 /* maskedDB: Apply dbMask to DB */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001688 ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1689 ctx->hash_id );
1690 if( ret != 0 )
1691 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001692
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001693 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001694 sig[0] &= 0xFF >> ( olen * 8 - msb );
1695
1696 p += hlen;
1697 *p++ = 0xBC;
1698
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001699 return mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001700}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001701
1702/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001703 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1704 * the option to pass in the salt length.
1705 */
1706int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1707 int (*f_rng)(void *, unsigned char *, size_t),
1708 void *p_rng,
1709 mbedtls_md_type_t md_alg,
1710 unsigned int hashlen,
1711 const unsigned char *hash,
1712 int saltlen,
1713 unsigned char *sig )
1714{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001715 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001716 hashlen, hash, saltlen, sig );
1717}
1718
1719
1720/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001721 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1722 */
1723int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1724 int (*f_rng)(void *, unsigned char *, size_t),
1725 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001726 mbedtls_md_type_t md_alg,
1727 unsigned int hashlen,
1728 const unsigned char *hash,
1729 unsigned char *sig )
1730{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001731 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001732 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001733}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001737/*
1738 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1739 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001740
1741/* Construct a PKCS v1.5 encoding of a hashed message
1742 *
1743 * This is used both for signature generation and verification.
1744 *
1745 * Parameters:
1746 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001747 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001748 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001749 * - hash: Buffer containing the hashed message or the raw data.
1750 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001751 * - dst: Buffer to hold the encoded message.
1752 *
1753 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001754 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001755 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001756 *
1757 */
1758static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1759 unsigned int hashlen,
1760 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001761 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001762 unsigned char *dst )
1763{
1764 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001765 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001766 unsigned char *p = dst;
1767 const char *oid = NULL;
1768
1769 /* Are we signing hashed or raw data? */
1770 if( md_alg != MBEDTLS_MD_NONE )
1771 {
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +02001772 unsigned char md_size = mbedtls_hash_info_get_size( md_alg );
Manuel Pégourié-Gonnardf493f2a2022-07-05 17:41:05 +02001773 if( md_size == 0 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001774 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1775
1776 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1777 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1778
Manuel Pégourié-Gonnardf493f2a2022-07-05 17:41:05 +02001779 if( hashlen != md_size )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001780 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001781
1782 /* Double-check that 8 + hashlen + oid_size can be used as a
1783 * 1-byte ASN.1 length encoding and that there's no overflow. */
1784 if( 8 + hashlen + oid_size >= 0x80 ||
1785 10 + hashlen < hashlen ||
1786 10 + hashlen + oid_size < 10 + hashlen )
1787 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1788
1789 /*
1790 * Static bounds check:
1791 * - Need 10 bytes for five tag-length pairs.
1792 * (Insist on 1-byte length encodings to protect against variants of
1793 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1794 * - Need hashlen bytes for hash
1795 * - Need oid_size bytes for hash alg OID.
1796 */
1797 if( nb_pad < 10 + hashlen + oid_size )
1798 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1799 nb_pad -= 10 + hashlen + oid_size;
1800 }
1801 else
1802 {
1803 if( nb_pad < hashlen )
1804 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1805
1806 nb_pad -= hashlen;
1807 }
1808
Hanno Becker2b2f8982017-09-27 17:10:03 +01001809 /* Need space for signature header and padding delimiter (3 bytes),
1810 * and 8 bytes for the minimal padding */
1811 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001812 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1813 nb_pad -= 3;
1814
1815 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001816 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001817
1818 /* Write signature header and padding */
1819 *p++ = 0;
1820 *p++ = MBEDTLS_RSA_SIGN;
1821 memset( p, 0xFF, nb_pad );
1822 p += nb_pad;
1823 *p++ = 0;
1824
1825 /* Are we signing raw data? */
1826 if( md_alg == MBEDTLS_MD_NONE )
1827 {
1828 memcpy( p, hash, hashlen );
1829 return( 0 );
1830 }
1831
1832 /* Signing hashed data, add corresponding ASN.1 structure
1833 *
1834 * DigestInfo ::= SEQUENCE {
1835 * digestAlgorithm DigestAlgorithmIdentifier,
1836 * digest Digest }
1837 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1838 * Digest ::= OCTET STRING
1839 *
1840 * Schematic:
1841 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1842 * TAG-NULL + LEN [ NULL ] ]
1843 * TAG-OCTET + LEN [ HASH ] ]
1844 */
1845 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001846 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001847 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001848 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001849 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001850 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001851 memcpy( p, oid, oid_size );
1852 p += oid_size;
1853 *p++ = MBEDTLS_ASN1_NULL;
1854 *p++ = 0x00;
1855 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001856 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001857 memcpy( p, hash, hashlen );
1858 p += hashlen;
1859
1860 /* Just a sanity-check, should be automatic
1861 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001862 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001863 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001864 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001865 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1866 }
1867
1868 return( 0 );
1869}
1870
Paul Bakkerb3869132013-02-28 17:21:01 +01001871/*
1872 * Do an RSA operation to sign the message digest
1873 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001875 int (*f_rng)(void *, unsigned char *, size_t),
1876 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001878 unsigned int hashlen,
1879 const unsigned char *hash,
1880 unsigned char *sig )
1881{
Janos Follath24eed8d2019-11-22 13:21:35 +00001882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001883 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001884
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001885 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
1886 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001887
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001888 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1889 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1890
Hanno Beckerfdf38032017-09-06 12:35:55 +01001891 /*
1892 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1893 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001894
Hanno Beckerfdf38032017-09-06 12:35:55 +01001895 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1896 ctx->len, sig ) ) != 0 )
1897 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001898
Hanno Beckerfdf38032017-09-06 12:35:55 +01001899 /* Private key operation
1900 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001901 * In order to prevent Lenstra's attack, make the signature in a
1902 * temporary buffer and check it before returning it.
1903 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001904
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001905 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001906 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001907 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1908
Hanno Beckerfdf38032017-09-06 12:35:55 +01001909 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001910 if( verif == NULL )
1911 {
1912 mbedtls_free( sig_try );
1913 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1914 }
1915
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001916 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1917 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1918
Gabor Mezei90437e32021-10-20 11:59:27 +02001919 if( mbedtls_ct_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001920 {
1921 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1922 goto cleanup;
1923 }
1924
1925 memcpy( sig, sig_try, ctx->len );
1926
1927cleanup:
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001928 mbedtls_platform_zeroize( sig_try, ctx->len );
1929 mbedtls_platform_zeroize( verif, ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001930 mbedtls_free( sig_try );
1931 mbedtls_free( verif );
1932
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001933 if( ret != 0 )
1934 memset( sig, '!', ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001935 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001936}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001938
1939/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 * Do an RSA operation to sign the message digest
1941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001943 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001944 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001946 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001947 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 unsigned char *sig )
1949{
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001950 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
1951 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001952
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 switch( ctx->padding )
1954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955#if defined(MBEDTLS_PKCS1_V15)
1956 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney52654982021-05-18 16:54:00 +01001957 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng,
Thomas Daubney140184d2021-05-18 16:04:07 +01001958 md_alg, hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001959#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961#if defined(MBEDTLS_PKCS1_V21)
1962 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyde9fdc42021-05-18 17:10:04 +01001963 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
1964 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001965#endif
1966
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001970}
1971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001973/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001974 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001978 unsigned int hashlen,
1979 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001981 int expected_salt_len,
1982 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001983{
Janos Follath24eed8d2019-11-22 13:21:35 +00001984 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001985 size_t siglen;
1986 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001987 unsigned char *hash_start;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001988 unsigned char result[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001989 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001990 size_t observed_salt_len, msb;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001991 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = {0};
Paul Bakkerb3869132013-02-28 17:21:01 +01001992
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001993 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
1994 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001995
Paul Bakker5121ce52009-01-03 21:22:43 +00001996 siglen = ctx->len;
1997
Paul Bakker27fdf462011-06-09 13:55:13 +00001998 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002000
Thomas Daubney782a7f52021-05-19 12:27:35 +01002001 ret = mbedtls_rsa_public( ctx, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
2003 if( ret != 0 )
2004 return( ret );
2005
2006 p = buf;
2007
Paul Bakkerb3869132013-02-28 17:21:01 +01002008 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 {
Simon Butcher02037452016-03-01 21:19:12 +00002013 /* Gather length of hash to sign */
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002014 size_t exp_hashlen = mbedtls_hash_info_get_size( md_alg );
2015 if( exp_hashlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002017
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002018 if( hashlen != exp_hashlen )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002019 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002020 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002021
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002022 hlen = mbedtls_hash_info_get_size( mgf1_hash_id );
2023 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002025
Simon Butcher02037452016-03-01 21:19:12 +00002026 /*
2027 * Note: EMSA-PSS verification is over the length of N - 1 bits
2028 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002029 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002030
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002031 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2032 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2033
Simon Butcher02037452016-03-01 21:19:12 +00002034 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002035 if( msb % 8 == 0 )
2036 {
2037 p++;
2038 siglen -= 1;
2039 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002040
Gilles Peskine139108a2017-10-18 19:03:42 +02002041 if( siglen < hlen + 2 )
2042 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2043 hash_start = p + siglen - hlen - 1;
2044
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02002045 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id );
Jaeden Amero66954e12018-01-25 16:05:54 +00002046 if( ret != 0 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002047 return( ret );
Paul Bakker02303e82013-01-03 11:08:31 +01002048
Paul Bakkerb3869132013-02-28 17:21:01 +01002049 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002050
Gilles Peskine6a54b022017-10-17 19:02:13 +02002051 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002052 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002053
Gilles Peskine91048a32017-10-19 17:46:14 +02002054 if( *p++ != 0x01 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002055 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002056
Gilles Peskine6a54b022017-10-17 19:02:13 +02002057 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002060 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002061 {
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002062 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002063 }
2064
Simon Butcher02037452016-03-01 21:19:12 +00002065 /*
2066 * Generate H = Hash( M' )
2067 */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002068 ret = hash_mprime( hash, hashlen, p, observed_salt_len,
2069 result, mgf1_hash_id );
2070 if( ret != 0 )
2071 return( ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002072
Jaeden Amero66954e12018-01-25 16:05:54 +00002073 if( memcmp( hash_start, result, hlen ) != 0 )
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002074 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002075
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002076 return( 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01002077}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002078
2079/*
2080 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002084 unsigned int hashlen,
2085 const unsigned char *hash,
2086 const unsigned char *sig )
2087{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002088 mbedtls_md_type_t mgf1_hash_id;
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002089 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
2090 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002091
2092 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002094 : md_alg;
2095
Thomas Daubney9e65f792021-05-19 12:18:58 +01002096 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx,
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002097 md_alg, hashlen, hash,
2098 mgf1_hash_id,
2099 MBEDTLS_RSA_SALT_LEN_ANY,
2100 sig ) );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002101
2102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002106/*
2107 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002111 unsigned int hashlen,
2112 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002113 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002114{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002115 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002116 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002117 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002118
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002119 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
2120 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002121
2122 sig_len = ctx->len;
2123
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002124 /*
2125 * Prepare expected PKCS1 v1.5 encoding of hash.
2126 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002127
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002128 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2129 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2130 {
2131 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2132 goto cleanup;
2133 }
2134
2135 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2136 encoded_expected ) ) != 0 )
2137 goto cleanup;
2138
2139 /*
2140 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2141 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002142
Thomas Daubney41e4ce42021-05-19 15:10:05 +01002143 ret = mbedtls_rsa_public( ctx, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002144 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002145 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002146
Simon Butcher02037452016-03-01 21:19:12 +00002147 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002148 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002149 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002150
Gabor Mezei90437e32021-10-20 11:59:27 +02002151 if( ( ret = mbedtls_ct_memcmp( encoded, encoded_expected,
gabor-mezei-arm46025642021-07-19 15:19:19 +02002152 sig_len ) ) != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002153 {
2154 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2155 goto cleanup;
2156 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002157
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002158cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002159
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002160 if( encoded != NULL )
2161 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002162 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002163 mbedtls_free( encoded );
2164 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002165
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002166 if( encoded_expected != NULL )
2167 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002168 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002169 mbedtls_free( encoded_expected );
2170 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002171
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002172 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002175
2176/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002177 * Do an RSA operation and check the message digest
2178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002181 unsigned int hashlen,
2182 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002183 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002184{
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002185 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
2186 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002187
Paul Bakkerb3869132013-02-28 17:21:01 +01002188 switch( ctx->padding )
2189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#if defined(MBEDTLS_PKCS1_V15)
2191 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney2e126252021-05-19 11:48:53 +01002192 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, md_alg,
2193 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002194#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196#if defined(MBEDTLS_PKCS1_V21)
2197 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002198 return mbedtls_rsa_rsassa_pss_verify( ctx, md_alg,
2199 hashlen, hash, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01002200#endif
2201
2202 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002204 }
2205}
2206
2207/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002208 * Copy the components of an RSA key
2209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002211{
Janos Follath24eed8d2019-11-22 13:21:35 +00002212 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002213
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002214 dst->len = src->len;
2215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2217 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2220 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2221 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002222
2223#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2225 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2226 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2228 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002229#endif
2230
2231 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2234 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002235
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002236 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002237 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002238
2239cleanup:
2240 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002242
2243 return( ret );
2244}
2245
2246/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 * Free the components of an RSA key
2248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002250{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002251 if( ctx == NULL )
2252 return;
2253
2254 mbedtls_mpi_free( &ctx->Vi );
2255 mbedtls_mpi_free( &ctx->Vf );
2256 mbedtls_mpi_free( &ctx->RN );
2257 mbedtls_mpi_free( &ctx->D );
2258 mbedtls_mpi_free( &ctx->Q );
2259 mbedtls_mpi_free( &ctx->P );
2260 mbedtls_mpi_free( &ctx->E );
2261 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002262
Hanno Becker33c30a02017-08-23 07:00:22 +01002263#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002264 mbedtls_mpi_free( &ctx->RQ );
2265 mbedtls_mpi_free( &ctx->RP );
2266 mbedtls_mpi_free( &ctx->QP );
2267 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002268 mbedtls_mpi_free( &ctx->DP );
2269#endif /* MBEDTLS_RSA_NO_CRT */
2270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002272 /* Free the mutex, but only if it hasn't been freed already. */
2273 if( ctx->ver != 0 )
2274 {
2275 mbedtls_mutex_free( &ctx->mutex );
2276 ctx->ver = 0;
2277 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002279}
2280
Hanno Beckerab377312017-08-23 16:24:51 +01002281#endif /* !MBEDTLS_RSA_ALT */
2282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002284
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002285#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002286
2287/*
2288 * Example RSA-1024 keypair, for test purposes
2289 */
2290#define KEY_LEN 128
2291
2292#define RSA_N "9292758453063D803DD603D5E777D788" \
2293 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2294 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2295 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2296 "93A89813FBF3C4F8066D2D800F7C38A8" \
2297 "1AE31942917403FF4946B0A83D3D3E05" \
2298 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2299 "5E94BB77B07507233A0BC7BAC8F90F79"
2300
2301#define RSA_E "10001"
2302
2303#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2304 "66CA472BC44D253102F8B4A9D3BFA750" \
2305 "91386C0077937FE33FA3252D28855837" \
2306 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2307 "DF79C5CE07EE72C7F123142198164234" \
2308 "CABB724CF78B8173B9F880FC86322407" \
2309 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2310 "071513A1E85B5DFA031F21ECAE91A34D"
2311
2312#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2313 "2C01CAD19EA484A87EA4377637E75500" \
2314 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2315 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2316
2317#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2318 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2319 "910E4168387E3C30AA1E00C339A79508" \
2320 "8452DD96A9A5EA5D9DCA68DA636032AF"
2321
Paul Bakker5121ce52009-01-03 21:22:43 +00002322#define PT_LEN 24
2323#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2324 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002327static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002328{
gufe44c2620da2020-08-03 17:56:50 +02002329#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002330 size_t i;
2331
Paul Bakker545570e2010-07-18 09:00:25 +00002332 if( rng_state != NULL )
2333 rng_state = NULL;
2334
Paul Bakkera3d195c2011-11-27 21:07:34 +00002335 for( i = 0; i < len; ++i )
2336 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002337#else
2338 if( rng_state != NULL )
2339 rng_state = NULL;
2340
2341 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002342#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002343
Paul Bakkera3d195c2011-11-27 21:07:34 +00002344 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002345}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002347
Paul Bakker5121ce52009-01-03 21:22:43 +00002348/*
2349 * Checkup routine
2350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002352{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002353 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002355 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 unsigned char rsa_plaintext[PT_LEN];
2358 unsigned char rsa_decrypted[PT_LEN];
2359 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002361 unsigned char sha1sum[20];
2362#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
Hanno Becker3a701162017-08-22 13:52:43 +01002364 mbedtls_mpi K;
2365
2366 mbedtls_mpi_init( &K );
Ronald Cronc1905a12021-06-05 11:11:14 +02002367 mbedtls_rsa_init( &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
Hanno Becker3a701162017-08-22 13:52:43 +01002369 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2370 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2371 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2372 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2373 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2374 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2375 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2376 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2377 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2378 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2379
Hanno Becker7f25f852017-10-10 16:56:22 +01002380 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
2382 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2386 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 {
2388 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002390
Hanno Becker5bc87292017-05-03 15:09:31 +01002391 ret = 1;
2392 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 }
2394
2395 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
2398 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2399
Thomas Daubney21772772021-05-13 17:30:32 +01002400 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002401 PT_LEN, rsa_plaintext,
2402 rsa_ciphertext ) != 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 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002414 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002415 &len, rsa_ciphertext, rsa_decrypted,
2416 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 {
2418 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
Hanno Becker5bc87292017-05-03 15:09:31 +01002421 ret = 1;
2422 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 }
2424
2425 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2426 {
2427 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Hanno Becker5bc87292017-05-03 15:09:31 +01002430 ret = 1;
2431 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 }
2433
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002434 if( verbose != 0 )
2435 mbedtls_printf( "passed\n" );
2436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002438 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002439 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
TRodziewicz26371e42021-06-08 16:45:41 +02002441 if( mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002442 {
2443 if( verbose != 0 )
2444 mbedtls_printf( "failed\n" );
2445
2446 return( 1 );
2447 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Hanno Becker98838b02017-10-02 13:16:10 +01002449 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002450 MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002451 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 {
2453 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002455
Hanno Becker5bc87292017-05-03 15:09:31 +01002456 ret = 1;
2457 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 }
2459
2460 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002463 if( mbedtls_rsa_pkcs1_verify( &rsa, MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002464 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 {
2466 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Hanno Becker5bc87292017-05-03 15:09:31 +01002469 ret = 1;
2470 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 }
2472
2473 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002474 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002477 if( verbose != 0 )
2478 mbedtls_printf( "\n" );
2479
Paul Bakker3d8fb632014-04-17 12:42:41 +02002480cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002481 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 mbedtls_rsa_free( &rsa );
2483#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002484 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002486 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002487}
2488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#endif /* MBEDTLS_RSA_C */