blob: c81aa71707c8815a0eb3162b5d9cbdb68beb6a44 [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)
60#if defined(MBEDTLS_MD_C)
61#define HASH_MAX_SIZE MBEDTLS_MD_MAX_SIZE
62#else /* MBEDTLS_MD_C */
63#include "psa/crypto.h"
64#include "mbedtls/psa_util.h"
65#define HASH_MAX_SIZE PSA_HASH_MAX_SIZE
66#endif /* MBEDTLS_MD_C */
67#endif /* MBEDTLS_PKCS1_V21 */
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000070#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010071#else
Rich Evans00ab4702015-02-06 13:43:58 +000072#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020074#define mbedtls_calloc calloc
75#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010076#endif
77
Hanno Beckera565f542017-10-11 11:00:19 +010078#if !defined(MBEDTLS_RSA_ALT)
79
Hanno Becker617c1ae2017-08-23 14:11:24 +010080int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
81 const mbedtls_mpi *N,
82 const mbedtls_mpi *P, const mbedtls_mpi *Q,
83 const mbedtls_mpi *D, const mbedtls_mpi *E )
84{
Janos Follath24eed8d2019-11-22 13:21:35 +000085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +010086
87 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
88 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
89 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
90 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
91 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
92 {
Chris Jonesb7d02e02021-04-01 17:40:03 +010093 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +010094 }
95
96 if( N != NULL )
97 ctx->len = mbedtls_mpi_size( &ctx->N );
98
99 return( 0 );
100}
101
102int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100103 unsigned char const *N, size_t N_len,
104 unsigned char const *P, size_t P_len,
105 unsigned char const *Q, size_t Q_len,
106 unsigned char const *D, size_t D_len,
107 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100108{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000109 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100110
111 if( N != NULL )
112 {
113 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
114 ctx->len = mbedtls_mpi_size( &ctx->N );
115 }
116
117 if( P != NULL )
118 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
119
120 if( Q != NULL )
121 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
122
123 if( D != NULL )
124 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
125
126 if( E != NULL )
127 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
128
129cleanup:
130
131 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100132 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100133
134 return( 0 );
135}
136
Hanno Becker705fc682017-10-10 17:57:02 +0100137/*
138 * Checks whether the context fields are set in such a way
139 * that the RSA primitives will be able to execute without error.
140 * It does *not* make guarantees for consistency of the parameters.
141 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100142static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
143 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100144{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100145#if !defined(MBEDTLS_RSA_NO_CRT)
146 /* blinding_needed is only used for NO_CRT to decide whether
147 * P,Q need to be present or not. */
148 ((void) blinding_needed);
149#endif
150
Hanno Becker3a760a12018-01-05 08:14:49 +0000151 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
152 ctx->len > MBEDTLS_MPI_MAX_SIZE )
153 {
Hanno Becker705fc682017-10-10 17:57:02 +0100154 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000155 }
Hanno Becker705fc682017-10-10 17:57:02 +0100156
157 /*
158 * 1. Modular exponentiation needs positive, odd moduli.
159 */
160
161 /* Modular exponentiation wrt. N is always used for
162 * RSA public key operations. */
163 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
164 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
165 {
166 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
167 }
168
169#if !defined(MBEDTLS_RSA_NO_CRT)
170 /* Modular exponentiation for P and Q is only
171 * used for private key operations and if CRT
172 * is used. */
173 if( is_priv &&
174 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
175 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
176 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
177 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
178 {
179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
180 }
181#endif /* !MBEDTLS_RSA_NO_CRT */
182
183 /*
184 * 2. Exponents must be positive
185 */
186
187 /* Always need E for public key operations */
188 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
189 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
190
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100191#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100192 /* For private key operations, use D or DP & DQ
193 * as (unblinded) exponents. */
194 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
195 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
196#else
197 if( is_priv &&
198 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
199 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
200 {
201 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
202 }
203#endif /* MBEDTLS_RSA_NO_CRT */
204
205 /* Blinding shouldn't make exponents negative either,
206 * so check that P, Q >= 1 if that hasn't yet been
207 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100208#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100209 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100210 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
211 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
212 {
213 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
214 }
215#endif
216
217 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100218 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100219#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100220 if( is_priv &&
221 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
222 {
223 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
224 }
225#endif
226
227 return( 0 );
228}
229
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100230int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100231{
232 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500233 int have_N, have_P, have_Q, have_D, have_E;
234#if !defined(MBEDTLS_RSA_NO_CRT)
235 int have_DP, have_DQ, have_QP;
236#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500237 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100238
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500239 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
240 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
241 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
242 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
243 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500244
245#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500246 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
247 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
248 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500249#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100250
Hanno Becker617c1ae2017-08-23 14:11:24 +0100251 /*
252 * Check whether provided parameters are enough
253 * to deduce all others. The following incomplete
254 * parameter sets for private keys are supported:
255 *
256 * (1) P, Q missing.
257 * (2) D and potentially N missing.
258 *
259 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100260
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500261 n_missing = have_P && have_Q && have_D && have_E;
262 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
263 d_missing = have_P && have_Q && !have_D && have_E;
264 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100265
266 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500267 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100268
Hanno Becker617c1ae2017-08-23 14:11:24 +0100269 if( !is_priv && !is_pub )
270 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
271
272 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100273 * Step 1: Deduce N if P, Q are provided.
274 */
275
276 if( !have_N && have_P && have_Q )
277 {
278 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
279 &ctx->Q ) ) != 0 )
280 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100281 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100282 }
283
284 ctx->len = mbedtls_mpi_size( &ctx->N );
285 }
286
287 /*
288 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100289 */
290
291 if( pq_missing )
292 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100293 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100294 &ctx->P, &ctx->Q );
295 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100296 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100297
298 }
299 else if( d_missing )
300 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100301 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
302 &ctx->Q,
303 &ctx->E,
304 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100305 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100306 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100307 }
308 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100309
Hanno Becker617c1ae2017-08-23 14:11:24 +0100310 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100311 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100312 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100313 */
314
Hanno Becker23344b52017-08-23 07:43:27 +0100315#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500316 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100317 {
318 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
319 &ctx->DP, &ctx->DQ, &ctx->QP );
320 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100321 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100322 }
Hanno Becker23344b52017-08-23 07:43:27 +0100323#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324
325 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100326 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327 */
328
Hanno Beckerebd2c022017-10-12 10:54:53 +0100329 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100330}
331
Hanno Becker617c1ae2017-08-23 14:11:24 +0100332int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
333 unsigned char *N, size_t N_len,
334 unsigned char *P, size_t P_len,
335 unsigned char *Q, size_t Q_len,
336 unsigned char *D, size_t D_len,
337 unsigned char *E, size_t E_len )
338{
339 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500340 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100341
342 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500343 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100344 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
345 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
346 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
347 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
348 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
349
350 if( !is_priv )
351 {
352 /* If we're trying to export private parameters for a public key,
353 * something must be wrong. */
354 if( P != NULL || Q != NULL || D != NULL )
355 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
356
357 }
358
359 if( N != NULL )
360 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
361
362 if( P != NULL )
363 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
364
365 if( Q != NULL )
366 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
367
368 if( D != NULL )
369 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
370
371 if( E != NULL )
372 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100373
374cleanup:
375
376 return( ret );
377}
378
Hanno Becker617c1ae2017-08-23 14:11:24 +0100379int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
380 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
381 mbedtls_mpi *D, mbedtls_mpi *E )
382{
Janos Follath24eed8d2019-11-22 13:21:35 +0000383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500384 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100385
386 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500387 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100388 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
389 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
390 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
391 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
392 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
393
394 if( !is_priv )
395 {
396 /* If we're trying to export private parameters for a public key,
397 * something must be wrong. */
398 if( P != NULL || Q != NULL || D != NULL )
399 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
400
401 }
402
403 /* Export all requested core parameters. */
404
405 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
406 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
407 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
408 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
409 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
410 {
411 return( ret );
412 }
413
414 return( 0 );
415}
416
417/*
418 * Export CRT parameters
419 * This must also be implemented if CRT is not used, for being able to
420 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
421 * can be used in this case.
422 */
423int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
424 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
425{
Janos Follath24eed8d2019-11-22 13:21:35 +0000426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500427 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100428
429 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500430 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100431 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
432 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
433 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
434 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
435 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
436
437 if( !is_priv )
438 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
439
Hanno Beckerdc95c892017-08-23 06:57:02 +0100440#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100441 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100442 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
443 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
444 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
445 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100446 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100447 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100448#else
449 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
450 DP, DQ, QP ) ) != 0 )
451 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100452 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100453 }
454#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100455
456 return( 0 );
457}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100458
Paul Bakker5121ce52009-01-03 21:22:43 +0000459/*
460 * Initialize an RSA context
461 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200462void mbedtls_rsa_init( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000465
Ronald Cronc1905a12021-06-05 11:11:14 +0200466 ctx->padding = MBEDTLS_RSA_PKCS_V15;
467 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100470 /* Set ctx->ver to nonzero to indicate that the mutex has been
471 * initialized and will need to be freed. */
472 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200474#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000475}
476
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100477/*
478 * Set padding for an existing RSA context
479 */
Ronald Cronea7631b2021-06-03 18:51:59 +0200480int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
481 mbedtls_md_type_t hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100482{
Ronald Cron3a0375f2021-06-08 10:22:28 +0200483 switch( padding )
484 {
485#if defined(MBEDTLS_PKCS1_V15)
486 case MBEDTLS_RSA_PKCS_V15:
487 break;
488#endif
489
490#if defined(MBEDTLS_PKCS1_V21)
491 case MBEDTLS_RSA_PKCS_V21:
492 break;
493#endif
494 default:
495 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
496 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200497
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200498#if defined(MBEDTLS_PKCS1_V21)
Ronald Cronea7631b2021-06-03 18:51:59 +0200499 if( ( padding == MBEDTLS_RSA_PKCS_V21 ) &&
500 ( hash_id != MBEDTLS_MD_NONE ) )
501 {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200502 /* Just make sure this hash is supported in this build. */
Przemek Stekiel712bb9c2022-07-29 11:12:00 +0200503 if( mbedtls_hash_info_psa_from_md( hash_id ) == PSA_ALG_NONE )
Ronald Cronea7631b2021-06-03 18:51:59 +0200504 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
505 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200506#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500507
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100508 ctx->padding = padding;
509 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200510
511 return( 0 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100512}
513
Hanno Becker617c1ae2017-08-23 14:11:24 +0100514/*
515 * Get length in bytes of RSA modulus
516 */
517
518size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
519{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100520 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100521}
522
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526/*
527 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800528 *
529 * This generation method follows the RSA key pair generation procedure of
530 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000533 int (*f_rng)(void *, unsigned char *, size_t),
534 void *p_rng,
535 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000536{
Janos Follath24eed8d2019-11-22 13:21:35 +0000537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800538 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100539 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Janos Follathb8fc1b02018-09-03 15:37:01 +0100541 /*
542 * If the modulus is 1024 bit long or shorter, then the security strength of
543 * the RSA algorithm is less than or equal to 80 bits and therefore an error
544 * rate of 2^-80 is sufficient.
545 */
546 if( nbits > 1024 )
547 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
548
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100549 mbedtls_mpi_init( &H );
550 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800551 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100553 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
554 {
555 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
556 goto cleanup;
557 }
558
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 /*
560 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800561 * 1. |P-Q| > 2^( nbits / 2 - 100 )
562 * 2. GCD( E, (P-1)*(Q-1) ) == 1
563 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567 do
568 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100569 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
570 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Janos Follathb8fc1b02018-09-03 15:37:01 +0100572 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
573 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800575 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
576 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
577 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 continue;
579
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800580 /* not required by any standards, but some users rely on the fact that P > Q */
581 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100582 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100583
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100584 /* Temporarily replace P,Q by P-1, Q-1 */
585 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
586 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
587 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800588
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800589 /* 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 +0200590 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800591 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
592 continue;
593
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800594 /* 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 -0800595 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
596 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
597 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
598
599 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
600 continue;
601
602 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800604 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100606 /* Restore P,Q */
607 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
608 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
609
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800610 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
611
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100612 ctx->len = mbedtls_mpi_size( &ctx->N );
613
Jethro Beekman97f95c92018-02-13 15:50:36 -0800614#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 * DP = D mod (P - 1)
617 * DQ = D mod (Q - 1)
618 * QP = Q^-1 mod P
619 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100620 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
621 &ctx->DP, &ctx->DQ, &ctx->QP ) );
622#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
Hanno Becker83aad1f2017-08-23 06:45:10 +0100624 /* Double-check */
625 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
627cleanup:
628
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100629 mbedtls_mpi_free( &H );
630 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800631 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000632
633 if( ret != 0 )
634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100636
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100637 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100638 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100639 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 }
641
Paul Bakker48377d92013-08-30 12:06:24 +0200642 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000643}
644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647/*
648 * Check a public RSA key
649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000651{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100652 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000654
Hanno Becker3a760a12018-01-05 08:14:49 +0000655 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100658 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000659
Hanno Becker705fc682017-10-10 17:57:02 +0100660 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
661 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100665 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
667 return( 0 );
668}
669
670/*
Hanno Becker705fc682017-10-10 17:57:02 +0100671 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000674{
Hanno Becker705fc682017-10-10 17:57:02 +0100675 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100676 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 {
Hanno Becker98838b02017-10-02 13:16:10 +0100678 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 }
Paul Bakker48377d92013-08-30 12:06:24 +0200680
Hanno Becker98838b02017-10-02 13:16:10 +0100681 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100682 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100684 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000686
Hanno Beckerb269a852017-08-25 08:03:21 +0100687#if !defined(MBEDTLS_RSA_NO_CRT)
688 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
689 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
690 {
691 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
692 }
693#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000694
695 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000696}
697
698/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100699 * Check if contexts holding a public and private key match
700 */
Hanno Becker98838b02017-10-02 13:16:10 +0100701int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
702 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100703{
Hanno Becker98838b02017-10-02 13:16:10 +0100704 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708 }
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
711 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100714 }
715
716 return( 0 );
717}
718
719/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 * Do an RSA public key operation
721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000723 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000724 unsigned char *output )
725{
Janos Follath24eed8d2019-11-22 13:21:35 +0000726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000727 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Hanno Beckerebd2c022017-10-12 10:54:53 +0100730 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100731 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200735#if defined(MBEDTLS_THREADING_C)
736 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
737 return( ret );
738#endif
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000743 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200744 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
745 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000746 }
747
748 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
750 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
752cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200754 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
755 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100756#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
760 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100761 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
763 return( 0 );
764}
765
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200766/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200767 * Generate or update blinding values, see section 10 of:
768 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200769 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200770 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200771 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200772static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200773 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
774{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200775 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200776 mbedtls_mpi R;
777
778 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200779
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200780 if( ctx->Vf.p != NULL )
781 {
782 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
784 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
785 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
786 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200787
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200788 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200789 }
790
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200791 /* Unblinding value: Vf = random number, invertible mod N */
792 do {
793 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200794 {
795 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
796 goto cleanup;
797 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 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 +0200800
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200801 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200802 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
803 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
804 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
805
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200806 /* At this point, Vi is invertible mod N if and only if both Vf and R
807 * are invertible mod N. If one of them isn't, we don't need to know
808 * which one, we just loop and choose new values for both of them.
809 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200810 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500811 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200812 goto cleanup;
813
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500814 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
815
816 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
817 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
818 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200819
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200820 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200821 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 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 +0200823
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200824
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200825cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200826 mbedtls_mpi_free( &R );
827
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200828 return( ret );
829}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200830
Paul Bakker5121ce52009-01-03 21:22:43 +0000831/*
Janos Follathe81102e2017-03-22 13:38:28 +0000832 * Exponent blinding supposed to prevent side-channel attacks using multiple
833 * traces of measurements to recover the RSA key. The more collisions are there,
834 * the more bits of the key can be recovered. See [3].
835 *
836 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800837 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000838 *
839 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800840 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000841 *
842 * (With the currently (as of 2017 April) known best algorithms breaking 2048
843 * bit RSA requires approximately as much time as trying out 2^112 random keys.
844 * Thus in this sense with 28 byte blinding the security is not reduced by
845 * side-channel attacks like the one in [3])
846 *
847 * This countermeasure does not help if the key recovery is possible with a
848 * single trace.
849 */
850#define RSA_EXPONENT_BLINDING 28
851
852/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000853 * Do an RSA private key operation
854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200856 int (*f_rng)(void *, unsigned char *, size_t),
857 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000858 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000859 unsigned char *output )
860{
Janos Follath24eed8d2019-11-22 13:21:35 +0000861 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000862 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100863
864 /* Temporary holding the result */
865 mbedtls_mpi T;
866
867 /* Temporaries holding P-1, Q-1 and the
868 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000869 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100870
871#if !defined(MBEDTLS_RSA_NO_CRT)
872 /* Temporaries holding the results mod p resp. mod q. */
873 mbedtls_mpi TP, TQ;
874
875 /* Temporaries holding the blinded exponents for
876 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000877 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100878
879 /* Pointers to actual exponents to be used - either the unblinded
880 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000881 mbedtls_mpi *DP = &ctx->DP;
882 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100883#else
884 /* Temporary holding the blinded exponent (if used). */
885 mbedtls_mpi D_blind;
886
887 /* Pointer to actual exponent to be used - either the unblinded
888 * or the blinded one, depending on the presence of a PRNG. */
889 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100890#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100891
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100892 /* Temporaries holding the initial input and the double
893 * checked result; should be the same in the end. */
894 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000895
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200896 if( f_rng == NULL )
897 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
898
899 if( rsa_check_context( ctx, 1 /* private key checks */,
900 1 /* blinding on */ ) != 0 )
Hanno Beckerebd2c022017-10-12 10:54:53 +0100901 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100902 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100903 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100904
Hanno Becker06811ce2017-05-03 15:10:34 +0100905#if defined(MBEDTLS_THREADING_C)
906 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
907 return( ret );
908#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000909
Hanno Becker06811ce2017-05-03 15:10:34 +0100910 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100911 mbedtls_mpi_init( &T );
912
913 mbedtls_mpi_init( &P1 );
914 mbedtls_mpi_init( &Q1 );
915 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000916
Janos Follathe81102e2017-03-22 13:38:28 +0000917#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200918 mbedtls_mpi_init( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +0000919#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200920 mbedtls_mpi_init( &DP_blind );
921 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000922#endif
923
Hanno Becker06811ce2017-05-03 15:10:34 +0100924#if !defined(MBEDTLS_RSA_NO_CRT)
925 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200926#endif
927
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100928 mbedtls_mpi_init( &I );
929 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100930
931 /* End of MPI initialization */
932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
934 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200936 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
937 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 }
939
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100940 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100941
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200942 /*
943 * Blinding
944 * T = T * Vi mod N
945 */
946 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
947 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
948 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000949
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200950 /*
951 * Exponent blinding
952 */
953 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
954 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000955
Janos Follathf9203b42017-03-22 15:13:15 +0000956#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200957 /*
958 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
959 */
960 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
961 f_rng, p_rng ) );
962 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
963 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
964 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000965
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200966 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000967#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200968 /*
969 * DP_blind = ( P - 1 ) * R + DP
970 */
971 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
972 f_rng, p_rng ) );
973 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
974 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
975 &ctx->DP ) );
Janos Follathf9203b42017-03-22 15:13:15 +0000976
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200977 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000978
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200979 /*
980 * DQ_blind = ( Q - 1 ) * R + DQ
981 */
982 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
983 f_rng, p_rng ) );
984 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
985 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
986 &ctx->DQ ) );
Janos Follathf9203b42017-03-22 15:13:15 +0000987
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200988 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000989#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000992 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100993#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200994 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000995 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100997 * TP = input ^ dP mod P
998 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001000
1001 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1002 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
1004 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001005 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001007 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1008 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1009 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
1011 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001012 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001014 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1015 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001017
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001018 /*
1019 * Unblind
1020 * T = T * Vf mod N
1021 */
1022 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
1023 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
Hanno Becker2dec5e82017-10-03 07:49:52 +01001025 /* Verify the result to prevent glitching attacks. */
1026 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1027 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001028 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001029 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001030 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1031 goto cleanup;
1032 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001033
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
1037cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001039 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1040 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001041#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001042
Hanno Becker06811ce2017-05-03 15:10:34 +01001043 mbedtls_mpi_free( &P1 );
1044 mbedtls_mpi_free( &Q1 );
1045 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001046
Janos Follathe81102e2017-03-22 13:38:28 +00001047#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001048 mbedtls_mpi_free( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +00001049#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001050 mbedtls_mpi_free( &DP_blind );
1051 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Hanno Becker06811ce2017-05-03 15:10:34 +01001054 mbedtls_mpi_free( &T );
1055
1056#if !defined(MBEDTLS_RSA_NO_CRT)
1057 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1058#endif
1059
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001060 mbedtls_mpi_free( &C );
1061 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001062
Gilles Peskineae3741e2020-11-25 00:10:31 +01001063 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001064 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
Gilles Peskineae3741e2020-11-25 00:10:31 +01001066 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067}
1068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001070/**
1071 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1072 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001073 * \param dst buffer to mask
1074 * \param dlen length of destination buffer
1075 * \param src source of the mask generation
1076 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001077 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001078 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001079static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001080 size_t slen, mbedtls_md_type_t md_alg )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001081{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001082 unsigned char counter[4];
1083 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001084 unsigned int hlen;
1085 size_t i, use_len;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001086 unsigned char mask[HASH_MAX_SIZE];
1087#if defined(MBEDTLS_MD_C)
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001088 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001089 const mbedtls_md_info_t *md_info;
1090 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001091
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001092 mbedtls_md_init( &md_ctx );
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001093 md_info = mbedtls_md_info_from_type( md_alg );
1094 if( md_info == NULL )
1095 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1096
1097 mbedtls_md_init( &md_ctx );
1098 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1099 goto exit;
1100
1101 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001102#else
1103 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
1104 psa_algorithm_t alg = mbedtls_psa_translate_md( md_alg );
1105 psa_status_t status = PSA_SUCCESS;
1106 size_t out_len;
1107
1108 hlen = PSA_HASH_LENGTH( alg );
1109#endif
1110
1111 memset( mask, 0, sizeof( mask ) );
1112 memset( counter, 0, 4 );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001113
Simon Butcher02037452016-03-01 21:19:12 +00001114 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001115 p = dst;
1116
1117 while( dlen > 0 )
1118 {
1119 use_len = hlen;
1120 if( dlen < hlen )
1121 use_len = dlen;
1122
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001123#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001124 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001125 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001126 if( ( ret = mbedtls_md_update( &md_ctx, src, slen ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001127 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001128 if( ( ret = mbedtls_md_update( &md_ctx, counter, 4 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001129 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001130 if( ( ret = mbedtls_md_finish( &md_ctx, mask ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001131 goto exit;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001132#else
1133 if( ( status = psa_hash_setup( &op, alg ) ) != PSA_SUCCESS )
1134 goto exit;
1135 if( ( status = psa_hash_update( &op, src, slen ) ) != PSA_SUCCESS )
1136 goto exit;
1137 if( ( status = psa_hash_update( &op, counter, 4 ) ) != PSA_SUCCESS )
1138 goto exit;
1139 status = psa_hash_finish( &op, mask, sizeof( mask ), &out_len );
1140 if( status != PSA_SUCCESS )
1141 goto exit;
1142#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001143
1144 for( i = 0; i < use_len; ++i )
1145 *p++ ^= mask[i];
1146
1147 counter[3]++;
1148
1149 dlen -= use_len;
1150 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001151
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001152exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001153 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001154#if defined(MBEDTLS_MD_C)
1155 mbedtls_md_free( &md_ctx );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001156
1157 return( ret );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001158#else
1159 psa_hash_abort( &op );
1160
Przemek Stekiel2aae0402022-07-29 11:20:07 +02001161 return( mbedtls_md_error_from_psa( status ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001162#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001163}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001164
1165/**
1166 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1167 *
1168 * \param hash the input hash
1169 * \param hlen length of the input hash
1170 * \param salt the input salt
1171 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001172 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001173 * \param md_alg message digest to use
1174 */
1175static int hash_mprime( const unsigned char *hash, size_t hlen,
1176 const unsigned char *salt, size_t slen,
1177 unsigned char *out, mbedtls_md_type_t md_alg )
1178{
1179 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001180
1181#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001182 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001184
1185 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1186 if( md_info == NULL )
1187 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1188
1189 mbedtls_md_init( &md_ctx );
1190 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1191 goto exit;
1192 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1193 goto exit;
1194 if( ( ret = mbedtls_md_update( &md_ctx, zeros, sizeof( zeros ) ) ) != 0 )
1195 goto exit;
1196 if( ( ret = mbedtls_md_update( &md_ctx, hash, hlen ) ) != 0 )
1197 goto exit;
1198 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1199 goto exit;
1200 if( ( ret = mbedtls_md_finish( &md_ctx, out ) ) != 0 )
1201 goto exit;
1202
1203exit:
1204 mbedtls_md_free( &md_ctx );
1205
1206 return( ret );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001207#else
1208 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
1209 psa_algorithm_t alg = mbedtls_psa_translate_md( md_alg );
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001210 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED ;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001211 size_t out_size = PSA_HASH_LENGTH( alg );
1212 size_t out_len;
1213
1214 if( ( status = psa_hash_setup( &op, alg ) ) != PSA_SUCCESS )
1215 goto exit;
1216 if( ( status = psa_hash_update( &op, zeros, sizeof( zeros ) ) ) != PSA_SUCCESS )
1217 goto exit;
1218 if( ( status = psa_hash_update( &op, hash, hlen ) ) != PSA_SUCCESS )
1219 goto exit;
1220 if( ( status = psa_hash_update( &op, salt, slen ) ) != PSA_SUCCESS )
1221 goto exit;
1222 status = psa_hash_finish( &op, out, out_size, &out_len );
1223 if( status != PSA_SUCCESS )
1224 goto exit;
1225
1226exit:
1227 psa_hash_abort( &op );
1228
Przemek Stekiel2aae0402022-07-29 11:20:07 +02001229 return( mbedtls_md_error_from_psa( status ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001230#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001231}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001232
1233/**
1234 * Compute a hash.
1235 *
1236 * \param md_alg algorithm to use
1237 * \param input input message to hash
1238 * \param ilen input length
1239 * \param output the output buffer - must be large enough for \p md_alg
1240 */
1241static int compute_hash( mbedtls_md_type_t md_alg,
1242 const unsigned char *input, size_t ilen,
1243 unsigned char *output )
1244{
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001245#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001246 const mbedtls_md_info_t *md_info;
1247
1248 md_info = mbedtls_md_info_from_type( md_alg );
1249 if( md_info == NULL )
1250 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1251
1252 return( mbedtls_md( md_info, input, ilen, output ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001253#else
1254 psa_algorithm_t alg = mbedtls_psa_translate_md( md_alg );
1255 psa_status_t status;
1256 size_t out_size = PSA_HASH_LENGTH( alg );
1257 size_t out_len;
1258
1259 status = psa_hash_compute( alg, input, ilen, output, out_size, &out_len );
1260
Przemek Stekiel2aae0402022-07-29 11:20:07 +02001261 return( mbedtls_md_error_from_psa( status ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001262#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001263}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001267/*
1268 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001271 int (*f_rng)(void *, unsigned char *, size_t),
1272 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001273 const unsigned char *label, size_t label_len,
1274 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001275 const unsigned char *input,
1276 unsigned char *output )
1277{
1278 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001279 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001280 unsigned char *p = output;
1281 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001282
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001283 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001285
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001286 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1287 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001289
1290 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001291
Simon Butcher02037452016-03-01 21:19:12 +00001292 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001293 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001295
1296 memset( output, 0, olen );
1297
1298 *p++ = 0;
1299
Simon Butcher02037452016-03-01 21:19:12 +00001300 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001301 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001302 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001303
1304 p += hlen;
1305
Simon Butcher02037452016-03-01 21:19:12 +00001306 /* Construct DB */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001307 ret = compute_hash( (mbedtls_md_type_t) ctx->hash_id, label, label_len, p );
1308 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001309 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001310 p += hlen;
1311 p += olen - 2 * hlen - 2 - ilen;
1312 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001313 if( ilen != 0 )
1314 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001315
Simon Butcher02037452016-03-01 21:19:12 +00001316 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001317 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001318 ctx->hash_id ) ) != 0 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02001319 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001320
Simon Butcher02037452016-03-01 21:19:12 +00001321 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001322 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001323 ctx->hash_id ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001324 return( ret );
1325
Thomas Daubney141700f2021-05-13 19:06:10 +01001326 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001327}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001331/*
1332 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001335 int (*f_rng)(void *, unsigned char *, size_t),
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001336 void *p_rng, size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001337 const unsigned char *input,
1338 unsigned char *output )
1339{
1340 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001342 unsigned char *p = output;
1343
Paul Bakkerb3869132013-02-28 17:21:01 +01001344 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001345
Simon Butcher02037452016-03-01 21:19:12 +00001346 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001347 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001349
1350 nb_pad = olen - 3 - ilen;
1351
1352 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001353
1354 if( f_rng == NULL )
1355 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1356
1357 *p++ = MBEDTLS_RSA_CRYPT;
1358
1359 while( nb_pad-- > 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001360 {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001361 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001362
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001363 do {
1364 ret = f_rng( p_rng, p, 1 );
1365 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001366
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001367 /* Check if RNG failed to generate data */
1368 if( rng_dl == 0 || ret != 0 )
1369 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001370
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001371 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001372 }
1373
1374 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001375 if( ilen != 0 )
1376 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001377
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001378 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001379}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001381
Paul Bakker5121ce52009-01-03 21:22:43 +00001382/*
1383 * Add the message padding, then do an RSA operation
1384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001386 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001387 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +01001388 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001389 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 unsigned char *output )
1391{
Paul Bakker5121ce52009-01-03 21:22:43 +00001392 switch( ctx->padding )
1393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#if defined(MBEDTLS_PKCS1_V15)
1395 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney21772772021-05-13 17:30:32 +01001396 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001397 ilen, input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001398#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_PKCS1_V21)
1401 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney141700f2021-05-13 19:06:10 +01001402 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, NULL, 0,
Thomas Daubney21772772021-05-13 17:30:32 +01001403 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001404#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001405
1406 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001409}
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001412/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001413 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001416 int (*f_rng)(void *, unsigned char *, size_t),
1417 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001418 const unsigned char *label, size_t label_len,
1419 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001420 const unsigned char *input,
1421 unsigned char *output,
1422 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001423{
Janos Follath24eed8d2019-11-22 13:21:35 +00001424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001425 size_t ilen, i, pad_len;
1426 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001428 unsigned char lhash[HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001429 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001430
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001431 /*
1432 * Parameters sanity checks
1433 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001434 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001436
1437 ilen = ctx->len;
1438
Paul Bakker27fdf462011-06-09 13:55:13 +00001439 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001441
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001442 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1443 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001445
Janos Follathc17cda12016-02-11 11:08:18 +00001446 // checking for integer underflow
1447 if( 2 * hlen + 2 > ilen )
1448 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1449
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001450 /*
1451 * RSA operation
1452 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001453 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
1455 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001456 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001457
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001458 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001459 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001460 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001461 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001462 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001463 ctx->hash_id ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001464 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001465 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001466 ctx->hash_id ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001467 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001468 goto cleanup;
1469 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001470
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001471 /* Generate lHash */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001472 ret = compute_hash( (mbedtls_md_type_t) ctx->hash_id,
1473 label, label_len, lhash );
1474 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001475 goto cleanup;
1476
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001477 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001478 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001479 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001481 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001482
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001483 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001484
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001485 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001486
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001487 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001488 for( i = 0; i < hlen; i++ )
1489 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001490
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001491 /* Get zero-padding len, but always read till end of buffer
1492 * (minus one, for the 01 byte) */
1493 pad_len = 0;
1494 pad_done = 0;
1495 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1496 {
1497 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001498 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001499 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001500
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001501 p += pad_len;
1502 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001503
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001504 /*
1505 * The only information "leaked" is whether the padding was correct or not
1506 * (eg, no data is copied if it was not correct). This meets the
1507 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1508 * the different error conditions.
1509 */
1510 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001511 {
1512 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1513 goto cleanup;
1514 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001515
Paul Bakker66d5d072014-06-17 16:39:18 +02001516 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001517 {
1518 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1519 goto cleanup;
1520 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001521
1522 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001523 if( *olen != 0 )
1524 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001525 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001526
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001527cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001528 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1529 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001530
1531 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001532}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001536/*
1537 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1538 */
1539int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
1540 int (*f_rng)(void *, unsigned char *, size_t),
1541 void *p_rng,
1542 size_t *olen,
1543 const unsigned char *input,
1544 unsigned char *output,
1545 size_t output_max_len )
1546{
1547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1548 size_t ilen;
1549 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1550
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001551 ilen = ctx->len;
1552
1553 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1554 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1555
1556 if( ilen < 16 || ilen > sizeof( buf ) )
1557 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1558
1559 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
1560
1561 if( ret != 0 )
1562 goto cleanup;
1563
Gabor Mezei90437e32021-10-20 11:59:27 +02001564 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding( buf, ilen,
Gabor Mezei63bbba52021-10-18 16:17:57 +02001565 output, output_max_len, olen );
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001566
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001567cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001568 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001569
1570 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001573
1574/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001575 * Do an RSA operation, then remove the message padding
1576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001578 int (*f_rng)(void *, unsigned char *, size_t),
1579 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001580 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001581 const unsigned char *input,
1582 unsigned char *output,
1583 size_t output_max_len)
1584{
1585 switch( ctx->padding )
1586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#if defined(MBEDTLS_PKCS1_V15)
1588 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney34733082021-05-12 09:24:29 +01001589 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001590 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001591#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#if defined(MBEDTLS_PKCS1_V21)
1594 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001595 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001596 olen, input, output,
1597 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001598#endif
1599
1600 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001602 }
1603}
1604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001606static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001607 int (*f_rng)(void *, unsigned char *, size_t),
1608 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001610 unsigned int hashlen,
1611 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001612 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001613 unsigned char *sig )
1614{
1615 size_t olen;
1616 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001617 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001618 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001620 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001621
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001622 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
1623 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Paul Bakkerb3869132013-02-28 17:21:01 +01001624
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001625 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1626 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1627
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001628 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001630
1631 olen = ctx->len;
1632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001634 {
Simon Butcher02037452016-03-01 21:19:12 +00001635 /* Gather length of hash to sign */
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001636 size_t exp_hashlen = mbedtls_hash_info_get_size( md_alg );
1637 if( exp_hashlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001639
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001640 if( hashlen != exp_hashlen )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001641 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001642 }
1643
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001644 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1645 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001647
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001648 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1649 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001650 /* Calculate the largest possible salt length, up to the hash size.
1651 * Normally this is the hash length, which is the maximum salt length
1652 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001653 * enough room, use the maximum salt length that fits. The constraint is
1654 * that the hash length plus the salt length plus 2 bytes must be at most
1655 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1656 * (PKCS#1 v2.2) §9.1.1 step 3. */
1657 min_slen = hlen - 2;
1658 if( olen < hlen + min_slen + 2 )
1659 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1660 else if( olen >= hlen + hlen + 2 )
1661 slen = hlen;
1662 else
1663 slen = olen - hlen - 2;
1664 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001665 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001668 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001669 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001670 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001671 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001672 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001673
1674 memset( sig, 0, olen );
1675
Simon Butcher02037452016-03-01 21:19:12 +00001676 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001677 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001678 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001679 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001680
1681 /* Generate salt of length slen in place in the encoded message */
1682 salt = p;
1683 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001684 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001685
Paul Bakkerb3869132013-02-28 17:21:01 +01001686 p += slen;
1687
Simon Butcher02037452016-03-01 21:19:12 +00001688 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001689 ret = hash_mprime( hash, hashlen, salt, slen, p, ctx->hash_id );
1690 if( ret != 0 )
1691 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001692
Simon Butcher02037452016-03-01 21:19:12 +00001693 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001694 if( msb % 8 == 0 )
1695 offset = 1;
1696
Simon Butcher02037452016-03-01 21:19:12 +00001697 /* maskedDB: Apply dbMask to DB */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001698 ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1699 ctx->hash_id );
1700 if( ret != 0 )
1701 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001702
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001703 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001704 sig[0] &= 0xFF >> ( olen * 8 - msb );
1705
1706 p += hlen;
1707 *p++ = 0xBC;
1708
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001709 return mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001710}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001711
1712/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001713 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1714 * the option to pass in the salt length.
1715 */
1716int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1717 int (*f_rng)(void *, unsigned char *, size_t),
1718 void *p_rng,
1719 mbedtls_md_type_t md_alg,
1720 unsigned int hashlen,
1721 const unsigned char *hash,
1722 int saltlen,
1723 unsigned char *sig )
1724{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001725 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001726 hashlen, hash, saltlen, sig );
1727}
1728
1729
1730/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001731 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1732 */
1733int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1734 int (*f_rng)(void *, unsigned char *, size_t),
1735 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001736 mbedtls_md_type_t md_alg,
1737 unsigned int hashlen,
1738 const unsigned char *hash,
1739 unsigned char *sig )
1740{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001741 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001742 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001747/*
1748 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1749 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001750
1751/* Construct a PKCS v1.5 encoding of a hashed message
1752 *
1753 * This is used both for signature generation and verification.
1754 *
1755 * Parameters:
1756 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001757 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001758 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001759 * - hash: Buffer containing the hashed message or the raw data.
1760 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001761 * - dst: Buffer to hold the encoded message.
1762 *
1763 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001764 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001765 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001766 *
1767 */
1768static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1769 unsigned int hashlen,
1770 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001771 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001772 unsigned char *dst )
1773{
1774 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001775 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001776 unsigned char *p = dst;
1777 const char *oid = NULL;
1778
1779 /* Are we signing hashed or raw data? */
1780 if( md_alg != MBEDTLS_MD_NONE )
1781 {
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +02001782 unsigned char md_size = mbedtls_hash_info_get_size( md_alg );
Manuel Pégourié-Gonnardf493f2a2022-07-05 17:41:05 +02001783 if( md_size == 0 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001784 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1785
1786 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1787 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1788
Manuel Pégourié-Gonnardf493f2a2022-07-05 17:41:05 +02001789 if( hashlen != md_size )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001790 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001791
1792 /* Double-check that 8 + hashlen + oid_size can be used as a
1793 * 1-byte ASN.1 length encoding and that there's no overflow. */
1794 if( 8 + hashlen + oid_size >= 0x80 ||
1795 10 + hashlen < hashlen ||
1796 10 + hashlen + oid_size < 10 + hashlen )
1797 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1798
1799 /*
1800 * Static bounds check:
1801 * - Need 10 bytes for five tag-length pairs.
1802 * (Insist on 1-byte length encodings to protect against variants of
1803 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1804 * - Need hashlen bytes for hash
1805 * - Need oid_size bytes for hash alg OID.
1806 */
1807 if( nb_pad < 10 + hashlen + oid_size )
1808 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1809 nb_pad -= 10 + hashlen + oid_size;
1810 }
1811 else
1812 {
1813 if( nb_pad < hashlen )
1814 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1815
1816 nb_pad -= hashlen;
1817 }
1818
Hanno Becker2b2f8982017-09-27 17:10:03 +01001819 /* Need space for signature header and padding delimiter (3 bytes),
1820 * and 8 bytes for the minimal padding */
1821 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001822 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1823 nb_pad -= 3;
1824
1825 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001826 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001827
1828 /* Write signature header and padding */
1829 *p++ = 0;
1830 *p++ = MBEDTLS_RSA_SIGN;
1831 memset( p, 0xFF, nb_pad );
1832 p += nb_pad;
1833 *p++ = 0;
1834
1835 /* Are we signing raw data? */
1836 if( md_alg == MBEDTLS_MD_NONE )
1837 {
1838 memcpy( p, hash, hashlen );
1839 return( 0 );
1840 }
1841
1842 /* Signing hashed data, add corresponding ASN.1 structure
1843 *
1844 * DigestInfo ::= SEQUENCE {
1845 * digestAlgorithm DigestAlgorithmIdentifier,
1846 * digest Digest }
1847 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1848 * Digest ::= OCTET STRING
1849 *
1850 * Schematic:
1851 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1852 * TAG-NULL + LEN [ NULL ] ]
1853 * TAG-OCTET + LEN [ HASH ] ]
1854 */
1855 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001856 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001857 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001858 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001859 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001860 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001861 memcpy( p, oid, oid_size );
1862 p += oid_size;
1863 *p++ = MBEDTLS_ASN1_NULL;
1864 *p++ = 0x00;
1865 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001866 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001867 memcpy( p, hash, hashlen );
1868 p += hashlen;
1869
1870 /* Just a sanity-check, should be automatic
1871 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001872 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001873 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001874 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001875 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1876 }
1877
1878 return( 0 );
1879}
1880
Paul Bakkerb3869132013-02-28 17:21:01 +01001881/*
1882 * Do an RSA operation to sign the message digest
1883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001885 int (*f_rng)(void *, unsigned char *, size_t),
1886 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001888 unsigned int hashlen,
1889 const unsigned char *hash,
1890 unsigned char *sig )
1891{
Janos Follath24eed8d2019-11-22 13:21:35 +00001892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001893 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001894
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001895 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
1896 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001897
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001898 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1899 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1900
Hanno Beckerfdf38032017-09-06 12:35:55 +01001901 /*
1902 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1903 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001904
Hanno Beckerfdf38032017-09-06 12:35:55 +01001905 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1906 ctx->len, sig ) ) != 0 )
1907 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001908
Hanno Beckerfdf38032017-09-06 12:35:55 +01001909 /* Private key operation
1910 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001911 * In order to prevent Lenstra's attack, make the signature in a
1912 * temporary buffer and check it before returning it.
1913 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001914
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001915 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001916 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001917 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1918
Hanno Beckerfdf38032017-09-06 12:35:55 +01001919 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001920 if( verif == NULL )
1921 {
1922 mbedtls_free( sig_try );
1923 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1924 }
1925
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001926 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1927 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1928
Gabor Mezei90437e32021-10-20 11:59:27 +02001929 if( mbedtls_ct_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001930 {
1931 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1932 goto cleanup;
1933 }
1934
1935 memcpy( sig, sig_try, ctx->len );
1936
1937cleanup:
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001938 mbedtls_platform_zeroize( sig_try, ctx->len );
1939 mbedtls_platform_zeroize( verif, ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001940 mbedtls_free( sig_try );
1941 mbedtls_free( verif );
1942
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001943 if( ret != 0 )
1944 memset( sig, '!', ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001945 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001946}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001948
1949/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 * Do an RSA operation to sign the message digest
1951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001953 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001954 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001956 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001957 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 unsigned char *sig )
1959{
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001960 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
1961 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001962
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 switch( ctx->padding )
1964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#if defined(MBEDTLS_PKCS1_V15)
1966 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney52654982021-05-18 16:54:00 +01001967 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng,
Thomas Daubney140184d2021-05-18 16:04:07 +01001968 md_alg, hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001969#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971#if defined(MBEDTLS_PKCS1_V21)
1972 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyde9fdc42021-05-18 17:10:04 +01001973 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
1974 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001975#endif
1976
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001980}
1981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001983/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001984 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001988 unsigned int hashlen,
1989 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001991 int expected_salt_len,
1992 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001993{
Janos Follath24eed8d2019-11-22 13:21:35 +00001994 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001995 size_t siglen;
1996 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001997 unsigned char *hash_start;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001998 unsigned char result[HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001999 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002000 size_t observed_salt_len, msb;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07002001 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = {0};
Paul Bakkerb3869132013-02-28 17:21:01 +01002002
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002003 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
2004 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002005
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 siglen = ctx->len;
2007
Paul Bakker27fdf462011-06-09 13:55:13 +00002008 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002010
Thomas Daubney782a7f52021-05-19 12:27:35 +01002011 ret = mbedtls_rsa_public( ctx, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002012
2013 if( ret != 0 )
2014 return( ret );
2015
2016 p = buf;
2017
Paul Bakkerb3869132013-02-28 17:21:01 +01002018 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 {
Simon Butcher02037452016-03-01 21:19:12 +00002023 /* Gather length of hash to sign */
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002024 size_t exp_hashlen = mbedtls_hash_info_get_size( md_alg );
2025 if( exp_hashlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002027
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002028 if( hashlen != exp_hashlen )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002029 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002030 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002031
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002032 hlen = mbedtls_hash_info_get_size( mgf1_hash_id );
2033 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002035
Simon Butcher02037452016-03-01 21:19:12 +00002036 /*
2037 * Note: EMSA-PSS verification is over the length of N - 1 bits
2038 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002039 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002040
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002041 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2042 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2043
Simon Butcher02037452016-03-01 21:19:12 +00002044 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002045 if( msb % 8 == 0 )
2046 {
2047 p++;
2048 siglen -= 1;
2049 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002050
Gilles Peskine139108a2017-10-18 19:03:42 +02002051 if( siglen < hlen + 2 )
2052 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2053 hash_start = p + siglen - hlen - 1;
2054
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02002055 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id );
Jaeden Amero66954e12018-01-25 16:05:54 +00002056 if( ret != 0 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002057 return( ret );
Paul Bakker02303e82013-01-03 11:08:31 +01002058
Paul Bakkerb3869132013-02-28 17:21:01 +01002059 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002060
Gilles Peskine6a54b022017-10-17 19:02:13 +02002061 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002062 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002063
Gilles Peskine91048a32017-10-19 17:46:14 +02002064 if( *p++ != 0x01 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002065 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002066
Gilles Peskine6a54b022017-10-17 19:02:13 +02002067 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002070 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002071 {
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002072 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002073 }
2074
Simon Butcher02037452016-03-01 21:19:12 +00002075 /*
2076 * Generate H = Hash( M' )
2077 */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002078 ret = hash_mprime( hash, hashlen, p, observed_salt_len,
2079 result, mgf1_hash_id );
2080 if( ret != 0 )
2081 return( ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002082
Jaeden Amero66954e12018-01-25 16:05:54 +00002083 if( memcmp( hash_start, result, hlen ) != 0 )
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002084 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002085
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002086 return( 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01002087}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002088
2089/*
2090 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002094 unsigned int hashlen,
2095 const unsigned char *hash,
2096 const unsigned char *sig )
2097{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002098 mbedtls_md_type_t mgf1_hash_id;
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002099 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
2100 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002101
2102 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002104 : md_alg;
2105
Thomas Daubney9e65f792021-05-19 12:18:58 +01002106 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx,
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002107 md_alg, hashlen, hash,
2108 mgf1_hash_id,
2109 MBEDTLS_RSA_SALT_LEN_ANY,
2110 sig ) );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002111
2112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002116/*
2117 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002121 unsigned int hashlen,
2122 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002123 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002124{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002125 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002126 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002127 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002128
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002129 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
2130 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002131
2132 sig_len = ctx->len;
2133
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002134 /*
2135 * Prepare expected PKCS1 v1.5 encoding of hash.
2136 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002137
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002138 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2139 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2140 {
2141 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2142 goto cleanup;
2143 }
2144
2145 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2146 encoded_expected ) ) != 0 )
2147 goto cleanup;
2148
2149 /*
2150 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2151 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002152
Thomas Daubney41e4ce42021-05-19 15:10:05 +01002153 ret = mbedtls_rsa_public( ctx, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002154 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002155 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002156
Simon Butcher02037452016-03-01 21:19:12 +00002157 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002158 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002159 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002160
Gabor Mezei90437e32021-10-20 11:59:27 +02002161 if( ( ret = mbedtls_ct_memcmp( encoded, encoded_expected,
gabor-mezei-arm46025642021-07-19 15:19:19 +02002162 sig_len ) ) != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002163 {
2164 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2165 goto cleanup;
2166 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002167
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002168cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002169
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002170 if( encoded != NULL )
2171 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002172 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002173 mbedtls_free( encoded );
2174 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002175
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002176 if( encoded_expected != NULL )
2177 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002178 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002179 mbedtls_free( encoded_expected );
2180 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002181
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002182 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
2186/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002187 * Do an RSA operation and check the message digest
2188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002191 unsigned int hashlen,
2192 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002193 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002194{
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002195 if( ( md_alg != MBEDTLS_MD_NONE || hashlen != 0 ) && hash == NULL )
2196 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002197
Paul Bakkerb3869132013-02-28 17:21:01 +01002198 switch( ctx->padding )
2199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#if defined(MBEDTLS_PKCS1_V15)
2201 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney2e126252021-05-19 11:48:53 +01002202 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, md_alg,
2203 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002204#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206#if defined(MBEDTLS_PKCS1_V21)
2207 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002208 return mbedtls_rsa_rsassa_pss_verify( ctx, md_alg,
2209 hashlen, hash, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01002210#endif
2211
2212 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002214 }
2215}
2216
2217/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002218 * Copy the components of an RSA key
2219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002221{
Janos Follath24eed8d2019-11-22 13:21:35 +00002222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002223
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002224 dst->len = src->len;
2225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2227 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2230 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2231 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002232
2233#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2235 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2236 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2238 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002239#endif
2240
2241 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2244 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002245
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002246 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002247 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002248
2249cleanup:
2250 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002252
2253 return( ret );
2254}
2255
2256/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 * Free the components of an RSA key
2258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002260{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002261 if( ctx == NULL )
2262 return;
2263
2264 mbedtls_mpi_free( &ctx->Vi );
2265 mbedtls_mpi_free( &ctx->Vf );
2266 mbedtls_mpi_free( &ctx->RN );
2267 mbedtls_mpi_free( &ctx->D );
2268 mbedtls_mpi_free( &ctx->Q );
2269 mbedtls_mpi_free( &ctx->P );
2270 mbedtls_mpi_free( &ctx->E );
2271 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002272
Hanno Becker33c30a02017-08-23 07:00:22 +01002273#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002274 mbedtls_mpi_free( &ctx->RQ );
2275 mbedtls_mpi_free( &ctx->RP );
2276 mbedtls_mpi_free( &ctx->QP );
2277 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002278 mbedtls_mpi_free( &ctx->DP );
2279#endif /* MBEDTLS_RSA_NO_CRT */
2280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002282 /* Free the mutex, but only if it hasn't been freed already. */
2283 if( ctx->ver != 0 )
2284 {
2285 mbedtls_mutex_free( &ctx->mutex );
2286 ctx->ver = 0;
2287 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002288#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002289}
2290
Hanno Beckerab377312017-08-23 16:24:51 +01002291#endif /* !MBEDTLS_RSA_ALT */
2292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002295#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002296
2297/*
2298 * Example RSA-1024 keypair, for test purposes
2299 */
2300#define KEY_LEN 128
2301
2302#define RSA_N "9292758453063D803DD603D5E777D788" \
2303 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2304 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2305 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2306 "93A89813FBF3C4F8066D2D800F7C38A8" \
2307 "1AE31942917403FF4946B0A83D3D3E05" \
2308 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2309 "5E94BB77B07507233A0BC7BAC8F90F79"
2310
2311#define RSA_E "10001"
2312
2313#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2314 "66CA472BC44D253102F8B4A9D3BFA750" \
2315 "91386C0077937FE33FA3252D28855837" \
2316 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2317 "DF79C5CE07EE72C7F123142198164234" \
2318 "CABB724CF78B8173B9F880FC86322407" \
2319 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2320 "071513A1E85B5DFA031F21ECAE91A34D"
2321
2322#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2323 "2C01CAD19EA484A87EA4377637E75500" \
2324 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2325 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2326
2327#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2328 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2329 "910E4168387E3C30AA1E00C339A79508" \
2330 "8452DD96A9A5EA5D9DCA68DA636032AF"
2331
Paul Bakker5121ce52009-01-03 21:22:43 +00002332#define PT_LEN 24
2333#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2334 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002337static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002338{
gufe44c2620da2020-08-03 17:56:50 +02002339#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002340 size_t i;
2341
Paul Bakker545570e2010-07-18 09:00:25 +00002342 if( rng_state != NULL )
2343 rng_state = NULL;
2344
Paul Bakkera3d195c2011-11-27 21:07:34 +00002345 for( i = 0; i < len; ++i )
2346 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002347#else
2348 if( rng_state != NULL )
2349 rng_state = NULL;
2350
2351 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002352#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002353
Paul Bakkera3d195c2011-11-27 21:07:34 +00002354 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002357
Paul Bakker5121ce52009-01-03 21:22:43 +00002358/*
2359 * Checkup routine
2360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002362{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002363 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002365 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002367 unsigned char rsa_plaintext[PT_LEN];
2368 unsigned char rsa_decrypted[PT_LEN];
2369 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002371 unsigned char sha1sum[20];
2372#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002373
Hanno Becker3a701162017-08-22 13:52:43 +01002374 mbedtls_mpi K;
2375
2376 mbedtls_mpi_init( &K );
Ronald Cronc1905a12021-06-05 11:11:14 +02002377 mbedtls_rsa_init( &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
Hanno Becker3a701162017-08-22 13:52:43 +01002379 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2380 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2381 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2382 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2383 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2384 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2385 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2386 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2387 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2388 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2389
Hanno Becker7f25f852017-10-10 16:56:22 +01002390 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002391
2392 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2396 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 {
2398 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
Hanno Becker5bc87292017-05-03 15:09:31 +01002401 ret = 1;
2402 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 }
2404
2405 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002407
2408 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2409
Thomas Daubney21772772021-05-13 17:30:32 +01002410 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002411 PT_LEN, rsa_plaintext,
2412 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 {
2414 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002416
Hanno Becker5bc87292017-05-03 15:09:31 +01002417 ret = 1;
2418 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 }
2420
2421 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002423
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002424 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002425 &len, rsa_ciphertext, rsa_decrypted,
2426 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 {
2428 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002430
Hanno Becker5bc87292017-05-03 15:09:31 +01002431 ret = 1;
2432 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 }
2434
2435 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2436 {
2437 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002439
Hanno Becker5bc87292017-05-03 15:09:31 +01002440 ret = 1;
2441 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 }
2443
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002444 if( verbose != 0 )
2445 mbedtls_printf( "passed\n" );
2446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002449 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
TRodziewicz26371e42021-06-08 16:45:41 +02002451 if( mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002452 {
2453 if( verbose != 0 )
2454 mbedtls_printf( "failed\n" );
2455
2456 return( 1 );
2457 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
Hanno Becker98838b02017-10-02 13:16:10 +01002459 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002460 MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002461 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 {
2463 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Hanno Becker5bc87292017-05-03 15:09:31 +01002466 ret = 1;
2467 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 }
2469
2470 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002472
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002473 if( mbedtls_rsa_pkcs1_verify( &rsa, MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002474 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 {
2476 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
Hanno Becker5bc87292017-05-03 15:09:31 +01002479 ret = 1;
2480 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 }
2482
2483 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002484 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002487 if( verbose != 0 )
2488 mbedtls_printf( "\n" );
2489
Paul Bakker3d8fb632014-04-17 12:42:41 +02002490cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002491 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 mbedtls_rsa_free( &rsa );
2493#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002494 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002496 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002497}
2498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#endif /* MBEDTLS_RSA_C */