blob: 5e7cbcc8d6badd50c87acfbebe8edb7052c928cf [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
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050080/* Parameter validation macros */
81#define RSA_VALIDATE_RET( cond ) \
82 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
83#define RSA_VALIDATE( cond ) \
84 MBEDTLS_INTERNAL_VALIDATE( cond )
85
Hanno Becker617c1ae2017-08-23 14:11:24 +010086int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
87 const mbedtls_mpi *N,
88 const mbedtls_mpi *P, const mbedtls_mpi *Q,
89 const mbedtls_mpi *D, const mbedtls_mpi *E )
90{
Janos Follath24eed8d2019-11-22 13:21:35 +000091 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050092 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +010093
94 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
95 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
96 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
97 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
98 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
99 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100100 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100101 }
102
103 if( N != NULL )
104 ctx->len = mbedtls_mpi_size( &ctx->N );
105
106 return( 0 );
107}
108
109int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100110 unsigned char const *N, size_t N_len,
111 unsigned char const *P, size_t P_len,
112 unsigned char const *Q, size_t Q_len,
113 unsigned char const *D, size_t D_len,
114 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100115{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000116 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500117 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100118
119 if( N != NULL )
120 {
121 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
122 ctx->len = mbedtls_mpi_size( &ctx->N );
123 }
124
125 if( P != NULL )
126 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
127
128 if( Q != NULL )
129 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
130
131 if( D != NULL )
132 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
133
134 if( E != NULL )
135 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
136
137cleanup:
138
139 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100140 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100141
142 return( 0 );
143}
144
Hanno Becker705fc682017-10-10 17:57:02 +0100145/*
146 * Checks whether the context fields are set in such a way
147 * that the RSA primitives will be able to execute without error.
148 * It does *not* make guarantees for consistency of the parameters.
149 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100150static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
151 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100152{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100153#if !defined(MBEDTLS_RSA_NO_CRT)
154 /* blinding_needed is only used for NO_CRT to decide whether
155 * P,Q need to be present or not. */
156 ((void) blinding_needed);
157#endif
158
Hanno Becker3a760a12018-01-05 08:14:49 +0000159 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
160 ctx->len > MBEDTLS_MPI_MAX_SIZE )
161 {
Hanno Becker705fc682017-10-10 17:57:02 +0100162 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000163 }
Hanno Becker705fc682017-10-10 17:57:02 +0100164
165 /*
166 * 1. Modular exponentiation needs positive, odd moduli.
167 */
168
169 /* Modular exponentiation wrt. N is always used for
170 * RSA public key operations. */
171 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
172 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
173 {
174 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
175 }
176
177#if !defined(MBEDTLS_RSA_NO_CRT)
178 /* Modular exponentiation for P and Q is only
179 * used for private key operations and if CRT
180 * is used. */
181 if( is_priv &&
182 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
183 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
184 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
185 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
186 {
187 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
188 }
189#endif /* !MBEDTLS_RSA_NO_CRT */
190
191 /*
192 * 2. Exponents must be positive
193 */
194
195 /* Always need E for public key operations */
196 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
197 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
198
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100199#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100200 /* For private key operations, use D or DP & DQ
201 * as (unblinded) exponents. */
202 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
203 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
204#else
205 if( is_priv &&
206 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
207 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
208 {
209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
210 }
211#endif /* MBEDTLS_RSA_NO_CRT */
212
213 /* Blinding shouldn't make exponents negative either,
214 * so check that P, Q >= 1 if that hasn't yet been
215 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100216#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100217 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100218 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
219 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
220 {
221 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
222 }
223#endif
224
225 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100226 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100227#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100228 if( is_priv &&
229 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
230 {
231 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
232 }
233#endif
234
235 return( 0 );
236}
237
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100238int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100239{
240 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500241 int have_N, have_P, have_Q, have_D, have_E;
242#if !defined(MBEDTLS_RSA_NO_CRT)
243 int have_DP, have_DQ, have_QP;
244#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500245 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100246
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500247 RSA_VALIDATE_RET( ctx != NULL );
248
249 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
250 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
251 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
252 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
253 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500254
255#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500256 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
257 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
258 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500259#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100260
Hanno Becker617c1ae2017-08-23 14:11:24 +0100261 /*
262 * Check whether provided parameters are enough
263 * to deduce all others. The following incomplete
264 * parameter sets for private keys are supported:
265 *
266 * (1) P, Q missing.
267 * (2) D and potentially N missing.
268 *
269 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100270
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500271 n_missing = have_P && have_Q && have_D && have_E;
272 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
273 d_missing = have_P && have_Q && !have_D && have_E;
274 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100275
276 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500277 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100278
Hanno Becker617c1ae2017-08-23 14:11:24 +0100279 if( !is_priv && !is_pub )
280 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
281
282 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100283 * Step 1: Deduce N if P, Q are provided.
284 */
285
286 if( !have_N && have_P && have_Q )
287 {
288 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
289 &ctx->Q ) ) != 0 )
290 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100291 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100292 }
293
294 ctx->len = mbedtls_mpi_size( &ctx->N );
295 }
296
297 /*
298 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100299 */
300
301 if( pq_missing )
302 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100303 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100304 &ctx->P, &ctx->Q );
305 if( ret != 0 )
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 }
309 else if( d_missing )
310 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100311 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
312 &ctx->Q,
313 &ctx->E,
314 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100315 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100316 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100317 }
318 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100319
Hanno Becker617c1ae2017-08-23 14:11:24 +0100320 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100321 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100322 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100323 */
324
Hanno Becker23344b52017-08-23 07:43:27 +0100325#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500326 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327 {
328 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
329 &ctx->DP, &ctx->DQ, &ctx->QP );
330 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100331 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100332 }
Hanno Becker23344b52017-08-23 07:43:27 +0100333#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100334
335 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100336 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100337 */
338
Hanno Beckerebd2c022017-10-12 10:54:53 +0100339 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100340}
341
Hanno Becker617c1ae2017-08-23 14:11:24 +0100342int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
343 unsigned char *N, size_t N_len,
344 unsigned char *P, size_t P_len,
345 unsigned char *Q, size_t Q_len,
346 unsigned char *D, size_t D_len,
347 unsigned char *E, size_t E_len )
348{
349 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500350 int is_priv;
351 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100352
353 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500354 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100355 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
356 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
357 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
358 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
359 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
360
361 if( !is_priv )
362 {
363 /* If we're trying to export private parameters for a public key,
364 * something must be wrong. */
365 if( P != NULL || Q != NULL || D != NULL )
366 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
367
368 }
369
370 if( N != NULL )
371 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
372
373 if( P != NULL )
374 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
375
376 if( Q != NULL )
377 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
378
379 if( D != NULL )
380 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
381
382 if( E != NULL )
383 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100384
385cleanup:
386
387 return( ret );
388}
389
Hanno Becker617c1ae2017-08-23 14:11:24 +0100390int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
391 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
392 mbedtls_mpi *D, mbedtls_mpi *E )
393{
Janos Follath24eed8d2019-11-22 13:21:35 +0000394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500395 int is_priv;
396 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100397
398 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500399 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100400 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
401 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
402 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
403 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
404 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
405
406 if( !is_priv )
407 {
408 /* If we're trying to export private parameters for a public key,
409 * something must be wrong. */
410 if( P != NULL || Q != NULL || D != NULL )
411 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
412
413 }
414
415 /* Export all requested core parameters. */
416
417 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
418 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
419 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
420 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
421 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
422 {
423 return( ret );
424 }
425
426 return( 0 );
427}
428
429/*
430 * Export CRT parameters
431 * This must also be implemented if CRT is not used, for being able to
432 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
433 * can be used in this case.
434 */
435int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
436 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
437{
Janos Follath24eed8d2019-11-22 13:21:35 +0000438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500439 int is_priv;
440 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100441
442 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500443 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
445 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
446 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
447 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
448 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
449
450 if( !is_priv )
451 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
452
Hanno Beckerdc95c892017-08-23 06:57:02 +0100453#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100454 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100455 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
456 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
457 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
458 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100459 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100460 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100461#else
462 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
463 DP, DQ, QP ) ) != 0 )
464 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100465 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100466 }
467#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100468
469 return( 0 );
470}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100471
Paul Bakker5121ce52009-01-03 21:22:43 +0000472/*
473 * Initialize an RSA context
474 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200475void mbedtls_rsa_init( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000476{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500477 RSA_VALIDATE( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
Ronald Cronc1905a12021-06-05 11:11:14 +0200481 ctx->padding = MBEDTLS_RSA_PKCS_V15;
482 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100485 /* Set ctx->ver to nonzero to indicate that the mutex has been
486 * initialized and will need to be freed. */
487 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200489#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000490}
491
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100492/*
493 * Set padding for an existing RSA context
494 */
Ronald Cronea7631b2021-06-03 18:51:59 +0200495int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
496 mbedtls_md_type_t hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100497{
Ronald Cron3a0375f2021-06-08 10:22:28 +0200498 switch( padding )
499 {
500#if defined(MBEDTLS_PKCS1_V15)
501 case MBEDTLS_RSA_PKCS_V15:
502 break;
503#endif
504
505#if defined(MBEDTLS_PKCS1_V21)
506 case MBEDTLS_RSA_PKCS_V21:
507 break;
508#endif
509 default:
510 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
511 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200512
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200513#if defined(MBEDTLS_PKCS1_V21)
Ronald Cronea7631b2021-06-03 18:51:59 +0200514 if( ( padding == MBEDTLS_RSA_PKCS_V21 ) &&
515 ( hash_id != MBEDTLS_MD_NONE ) )
516 {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200517 /* Just make sure this hash is supported in this build. */
518 if( mbedtls_hash_info_get_size( hash_id ) == 0 )
Ronald Cronea7631b2021-06-03 18:51:59 +0200519 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
520 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200521#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500522
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100523 ctx->padding = padding;
524 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200525
526 return( 0 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100527}
528
Hanno Becker617c1ae2017-08-23 14:11:24 +0100529/*
530 * Get length in bytes of RSA modulus
531 */
532
533size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
534{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100535 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100536}
537
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
541/*
542 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800543 *
544 * This generation method follows the RSA key pair generation procedure of
545 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000548 int (*f_rng)(void *, unsigned char *, size_t),
549 void *p_rng,
550 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000551{
Janos Follath24eed8d2019-11-22 13:21:35 +0000552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800553 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100554 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500555 RSA_VALIDATE_RET( ctx != NULL );
556 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
Janos Follathb8fc1b02018-09-03 15:37:01 +0100558 /*
559 * If the modulus is 1024 bit long or shorter, then the security strength of
560 * the RSA algorithm is less than or equal to 80 bits and therefore an error
561 * rate of 2^-80 is sufficient.
562 */
563 if( nbits > 1024 )
564 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
565
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100566 mbedtls_mpi_init( &H );
567 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800568 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100570 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
571 {
572 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
573 goto cleanup;
574 }
575
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 /*
577 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800578 * 1. |P-Q| > 2^( nbits / 2 - 100 )
579 * 2. GCD( E, (P-1)*(Q-1) ) == 1
580 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
584 do
585 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100586 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
587 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
Janos Follathb8fc1b02018-09-03 15:37:01 +0100589 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
590 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800592 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
593 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
594 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 continue;
596
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800597 /* not required by any standards, but some users rely on the fact that P > Q */
598 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100599 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100600
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100601 /* Temporarily replace P,Q by P-1, Q-1 */
602 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
603 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
604 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800605
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800606 /* 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 +0200607 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800608 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
609 continue;
610
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800611 /* 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 -0800612 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
613 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
614 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
615
616 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
617 continue;
618
619 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800621 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100623 /* Restore P,Q */
624 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
625 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
626
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800627 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
628
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100629 ctx->len = mbedtls_mpi_size( &ctx->N );
630
Jethro Beekman97f95c92018-02-13 15:50:36 -0800631#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 * DP = D mod (P - 1)
634 * DQ = D mod (Q - 1)
635 * QP = Q^-1 mod P
636 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100637 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
638 &ctx->DP, &ctx->DQ, &ctx->QP ) );
639#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
Hanno Becker83aad1f2017-08-23 06:45:10 +0100641 /* Double-check */
642 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
644cleanup:
645
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100646 mbedtls_mpi_free( &H );
647 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800648 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
650 if( ret != 0 )
651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100653
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100654 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100655 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100656 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 }
658
Paul Bakker48377d92013-08-30 12:06:24 +0200659 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000660}
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
664/*
665 * Check a public RSA key
666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000668{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500669 RSA_VALIDATE_RET( ctx != NULL );
670
Hanno Beckerebd2c022017-10-12 10:54:53 +0100671 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000673
Hanno Becker3a760a12018-01-05 08:14:49 +0000674 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100677 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000678
Hanno Becker705fc682017-10-10 17:57:02 +0100679 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
680 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100684 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
686 return( 0 );
687}
688
689/*
Hanno Becker705fc682017-10-10 17:57:02 +0100690 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000693{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500694 RSA_VALIDATE_RET( ctx != NULL );
695
Hanno Becker705fc682017-10-10 17:57:02 +0100696 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100697 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 {
Hanno Becker98838b02017-10-02 13:16:10 +0100699 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 }
Paul Bakker48377d92013-08-30 12:06:24 +0200701
Hanno Becker98838b02017-10-02 13:16:10 +0100702 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100703 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100705 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000707
Hanno Beckerb269a852017-08-25 08:03:21 +0100708#if !defined(MBEDTLS_RSA_NO_CRT)
709 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
710 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
711 {
712 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
713 }
714#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000715
716 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000717}
718
719/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100720 * Check if contexts holding a public and private key match
721 */
Hanno Becker98838b02017-10-02 13:16:10 +0100722int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
723 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100724{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500725 RSA_VALIDATE_RET( pub != NULL );
726 RSA_VALIDATE_RET( prv != NULL );
727
Hanno Becker98838b02017-10-02 13:16:10 +0100728 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100732 }
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
735 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100738 }
739
740 return( 0 );
741}
742
743/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 * Do an RSA public key operation
745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000747 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000748 unsigned char *output )
749{
Janos Follath24eed8d2019-11-22 13:21:35 +0000750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000751 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500753 RSA_VALIDATE_RET( ctx != NULL );
754 RSA_VALIDATE_RET( input != NULL );
755 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Hanno Beckerebd2c022017-10-12 10:54:53 +0100757 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100758 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200762#if defined(MBEDTLS_THREADING_C)
763 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
764 return( ret );
765#endif
766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000770 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200771 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
772 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 }
774
775 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
777 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
779cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200781 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
782 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100783#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
787 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100788 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000789
790 return( 0 );
791}
792
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200793/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200794 * Generate or update blinding values, see section 10 of:
795 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200796 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200797 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200798 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200799static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200800 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
801{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200802 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200803 mbedtls_mpi R;
804
805 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200806
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200807 if( ctx->Vf.p != NULL )
808 {
809 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
811 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
812 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
813 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200814
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200815 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200816 }
817
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200818 /* Unblinding value: Vf = random number, invertible mod N */
819 do {
820 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200821 {
822 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
823 goto cleanup;
824 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 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 +0200827
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200828 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200829 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
830 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
831 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
832
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200833 /* At this point, Vi is invertible mod N if and only if both Vf and R
834 * are invertible mod N. If one of them isn't, we don't need to know
835 * which one, we just loop and choose new values for both of them.
836 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200837 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500838 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200839 goto cleanup;
840
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500841 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
842
843 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
844 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
845 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200846
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200847 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200848 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 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 +0200850
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200851
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200852cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200853 mbedtls_mpi_free( &R );
854
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200855 return( ret );
856}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200857
Paul Bakker5121ce52009-01-03 21:22:43 +0000858/*
Janos Follathe81102e2017-03-22 13:38:28 +0000859 * Exponent blinding supposed to prevent side-channel attacks using multiple
860 * traces of measurements to recover the RSA key. The more collisions are there,
861 * the more bits of the key can be recovered. See [3].
862 *
863 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800864 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000865 *
866 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800867 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000868 *
869 * (With the currently (as of 2017 April) known best algorithms breaking 2048
870 * bit RSA requires approximately as much time as trying out 2^112 random keys.
871 * Thus in this sense with 28 byte blinding the security is not reduced by
872 * side-channel attacks like the one in [3])
873 *
874 * This countermeasure does not help if the key recovery is possible with a
875 * single trace.
876 */
877#define RSA_EXPONENT_BLINDING 28
878
879/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000880 * Do an RSA private key operation
881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200883 int (*f_rng)(void *, unsigned char *, size_t),
884 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000885 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000886 unsigned char *output )
887{
Janos Follath24eed8d2019-11-22 13:21:35 +0000888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000889 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100890
891 /* Temporary holding the result */
892 mbedtls_mpi T;
893
894 /* Temporaries holding P-1, Q-1 and the
895 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000896 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100897
898#if !defined(MBEDTLS_RSA_NO_CRT)
899 /* Temporaries holding the results mod p resp. mod q. */
900 mbedtls_mpi TP, TQ;
901
902 /* Temporaries holding the blinded exponents for
903 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000904 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100905
906 /* Pointers to actual exponents to be used - either the unblinded
907 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000908 mbedtls_mpi *DP = &ctx->DP;
909 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100910#else
911 /* Temporary holding the blinded exponent (if used). */
912 mbedtls_mpi D_blind;
913
914 /* Pointer to actual exponent to be used - either the unblinded
915 * or the blinded one, depending on the presence of a PRNG. */
916 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100917#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100918
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100919 /* Temporaries holding the initial input and the double
920 * checked result; should be the same in the end. */
921 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500923 RSA_VALIDATE_RET( ctx != NULL );
924 RSA_VALIDATE_RET( input != NULL );
925 RSA_VALIDATE_RET( output != NULL );
926
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200927 if( f_rng == NULL )
928 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
929
930 if( rsa_check_context( ctx, 1 /* private key checks */,
931 1 /* blinding on */ ) != 0 )
Hanno Beckerebd2c022017-10-12 10:54:53 +0100932 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100933 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100934 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100935
Hanno Becker06811ce2017-05-03 15:10:34 +0100936#if defined(MBEDTLS_THREADING_C)
937 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
938 return( ret );
939#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000940
Hanno Becker06811ce2017-05-03 15:10:34 +0100941 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100942 mbedtls_mpi_init( &T );
943
944 mbedtls_mpi_init( &P1 );
945 mbedtls_mpi_init( &Q1 );
946 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000947
Janos Follathe81102e2017-03-22 13:38:28 +0000948#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200949 mbedtls_mpi_init( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +0000950#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200951 mbedtls_mpi_init( &DP_blind );
952 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000953#endif
954
Hanno Becker06811ce2017-05-03 15:10:34 +0100955#if !defined(MBEDTLS_RSA_NO_CRT)
956 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200957#endif
958
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100959 mbedtls_mpi_init( &I );
960 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100961
962 /* End of MPI initialization */
963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
965 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000966 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200967 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
968 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 }
970
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100971 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100972
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200973 /*
974 * Blinding
975 * T = T * Vi mod N
976 */
977 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
978 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
979 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000980
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200981 /*
982 * Exponent blinding
983 */
984 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
985 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000986
Janos Follathf9203b42017-03-22 15:13:15 +0000987#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200988 /*
989 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
990 */
991 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
992 f_rng, p_rng ) );
993 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
994 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
995 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000996
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200997 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000998#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200999 /*
1000 * DP_blind = ( P - 1 ) * R + DP
1001 */
1002 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1003 f_rng, p_rng ) );
1004 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1005 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1006 &ctx->DP ) );
Janos Follathf9203b42017-03-22 15:13:15 +00001007
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001008 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001009
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001010 /*
1011 * DQ_blind = ( Q - 1 ) * R + DQ
1012 */
1013 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1014 f_rng, p_rng ) );
1015 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1016 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1017 &ctx->DQ ) );
Janos Follathf9203b42017-03-22 15:13:15 +00001018
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001019 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001020#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001023 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001024#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001025 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001026 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001028 * TP = input ^ dP mod P
1029 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001031
1032 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1033 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
1035 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001036 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001038 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1039 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1040 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
1042 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001043 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001045 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1046 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001048
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001049 /*
1050 * Unblind
1051 * T = T * Vf mod N
1052 */
1053 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
1054 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
Hanno Becker2dec5e82017-10-03 07:49:52 +01001056 /* Verify the result to prevent glitching attacks. */
1057 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1058 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001059 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001060 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001061 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1062 goto cleanup;
1063 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001064
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
1068cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001070 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1071 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001072#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001073
Hanno Becker06811ce2017-05-03 15:10:34 +01001074 mbedtls_mpi_free( &P1 );
1075 mbedtls_mpi_free( &Q1 );
1076 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001077
Janos Follathe81102e2017-03-22 13:38:28 +00001078#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001079 mbedtls_mpi_free( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +00001080#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001081 mbedtls_mpi_free( &DP_blind );
1082 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001083#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Hanno Becker06811ce2017-05-03 15:10:34 +01001085 mbedtls_mpi_free( &T );
1086
1087#if !defined(MBEDTLS_RSA_NO_CRT)
1088 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1089#endif
1090
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001091 mbedtls_mpi_free( &C );
1092 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001093
Gilles Peskineae3741e2020-11-25 00:10:31 +01001094 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001095 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001096
Gilles Peskineae3741e2020-11-25 00:10:31 +01001097 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001098}
1099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001101#if !defined(MBEDTLS_MD_C)
1102static int ret_from_status( psa_status_t status )
1103{
1104 switch( status )
1105 {
1106 case PSA_SUCCESS:
1107 return( 0 );
1108 case PSA_ERROR_NOT_SUPPORTED:
1109 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
1110 case PSA_ERROR_INVALID_ARGUMENT:
1111 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
1112 case PSA_ERROR_INSUFFICIENT_MEMORY:
1113 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
1114 default:
1115 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1116 }
1117}
1118#endif /* !MBEDTLS_MD_C */
1119
Paul Bakker9dcc3222011-03-08 14:16:06 +00001120/**
1121 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1122 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001123 * \param dst buffer to mask
1124 * \param dlen length of destination buffer
1125 * \param src source of the mask generation
1126 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001127 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001128 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001129static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001130 size_t slen, mbedtls_md_type_t md_alg )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001131{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001132 unsigned char counter[4];
1133 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001134 unsigned int hlen;
1135 size_t i, use_len;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001136 unsigned char mask[HASH_MAX_SIZE];
1137#if defined(MBEDTLS_MD_C)
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001138 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001139 const mbedtls_md_info_t *md_info;
1140 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001141
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001142 mbedtls_md_init( &md_ctx );
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001143 md_info = mbedtls_md_info_from_type( md_alg );
1144 if( md_info == NULL )
1145 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1146
1147 mbedtls_md_init( &md_ctx );
1148 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1149 goto exit;
1150
1151 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001152#else
1153 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
1154 psa_algorithm_t alg = mbedtls_psa_translate_md( md_alg );
1155 psa_status_t status = PSA_SUCCESS;
1156 size_t out_len;
1157
1158 hlen = PSA_HASH_LENGTH( alg );
1159#endif
1160
1161 memset( mask, 0, sizeof( mask ) );
1162 memset( counter, 0, 4 );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001163
Simon Butcher02037452016-03-01 21:19:12 +00001164 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001165 p = dst;
1166
1167 while( dlen > 0 )
1168 {
1169 use_len = hlen;
1170 if( dlen < hlen )
1171 use_len = dlen;
1172
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001173#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001174 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001175 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001176 if( ( ret = mbedtls_md_update( &md_ctx, src, slen ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001177 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001178 if( ( ret = mbedtls_md_update( &md_ctx, counter, 4 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001179 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001180 if( ( ret = mbedtls_md_finish( &md_ctx, mask ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001181 goto exit;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001182#else
1183 if( ( status = psa_hash_setup( &op, alg ) ) != PSA_SUCCESS )
1184 goto exit;
1185 if( ( status = psa_hash_update( &op, src, slen ) ) != PSA_SUCCESS )
1186 goto exit;
1187 if( ( status = psa_hash_update( &op, counter, 4 ) ) != PSA_SUCCESS )
1188 goto exit;
1189 status = psa_hash_finish( &op, mask, sizeof( mask ), &out_len );
1190 if( status != PSA_SUCCESS )
1191 goto exit;
1192#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001193
1194 for( i = 0; i < use_len; ++i )
1195 *p++ ^= mask[i];
1196
1197 counter[3]++;
1198
1199 dlen -= use_len;
1200 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001201
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001202exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001203 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001204#if defined(MBEDTLS_MD_C)
1205 mbedtls_md_free( &md_ctx );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001206
1207 return( ret );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001208#else
1209 psa_hash_abort( &op );
1210
1211 return( ret_from_status( status ) );
1212#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001213}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001214
1215/**
1216 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1217 *
1218 * \param hash the input hash
1219 * \param hlen length of the input hash
1220 * \param salt the input salt
1221 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001222 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001223 * \param md_alg message digest to use
1224 */
1225static int hash_mprime( const unsigned char *hash, size_t hlen,
1226 const unsigned char *salt, size_t slen,
1227 unsigned char *out, mbedtls_md_type_t md_alg )
1228{
1229 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001230
1231#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001232 mbedtls_md_context_t md_ctx;
1233 int ret;
1234
1235 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1236 if( md_info == NULL )
1237 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1238
1239 mbedtls_md_init( &md_ctx );
1240 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1241 goto exit;
1242 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1243 goto exit;
1244 if( ( ret = mbedtls_md_update( &md_ctx, zeros, sizeof( zeros ) ) ) != 0 )
1245 goto exit;
1246 if( ( ret = mbedtls_md_update( &md_ctx, hash, hlen ) ) != 0 )
1247 goto exit;
1248 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1249 goto exit;
1250 if( ( ret = mbedtls_md_finish( &md_ctx, out ) ) != 0 )
1251 goto exit;
1252
1253exit:
1254 mbedtls_md_free( &md_ctx );
1255
1256 return( ret );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001257#else
1258 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
1259 psa_algorithm_t alg = mbedtls_psa_translate_md( md_alg );
1260 psa_status_t status = PSA_SUCCESS;
1261 size_t out_size = PSA_HASH_LENGTH( alg );
1262 size_t out_len;
1263
1264 if( ( status = psa_hash_setup( &op, alg ) ) != PSA_SUCCESS )
1265 goto exit;
1266 if( ( status = psa_hash_update( &op, zeros, sizeof( zeros ) ) ) != PSA_SUCCESS )
1267 goto exit;
1268 if( ( status = psa_hash_update( &op, hash, hlen ) ) != PSA_SUCCESS )
1269 goto exit;
1270 if( ( status = psa_hash_update( &op, salt, slen ) ) != PSA_SUCCESS )
1271 goto exit;
1272 status = psa_hash_finish( &op, out, out_size, &out_len );
1273 if( status != PSA_SUCCESS )
1274 goto exit;
1275
1276exit:
1277 psa_hash_abort( &op );
1278
1279 return( ret_from_status( status ) );
1280#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001281}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001282
1283/**
1284 * Compute a hash.
1285 *
1286 * \param md_alg algorithm to use
1287 * \param input input message to hash
1288 * \param ilen input length
1289 * \param output the output buffer - must be large enough for \p md_alg
1290 */
1291static int compute_hash( mbedtls_md_type_t md_alg,
1292 const unsigned char *input, size_t ilen,
1293 unsigned char *output )
1294{
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001295#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001296 const mbedtls_md_info_t *md_info;
1297
1298 md_info = mbedtls_md_info_from_type( md_alg );
1299 if( md_info == NULL )
1300 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1301
1302 return( mbedtls_md( md_info, input, ilen, output ) );
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001303#else
1304 psa_algorithm_t alg = mbedtls_psa_translate_md( md_alg );
1305 psa_status_t status;
1306 size_t out_size = PSA_HASH_LENGTH( alg );
1307 size_t out_len;
1308
1309 status = psa_hash_compute( alg, input, ilen, output, out_size, &out_len );
1310
1311 return( ret_from_status( status ) );
1312#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001313}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001317/*
1318 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001321 int (*f_rng)(void *, unsigned char *, size_t),
1322 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001323 const unsigned char *label, size_t label_len,
1324 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001325 const unsigned char *input,
1326 unsigned char *output )
1327{
1328 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001330 unsigned char *p = output;
1331 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001332
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001333 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001334 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001335 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001336 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1337
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001338 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001340
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001341 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1342 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001344
1345 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001346
Simon Butcher02037452016-03-01 21:19:12 +00001347 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001348 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001350
1351 memset( output, 0, olen );
1352
1353 *p++ = 0;
1354
Simon Butcher02037452016-03-01 21:19:12 +00001355 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001356 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001357 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001358
1359 p += hlen;
1360
Simon Butcher02037452016-03-01 21:19:12 +00001361 /* Construct DB */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001362 ret = compute_hash( (mbedtls_md_type_t) ctx->hash_id, label, label_len, p );
1363 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001364 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001365 p += hlen;
1366 p += olen - 2 * hlen - 2 - ilen;
1367 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001368 if( ilen != 0 )
1369 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001370
Simon Butcher02037452016-03-01 21:19:12 +00001371 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001372 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001373 ctx->hash_id ) ) != 0 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02001374 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001375
Simon Butcher02037452016-03-01 21:19:12 +00001376 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001377 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001378 ctx->hash_id ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001379 return( ret );
1380
Thomas Daubney141700f2021-05-13 19:06:10 +01001381 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001386/*
1387 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001390 int (*f_rng)(void *, unsigned char *, size_t),
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001391 void *p_rng, size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001392 const unsigned char *input,
1393 unsigned char *output )
1394{
1395 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001397 unsigned char *p = output;
1398
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001399 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001400 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001401 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001402
Paul Bakkerb3869132013-02-28 17:21:01 +01001403 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001404
Simon Butcher02037452016-03-01 21:19:12 +00001405 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001406 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001408
1409 nb_pad = olen - 3 - ilen;
1410
1411 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001412
1413 if( f_rng == NULL )
1414 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1415
1416 *p++ = MBEDTLS_RSA_CRYPT;
1417
1418 while( nb_pad-- > 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001419 {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001420 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001421
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001422 do {
1423 ret = f_rng( p_rng, p, 1 );
1424 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001425
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001426 /* Check if RNG failed to generate data */
1427 if( rng_dl == 0 || ret != 0 )
1428 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001429
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001430 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001431 }
1432
1433 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001434 if( ilen != 0 )
1435 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001436
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001437 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001440
Paul Bakker5121ce52009-01-03 21:22:43 +00001441/*
1442 * Add the message padding, then do an RSA operation
1443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001445 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001446 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +01001447 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001448 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 unsigned char *output )
1450{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001451 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001452 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001453 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001454
Paul Bakker5121ce52009-01-03 21:22:43 +00001455 switch( ctx->padding )
1456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_PKCS1_V15)
1458 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney21772772021-05-13 17:30:32 +01001459 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001460 ilen, input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001461#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_PKCS1_V21)
1464 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney141700f2021-05-13 19:06:10 +01001465 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, NULL, 0,
Thomas Daubney21772772021-05-13 17:30:32 +01001466 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001467#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
1469 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001472}
1473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001475/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001476 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001479 int (*f_rng)(void *, unsigned char *, size_t),
1480 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001481 const unsigned char *label, size_t label_len,
1482 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001483 const unsigned char *input,
1484 unsigned char *output,
1485 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001486{
Janos Follath24eed8d2019-11-22 13:21:35 +00001487 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001488 size_t ilen, i, pad_len;
1489 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001491 unsigned char lhash[HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001492 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001493
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001494 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001495 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1496 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1497 RSA_VALIDATE_RET( input != NULL );
1498 RSA_VALIDATE_RET( olen != NULL );
1499
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001500 /*
1501 * Parameters sanity checks
1502 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001503 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001505
1506 ilen = ctx->len;
1507
Paul Bakker27fdf462011-06-09 13:55:13 +00001508 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001511 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1512 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001514
Janos Follathc17cda12016-02-11 11:08:18 +00001515 // checking for integer underflow
1516 if( 2 * hlen + 2 > ilen )
1517 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1518
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001519 /*
1520 * RSA operation
1521 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001522 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001523
1524 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001525 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001526
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001527 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001528 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001529 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001530 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001531 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001532 ctx->hash_id ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001533 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001534 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001535 ctx->hash_id ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001536 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001537 goto cleanup;
1538 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001539
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001540 /* Generate lHash */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001541 ret = compute_hash( (mbedtls_md_type_t) ctx->hash_id,
1542 label, label_len, lhash );
1543 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001544 goto cleanup;
1545
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001546 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001547 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001548 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001549 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001550 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001551
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001552 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001553
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001554 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001555
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001556 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001557 for( i = 0; i < hlen; i++ )
1558 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001559
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001560 /* Get zero-padding len, but always read till end of buffer
1561 * (minus one, for the 01 byte) */
1562 pad_len = 0;
1563 pad_done = 0;
1564 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1565 {
1566 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001567 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001568 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001569
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001570 p += pad_len;
1571 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001572
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001573 /*
1574 * The only information "leaked" is whether the padding was correct or not
1575 * (eg, no data is copied if it was not correct). This meets the
1576 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1577 * the different error conditions.
1578 */
1579 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001580 {
1581 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1582 goto cleanup;
1583 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001584
Paul Bakker66d5d072014-06-17 16:39:18 +02001585 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001586 {
1587 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1588 goto cleanup;
1589 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001590
1591 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001592 if( *olen != 0 )
1593 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001594 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001595
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001596cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001597 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1598 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001599
1600 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001601}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001605/*
1606 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1607 */
1608int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
1609 int (*f_rng)(void *, unsigned char *, size_t),
1610 void *p_rng,
1611 size_t *olen,
1612 const unsigned char *input,
1613 unsigned char *output,
1614 size_t output_max_len )
1615{
1616 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1617 size_t ilen;
1618 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1619
1620 RSA_VALIDATE_RET( ctx != NULL );
1621 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1622 RSA_VALIDATE_RET( input != NULL );
1623 RSA_VALIDATE_RET( olen != NULL );
1624
1625 ilen = ctx->len;
1626
1627 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1628 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1629
1630 if( ilen < 16 || ilen > sizeof( buf ) )
1631 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1632
1633 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
1634
1635 if( ret != 0 )
1636 goto cleanup;
1637
Gabor Mezei90437e32021-10-20 11:59:27 +02001638 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding( buf, ilen,
Gabor Mezei63bbba52021-10-18 16:17:57 +02001639 output, output_max_len, olen );
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001640
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001641cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001642 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001643
1644 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001645}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
1648/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001649 * Do an RSA operation, then remove the message padding
1650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001652 int (*f_rng)(void *, unsigned char *, size_t),
1653 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001654 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001655 const unsigned char *input,
1656 unsigned char *output,
1657 size_t output_max_len)
1658{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001659 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001660 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1661 RSA_VALIDATE_RET( input != NULL );
1662 RSA_VALIDATE_RET( olen != NULL );
1663
Paul Bakkerb3869132013-02-28 17:21:01 +01001664 switch( ctx->padding )
1665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#if defined(MBEDTLS_PKCS1_V15)
1667 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney34733082021-05-12 09:24:29 +01001668 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001669 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001670#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672#if defined(MBEDTLS_PKCS1_V21)
1673 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001674 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001675 olen, input, output,
1676 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001677#endif
1678
1679 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001681 }
1682}
1683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001685static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001686 int (*f_rng)(void *, unsigned char *, size_t),
1687 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001689 unsigned int hashlen,
1690 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001691 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001692 unsigned char *sig )
1693{
1694 size_t olen;
1695 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001696 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001697 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001699 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001700
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001701 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001702 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1703 hashlen == 0 ) ||
1704 hash != NULL );
1705 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001706
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001707 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1708 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1709
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001710 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001712
1713 olen = ctx->len;
1714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001716 {
Simon Butcher02037452016-03-01 21:19:12 +00001717 /* Gather length of hash to sign */
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001718 size_t exp_hashlen = mbedtls_hash_info_get_size( md_alg );
1719 if( exp_hashlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001721
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001722 if( hashlen != exp_hashlen )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001723 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001724 }
1725
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001726 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1727 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001729
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001730 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1731 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001732 /* Calculate the largest possible salt length, up to the hash size.
1733 * Normally this is the hash length, which is the maximum salt length
1734 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001735 * enough room, use the maximum salt length that fits. The constraint is
1736 * that the hash length plus the salt length plus 2 bytes must be at most
1737 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1738 * (PKCS#1 v2.2) §9.1.1 step 3. */
1739 min_slen = hlen - 2;
1740 if( olen < hlen + min_slen + 2 )
1741 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1742 else if( olen >= hlen + hlen + 2 )
1743 slen = hlen;
1744 else
1745 slen = olen - hlen - 2;
1746 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001747 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001750 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001751 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001752 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001753 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001754 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001755
1756 memset( sig, 0, olen );
1757
Simon Butcher02037452016-03-01 21:19:12 +00001758 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001759 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001760 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001761 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001762
1763 /* Generate salt of length slen in place in the encoded message */
1764 salt = p;
1765 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001766 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001767
Paul Bakkerb3869132013-02-28 17:21:01 +01001768 p += slen;
1769
Simon Butcher02037452016-03-01 21:19:12 +00001770 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001771 ret = hash_mprime( hash, hashlen, salt, slen, p, ctx->hash_id );
1772 if( ret != 0 )
1773 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001774
Simon Butcher02037452016-03-01 21:19:12 +00001775 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001776 if( msb % 8 == 0 )
1777 offset = 1;
1778
Simon Butcher02037452016-03-01 21:19:12 +00001779 /* maskedDB: Apply dbMask to DB */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001780 ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1781 ctx->hash_id );
1782 if( ret != 0 )
1783 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001784
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001785 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001786 sig[0] &= 0xFF >> ( olen * 8 - msb );
1787
1788 p += hlen;
1789 *p++ = 0xBC;
1790
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001791 return mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001792}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001793
1794/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001795 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1796 * the option to pass in the salt length.
1797 */
1798int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1799 int (*f_rng)(void *, unsigned char *, size_t),
1800 void *p_rng,
1801 mbedtls_md_type_t md_alg,
1802 unsigned int hashlen,
1803 const unsigned char *hash,
1804 int saltlen,
1805 unsigned char *sig )
1806{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001807 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001808 hashlen, hash, saltlen, sig );
1809}
1810
1811
1812/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001813 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1814 */
1815int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1816 int (*f_rng)(void *, unsigned char *, size_t),
1817 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001818 mbedtls_md_type_t md_alg,
1819 unsigned int hashlen,
1820 const unsigned char *hash,
1821 unsigned char *sig )
1822{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001823 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001824 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001825}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001829/*
1830 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1831 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001832
1833/* Construct a PKCS v1.5 encoding of a hashed message
1834 *
1835 * This is used both for signature generation and verification.
1836 *
1837 * Parameters:
1838 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001839 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001840 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001841 * - hash: Buffer containing the hashed message or the raw data.
1842 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001843 * - dst: Buffer to hold the encoded message.
1844 *
1845 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001846 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001847 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001848 *
1849 */
1850static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1851 unsigned int hashlen,
1852 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001853 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001854 unsigned char *dst )
1855{
1856 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001857 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001858 unsigned char *p = dst;
1859 const char *oid = NULL;
1860
1861 /* Are we signing hashed or raw data? */
1862 if( md_alg != MBEDTLS_MD_NONE )
1863 {
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +02001864 unsigned char md_size = mbedtls_hash_info_get_size( md_alg );
Manuel Pégourié-Gonnardf493f2a2022-07-05 17:41:05 +02001865 if( md_size == 0 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001866 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1867
1868 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1869 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1870
Manuel Pégourié-Gonnardf493f2a2022-07-05 17:41:05 +02001871 if( hashlen != md_size )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001872 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001873
1874 /* Double-check that 8 + hashlen + oid_size can be used as a
1875 * 1-byte ASN.1 length encoding and that there's no overflow. */
1876 if( 8 + hashlen + oid_size >= 0x80 ||
1877 10 + hashlen < hashlen ||
1878 10 + hashlen + oid_size < 10 + hashlen )
1879 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1880
1881 /*
1882 * Static bounds check:
1883 * - Need 10 bytes for five tag-length pairs.
1884 * (Insist on 1-byte length encodings to protect against variants of
1885 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1886 * - Need hashlen bytes for hash
1887 * - Need oid_size bytes for hash alg OID.
1888 */
1889 if( nb_pad < 10 + hashlen + oid_size )
1890 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1891 nb_pad -= 10 + hashlen + oid_size;
1892 }
1893 else
1894 {
1895 if( nb_pad < hashlen )
1896 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1897
1898 nb_pad -= hashlen;
1899 }
1900
Hanno Becker2b2f8982017-09-27 17:10:03 +01001901 /* Need space for signature header and padding delimiter (3 bytes),
1902 * and 8 bytes for the minimal padding */
1903 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001904 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1905 nb_pad -= 3;
1906
1907 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001908 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001909
1910 /* Write signature header and padding */
1911 *p++ = 0;
1912 *p++ = MBEDTLS_RSA_SIGN;
1913 memset( p, 0xFF, nb_pad );
1914 p += nb_pad;
1915 *p++ = 0;
1916
1917 /* Are we signing raw data? */
1918 if( md_alg == MBEDTLS_MD_NONE )
1919 {
1920 memcpy( p, hash, hashlen );
1921 return( 0 );
1922 }
1923
1924 /* Signing hashed data, add corresponding ASN.1 structure
1925 *
1926 * DigestInfo ::= SEQUENCE {
1927 * digestAlgorithm DigestAlgorithmIdentifier,
1928 * digest Digest }
1929 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1930 * Digest ::= OCTET STRING
1931 *
1932 * Schematic:
1933 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1934 * TAG-NULL + LEN [ NULL ] ]
1935 * TAG-OCTET + LEN [ HASH ] ]
1936 */
1937 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001938 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001939 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001940 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001941 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001942 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001943 memcpy( p, oid, oid_size );
1944 p += oid_size;
1945 *p++ = MBEDTLS_ASN1_NULL;
1946 *p++ = 0x00;
1947 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001948 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001949 memcpy( p, hash, hashlen );
1950 p += hashlen;
1951
1952 /* Just a sanity-check, should be automatic
1953 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001954 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001955 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001956 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001957 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1958 }
1959
1960 return( 0 );
1961}
1962
Paul Bakkerb3869132013-02-28 17:21:01 +01001963/*
1964 * Do an RSA operation to sign the message digest
1965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001967 int (*f_rng)(void *, unsigned char *, size_t),
1968 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001970 unsigned int hashlen,
1971 const unsigned char *hash,
1972 unsigned char *sig )
1973{
Janos Follath24eed8d2019-11-22 13:21:35 +00001974 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001975 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001976
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001977 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001978 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1979 hashlen == 0 ) ||
1980 hash != NULL );
1981 RSA_VALIDATE_RET( sig != NULL );
1982
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001983 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1984 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1985
Hanno Beckerfdf38032017-09-06 12:35:55 +01001986 /*
1987 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1988 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001989
Hanno Beckerfdf38032017-09-06 12:35:55 +01001990 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1991 ctx->len, sig ) ) != 0 )
1992 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001993
Hanno Beckerfdf38032017-09-06 12:35:55 +01001994 /* Private key operation
1995 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001996 * In order to prevent Lenstra's attack, make the signature in a
1997 * temporary buffer and check it before returning it.
1998 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001999
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002000 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002001 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002002 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2003
Hanno Beckerfdf38032017-09-06 12:35:55 +01002004 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002005 if( verif == NULL )
2006 {
2007 mbedtls_free( sig_try );
2008 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2009 }
2010
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002011 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2012 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2013
Gabor Mezei90437e32021-10-20 11:59:27 +02002014 if( mbedtls_ct_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002015 {
2016 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2017 goto cleanup;
2018 }
2019
2020 memcpy( sig, sig_try, ctx->len );
2021
2022cleanup:
Gilles Peskine14d5fef2021-12-13 12:37:55 +01002023 mbedtls_platform_zeroize( sig_try, ctx->len );
2024 mbedtls_platform_zeroize( verif, ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002025 mbedtls_free( sig_try );
2026 mbedtls_free( verif );
2027
Gilles Peskine14d5fef2021-12-13 12:37:55 +01002028 if( ret != 0 )
2029 memset( sig, '!', ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002030 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002031}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002033
2034/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 * Do an RSA operation to sign the message digest
2036 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002038 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002039 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002041 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002042 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 unsigned char *sig )
2044{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002045 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002046 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2047 hashlen == 0 ) ||
2048 hash != NULL );
2049 RSA_VALIDATE_RET( sig != NULL );
2050
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 switch( ctx->padding )
2052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053#if defined(MBEDTLS_PKCS1_V15)
2054 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney52654982021-05-18 16:54:00 +01002055 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng,
Thomas Daubney140184d2021-05-18 16:04:07 +01002056 md_alg, hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059#if defined(MBEDTLS_PKCS1_V21)
2060 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyde9fdc42021-05-18 17:10:04 +01002061 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
2062 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002063#endif
2064
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002068}
2069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002071/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002072 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002076 unsigned int hashlen,
2077 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002079 int expected_salt_len,
2080 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002081{
Janos Follath24eed8d2019-11-22 13:21:35 +00002082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002083 size_t siglen;
2084 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002085 unsigned char *hash_start;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02002086 unsigned char result[HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002087 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002088 size_t observed_salt_len, msb;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07002089 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = {0};
Paul Bakkerb3869132013-02-28 17:21:01 +01002090
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002091 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002092 RSA_VALIDATE_RET( sig != NULL );
2093 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2094 hashlen == 0 ) ||
2095 hash != NULL );
2096
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 siglen = ctx->len;
2098
Paul Bakker27fdf462011-06-09 13:55:13 +00002099 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101
Thomas Daubney782a7f52021-05-19 12:27:35 +01002102 ret = mbedtls_rsa_public( ctx, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002103
2104 if( ret != 0 )
2105 return( ret );
2106
2107 p = buf;
2108
Paul Bakkerb3869132013-02-28 17:21:01 +01002109 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 {
Simon Butcher02037452016-03-01 21:19:12 +00002114 /* Gather length of hash to sign */
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002115 size_t exp_hashlen = mbedtls_hash_info_get_size( md_alg );
2116 if( exp_hashlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002118
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002119 if( hashlen != exp_hashlen )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002120 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002121 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002122
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002123 hlen = mbedtls_hash_info_get_size( mgf1_hash_id );
2124 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002126
Simon Butcher02037452016-03-01 21:19:12 +00002127 /*
2128 * Note: EMSA-PSS verification is over the length of N - 1 bits
2129 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002130 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002131
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002132 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2133 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2134
Simon Butcher02037452016-03-01 21:19:12 +00002135 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002136 if( msb % 8 == 0 )
2137 {
2138 p++;
2139 siglen -= 1;
2140 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002141
Gilles Peskine139108a2017-10-18 19:03:42 +02002142 if( siglen < hlen + 2 )
2143 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2144 hash_start = p + siglen - hlen - 1;
2145
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02002146 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id );
Jaeden Amero66954e12018-01-25 16:05:54 +00002147 if( ret != 0 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002148 return( ret );
Paul Bakker02303e82013-01-03 11:08:31 +01002149
Paul Bakkerb3869132013-02-28 17:21:01 +01002150 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002151
Gilles Peskine6a54b022017-10-17 19:02:13 +02002152 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002153 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002154
Gilles Peskine91048a32017-10-19 17:46:14 +02002155 if( *p++ != 0x01 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002156 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002157
Gilles Peskine6a54b022017-10-17 19:02:13 +02002158 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002161 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002162 {
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002163 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002164 }
2165
Simon Butcher02037452016-03-01 21:19:12 +00002166 /*
2167 * Generate H = Hash( M' )
2168 */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002169 ret = hash_mprime( hash, hashlen, p, observed_salt_len,
2170 result, mgf1_hash_id );
2171 if( ret != 0 )
2172 return( ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002173
Jaeden Amero66954e12018-01-25 16:05:54 +00002174 if( memcmp( hash_start, result, hlen ) != 0 )
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002175 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002176
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002177 return( 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01002178}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002179
2180/*
2181 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002185 unsigned int hashlen,
2186 const unsigned char *hash,
2187 const unsigned char *sig )
2188{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002189 mbedtls_md_type_t mgf1_hash_id;
2190 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002191 RSA_VALIDATE_RET( sig != NULL );
2192 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2193 hashlen == 0 ) ||
2194 hash != NULL );
2195
2196 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002198 : md_alg;
2199
Thomas Daubney9e65f792021-05-19 12:18:58 +01002200 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx,
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002201 md_alg, hashlen, hash,
2202 mgf1_hash_id,
2203 MBEDTLS_RSA_SALT_LEN_ANY,
2204 sig ) );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002205
2206}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002210/*
2211 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002215 unsigned int hashlen,
2216 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002217 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002218{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002219 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002220 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002221 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002222
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002223 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002224 RSA_VALIDATE_RET( sig != NULL );
2225 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2226 hashlen == 0 ) ||
2227 hash != NULL );
2228
2229 sig_len = ctx->len;
2230
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002231 /*
2232 * Prepare expected PKCS1 v1.5 encoding of hash.
2233 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002234
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002235 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2236 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2237 {
2238 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2239 goto cleanup;
2240 }
2241
2242 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2243 encoded_expected ) ) != 0 )
2244 goto cleanup;
2245
2246 /*
2247 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2248 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002249
Thomas Daubney41e4ce42021-05-19 15:10:05 +01002250 ret = mbedtls_rsa_public( ctx, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002251 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002252 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002253
Simon Butcher02037452016-03-01 21:19:12 +00002254 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002255 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002256 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002257
Gabor Mezei90437e32021-10-20 11:59:27 +02002258 if( ( ret = mbedtls_ct_memcmp( encoded, encoded_expected,
gabor-mezei-arm46025642021-07-19 15:19:19 +02002259 sig_len ) ) != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002260 {
2261 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2262 goto cleanup;
2263 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002264
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002265cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002266
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002267 if( encoded != NULL )
2268 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002269 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002270 mbedtls_free( encoded );
2271 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002272
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002273 if( encoded_expected != NULL )
2274 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002275 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002276 mbedtls_free( encoded_expected );
2277 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002278
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002279 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
2283/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002284 * Do an RSA operation and check the message digest
2285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002288 unsigned int hashlen,
2289 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002290 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002291{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002292 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002293 RSA_VALIDATE_RET( sig != NULL );
2294 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2295 hashlen == 0 ) ||
2296 hash != NULL );
2297
Paul Bakkerb3869132013-02-28 17:21:01 +01002298 switch( ctx->padding )
2299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300#if defined(MBEDTLS_PKCS1_V15)
2301 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney2e126252021-05-19 11:48:53 +01002302 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, md_alg,
2303 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002304#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306#if defined(MBEDTLS_PKCS1_V21)
2307 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002308 return mbedtls_rsa_rsassa_pss_verify( ctx, md_alg,
2309 hashlen, hash, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01002310#endif
2311
2312 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002314 }
2315}
2316
2317/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002318 * Copy the components of an RSA key
2319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002321{
Janos Follath24eed8d2019-11-22 13:21:35 +00002322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002323 RSA_VALIDATE_RET( dst != NULL );
2324 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002325
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002326 dst->len = src->len;
2327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2329 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2332 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2333 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002334
2335#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2337 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2338 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2340 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002341#endif
2342
2343 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2346 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002347
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002348 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002349 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002350
2351cleanup:
2352 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002354
2355 return( ret );
2356}
2357
2358/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002359 * Free the components of an RSA key
2360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002362{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002363 if( ctx == NULL )
2364 return;
2365
2366 mbedtls_mpi_free( &ctx->Vi );
2367 mbedtls_mpi_free( &ctx->Vf );
2368 mbedtls_mpi_free( &ctx->RN );
2369 mbedtls_mpi_free( &ctx->D );
2370 mbedtls_mpi_free( &ctx->Q );
2371 mbedtls_mpi_free( &ctx->P );
2372 mbedtls_mpi_free( &ctx->E );
2373 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002374
Hanno Becker33c30a02017-08-23 07:00:22 +01002375#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002376 mbedtls_mpi_free( &ctx->RQ );
2377 mbedtls_mpi_free( &ctx->RP );
2378 mbedtls_mpi_free( &ctx->QP );
2379 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002380 mbedtls_mpi_free( &ctx->DP );
2381#endif /* MBEDTLS_RSA_NO_CRT */
2382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002384 /* Free the mutex, but only if it hasn't been freed already. */
2385 if( ctx->ver != 0 )
2386 {
2387 mbedtls_mutex_free( &ctx->mutex );
2388 ctx->ver = 0;
2389 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002390#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002391}
2392
Hanno Beckerab377312017-08-23 16:24:51 +01002393#endif /* !MBEDTLS_RSA_ALT */
2394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002397#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
2399/*
2400 * Example RSA-1024 keypair, for test purposes
2401 */
2402#define KEY_LEN 128
2403
2404#define RSA_N "9292758453063D803DD603D5E777D788" \
2405 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2406 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2407 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2408 "93A89813FBF3C4F8066D2D800F7C38A8" \
2409 "1AE31942917403FF4946B0A83D3D3E05" \
2410 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2411 "5E94BB77B07507233A0BC7BAC8F90F79"
2412
2413#define RSA_E "10001"
2414
2415#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2416 "66CA472BC44D253102F8B4A9D3BFA750" \
2417 "91386C0077937FE33FA3252D28855837" \
2418 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2419 "DF79C5CE07EE72C7F123142198164234" \
2420 "CABB724CF78B8173B9F880FC86322407" \
2421 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2422 "071513A1E85B5DFA031F21ECAE91A34D"
2423
2424#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2425 "2C01CAD19EA484A87EA4377637E75500" \
2426 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2427 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2428
2429#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2430 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2431 "910E4168387E3C30AA1E00C339A79508" \
2432 "8452DD96A9A5EA5D9DCA68DA636032AF"
2433
Paul Bakker5121ce52009-01-03 21:22:43 +00002434#define PT_LEN 24
2435#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2436 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002439static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002440{
gufe44c2620da2020-08-03 17:56:50 +02002441#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002442 size_t i;
2443
Paul Bakker545570e2010-07-18 09:00:25 +00002444 if( rng_state != NULL )
2445 rng_state = NULL;
2446
Paul Bakkera3d195c2011-11-27 21:07:34 +00002447 for( i = 0; i < len; ++i )
2448 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002449#else
2450 if( rng_state != NULL )
2451 rng_state = NULL;
2452
2453 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002454#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002455
Paul Bakkera3d195c2011-11-27 21:07:34 +00002456 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002459
Paul Bakker5121ce52009-01-03 21:22:43 +00002460/*
2461 * Checkup routine
2462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002464{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002465 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002467 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 unsigned char rsa_plaintext[PT_LEN];
2470 unsigned char rsa_decrypted[PT_LEN];
2471 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002473 unsigned char sha1sum[20];
2474#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002475
Hanno Becker3a701162017-08-22 13:52:43 +01002476 mbedtls_mpi K;
2477
2478 mbedtls_mpi_init( &K );
Ronald Cronc1905a12021-06-05 11:11:14 +02002479 mbedtls_rsa_init( &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Hanno Becker3a701162017-08-22 13:52:43 +01002481 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2482 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2483 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2484 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2485 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2486 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2487 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2488 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2489 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2490 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2491
Hanno Becker7f25f852017-10-10 16:56:22 +01002492 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002493
2494 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2498 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 {
2500 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Hanno Becker5bc87292017-05-03 15:09:31 +01002503 ret = 1;
2504 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 }
2506
2507 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
2510 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2511
Thomas Daubney21772772021-05-13 17:30:32 +01002512 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002513 PT_LEN, rsa_plaintext,
2514 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 {
2516 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
Hanno Becker5bc87292017-05-03 15:09:31 +01002519 ret = 1;
2520 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 }
2522
2523 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002525
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002526 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002527 &len, rsa_ciphertext, rsa_decrypted,
2528 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 {
2530 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002532
Hanno Becker5bc87292017-05-03 15:09:31 +01002533 ret = 1;
2534 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 }
2536
2537 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2538 {
2539 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
Hanno Becker5bc87292017-05-03 15:09:31 +01002542 ret = 1;
2543 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 }
2545
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002546 if( verbose != 0 )
2547 mbedtls_printf( "passed\n" );
2548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002551 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
TRodziewicz26371e42021-06-08 16:45:41 +02002553 if( mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002554 {
2555 if( verbose != 0 )
2556 mbedtls_printf( "failed\n" );
2557
2558 return( 1 );
2559 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Hanno Becker98838b02017-10-02 13:16:10 +01002561 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002562 MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002563 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 {
2565 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Hanno Becker5bc87292017-05-03 15:09:31 +01002568 ret = 1;
2569 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 }
2571
2572 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002575 if( mbedtls_rsa_pkcs1_verify( &rsa, MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002576 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 {
2578 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
Hanno Becker5bc87292017-05-03 15:09:31 +01002581 ret = 1;
2582 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 }
2584
2585 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002586 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002589 if( verbose != 0 )
2590 mbedtls_printf( "\n" );
2591
Paul Bakker3d8fb632014-04-17 12:42:41 +02002592cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002593 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 mbedtls_rsa_free( &rsa );
2595#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002596 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002598 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599}
2600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#endif /* MBEDTLS_RSA_C */