blob: 4df240abf3ee134bdfdfaa8ae39a4b873d22b129 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010068#else
Rich Evans00ab4702015-02-06 13:43:58 +000069#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020071#define mbedtls_calloc calloc
72#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010073#endif
74
Hanno Beckera565f542017-10-11 11:00:19 +010075#if !defined(MBEDTLS_RSA_ALT)
76
Hanno Becker617c1ae2017-08-23 14:11:24 +010077int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
78 const mbedtls_mpi *N,
79 const mbedtls_mpi *P, const mbedtls_mpi *Q,
80 const mbedtls_mpi *D, const mbedtls_mpi *E )
81{
Janos Follath24eed8d2019-11-22 13:21:35 +000082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +010083
84 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
85 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
86 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
87 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
88 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
89 {
Chris Jonesb7d02e02021-04-01 17:40:03 +010090 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +010091 }
92
93 if( N != NULL )
94 ctx->len = mbedtls_mpi_size( &ctx->N );
95
96 return( 0 );
97}
98
99int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100100 unsigned char const *N, size_t N_len,
101 unsigned char const *P, size_t P_len,
102 unsigned char const *Q, size_t Q_len,
103 unsigned char const *D, size_t D_len,
104 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100105{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000106 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100107
108 if( N != NULL )
109 {
110 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
111 ctx->len = mbedtls_mpi_size( &ctx->N );
112 }
113
114 if( P != NULL )
115 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
116
117 if( Q != NULL )
118 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
119
120 if( D != NULL )
121 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
122
123 if( E != NULL )
124 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
125
126cleanup:
127
128 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100129 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100130
131 return( 0 );
132}
133
Hanno Becker705fc682017-10-10 17:57:02 +0100134/*
135 * Checks whether the context fields are set in such a way
136 * that the RSA primitives will be able to execute without error.
137 * It does *not* make guarantees for consistency of the parameters.
138 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100139static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
140 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100141{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100142#if !defined(MBEDTLS_RSA_NO_CRT)
143 /* blinding_needed is only used for NO_CRT to decide whether
144 * P,Q need to be present or not. */
145 ((void) blinding_needed);
146#endif
147
Hanno Becker3a760a12018-01-05 08:14:49 +0000148 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
149 ctx->len > MBEDTLS_MPI_MAX_SIZE )
150 {
Hanno Becker705fc682017-10-10 17:57:02 +0100151 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000152 }
Hanno Becker705fc682017-10-10 17:57:02 +0100153
154 /*
155 * 1. Modular exponentiation needs positive, odd moduli.
156 */
157
158 /* Modular exponentiation wrt. N is always used for
159 * RSA public key operations. */
160 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
161 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
162 {
163 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
164 }
165
166#if !defined(MBEDTLS_RSA_NO_CRT)
167 /* Modular exponentiation for P and Q is only
168 * used for private key operations and if CRT
169 * is used. */
170 if( is_priv &&
171 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
172 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
173 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
174 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
175 {
176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
177 }
178#endif /* !MBEDTLS_RSA_NO_CRT */
179
180 /*
181 * 2. Exponents must be positive
182 */
183
184 /* Always need E for public key operations */
185 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
186 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
187
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100188#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100189 /* For private key operations, use D or DP & DQ
190 * as (unblinded) exponents. */
191 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
192 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
193#else
194 if( is_priv &&
195 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
196 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
197 {
198 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
199 }
200#endif /* MBEDTLS_RSA_NO_CRT */
201
202 /* Blinding shouldn't make exponents negative either,
203 * so check that P, Q >= 1 if that hasn't yet been
204 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100205#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100206 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100207 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
208 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
209 {
210 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
211 }
212#endif
213
214 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100215 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100216#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100217 if( is_priv &&
218 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
219 {
220 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
221 }
222#endif
223
224 return( 0 );
225}
226
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100227int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100228{
229 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500230 int have_N, have_P, have_Q, have_D, have_E;
231#if !defined(MBEDTLS_RSA_NO_CRT)
232 int have_DP, have_DQ, have_QP;
233#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500234 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100235
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500236 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
237 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
238 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
239 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
240 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500241
242#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500243 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
244 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
245 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500246#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100247
Hanno Becker617c1ae2017-08-23 14:11:24 +0100248 /*
249 * Check whether provided parameters are enough
250 * to deduce all others. The following incomplete
251 * parameter sets for private keys are supported:
252 *
253 * (1) P, Q missing.
254 * (2) D and potentially N missing.
255 *
256 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100257
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500258 n_missing = have_P && have_Q && have_D && have_E;
259 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
260 d_missing = have_P && have_Q && !have_D && have_E;
261 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100262
263 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500264 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100265
Hanno Becker617c1ae2017-08-23 14:11:24 +0100266 if( !is_priv && !is_pub )
267 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
268
269 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100270 * Step 1: Deduce N if P, Q are provided.
271 */
272
273 if( !have_N && have_P && have_Q )
274 {
275 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
276 &ctx->Q ) ) != 0 )
277 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100278 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100279 }
280
281 ctx->len = mbedtls_mpi_size( &ctx->N );
282 }
283
284 /*
285 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100286 */
287
288 if( pq_missing )
289 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100290 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100291 &ctx->P, &ctx->Q );
292 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100293 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100294
295 }
296 else if( d_missing )
297 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100298 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
299 &ctx->Q,
300 &ctx->E,
301 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100302 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100303 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100304 }
305 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100306
Hanno Becker617c1ae2017-08-23 14:11:24 +0100307 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100308 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100309 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100310 */
311
Hanno Becker23344b52017-08-23 07:43:27 +0100312#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500313 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100314 {
315 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
316 &ctx->DP, &ctx->DQ, &ctx->QP );
317 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100318 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100319 }
Hanno Becker23344b52017-08-23 07:43:27 +0100320#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100321
322 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100323 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324 */
325
Hanno Beckerebd2c022017-10-12 10:54:53 +0100326 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327}
328
Hanno Becker617c1ae2017-08-23 14:11:24 +0100329int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
330 unsigned char *N, size_t N_len,
331 unsigned char *P, size_t P_len,
332 unsigned char *Q, size_t Q_len,
333 unsigned char *D, size_t D_len,
334 unsigned char *E, size_t E_len )
335{
336 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500337 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100338
339 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500340 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100341 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
342 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
343 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
344 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
345 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
346
347 if( !is_priv )
348 {
349 /* If we're trying to export private parameters for a public key,
350 * something must be wrong. */
351 if( P != NULL || Q != NULL || D != NULL )
352 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
353
354 }
355
356 if( N != NULL )
357 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
358
359 if( P != NULL )
360 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
361
362 if( Q != NULL )
363 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
364
365 if( D != NULL )
366 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
367
368 if( E != NULL )
369 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100370
371cleanup:
372
373 return( ret );
374}
375
Hanno Becker617c1ae2017-08-23 14:11:24 +0100376int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
377 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
378 mbedtls_mpi *D, mbedtls_mpi *E )
379{
Janos Follath24eed8d2019-11-22 13:21:35 +0000380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500381 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100382
383 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500384 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100385 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
386 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
387 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
388 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
389 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
390
391 if( !is_priv )
392 {
393 /* If we're trying to export private parameters for a public key,
394 * something must be wrong. */
395 if( P != NULL || Q != NULL || D != NULL )
396 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
397
398 }
399
400 /* Export all requested core parameters. */
401
402 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
403 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
404 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
405 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
406 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
407 {
408 return( ret );
409 }
410
411 return( 0 );
412}
413
414/*
415 * Export CRT parameters
416 * This must also be implemented if CRT is not used, for being able to
417 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
418 * can be used in this case.
419 */
420int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
421 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
422{
Janos Follath24eed8d2019-11-22 13:21:35 +0000423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500424 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100425
426 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500427 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100428 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
429 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
430 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
431 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
432 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
433
434 if( !is_priv )
435 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
436
Hanno Beckerdc95c892017-08-23 06:57:02 +0100437#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100438 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100439 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
440 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
441 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
442 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100443 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100445#else
446 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
447 DP, DQ, QP ) ) != 0 )
448 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100449 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100450 }
451#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100452
453 return( 0 );
454}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100455
Paul Bakker5121ce52009-01-03 21:22:43 +0000456/*
457 * Initialize an RSA context
458 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200459void mbedtls_rsa_init( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000460{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
Ronald Cronc1905a12021-06-05 11:11:14 +0200463 ctx->padding = MBEDTLS_RSA_PKCS_V15;
464 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100467 /* Set ctx->ver to nonzero to indicate that the mutex has been
468 * initialized and will need to be freed. */
469 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200471#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000472}
473
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100474/*
475 * Set padding for an existing RSA context
476 */
Ronald Cronea7631b2021-06-03 18:51:59 +0200477int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
478 mbedtls_md_type_t hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100479{
Ronald Cron3a0375f2021-06-08 10:22:28 +0200480 switch( padding )
481 {
482#if defined(MBEDTLS_PKCS1_V15)
483 case MBEDTLS_RSA_PKCS_V15:
484 break;
485#endif
486
487#if defined(MBEDTLS_PKCS1_V21)
488 case MBEDTLS_RSA_PKCS_V21:
489 break;
490#endif
491 default:
492 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
493 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200494
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200495#if defined(MBEDTLS_PKCS1_V21)
Ronald Cronea7631b2021-06-03 18:51:59 +0200496 if( ( padding == MBEDTLS_RSA_PKCS_V21 ) &&
497 ( hash_id != MBEDTLS_MD_NONE ) )
498 {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200499 /* Just make sure this hash is supported in this build. */
Przemek Stekiel712bb9c2022-07-29 11:12:00 +0200500 if( mbedtls_hash_info_psa_from_md( hash_id ) == PSA_ALG_NONE )
Ronald Cronea7631b2021-06-03 18:51:59 +0200501 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
502 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200503#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500504
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100505 ctx->padding = padding;
506 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200507
508 return( 0 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100509}
510
Hanno Becker617c1ae2017-08-23 14:11:24 +0100511/*
512 * Get length in bytes of RSA modulus
513 */
514
515size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
516{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100517 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100518}
519
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523/*
524 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800525 *
526 * This generation method follows the RSA key pair generation procedure of
527 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000530 int (*f_rng)(void *, unsigned char *, size_t),
531 void *p_rng,
532 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000533{
Janos Follath24eed8d2019-11-22 13:21:35 +0000534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800535 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100536 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
Janos Follathb8fc1b02018-09-03 15:37:01 +0100538 /*
539 * If the modulus is 1024 bit long or shorter, then the security strength of
540 * the RSA algorithm is less than or equal to 80 bits and therefore an error
541 * rate of 2^-80 is sufficient.
542 */
543 if( nbits > 1024 )
544 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
545
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100546 mbedtls_mpi_init( &H );
547 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800548 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100550 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
551 {
552 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
553 goto cleanup;
554 }
555
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 /*
557 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800558 * 1. |P-Q| > 2^( nbits / 2 - 100 )
559 * 2. GCD( E, (P-1)*(Q-1) ) == 1
560 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 do
565 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100566 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
567 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
Janos Follathb8fc1b02018-09-03 15:37:01 +0100569 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
570 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800572 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
573 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
574 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 continue;
576
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800577 /* not required by any standards, but some users rely on the fact that P > Q */
578 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100579 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100580
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100581 /* Temporarily replace P,Q by P-1, Q-1 */
582 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
583 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
584 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800585
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800586 /* 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 +0200587 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800588 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
589 continue;
590
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800591 /* 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 -0800592 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
593 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
594 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
595
596 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
597 continue;
598
599 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800601 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100603 /* Restore P,Q */
604 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
605 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
606
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800607 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
608
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100609 ctx->len = mbedtls_mpi_size( &ctx->N );
610
Jethro Beekman97f95c92018-02-13 15:50:36 -0800611#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 * DP = D mod (P - 1)
614 * DQ = D mod (Q - 1)
615 * QP = Q^-1 mod P
616 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100617 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
618 &ctx->DP, &ctx->DQ, &ctx->QP ) );
619#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Hanno Becker83aad1f2017-08-23 06:45:10 +0100621 /* Double-check */
622 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
624cleanup:
625
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100626 mbedtls_mpi_free( &H );
627 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800628 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630 if( ret != 0 )
631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100633
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100634 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100635 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100636 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 }
638
Paul Bakker48377d92013-08-30 12:06:24 +0200639 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640}
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
644/*
645 * Check a public RSA key
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000648{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100649 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000651
Hanno Becker3a760a12018-01-05 08:14:49 +0000652 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
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
Hanno Becker705fc682017-10-10 17:57:02 +0100657 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
658 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100662 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
664 return( 0 );
665}
666
667/*
Hanno Becker705fc682017-10-10 17:57:02 +0100668 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000671{
Hanno Becker705fc682017-10-10 17:57:02 +0100672 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100673 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000674 {
Hanno Becker98838b02017-10-02 13:16:10 +0100675 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 }
Paul Bakker48377d92013-08-30 12:06:24 +0200677
Hanno Becker98838b02017-10-02 13:16:10 +0100678 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100679 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100681 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000683
Hanno Beckerb269a852017-08-25 08:03:21 +0100684#if !defined(MBEDTLS_RSA_NO_CRT)
685 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
686 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
687 {
688 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
689 }
690#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000691
692 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000693}
694
695/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696 * Check if contexts holding a public and private key match
697 */
Hanno Becker98838b02017-10-02 13:16:10 +0100698int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
699 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100700{
Hanno Becker98838b02017-10-02 13:16:10 +0100701 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100705 }
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
708 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100711 }
712
713 return( 0 );
714}
715
716/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 * Do an RSA public key operation
718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000720 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 unsigned char *output )
722{
Janos Follath24eed8d2019-11-22 13:21:35 +0000723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000724 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000726
Hanno Beckerebd2c022017-10-12 10:54:53 +0100727 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100728 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200732#if defined(MBEDTLS_THREADING_C)
733 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
734 return( ret );
735#endif
736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200741 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
742 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000743 }
744
745 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
747 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000748
749cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200751 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
752 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100753#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
757 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100758 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
760 return( 0 );
761}
762
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200763/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200764 * Generate or update blinding values, see section 10 of:
765 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200766 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200767 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200768 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200769static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200770 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
771{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200772 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200773 mbedtls_mpi R;
774
775 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200776
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200777 if( ctx->Vf.p != NULL )
778 {
779 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
781 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
782 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
783 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200784
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200785 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200786 }
787
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200788 /* Unblinding value: Vf = random number, invertible mod N */
789 do {
790 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200791 {
792 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
793 goto cleanup;
794 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 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 +0200797
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200798 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200799 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
800 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
801 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
802
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200803 /* At this point, Vi is invertible mod N if and only if both Vf and R
804 * are invertible mod N. If one of them isn't, we don't need to know
805 * which one, we just loop and choose new values for both of them.
806 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200807 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500808 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200809 goto cleanup;
810
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500811 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
812
813 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
814 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
815 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200816
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200817 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200818 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 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 +0200820
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200821
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200822cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200823 mbedtls_mpi_free( &R );
824
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200825 return( ret );
826}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200827
Paul Bakker5121ce52009-01-03 21:22:43 +0000828/*
Janos Follathe81102e2017-03-22 13:38:28 +0000829 * Exponent blinding supposed to prevent side-channel attacks using multiple
830 * traces of measurements to recover the RSA key. The more collisions are there,
831 * the more bits of the key can be recovered. See [3].
832 *
833 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800834 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000835 *
836 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800837 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000838 *
839 * (With the currently (as of 2017 April) known best algorithms breaking 2048
840 * bit RSA requires approximately as much time as trying out 2^112 random keys.
841 * Thus in this sense with 28 byte blinding the security is not reduced by
842 * side-channel attacks like the one in [3])
843 *
844 * This countermeasure does not help if the key recovery is possible with a
845 * single trace.
846 */
847#define RSA_EXPONENT_BLINDING 28
848
849/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000850 * Do an RSA private key operation
851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200853 int (*f_rng)(void *, unsigned char *, size_t),
854 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000855 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 unsigned char *output )
857{
Janos Follath24eed8d2019-11-22 13:21:35 +0000858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000859 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100860
861 /* Temporary holding the result */
862 mbedtls_mpi T;
863
864 /* Temporaries holding P-1, Q-1 and the
865 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000866 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100867
868#if !defined(MBEDTLS_RSA_NO_CRT)
869 /* Temporaries holding the results mod p resp. mod q. */
870 mbedtls_mpi TP, TQ;
871
872 /* Temporaries holding the blinded exponents for
873 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000874 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100875
876 /* Pointers to actual exponents to be used - either the unblinded
877 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000878 mbedtls_mpi *DP = &ctx->DP;
879 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100880#else
881 /* Temporary holding the blinded exponent (if used). */
882 mbedtls_mpi D_blind;
883
884 /* Pointer to actual exponent to be used - either the unblinded
885 * or the blinded one, depending on the presence of a PRNG. */
886 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100887#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100888
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100889 /* Temporaries holding the initial input and the double
890 * checked result; should be the same in the end. */
891 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200893 if( f_rng == NULL )
894 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
895
896 if( rsa_check_context( ctx, 1 /* private key checks */,
897 1 /* blinding on */ ) != 0 )
Hanno Beckerebd2c022017-10-12 10:54:53 +0100898 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100899 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100900 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100901
Hanno Becker06811ce2017-05-03 15:10:34 +0100902#if defined(MBEDTLS_THREADING_C)
903 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
904 return( ret );
905#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000906
Hanno Becker06811ce2017-05-03 15:10:34 +0100907 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100908 mbedtls_mpi_init( &T );
909
910 mbedtls_mpi_init( &P1 );
911 mbedtls_mpi_init( &Q1 );
912 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000913
Janos Follathe81102e2017-03-22 13:38:28 +0000914#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200915 mbedtls_mpi_init( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +0000916#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200917 mbedtls_mpi_init( &DP_blind );
918 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000919#endif
920
Hanno Becker06811ce2017-05-03 15:10:34 +0100921#if !defined(MBEDTLS_RSA_NO_CRT)
922 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200923#endif
924
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100925 mbedtls_mpi_init( &I );
926 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100927
928 /* End of MPI initialization */
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
931 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000932 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200933 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
934 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 }
936
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100937 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100938
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200939 /*
940 * Blinding
941 * T = T * Vi mod N
942 */
943 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
944 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
945 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000946
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200947 /*
948 * Exponent blinding
949 */
950 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
951 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000952
Janos Follathf9203b42017-03-22 15:13:15 +0000953#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200954 /*
955 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
956 */
957 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
958 f_rng, p_rng ) );
959 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
960 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
961 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000962
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200963 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000964#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200965 /*
966 * DP_blind = ( P - 1 ) * R + DP
967 */
968 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
969 f_rng, p_rng ) );
970 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
971 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
972 &ctx->DP ) );
Janos Follathf9203b42017-03-22 15:13:15 +0000973
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200974 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000975
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200976 /*
977 * DQ_blind = ( Q - 1 ) * R + DQ
978 */
979 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
980 f_rng, p_rng ) );
981 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
982 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
983 &ctx->DQ ) );
Janos Follathf9203b42017-03-22 15:13:15 +0000984
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200985 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000986#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000989 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100990#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200991 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000992 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100994 * TP = input ^ dP mod P
995 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100997
998 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
999 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
1001 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001002 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001004 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1005 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1006 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
1008 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001009 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001011 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1012 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001014
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001015 /*
1016 * Unblind
1017 * T = T * Vf mod N
1018 */
1019 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
1020 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Hanno Becker2dec5e82017-10-03 07:49:52 +01001022 /* Verify the result to prevent glitching attacks. */
1023 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1024 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001025 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001026 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001027 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1028 goto cleanup;
1029 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001030
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033
1034cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001036 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1037 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001038#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001039
Hanno Becker06811ce2017-05-03 15:10:34 +01001040 mbedtls_mpi_free( &P1 );
1041 mbedtls_mpi_free( &Q1 );
1042 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001043
Janos Follathe81102e2017-03-22 13:38:28 +00001044#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001045 mbedtls_mpi_free( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +00001046#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001047 mbedtls_mpi_free( &DP_blind );
1048 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
Hanno Becker06811ce2017-05-03 15:10:34 +01001051 mbedtls_mpi_free( &T );
1052
1053#if !defined(MBEDTLS_RSA_NO_CRT)
1054 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1055#endif
1056
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001057 mbedtls_mpi_free( &C );
1058 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001059
Gilles Peskineae3741e2020-11-25 00:10:31 +01001060 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001061 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
Gilles Peskineae3741e2020-11-25 00:10:31 +01001063 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001064}
1065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001067/**
1068 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1069 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001070 * \param dst buffer to mask
1071 * \param dlen length of destination buffer
1072 * \param src source of the mask generation
1073 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001074 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001075 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001076static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001077 size_t slen, mbedtls_md_type_t md_alg )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001078{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001079 unsigned char counter[4];
1080 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001081 unsigned int hlen;
1082 size_t i, use_len;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001083 unsigned char mask[MBEDTLS_HASH_MAX_SIZE];
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001084#if defined(MBEDTLS_MD_C)
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001085 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001086 const mbedtls_md_info_t *md_info;
1087 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001088
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001089 mbedtls_md_init( &md_ctx );
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001090 md_info = mbedtls_md_info_from_type( md_alg );
1091 if( md_info == NULL )
1092 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1093
1094 mbedtls_md_init( &md_ctx );
1095 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1096 goto exit;
1097
1098 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001099#else
1100 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
1101 psa_algorithm_t alg = mbedtls_psa_translate_md( md_alg );
1102 psa_status_t status = PSA_SUCCESS;
1103 size_t out_len;
1104
1105 hlen = PSA_HASH_LENGTH( alg );
1106#endif
1107
1108 memset( mask, 0, sizeof( mask ) );
1109 memset( counter, 0, 4 );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001110
Simon Butcher02037452016-03-01 21:19:12 +00001111 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001112 p = dst;
1113
1114 while( dlen > 0 )
1115 {
1116 use_len = hlen;
1117 if( dlen < hlen )
1118 use_len = dlen;
1119
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001120#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001121 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001122 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001123 if( ( ret = mbedtls_md_update( &md_ctx, src, slen ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001124 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001125 if( ( ret = mbedtls_md_update( &md_ctx, counter, 4 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001126 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001127 if( ( ret = mbedtls_md_finish( &md_ctx, mask ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001128 goto exit;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001129#else
1130 if( ( status = psa_hash_setup( &op, alg ) ) != PSA_SUCCESS )
1131 goto exit;
1132 if( ( status = psa_hash_update( &op, src, slen ) ) != PSA_SUCCESS )
1133 goto exit;
1134 if( ( status = psa_hash_update( &op, counter, 4 ) ) != PSA_SUCCESS )
1135 goto exit;
1136 status = psa_hash_finish( &op, mask, sizeof( mask ), &out_len );
1137 if( status != PSA_SUCCESS )
1138 goto exit;
1139#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001140
1141 for( i = 0; i < use_len; ++i )
1142 *p++ ^= mask[i];
1143
1144 counter[3]++;
1145
1146 dlen -= use_len;
1147 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001148
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001149exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001150 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001151#if defined(MBEDTLS_MD_C)
1152 mbedtls_md_free( &md_ctx );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001153
1154 return( ret );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001155#else
1156 psa_hash_abort( &op );
1157
Przemek Stekiel2aae0402022-07-29 11:20:07 +02001158 return( mbedtls_md_error_from_psa( status ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001159#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001160}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001161
1162/**
1163 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1164 *
1165 * \param hash the input hash
1166 * \param hlen length of the input hash
1167 * \param salt the input salt
1168 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001169 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001170 * \param md_alg message digest to use
1171 */
1172static int hash_mprime( const unsigned char *hash, size_t hlen,
1173 const unsigned char *salt, size_t slen,
1174 unsigned char *out, mbedtls_md_type_t md_alg )
1175{
1176 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001177
1178#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001179 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001181
1182 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1183 if( md_info == NULL )
1184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1185
1186 mbedtls_md_init( &md_ctx );
1187 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1188 goto exit;
1189 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1190 goto exit;
1191 if( ( ret = mbedtls_md_update( &md_ctx, zeros, sizeof( zeros ) ) ) != 0 )
1192 goto exit;
1193 if( ( ret = mbedtls_md_update( &md_ctx, hash, hlen ) ) != 0 )
1194 goto exit;
1195 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1196 goto exit;
1197 if( ( ret = mbedtls_md_finish( &md_ctx, out ) ) != 0 )
1198 goto exit;
1199
1200exit:
1201 mbedtls_md_free( &md_ctx );
1202
1203 return( ret );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001204#else
1205 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
1206 psa_algorithm_t alg = mbedtls_psa_translate_md( md_alg );
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001207 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED ;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001208 size_t out_size = PSA_HASH_LENGTH( alg );
1209 size_t out_len;
1210
1211 if( ( status = psa_hash_setup( &op, alg ) ) != PSA_SUCCESS )
1212 goto exit;
1213 if( ( status = psa_hash_update( &op, zeros, sizeof( zeros ) ) ) != PSA_SUCCESS )
1214 goto exit;
1215 if( ( status = psa_hash_update( &op, hash, hlen ) ) != PSA_SUCCESS )
1216 goto exit;
1217 if( ( status = psa_hash_update( &op, salt, slen ) ) != PSA_SUCCESS )
1218 goto exit;
1219 status = psa_hash_finish( &op, out, out_size, &out_len );
1220 if( status != PSA_SUCCESS )
1221 goto exit;
1222
1223exit:
1224 psa_hash_abort( &op );
1225
Przemek Stekiel2aae0402022-07-29 11:20:07 +02001226 return( mbedtls_md_error_from_psa( status ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001227#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001228}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001229
1230/**
1231 * Compute a hash.
1232 *
1233 * \param md_alg algorithm to use
1234 * \param input input message to hash
1235 * \param ilen input length
1236 * \param output the output buffer - must be large enough for \p md_alg
1237 */
1238static int compute_hash( mbedtls_md_type_t md_alg,
1239 const unsigned char *input, size_t ilen,
1240 unsigned char *output )
1241{
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001242#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001243 const mbedtls_md_info_t *md_info;
1244
1245 md_info = mbedtls_md_info_from_type( md_alg );
1246 if( md_info == NULL )
1247 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1248
1249 return( mbedtls_md( md_info, input, ilen, output ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001250#else
1251 psa_algorithm_t alg = mbedtls_psa_translate_md( md_alg );
1252 psa_status_t status;
1253 size_t out_size = PSA_HASH_LENGTH( alg );
1254 size_t out_len;
1255
1256 status = psa_hash_compute( alg, input, ilen, output, out_size, &out_len );
1257
Przemek Stekiel2aae0402022-07-29 11:20:07 +02001258 return( mbedtls_md_error_from_psa( status ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001259#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001260}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001264/*
1265 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001268 int (*f_rng)(void *, unsigned char *, size_t),
1269 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001270 const unsigned char *label, size_t label_len,
1271 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001272 const unsigned char *input,
1273 unsigned char *output )
1274{
1275 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001277 unsigned char *p = output;
1278 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001279
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001280 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001282
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001283 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1284 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001286
1287 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001288
Simon Butcher02037452016-03-01 21:19:12 +00001289 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001290 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001292
1293 memset( output, 0, olen );
1294
1295 *p++ = 0;
1296
Simon Butcher02037452016-03-01 21:19:12 +00001297 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001298 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001299 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001300
1301 p += hlen;
1302
Simon Butcher02037452016-03-01 21:19:12 +00001303 /* Construct DB */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001304 ret = compute_hash( (mbedtls_md_type_t) ctx->hash_id, label, label_len, p );
1305 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001306 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001307 p += hlen;
1308 p += olen - 2 * hlen - 2 - ilen;
1309 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001310 if( ilen != 0 )
1311 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001312
Simon Butcher02037452016-03-01 21:19:12 +00001313 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001314 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001315 ctx->hash_id ) ) != 0 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02001316 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001317
Simon Butcher02037452016-03-01 21:19:12 +00001318 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001319 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001320 ctx->hash_id ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001321 return( ret );
1322
Thomas Daubney141700f2021-05-13 19:06:10 +01001323 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001328/*
1329 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001332 int (*f_rng)(void *, unsigned char *, size_t),
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001333 void *p_rng, size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001334 const unsigned char *input,
1335 unsigned char *output )
1336{
1337 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001339 unsigned char *p = output;
1340
Paul Bakkerb3869132013-02-28 17:21:01 +01001341 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001342
Simon Butcher02037452016-03-01 21:19:12 +00001343 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001344 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001346
1347 nb_pad = olen - 3 - ilen;
1348
1349 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001350
1351 if( f_rng == NULL )
1352 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1353
1354 *p++ = MBEDTLS_RSA_CRYPT;
1355
1356 while( nb_pad-- > 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001357 {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001358 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001359
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001360 do {
1361 ret = f_rng( p_rng, p, 1 );
1362 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001363
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001364 /* Check if RNG failed to generate data */
1365 if( rng_dl == 0 || ret != 0 )
1366 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001367
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001368 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001369 }
1370
1371 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001372 if( ilen != 0 )
1373 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001374
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001375 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001378
Paul Bakker5121ce52009-01-03 21:22:43 +00001379/*
1380 * Add the message padding, then do an RSA operation
1381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001383 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001384 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +01001385 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001386 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 unsigned char *output )
1388{
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 switch( ctx->padding )
1390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#if defined(MBEDTLS_PKCS1_V15)
1392 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney21772772021-05-13 17:30:32 +01001393 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001394 ilen, input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001395#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#if defined(MBEDTLS_PKCS1_V21)
1398 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney141700f2021-05-13 19:06:10 +01001399 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, NULL, 0,
Thomas Daubney21772772021-05-13 17:30:32 +01001400 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001401#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
1403 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001406}
1407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001409/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001410 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001413 int (*f_rng)(void *, unsigned char *, size_t),
1414 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001415 const unsigned char *label, size_t label_len,
1416 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001417 const unsigned char *input,
1418 unsigned char *output,
1419 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001420{
Janos Follath24eed8d2019-11-22 13:21:35 +00001421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001422 size_t ilen, i, pad_len;
1423 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001425 unsigned char lhash[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001426 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001427
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001428 /*
1429 * Parameters sanity checks
1430 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001431 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
1434 ilen = ctx->len;
1435
Paul Bakker27fdf462011-06-09 13:55:13 +00001436 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001438
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001439 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1440 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001442
Janos Follathc17cda12016-02-11 11:08:18 +00001443 // checking for integer underflow
1444 if( 2 * hlen + 2 > ilen )
1445 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1446
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001447 /*
1448 * RSA operation
1449 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001450 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451
1452 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001453 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001455 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001456 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001457 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001458 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001459 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001460 ctx->hash_id ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001461 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001462 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001463 ctx->hash_id ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001464 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001465 goto cleanup;
1466 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001467
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001468 /* Generate lHash */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001469 ret = compute_hash( (mbedtls_md_type_t) ctx->hash_id,
1470 label, label_len, lhash );
1471 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001472 goto cleanup;
1473
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001474 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001475 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001476 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001477 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001478 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001479
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001480 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001481
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001482 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001483
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001484 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001485 for( i = 0; i < hlen; i++ )
1486 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001487
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001488 /* Get zero-padding len, but always read till end of buffer
1489 * (minus one, for the 01 byte) */
1490 pad_len = 0;
1491 pad_done = 0;
1492 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1493 {
1494 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001495 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001496 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001497
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001498 p += pad_len;
1499 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001500
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001501 /*
1502 * The only information "leaked" is whether the padding was correct or not
1503 * (eg, no data is copied if it was not correct). This meets the
1504 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1505 * the different error conditions.
1506 */
1507 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001508 {
1509 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1510 goto cleanup;
1511 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001512
Paul Bakker66d5d072014-06-17 16:39:18 +02001513 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001514 {
1515 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1516 goto cleanup;
1517 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001518
1519 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001520 if( *olen != 0 )
1521 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001522 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001523
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001524cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001525 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1526 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001527
1528 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001529}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001533/*
1534 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1535 */
1536int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
1537 int (*f_rng)(void *, unsigned char *, size_t),
1538 void *p_rng,
1539 size_t *olen,
1540 const unsigned char *input,
1541 unsigned char *output,
1542 size_t output_max_len )
1543{
1544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1545 size_t ilen;
1546 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1547
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001548 ilen = ctx->len;
1549
1550 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1551 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1552
1553 if( ilen < 16 || ilen > sizeof( buf ) )
1554 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1555
1556 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
1557
1558 if( ret != 0 )
1559 goto cleanup;
1560
Gabor Mezei90437e32021-10-20 11:59:27 +02001561 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding( buf, ilen,
Gabor Mezei63bbba52021-10-18 16:17:57 +02001562 output, output_max_len, olen );
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001563
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001564cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001565 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001566
1567 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001568}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001570
1571/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001572 * Do an RSA operation, then remove the message padding
1573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001575 int (*f_rng)(void *, unsigned char *, size_t),
1576 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001577 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001578 const unsigned char *input,
1579 unsigned char *output,
1580 size_t output_max_len)
1581{
1582 switch( ctx->padding )
1583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#if defined(MBEDTLS_PKCS1_V15)
1585 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney34733082021-05-12 09:24:29 +01001586 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001587 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001588#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#if defined(MBEDTLS_PKCS1_V21)
1591 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001592 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001593 olen, input, output,
1594 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001595#endif
1596
1597 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001599 }
1600}
1601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001603static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001604 int (*f_rng)(void *, unsigned char *, size_t),
1605 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001607 unsigned int hashlen,
1608 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001609 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001610 unsigned char *sig )
1611{
1612 size_t olen;
1613 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001614 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001615 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001616 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001617 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001618
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001619 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
1620 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01001621
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001622 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1623 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1624
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001625 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001627
1628 olen = ctx->len;
1629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001631 {
Simon Butcher02037452016-03-01 21:19:12 +00001632 /* Gather length of hash to sign */
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001633 size_t exp_hashlen = mbedtls_hash_info_get_size( md_alg );
1634 if( exp_hashlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001636
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001637 if( hashlen != exp_hashlen )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001638 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001639 }
1640
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001641 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1642 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001644
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001645 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1646 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001647 /* Calculate the largest possible salt length, up to the hash size.
1648 * Normally this is the hash length, which is the maximum salt length
1649 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001650 * enough room, use the maximum salt length that fits. The constraint is
1651 * that the hash length plus the salt length plus 2 bytes must be at most
1652 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1653 * (PKCS#1 v2.2) §9.1.1 step 3. */
1654 min_slen = hlen - 2;
1655 if( olen < hlen + min_slen + 2 )
1656 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1657 else if( olen >= hlen + hlen + 2 )
1658 slen = hlen;
1659 else
1660 slen = olen - hlen - 2;
1661 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001662 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001665 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001666 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001667 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001668 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001669 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001670
1671 memset( sig, 0, olen );
1672
Simon Butcher02037452016-03-01 21:19:12 +00001673 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001674 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001675 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001676 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001677
1678 /* Generate salt of length slen in place in the encoded message */
1679 salt = p;
1680 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001681 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001682
Paul Bakkerb3869132013-02-28 17:21:01 +01001683 p += slen;
1684
Simon Butcher02037452016-03-01 21:19:12 +00001685 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001686 ret = hash_mprime( hash, hashlen, salt, slen, p, ctx->hash_id );
1687 if( ret != 0 )
1688 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001689
Simon Butcher02037452016-03-01 21:19:12 +00001690 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001691 if( msb % 8 == 0 )
1692 offset = 1;
1693
Simon Butcher02037452016-03-01 21:19:12 +00001694 /* maskedDB: Apply dbMask to DB */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001695 ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1696 ctx->hash_id );
1697 if( ret != 0 )
1698 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001699
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001700 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001701 sig[0] &= 0xFF >> ( olen * 8 - msb );
1702
1703 p += hlen;
1704 *p++ = 0xBC;
1705
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001706 return mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001707}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001708
1709/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001710 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1711 * the option to pass in the salt length.
1712 */
1713int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1714 int (*f_rng)(void *, unsigned char *, size_t),
1715 void *p_rng,
1716 mbedtls_md_type_t md_alg,
1717 unsigned int hashlen,
1718 const unsigned char *hash,
1719 int saltlen,
1720 unsigned char *sig )
1721{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001722 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001723 hashlen, hash, saltlen, sig );
1724}
1725
1726
1727/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001728 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1729 */
1730int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1731 int (*f_rng)(void *, unsigned char *, size_t),
1732 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001733 mbedtls_md_type_t md_alg,
1734 unsigned int hashlen,
1735 const unsigned char *hash,
1736 unsigned char *sig )
1737{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001738 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001739 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001740}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001744/*
1745 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1746 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001747
1748/* Construct a PKCS v1.5 encoding of a hashed message
1749 *
1750 * This is used both for signature generation and verification.
1751 *
1752 * Parameters:
1753 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001754 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001755 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001756 * - hash: Buffer containing the hashed message or the raw data.
1757 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001758 * - dst: Buffer to hold the encoded message.
1759 *
1760 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001761 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001762 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001763 *
1764 */
1765static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1766 unsigned int hashlen,
1767 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001768 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001769 unsigned char *dst )
1770{
1771 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001772 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001773 unsigned char *p = dst;
1774 const char *oid = NULL;
1775
1776 /* Are we signing hashed or raw data? */
1777 if( md_alg != MBEDTLS_MD_NONE )
1778 {
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +02001779 unsigned char md_size = mbedtls_hash_info_get_size( md_alg );
Manuel Pégourié-Gonnardf493f2a2022-07-05 17:41:05 +02001780 if( md_size == 0 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001781 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1782
1783 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1784 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1785
Manuel Pégourié-Gonnardf493f2a2022-07-05 17:41:05 +02001786 if( hashlen != md_size )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001787 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001788
1789 /* Double-check that 8 + hashlen + oid_size can be used as a
1790 * 1-byte ASN.1 length encoding and that there's no overflow. */
1791 if( 8 + hashlen + oid_size >= 0x80 ||
1792 10 + hashlen < hashlen ||
1793 10 + hashlen + oid_size < 10 + hashlen )
1794 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1795
1796 /*
1797 * Static bounds check:
1798 * - Need 10 bytes for five tag-length pairs.
1799 * (Insist on 1-byte length encodings to protect against variants of
1800 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1801 * - Need hashlen bytes for hash
1802 * - Need oid_size bytes for hash alg OID.
1803 */
1804 if( nb_pad < 10 + hashlen + oid_size )
1805 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1806 nb_pad -= 10 + hashlen + oid_size;
1807 }
1808 else
1809 {
1810 if( nb_pad < hashlen )
1811 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1812
1813 nb_pad -= hashlen;
1814 }
1815
Hanno Becker2b2f8982017-09-27 17:10:03 +01001816 /* Need space for signature header and padding delimiter (3 bytes),
1817 * and 8 bytes for the minimal padding */
1818 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001819 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1820 nb_pad -= 3;
1821
1822 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001823 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001824
1825 /* Write signature header and padding */
1826 *p++ = 0;
1827 *p++ = MBEDTLS_RSA_SIGN;
1828 memset( p, 0xFF, nb_pad );
1829 p += nb_pad;
1830 *p++ = 0;
1831
1832 /* Are we signing raw data? */
1833 if( md_alg == MBEDTLS_MD_NONE )
1834 {
1835 memcpy( p, hash, hashlen );
1836 return( 0 );
1837 }
1838
1839 /* Signing hashed data, add corresponding ASN.1 structure
1840 *
1841 * DigestInfo ::= SEQUENCE {
1842 * digestAlgorithm DigestAlgorithmIdentifier,
1843 * digest Digest }
1844 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1845 * Digest ::= OCTET STRING
1846 *
1847 * Schematic:
1848 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1849 * TAG-NULL + LEN [ NULL ] ]
1850 * TAG-OCTET + LEN [ HASH ] ]
1851 */
1852 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001853 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001854 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001855 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001856 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001857 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001858 memcpy( p, oid, oid_size );
1859 p += oid_size;
1860 *p++ = MBEDTLS_ASN1_NULL;
1861 *p++ = 0x00;
1862 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001863 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001864 memcpy( p, hash, hashlen );
1865 p += hashlen;
1866
1867 /* Just a sanity-check, should be automatic
1868 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001869 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001870 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001871 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001872 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1873 }
1874
1875 return( 0 );
1876}
1877
Paul Bakkerb3869132013-02-28 17:21:01 +01001878/*
1879 * Do an RSA operation to sign the message digest
1880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001882 int (*f_rng)(void *, unsigned char *, size_t),
1883 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001885 unsigned int hashlen,
1886 const unsigned char *hash,
1887 unsigned char *sig )
1888{
Janos Follath24eed8d2019-11-22 13:21:35 +00001889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001890 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001891
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001892 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
1893 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001894
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001895 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1896 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1897
Hanno Beckerfdf38032017-09-06 12:35:55 +01001898 /*
1899 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1900 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001901
Hanno Beckerfdf38032017-09-06 12:35:55 +01001902 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1903 ctx->len, sig ) ) != 0 )
1904 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001905
Hanno Beckerfdf38032017-09-06 12:35:55 +01001906 /* Private key operation
1907 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001908 * In order to prevent Lenstra's attack, make the signature in a
1909 * temporary buffer and check it before returning it.
1910 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001911
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001912 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001913 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001914 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1915
Hanno Beckerfdf38032017-09-06 12:35:55 +01001916 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001917 if( verif == NULL )
1918 {
1919 mbedtls_free( sig_try );
1920 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1921 }
1922
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001923 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1924 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1925
Gabor Mezei90437e32021-10-20 11:59:27 +02001926 if( mbedtls_ct_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001927 {
1928 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1929 goto cleanup;
1930 }
1931
1932 memcpy( sig, sig_try, ctx->len );
1933
1934cleanup:
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001935 mbedtls_platform_zeroize( sig_try, ctx->len );
1936 mbedtls_platform_zeroize( verif, ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001937 mbedtls_free( sig_try );
1938 mbedtls_free( verif );
1939
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001940 if( ret != 0 )
1941 memset( sig, '!', ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001942 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001943}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001945
1946/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 * Do an RSA operation to sign the message digest
1948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001950 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001951 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001953 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001954 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 unsigned char *sig )
1956{
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001957 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
1958 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001959
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 switch( ctx->padding )
1961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962#if defined(MBEDTLS_PKCS1_V15)
1963 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney52654982021-05-18 16:54:00 +01001964 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng,
Thomas Daubney140184d2021-05-18 16:04:07 +01001965 md_alg, hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001966#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968#if defined(MBEDTLS_PKCS1_V21)
1969 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyde9fdc42021-05-18 17:10:04 +01001970 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
1971 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001972#endif
1973
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001977}
1978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001980/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001981 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001985 unsigned int hashlen,
1986 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001988 int expected_salt_len,
1989 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001990{
Janos Follath24eed8d2019-11-22 13:21:35 +00001991 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001992 size_t siglen;
1993 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001994 unsigned char *hash_start;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001995 unsigned char result[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001996 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001997 size_t observed_salt_len, msb;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001998 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = {0};
Paul Bakkerb3869132013-02-28 17:21:01 +01001999
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002000 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
2001 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002002
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 siglen = ctx->len;
2004
Paul Bakker27fdf462011-06-09 13:55:13 +00002005 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002007
Thomas Daubney782a7f52021-05-19 12:27:35 +01002008 ret = mbedtls_rsa_public( ctx, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002009
2010 if( ret != 0 )
2011 return( ret );
2012
2013 p = buf;
2014
Paul Bakkerb3869132013-02-28 17:21:01 +01002015 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 {
Simon Butcher02037452016-03-01 21:19:12 +00002020 /* Gather length of hash to sign */
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002021 size_t exp_hashlen = mbedtls_hash_info_get_size( md_alg );
2022 if( exp_hashlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002024
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002025 if( hashlen != exp_hashlen )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002026 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002027 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002028
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002029 hlen = mbedtls_hash_info_get_size( mgf1_hash_id );
2030 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002032
Simon Butcher02037452016-03-01 21:19:12 +00002033 /*
2034 * Note: EMSA-PSS verification is over the length of N - 1 bits
2035 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002036 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002037
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002038 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2039 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2040
Simon Butcher02037452016-03-01 21:19:12 +00002041 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002042 if( msb % 8 == 0 )
2043 {
2044 p++;
2045 siglen -= 1;
2046 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002047
Gilles Peskine139108a2017-10-18 19:03:42 +02002048 if( siglen < hlen + 2 )
2049 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2050 hash_start = p + siglen - hlen - 1;
2051
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02002052 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id );
Jaeden Amero66954e12018-01-25 16:05:54 +00002053 if( ret != 0 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002054 return( ret );
Paul Bakker02303e82013-01-03 11:08:31 +01002055
Paul Bakkerb3869132013-02-28 17:21:01 +01002056 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002057
Gilles Peskine6a54b022017-10-17 19:02:13 +02002058 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002059 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002060
Gilles Peskine91048a32017-10-19 17:46:14 +02002061 if( *p++ != 0x01 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002062 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002063
Gilles Peskine6a54b022017-10-17 19:02:13 +02002064 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002067 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002068 {
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002069 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002070 }
2071
Simon Butcher02037452016-03-01 21:19:12 +00002072 /*
2073 * Generate H = Hash( M' )
2074 */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002075 ret = hash_mprime( hash, hashlen, p, observed_salt_len,
2076 result, mgf1_hash_id );
2077 if( ret != 0 )
2078 return( ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002079
Jaeden Amero66954e12018-01-25 16:05:54 +00002080 if( memcmp( hash_start, result, hlen ) != 0 )
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002081 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002082
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002083 return( 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01002084}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002085
2086/*
2087 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002091 unsigned int hashlen,
2092 const unsigned char *hash,
2093 const unsigned char *sig )
2094{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002095 mbedtls_md_type_t mgf1_hash_id;
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002096 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
2097 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002098
2099 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002101 : md_alg;
2102
Thomas Daubney9e65f792021-05-19 12:18:58 +01002103 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx,
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002104 md_alg, hashlen, hash,
2105 mgf1_hash_id,
2106 MBEDTLS_RSA_SALT_LEN_ANY,
2107 sig ) );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002108
2109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002113/*
2114 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002118 unsigned int hashlen,
2119 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002120 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002121{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002122 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002123 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002124 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002125
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002126 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
2127 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002128
2129 sig_len = ctx->len;
2130
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002131 /*
2132 * Prepare expected PKCS1 v1.5 encoding of hash.
2133 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002134
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002135 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2136 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2137 {
2138 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2139 goto cleanup;
2140 }
2141
2142 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2143 encoded_expected ) ) != 0 )
2144 goto cleanup;
2145
2146 /*
2147 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2148 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002149
Thomas Daubney41e4ce42021-05-19 15:10:05 +01002150 ret = mbedtls_rsa_public( ctx, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002151 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002152 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002153
Simon Butcher02037452016-03-01 21:19:12 +00002154 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002155 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002156 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002157
Gabor Mezei90437e32021-10-20 11:59:27 +02002158 if( ( ret = mbedtls_ct_memcmp( encoded, encoded_expected,
gabor-mezei-arm46025642021-07-19 15:19:19 +02002159 sig_len ) ) != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002160 {
2161 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2162 goto cleanup;
2163 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002164
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002165cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002166
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002167 if( encoded != NULL )
2168 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002169 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002170 mbedtls_free( encoded );
2171 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002172
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002173 if( encoded_expected != NULL )
2174 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002175 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002176 mbedtls_free( encoded_expected );
2177 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002178
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002179 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002180}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002182
2183/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002184 * Do an RSA operation and check the message digest
2185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002188 unsigned int hashlen,
2189 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002190 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002191{
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002192 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
2193 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002194
Paul Bakkerb3869132013-02-28 17:21:01 +01002195 switch( ctx->padding )
2196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197#if defined(MBEDTLS_PKCS1_V15)
2198 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney2e126252021-05-19 11:48:53 +01002199 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, md_alg,
2200 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002201#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203#if defined(MBEDTLS_PKCS1_V21)
2204 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002205 return mbedtls_rsa_rsassa_pss_verify( ctx, md_alg,
2206 hashlen, hash, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01002207#endif
2208
2209 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002211 }
2212}
2213
2214/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002215 * Copy the components of an RSA key
2216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002218{
Janos Follath24eed8d2019-11-22 13:21:35 +00002219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002220
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002221 dst->len = src->len;
2222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2224 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2227 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2228 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002229
2230#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2232 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2233 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2235 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002236#endif
2237
2238 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2241 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002242
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002243 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002244 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002245
2246cleanup:
2247 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002249
2250 return( ret );
2251}
2252
2253/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 * Free the components of an RSA key
2255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002257{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002258 if( ctx == NULL )
2259 return;
2260
2261 mbedtls_mpi_free( &ctx->Vi );
2262 mbedtls_mpi_free( &ctx->Vf );
2263 mbedtls_mpi_free( &ctx->RN );
2264 mbedtls_mpi_free( &ctx->D );
2265 mbedtls_mpi_free( &ctx->Q );
2266 mbedtls_mpi_free( &ctx->P );
2267 mbedtls_mpi_free( &ctx->E );
2268 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002269
Hanno Becker33c30a02017-08-23 07:00:22 +01002270#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002271 mbedtls_mpi_free( &ctx->RQ );
2272 mbedtls_mpi_free( &ctx->RP );
2273 mbedtls_mpi_free( &ctx->QP );
2274 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002275 mbedtls_mpi_free( &ctx->DP );
2276#endif /* MBEDTLS_RSA_NO_CRT */
2277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002279 /* Free the mutex, but only if it hasn't been freed already. */
2280 if( ctx->ver != 0 )
2281 {
2282 mbedtls_mutex_free( &ctx->mutex );
2283 ctx->ver = 0;
2284 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002285#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002286}
2287
Hanno Beckerab377312017-08-23 16:24:51 +01002288#endif /* !MBEDTLS_RSA_ALT */
2289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002291
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002292#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002293
2294/*
2295 * Example RSA-1024 keypair, for test purposes
2296 */
2297#define KEY_LEN 128
2298
2299#define RSA_N "9292758453063D803DD603D5E777D788" \
2300 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2301 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2302 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2303 "93A89813FBF3C4F8066D2D800F7C38A8" \
2304 "1AE31942917403FF4946B0A83D3D3E05" \
2305 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2306 "5E94BB77B07507233A0BC7BAC8F90F79"
2307
2308#define RSA_E "10001"
2309
2310#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2311 "66CA472BC44D253102F8B4A9D3BFA750" \
2312 "91386C0077937FE33FA3252D28855837" \
2313 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2314 "DF79C5CE07EE72C7F123142198164234" \
2315 "CABB724CF78B8173B9F880FC86322407" \
2316 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2317 "071513A1E85B5DFA031F21ECAE91A34D"
2318
2319#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2320 "2C01CAD19EA484A87EA4377637E75500" \
2321 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2322 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2323
2324#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2325 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2326 "910E4168387E3C30AA1E00C339A79508" \
2327 "8452DD96A9A5EA5D9DCA68DA636032AF"
2328
Paul Bakker5121ce52009-01-03 21:22:43 +00002329#define PT_LEN 24
2330#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2331 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002334static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002335{
gufe44c2620da2020-08-03 17:56:50 +02002336#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002337 size_t i;
2338
Paul Bakker545570e2010-07-18 09:00:25 +00002339 if( rng_state != NULL )
2340 rng_state = NULL;
2341
Paul Bakkera3d195c2011-11-27 21:07:34 +00002342 for( i = 0; i < len; ++i )
2343 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002344#else
2345 if( rng_state != NULL )
2346 rng_state = NULL;
2347
2348 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002349#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002350
Paul Bakkera3d195c2011-11-27 21:07:34 +00002351 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002354
Paul Bakker5121ce52009-01-03 21:22:43 +00002355/*
2356 * Checkup routine
2357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002359{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002360 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002362 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 unsigned char rsa_plaintext[PT_LEN];
2365 unsigned char rsa_decrypted[PT_LEN];
2366 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002368 unsigned char sha1sum[20];
2369#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002370
Hanno Becker3a701162017-08-22 13:52:43 +01002371 mbedtls_mpi K;
2372
2373 mbedtls_mpi_init( &K );
Ronald Cronc1905a12021-06-05 11:11:14 +02002374 mbedtls_rsa_init( &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
Hanno Becker3a701162017-08-22 13:52:43 +01002376 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2377 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2378 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2379 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2380 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2381 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2382 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2383 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2384 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2385 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2386
Hanno Becker7f25f852017-10-10 16:56:22 +01002387 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
2389 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2393 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 {
2395 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
Hanno Becker5bc87292017-05-03 15:09:31 +01002398 ret = 1;
2399 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 }
2401
2402 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002404
2405 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2406
Thomas Daubney21772772021-05-13 17:30:32 +01002407 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002408 PT_LEN, rsa_plaintext,
2409 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 {
2411 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
Hanno Becker5bc87292017-05-03 15:09:31 +01002414 ret = 1;
2415 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 }
2417
2418 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002421 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002422 &len, rsa_ciphertext, rsa_decrypted,
2423 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 {
2425 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427
Hanno Becker5bc87292017-05-03 15:09:31 +01002428 ret = 1;
2429 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 }
2431
2432 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2433 {
2434 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
Hanno Becker5bc87292017-05-03 15:09:31 +01002437 ret = 1;
2438 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 }
2440
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002441 if( verbose != 0 )
2442 mbedtls_printf( "passed\n" );
2443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002446 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002447
TRodziewicz26371e42021-06-08 16:45:41 +02002448 if( mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002449 {
2450 if( verbose != 0 )
2451 mbedtls_printf( "failed\n" );
2452
2453 return( 1 );
2454 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002455
Hanno Becker98838b02017-10-02 13:16:10 +01002456 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002457 MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002458 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 {
2460 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
Hanno Becker5bc87292017-05-03 15:09:31 +01002463 ret = 1;
2464 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 }
2466
2467 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002470 if( mbedtls_rsa_pkcs1_verify( &rsa, MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002471 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 {
2473 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002475
Hanno Becker5bc87292017-05-03 15:09:31 +01002476 ret = 1;
2477 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 }
2479
2480 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002481 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002484 if( verbose != 0 )
2485 mbedtls_printf( "\n" );
2486
Paul Bakker3d8fb632014-04-17 12:42:41 +02002487cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002488 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 mbedtls_rsa_free( &rsa );
2490#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002491 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002493 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002494}
2495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498#endif /* MBEDTLS_RSA_C */