blob: 7e7af2a835bcd0bcf791b3b48f7ff2b9bf7ad598 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010059#else
Rich Evans00ab4702015-02-06 13:43:58 +000060#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020062#define mbedtls_calloc calloc
63#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010064#endif
65
Hanno Beckera565f542017-10-11 11:00:19 +010066#if !defined(MBEDTLS_RSA_ALT)
67
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050068/* Parameter validation macros */
69#define RSA_VALIDATE_RET( cond ) \
70 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
71#define RSA_VALIDATE( cond ) \
72 MBEDTLS_INTERNAL_VALIDATE( cond )
73
Hanno Becker617c1ae2017-08-23 14:11:24 +010074int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
75 const mbedtls_mpi *N,
76 const mbedtls_mpi *P, const mbedtls_mpi *Q,
77 const mbedtls_mpi *D, const mbedtls_mpi *E )
78{
Janos Follath24eed8d2019-11-22 13:21:35 +000079 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050080 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +010081
82 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
83 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
84 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
85 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
86 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
87 {
Chris Jonesb7d02e02021-04-01 17:40:03 +010088 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +010089 }
90
91 if( N != NULL )
92 ctx->len = mbedtls_mpi_size( &ctx->N );
93
94 return( 0 );
95}
96
97int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +010098 unsigned char const *N, size_t N_len,
99 unsigned char const *P, size_t P_len,
100 unsigned char const *Q, size_t Q_len,
101 unsigned char const *D, size_t D_len,
102 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100103{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000104 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500105 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100106
107 if( N != NULL )
108 {
109 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
110 ctx->len = mbedtls_mpi_size( &ctx->N );
111 }
112
113 if( P != NULL )
114 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
115
116 if( Q != NULL )
117 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
118
119 if( D != NULL )
120 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
121
122 if( E != NULL )
123 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
124
125cleanup:
126
127 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100128 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100129
130 return( 0 );
131}
132
Hanno Becker705fc682017-10-10 17:57:02 +0100133/*
134 * Checks whether the context fields are set in such a way
135 * that the RSA primitives will be able to execute without error.
136 * It does *not* make guarantees for consistency of the parameters.
137 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100138static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
139 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100140{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100141#if !defined(MBEDTLS_RSA_NO_CRT)
142 /* blinding_needed is only used for NO_CRT to decide whether
143 * P,Q need to be present or not. */
144 ((void) blinding_needed);
145#endif
146
Hanno Becker3a760a12018-01-05 08:14:49 +0000147 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
148 ctx->len > MBEDTLS_MPI_MAX_SIZE )
149 {
Hanno Becker705fc682017-10-10 17:57:02 +0100150 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000151 }
Hanno Becker705fc682017-10-10 17:57:02 +0100152
153 /*
154 * 1. Modular exponentiation needs positive, odd moduli.
155 */
156
157 /* Modular exponentiation wrt. N is always used for
158 * RSA public key operations. */
159 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
160 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
161 {
162 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
163 }
164
165#if !defined(MBEDTLS_RSA_NO_CRT)
166 /* Modular exponentiation for P and Q is only
167 * used for private key operations and if CRT
168 * is used. */
169 if( is_priv &&
170 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
171 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
172 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
173 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
174 {
175 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
176 }
177#endif /* !MBEDTLS_RSA_NO_CRT */
178
179 /*
180 * 2. Exponents must be positive
181 */
182
183 /* Always need E for public key operations */
184 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
185 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
186
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100187#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100188 /* For private key operations, use D or DP & DQ
189 * as (unblinded) exponents. */
190 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
191 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
192#else
193 if( is_priv &&
194 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
195 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
196 {
197 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
198 }
199#endif /* MBEDTLS_RSA_NO_CRT */
200
201 /* Blinding shouldn't make exponents negative either,
202 * so check that P, Q >= 1 if that hasn't yet been
203 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100204#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100205 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100206 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
207 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
208 {
209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
210 }
211#endif
212
213 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100214 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100215#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100216 if( is_priv &&
217 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
218 {
219 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
220 }
221#endif
222
223 return( 0 );
224}
225
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100226int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100227{
228 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500229 int have_N, have_P, have_Q, have_D, have_E;
230#if !defined(MBEDTLS_RSA_NO_CRT)
231 int have_DP, have_DQ, have_QP;
232#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500233 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100234
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500235 RSA_VALIDATE_RET( ctx != NULL );
236
237 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
238 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
239 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
240 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
241 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500242
243#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500244 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
245 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
246 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500247#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100248
Hanno Becker617c1ae2017-08-23 14:11:24 +0100249 /*
250 * Check whether provided parameters are enough
251 * to deduce all others. The following incomplete
252 * parameter sets for private keys are supported:
253 *
254 * (1) P, Q missing.
255 * (2) D and potentially N missing.
256 *
257 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100258
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500259 n_missing = have_P && have_Q && have_D && have_E;
260 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
261 d_missing = have_P && have_Q && !have_D && have_E;
262 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100263
264 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500265 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100266
Hanno Becker617c1ae2017-08-23 14:11:24 +0100267 if( !is_priv && !is_pub )
268 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
269
270 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100271 * Step 1: Deduce N if P, Q are provided.
272 */
273
274 if( !have_N && have_P && have_Q )
275 {
276 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
277 &ctx->Q ) ) != 0 )
278 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100279 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100280 }
281
282 ctx->len = mbedtls_mpi_size( &ctx->N );
283 }
284
285 /*
286 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100287 */
288
289 if( pq_missing )
290 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100291 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100292 &ctx->P, &ctx->Q );
293 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100294 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100295
296 }
297 else if( d_missing )
298 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100299 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
300 &ctx->Q,
301 &ctx->E,
302 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100303 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100304 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100305 }
306 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100307
Hanno Becker617c1ae2017-08-23 14:11:24 +0100308 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100309 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100310 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100311 */
312
Hanno Becker23344b52017-08-23 07:43:27 +0100313#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500314 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100315 {
316 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
317 &ctx->DP, &ctx->DQ, &ctx->QP );
318 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100319 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100320 }
Hanno Becker23344b52017-08-23 07:43:27 +0100321#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100322
323 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100324 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100325 */
326
Hanno Beckerebd2c022017-10-12 10:54:53 +0100327 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100328}
329
Hanno Becker617c1ae2017-08-23 14:11:24 +0100330int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
331 unsigned char *N, size_t N_len,
332 unsigned char *P, size_t P_len,
333 unsigned char *Q, size_t Q_len,
334 unsigned char *D, size_t D_len,
335 unsigned char *E, size_t E_len )
336{
337 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500338 int is_priv;
339 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100340
341 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500342 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100343 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
344 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
345 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
346 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
347 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
348
349 if( !is_priv )
350 {
351 /* If we're trying to export private parameters for a public key,
352 * something must be wrong. */
353 if( P != NULL || Q != NULL || D != NULL )
354 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
355
356 }
357
358 if( N != NULL )
359 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
360
361 if( P != NULL )
362 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
363
364 if( Q != NULL )
365 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
366
367 if( D != NULL )
368 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
369
370 if( E != NULL )
371 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100372
373cleanup:
374
375 return( ret );
376}
377
Hanno Becker617c1ae2017-08-23 14:11:24 +0100378int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
379 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
380 mbedtls_mpi *D, mbedtls_mpi *E )
381{
Janos Follath24eed8d2019-11-22 13:21:35 +0000382 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500383 int is_priv;
384 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100385
386 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500387 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100388 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
389 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
390 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
391 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
392 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
393
394 if( !is_priv )
395 {
396 /* If we're trying to export private parameters for a public key,
397 * something must be wrong. */
398 if( P != NULL || Q != NULL || D != NULL )
399 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
400
401 }
402
403 /* Export all requested core parameters. */
404
405 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
406 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
407 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
408 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
409 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
410 {
411 return( ret );
412 }
413
414 return( 0 );
415}
416
417/*
418 * Export CRT parameters
419 * This must also be implemented if CRT is not used, for being able to
420 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
421 * can be used in this case.
422 */
423int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
424 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
425{
Janos Follath24eed8d2019-11-22 13:21:35 +0000426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500427 int is_priv;
428 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100429
430 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500431 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100432 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
433 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
434 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
435 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
436 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
437
438 if( !is_priv )
439 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
440
Hanno Beckerdc95c892017-08-23 06:57:02 +0100441#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100442 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100443 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
444 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
445 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
446 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100447 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100448 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100449#else
450 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
451 DP, DQ, QP ) ) != 0 )
452 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100453 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100454 }
455#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100456
457 return( 0 );
458}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100459
Paul Bakker5121ce52009-01-03 21:22:43 +0000460/*
461 * Initialize an RSA context
462 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200463void mbedtls_rsa_init( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000464{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500465 RSA_VALIDATE( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Ronald Cronc1905a12021-06-05 11:11:14 +0200469 ctx->padding = MBEDTLS_RSA_PKCS_V15;
470 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100473 /* Set ctx->ver to nonzero to indicate that the mutex has been
474 * initialized and will need to be freed. */
475 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200477#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000478}
479
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100480/*
481 * Set padding for an existing RSA context
482 */
Ronald Cronea7631b2021-06-03 18:51:59 +0200483int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
484 mbedtls_md_type_t hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100485{
Ronald Cron3a0375f2021-06-08 10:22:28 +0200486 switch( padding )
487 {
488#if defined(MBEDTLS_PKCS1_V15)
489 case MBEDTLS_RSA_PKCS_V15:
490 break;
491#endif
492
493#if defined(MBEDTLS_PKCS1_V21)
494 case MBEDTLS_RSA_PKCS_V21:
495 break;
496#endif
497 default:
498 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
499 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200500
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200501#if defined(MBEDTLS_PKCS1_V21)
Ronald Cronea7631b2021-06-03 18:51:59 +0200502 if( ( padding == MBEDTLS_RSA_PKCS_V21 ) &&
503 ( hash_id != MBEDTLS_MD_NONE ) )
504 {
505 const mbedtls_md_info_t *md_info;
506
507 md_info = mbedtls_md_info_from_type( hash_id );
508 if( md_info == NULL )
509 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
510 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200511#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500512
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100513 ctx->padding = padding;
514 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200515
516 return( 0 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100517}
518
Hanno Becker617c1ae2017-08-23 14:11:24 +0100519/*
520 * Get length in bytes of RSA modulus
521 */
522
523size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
524{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100525 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100526}
527
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
531/*
532 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800533 *
534 * This generation method follows the RSA key pair generation procedure of
535 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000538 int (*f_rng)(void *, unsigned char *, size_t),
539 void *p_rng,
540 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000541{
Janos Follath24eed8d2019-11-22 13:21:35 +0000542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800543 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100544 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500545 RSA_VALIDATE_RET( ctx != NULL );
546 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Janos Follathb8fc1b02018-09-03 15:37:01 +0100548 /*
549 * If the modulus is 1024 bit long or shorter, then the security strength of
550 * the RSA algorithm is less than or equal to 80 bits and therefore an error
551 * rate of 2^-80 is sufficient.
552 */
553 if( nbits > 1024 )
554 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
555
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100556 mbedtls_mpi_init( &H );
557 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800558 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100560 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
561 {
562 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
563 goto cleanup;
564 }
565
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 /*
567 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800568 * 1. |P-Q| > 2^( nbits / 2 - 100 )
569 * 2. GCD( E, (P-1)*(Q-1) ) == 1
570 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 do
575 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100576 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
577 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Janos Follathb8fc1b02018-09-03 15:37:01 +0100579 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
580 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800582 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
583 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
584 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 continue;
586
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800587 /* not required by any standards, but some users rely on the fact that P > Q */
588 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100589 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100590
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100591 /* Temporarily replace P,Q by P-1, Q-1 */
592 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
593 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
594 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800595
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800596 /* 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 +0200597 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800598 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
599 continue;
600
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800601 /* 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 -0800602 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
603 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
604 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
605
606 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
607 continue;
608
609 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800611 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100613 /* Restore P,Q */
614 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
615 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
616
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800617 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
618
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100619 ctx->len = mbedtls_mpi_size( &ctx->N );
620
Jethro Beekman97f95c92018-02-13 15:50:36 -0800621#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 * DP = D mod (P - 1)
624 * DQ = D mod (Q - 1)
625 * QP = Q^-1 mod P
626 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100627 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
628 &ctx->DP, &ctx->DQ, &ctx->QP ) );
629#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
Hanno Becker83aad1f2017-08-23 06:45:10 +0100631 /* Double-check */
632 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
634cleanup:
635
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100636 mbedtls_mpi_free( &H );
637 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800638 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640 if( ret != 0 )
641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100643
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100644 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100645 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100646 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 }
648
Paul Bakker48377d92013-08-30 12:06:24 +0200649 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000650}
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
654/*
655 * Check a public RSA key
656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000658{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500659 RSA_VALIDATE_RET( ctx != NULL );
660
Hanno Beckerebd2c022017-10-12 10:54:53 +0100661 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000663
Hanno Becker3a760a12018-01-05 08:14:49 +0000664 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100667 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
Hanno Becker705fc682017-10-10 17:57:02 +0100669 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
670 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100674 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676 return( 0 );
677}
678
679/*
Hanno Becker705fc682017-10-10 17:57:02 +0100680 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500684 RSA_VALIDATE_RET( ctx != NULL );
685
Hanno Becker705fc682017-10-10 17:57:02 +0100686 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100687 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 {
Hanno Becker98838b02017-10-02 13:16:10 +0100689 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 }
Paul Bakker48377d92013-08-30 12:06:24 +0200691
Hanno Becker98838b02017-10-02 13:16:10 +0100692 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100693 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100695 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000697
Hanno Beckerb269a852017-08-25 08:03:21 +0100698#if !defined(MBEDTLS_RSA_NO_CRT)
699 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
700 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
701 {
702 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
703 }
704#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000705
706 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707}
708
709/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100710 * Check if contexts holding a public and private key match
711 */
Hanno Becker98838b02017-10-02 13:16:10 +0100712int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
713 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100714{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500715 RSA_VALIDATE_RET( pub != NULL );
716 RSA_VALIDATE_RET( prv != NULL );
717
Hanno Becker98838b02017-10-02 13:16:10 +0100718 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100722 }
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
725 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100728 }
729
730 return( 0 );
731}
732
733/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 * Do an RSA public key operation
735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000737 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 unsigned char *output )
739{
Janos Follath24eed8d2019-11-22 13:21:35 +0000740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000741 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500743 RSA_VALIDATE_RET( ctx != NULL );
744 RSA_VALIDATE_RET( input != NULL );
745 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Hanno Beckerebd2c022017-10-12 10:54:53 +0100747 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100748 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200752#if defined(MBEDTLS_THREADING_C)
753 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
754 return( ret );
755#endif
756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200761 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
762 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000763 }
764
765 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
767 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
769cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200771 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
772 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100773#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000776
777 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100778 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
780 return( 0 );
781}
782
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200783/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200784 * Generate or update blinding values, see section 10 of:
785 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200786 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200787 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200788 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200789static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200790 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
791{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200792 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200793 mbedtls_mpi R;
794
795 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200796
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200797 if( ctx->Vf.p != NULL )
798 {
799 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
801 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
802 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
803 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200804
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200805 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200806 }
807
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200808 /* Unblinding value: Vf = random number, invertible mod N */
809 do {
810 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200811 {
812 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
813 goto cleanup;
814 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 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 +0200817
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200818 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200819 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
820 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
821 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
822
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200823 /* At this point, Vi is invertible mod N if and only if both Vf and R
824 * are invertible mod N. If one of them isn't, we don't need to know
825 * which one, we just loop and choose new values for both of them.
826 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200827 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500828 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200829 goto cleanup;
830
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500831 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
832
833 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
834 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
835 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200836
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200837 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200838 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 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 +0200840
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200841
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200842cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200843 mbedtls_mpi_free( &R );
844
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200845 return( ret );
846}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200847
Paul Bakker5121ce52009-01-03 21:22:43 +0000848/*
Janos Follathe81102e2017-03-22 13:38:28 +0000849 * Exponent blinding supposed to prevent side-channel attacks using multiple
850 * traces of measurements to recover the RSA key. The more collisions are there,
851 * the more bits of the key can be recovered. See [3].
852 *
853 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800854 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000855 *
856 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800857 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000858 *
859 * (With the currently (as of 2017 April) known best algorithms breaking 2048
860 * bit RSA requires approximately as much time as trying out 2^112 random keys.
861 * Thus in this sense with 28 byte blinding the security is not reduced by
862 * side-channel attacks like the one in [3])
863 *
864 * This countermeasure does not help if the key recovery is possible with a
865 * single trace.
866 */
867#define RSA_EXPONENT_BLINDING 28
868
869/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000870 * Do an RSA private key operation
871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200873 int (*f_rng)(void *, unsigned char *, size_t),
874 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000875 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 unsigned char *output )
877{
Janos Follath24eed8d2019-11-22 13:21:35 +0000878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000879 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100880
881 /* Temporary holding the result */
882 mbedtls_mpi T;
883
884 /* Temporaries holding P-1, Q-1 and the
885 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000886 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100887
888#if !defined(MBEDTLS_RSA_NO_CRT)
889 /* Temporaries holding the results mod p resp. mod q. */
890 mbedtls_mpi TP, TQ;
891
892 /* Temporaries holding the blinded exponents for
893 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000894 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100895
896 /* Pointers to actual exponents to be used - either the unblinded
897 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000898 mbedtls_mpi *DP = &ctx->DP;
899 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100900#else
901 /* Temporary holding the blinded exponent (if used). */
902 mbedtls_mpi D_blind;
903
904 /* Pointer to actual exponent to be used - either the unblinded
905 * or the blinded one, depending on the presence of a PRNG. */
906 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100907#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100908
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100909 /* Temporaries holding the initial input and the double
910 * checked result; should be the same in the end. */
911 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500913 RSA_VALIDATE_RET( ctx != NULL );
914 RSA_VALIDATE_RET( input != NULL );
915 RSA_VALIDATE_RET( output != NULL );
916
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200917 if( f_rng == NULL )
918 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
919
920 if( rsa_check_context( ctx, 1 /* private key checks */,
921 1 /* blinding on */ ) != 0 )
Hanno Beckerebd2c022017-10-12 10:54:53 +0100922 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100923 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100924 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100925
Hanno Becker06811ce2017-05-03 15:10:34 +0100926#if defined(MBEDTLS_THREADING_C)
927 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
928 return( ret );
929#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000930
Hanno Becker06811ce2017-05-03 15:10:34 +0100931 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100932 mbedtls_mpi_init( &T );
933
934 mbedtls_mpi_init( &P1 );
935 mbedtls_mpi_init( &Q1 );
936 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000937
Janos Follathe81102e2017-03-22 13:38:28 +0000938#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200939 mbedtls_mpi_init( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +0000940#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200941 mbedtls_mpi_init( &DP_blind );
942 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000943#endif
944
Hanno Becker06811ce2017-05-03 15:10:34 +0100945#if !defined(MBEDTLS_RSA_NO_CRT)
946 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200947#endif
948
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100949 mbedtls_mpi_init( &I );
950 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100951
952 /* End of MPI initialization */
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
955 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200957 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
958 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000959 }
960
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100961 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100962
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200963 /*
964 * Blinding
965 * T = T * Vi mod N
966 */
967 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
968 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
969 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000970
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200971 /*
972 * Exponent blinding
973 */
974 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
975 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000976
Janos Follathf9203b42017-03-22 15:13:15 +0000977#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200978 /*
979 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
980 */
981 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
982 f_rng, p_rng ) );
983 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
984 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
985 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000986
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200987 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000988#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200989 /*
990 * DP_blind = ( P - 1 ) * R + DP
991 */
992 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
993 f_rng, p_rng ) );
994 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
995 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
996 &ctx->DP ) );
Janos Follathf9203b42017-03-22 15:13:15 +0000997
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200998 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000999
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001000 /*
1001 * DQ_blind = ( Q - 1 ) * R + DQ
1002 */
1003 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1004 f_rng, p_rng ) );
1005 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1006 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1007 &ctx->DQ ) );
Janos Follathf9203b42017-03-22 15:13:15 +00001008
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001009 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001010#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001013 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001014#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001015 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001016 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001017 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001018 * TP = input ^ dP mod P
1019 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001021
1022 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1023 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
1025 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001026 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001028 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1029 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1030 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
1032 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001033 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001035 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1036 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001038
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001039 /*
1040 * Unblind
1041 * T = T * Vf mod N
1042 */
1043 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
1044 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
Hanno Becker2dec5e82017-10-03 07:49:52 +01001046 /* Verify the result to prevent glitching attacks. */
1047 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1048 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001049 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001050 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001051 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1052 goto cleanup;
1053 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001054
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
1058cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001060 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1061 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001062#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001063
Hanno Becker06811ce2017-05-03 15:10:34 +01001064 mbedtls_mpi_free( &P1 );
1065 mbedtls_mpi_free( &Q1 );
1066 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001067
Janos Follathe81102e2017-03-22 13:38:28 +00001068#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001069 mbedtls_mpi_free( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +00001070#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001071 mbedtls_mpi_free( &DP_blind );
1072 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001073#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Hanno Becker06811ce2017-05-03 15:10:34 +01001075 mbedtls_mpi_free( &T );
1076
1077#if !defined(MBEDTLS_RSA_NO_CRT)
1078 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1079#endif
1080
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001081 mbedtls_mpi_free( &C );
1082 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001083
Gilles Peskineae3741e2020-11-25 00:10:31 +01001084 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001085 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
Gilles Peskineae3741e2020-11-25 00:10:31 +01001087 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088}
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001091/**
1092 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1093 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001094 * \param dst buffer to mask
1095 * \param dlen length of destination buffer
1096 * \param src source of the mask generation
1097 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001098 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001099 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001100static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001101 size_t slen, mbedtls_md_type_t md_alg )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001102{
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001103 const mbedtls_md_info_t *md_info;
1104 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001106 unsigned char counter[4];
1107 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001108 unsigned int hlen;
1109 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001110 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001111
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001112 mbedtls_md_init( &md_ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114 memset( counter, 0, 4 );
1115
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001116 md_info = mbedtls_md_info_from_type( md_alg );
1117 if( md_info == NULL )
1118 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1119
1120 mbedtls_md_init( &md_ctx );
1121 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1122 goto exit;
1123
1124 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001125
Simon Butcher02037452016-03-01 21:19:12 +00001126 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001127 p = dst;
1128
1129 while( dlen > 0 )
1130 {
1131 use_len = hlen;
1132 if( dlen < hlen )
1133 use_len = dlen;
1134
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001135 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001136 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001137 if( ( ret = mbedtls_md_update( &md_ctx, src, slen ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001138 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001139 if( ( ret = mbedtls_md_update( &md_ctx, counter, 4 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001140 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001141 if( ( ret = mbedtls_md_finish( &md_ctx, mask ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001142 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001143
1144 for( i = 0; i < use_len; ++i )
1145 *p++ ^= mask[i];
1146
1147 counter[3]++;
1148
1149 dlen -= use_len;
1150 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001151
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001152exit:
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001153 mbedtls_md_free( &md_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001154 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001155
1156 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001157}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001158
1159/**
1160 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1161 *
1162 * \param hash the input hash
1163 * \param hlen length of the input hash
1164 * \param salt the input salt
1165 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001166 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001167 * \param md_alg message digest to use
1168 */
1169static int hash_mprime( const unsigned char *hash, size_t hlen,
1170 const unsigned char *salt, size_t slen,
1171 unsigned char *out, mbedtls_md_type_t md_alg )
1172{
1173 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1174 mbedtls_md_context_t md_ctx;
1175 int ret;
1176
1177 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1178 if( md_info == NULL )
1179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1180
1181 mbedtls_md_init( &md_ctx );
1182 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1183 goto exit;
1184 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1185 goto exit;
1186 if( ( ret = mbedtls_md_update( &md_ctx, zeros, sizeof( zeros ) ) ) != 0 )
1187 goto exit;
1188 if( ( ret = mbedtls_md_update( &md_ctx, hash, hlen ) ) != 0 )
1189 goto exit;
1190 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1191 goto exit;
1192 if( ( ret = mbedtls_md_finish( &md_ctx, out ) ) != 0 )
1193 goto exit;
1194
1195exit:
1196 mbedtls_md_free( &md_ctx );
1197
1198 return( ret );
1199}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001200
1201/**
1202 * Compute a hash.
1203 *
1204 * \param md_alg algorithm to use
1205 * \param input input message to hash
1206 * \param ilen input length
1207 * \param output the output buffer - must be large enough for \p md_alg
1208 */
1209static int compute_hash( mbedtls_md_type_t md_alg,
1210 const unsigned char *input, size_t ilen,
1211 unsigned char *output )
1212{
1213 const mbedtls_md_info_t *md_info;
1214
1215 md_info = mbedtls_md_info_from_type( md_alg );
1216 if( md_info == NULL )
1217 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1218
1219 return( mbedtls_md( md_info, input, ilen, output ) );
1220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001224/*
1225 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001228 int (*f_rng)(void *, unsigned char *, size_t),
1229 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001230 const unsigned char *label, size_t label_len,
1231 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001232 const unsigned char *input,
1233 unsigned char *output )
1234{
1235 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001237 unsigned char *p = output;
1238 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 const mbedtls_md_info_t *md_info;
Paul Bakkerb3869132013-02-28 17:21:01 +01001240
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001241 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001242 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001243 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001244 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1245
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001246 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001250 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001252
1253 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001255
Simon Butcher02037452016-03-01 21:19:12 +00001256 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001257 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001259
1260 memset( output, 0, olen );
1261
1262 *p++ = 0;
1263
Simon Butcher02037452016-03-01 21:19:12 +00001264 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001265 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001266 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001267
1268 p += hlen;
1269
Simon Butcher02037452016-03-01 21:19:12 +00001270 /* Construct DB */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001271 ret = compute_hash( (mbedtls_md_type_t) ctx->hash_id, label, label_len, p );
1272 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001273 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001274 p += hlen;
1275 p += olen - 2 * hlen - 2 - ilen;
1276 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001277 if( ilen != 0 )
1278 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001279
Simon Butcher02037452016-03-01 21:19:12 +00001280 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001281 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001282 ctx->hash_id ) ) != 0 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02001283 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001284
Simon Butcher02037452016-03-01 21:19:12 +00001285 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001286 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001287 ctx->hash_id ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001288 return( ret );
1289
Thomas Daubney141700f2021-05-13 19:06:10 +01001290 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001291}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001295/*
1296 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001299 int (*f_rng)(void *, unsigned char *, size_t),
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001300 void *p_rng, size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001301 const unsigned char *input,
1302 unsigned char *output )
1303{
1304 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001306 unsigned char *p = output;
1307
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001308 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001309 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001310 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001311
Paul Bakkerb3869132013-02-28 17:21:01 +01001312 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001313
Simon Butcher02037452016-03-01 21:19:12 +00001314 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001315 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001317
1318 nb_pad = olen - 3 - ilen;
1319
1320 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001321
1322 if( f_rng == NULL )
1323 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1324
1325 *p++ = MBEDTLS_RSA_CRYPT;
1326
1327 while( nb_pad-- > 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001328 {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001329 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001330
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001331 do {
1332 ret = f_rng( p_rng, p, 1 );
1333 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001334
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001335 /* Check if RNG failed to generate data */
1336 if( rng_dl == 0 || ret != 0 )
1337 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001338
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001339 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001340 }
1341
1342 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001343 if( ilen != 0 )
1344 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001345
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001346 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001347}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001349
Paul Bakker5121ce52009-01-03 21:22:43 +00001350/*
1351 * Add the message padding, then do an RSA operation
1352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001354 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001355 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +01001356 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001357 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 unsigned char *output )
1359{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001360 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001361 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001362 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001363
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 switch( ctx->padding )
1365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_PKCS1_V15)
1367 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney21772772021-05-13 17:30:32 +01001368 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001369 ilen, input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001370#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_PKCS1_V21)
1373 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney141700f2021-05-13 19:06:10 +01001374 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, NULL, 0,
Thomas Daubney21772772021-05-13 17:30:32 +01001375 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001376#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
1378 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001381}
1382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001384/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001385 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001388 int (*f_rng)(void *, unsigned char *, size_t),
1389 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001390 const unsigned char *label, size_t label_len,
1391 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001392 const unsigned char *input,
1393 unsigned char *output,
1394 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001395{
Janos Follath24eed8d2019-11-22 13:21:35 +00001396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001397 size_t ilen, i, pad_len;
1398 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1400 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001401 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 const mbedtls_md_info_t *md_info;
Paul Bakkerb3869132013-02-28 17:21:01 +01001403
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001404 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001405 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1406 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1407 RSA_VALIDATE_RET( input != NULL );
1408 RSA_VALIDATE_RET( olen != NULL );
1409
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001410 /*
1411 * Parameters sanity checks
1412 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001413 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
1416 ilen = ctx->len;
1417
Paul Bakker27fdf462011-06-09 13:55:13 +00001418 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001422 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001424
Janos Follathc17cda12016-02-11 11:08:18 +00001425 hlen = mbedtls_md_get_size( md_info );
1426
1427 // checking for integer underflow
1428 if( 2 * hlen + 2 > ilen )
1429 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1430
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001431 /*
1432 * RSA operation
1433 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001434 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
1436 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001437 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001438
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001439 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001440 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001441 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001442 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001443 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001444 ctx->hash_id ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001445 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001446 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001447 ctx->hash_id ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001448 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001449 goto cleanup;
1450 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001451
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001452 /* Generate lHash */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001453 ret = compute_hash( (mbedtls_md_type_t) ctx->hash_id,
1454 label, label_len, lhash );
1455 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001456 goto cleanup;
1457
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001458 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001459 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001460 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001461 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001462 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001463
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001464 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001465
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001466 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001467
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001468 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001469 for( i = 0; i < hlen; i++ )
1470 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001471
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001472 /* Get zero-padding len, but always read till end of buffer
1473 * (minus one, for the 01 byte) */
1474 pad_len = 0;
1475 pad_done = 0;
1476 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1477 {
1478 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001479 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001480 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001481
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001482 p += pad_len;
1483 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001484
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001485 /*
1486 * The only information "leaked" is whether the padding was correct or not
1487 * (eg, no data is copied if it was not correct). This meets the
1488 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1489 * the different error conditions.
1490 */
1491 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001492 {
1493 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1494 goto cleanup;
1495 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001496
Paul Bakker66d5d072014-06-17 16:39:18 +02001497 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001498 {
1499 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1500 goto cleanup;
1501 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001502
1503 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001504 if( *olen != 0 )
1505 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001506 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001507
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001508cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001509 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1510 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001511
1512 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001513}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001517/*
1518 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1519 */
1520int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
1521 int (*f_rng)(void *, unsigned char *, size_t),
1522 void *p_rng,
1523 size_t *olen,
1524 const unsigned char *input,
1525 unsigned char *output,
1526 size_t output_max_len )
1527{
1528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1529 size_t ilen;
1530 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1531
1532 RSA_VALIDATE_RET( ctx != NULL );
1533 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1534 RSA_VALIDATE_RET( input != NULL );
1535 RSA_VALIDATE_RET( olen != NULL );
1536
1537 ilen = ctx->len;
1538
1539 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1540 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1541
1542 if( ilen < 16 || ilen > sizeof( buf ) )
1543 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1544
1545 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
1546
1547 if( ret != 0 )
1548 goto cleanup;
1549
Gabor Mezei90437e32021-10-20 11:59:27 +02001550 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding( buf, ilen,
Gabor Mezei63bbba52021-10-18 16:17:57 +02001551 output, output_max_len, olen );
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001552
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001553cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001554 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001555
1556 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001559
1560/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001561 * Do an RSA operation, then remove the message padding
1562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001564 int (*f_rng)(void *, unsigned char *, size_t),
1565 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001566 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001567 const unsigned char *input,
1568 unsigned char *output,
1569 size_t output_max_len)
1570{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001571 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001572 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1573 RSA_VALIDATE_RET( input != NULL );
1574 RSA_VALIDATE_RET( olen != NULL );
1575
Paul Bakkerb3869132013-02-28 17:21:01 +01001576 switch( ctx->padding )
1577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#if defined(MBEDTLS_PKCS1_V15)
1579 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney34733082021-05-12 09:24:29 +01001580 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001581 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001582#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#if defined(MBEDTLS_PKCS1_V21)
1585 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001586 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001587 olen, input, output,
1588 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001589#endif
1590
1591 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001593 }
1594}
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001597static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001598 int (*f_rng)(void *, unsigned char *, size_t),
1599 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001601 unsigned int hashlen,
1602 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001603 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001604 unsigned char *sig )
1605{
1606 size_t olen;
1607 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001608 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001609 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001611 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001613
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001614 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001615 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1616 hashlen == 0 ) ||
1617 hash != NULL );
1618 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001619
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001620 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1621 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1622
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001623 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001625
1626 olen = ctx->len;
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001629 {
Simon Butcher02037452016-03-01 21:19:12 +00001630 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001632 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001634
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001635 if( hashlen != mbedtls_md_get_size( md_info ) )
1636 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001637 }
1638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001640 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001644
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001645 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1646 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001647 /* Calculate the largest possible salt length, up to the hash size.
1648 * Normally this is the hash length, which is the maximum salt length
1649 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001650 * enough room, use the maximum salt length that fits. The constraint is
1651 * that the hash length plus the salt length plus 2 bytes must be at most
1652 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1653 * (PKCS#1 v2.2) §9.1.1 step 3. */
1654 min_slen = hlen - 2;
1655 if( olen < hlen + min_slen + 2 )
1656 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1657 else if( olen >= hlen + hlen + 2 )
1658 slen = hlen;
1659 else
1660 slen = olen - hlen - 2;
1661 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001662 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001665 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001666 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001667 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001668 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001669 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001670
1671 memset( sig, 0, olen );
1672
Simon Butcher02037452016-03-01 21:19:12 +00001673 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001674 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001675 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001676 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001677
1678 /* Generate salt of length slen in place in the encoded message */
1679 salt = p;
1680 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001681 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001682
Paul Bakkerb3869132013-02-28 17:21:01 +01001683 p += slen;
1684
Simon Butcher02037452016-03-01 21:19:12 +00001685 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001686 ret = hash_mprime( hash, hashlen, salt, slen, p, ctx->hash_id );
1687 if( ret != 0 )
1688 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001689
Simon Butcher02037452016-03-01 21:19:12 +00001690 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001691 if( msb % 8 == 0 )
1692 offset = 1;
1693
Simon Butcher02037452016-03-01 21:19:12 +00001694 /* maskedDB: Apply dbMask to DB */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001695 ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1696 ctx->hash_id );
1697 if( ret != 0 )
1698 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001699
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001700 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001701 sig[0] &= 0xFF >> ( olen * 8 - msb );
1702
1703 p += hlen;
1704 *p++ = 0xBC;
1705
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001706 return mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001707}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001708
1709/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001710 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1711 * the option to pass in the salt length.
1712 */
1713int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1714 int (*f_rng)(void *, unsigned char *, size_t),
1715 void *p_rng,
1716 mbedtls_md_type_t md_alg,
1717 unsigned int hashlen,
1718 const unsigned char *hash,
1719 int saltlen,
1720 unsigned char *sig )
1721{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001722 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001723 hashlen, hash, saltlen, sig );
1724}
1725
1726
1727/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001728 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1729 */
1730int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1731 int (*f_rng)(void *, unsigned char *, size_t),
1732 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001733 mbedtls_md_type_t md_alg,
1734 unsigned int hashlen,
1735 const unsigned char *hash,
1736 unsigned char *sig )
1737{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001738 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001739 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001740}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001744/*
1745 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1746 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001747
1748/* Construct a PKCS v1.5 encoding of a hashed message
1749 *
1750 * This is used both for signature generation and verification.
1751 *
1752 * Parameters:
1753 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001754 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001755 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001756 * - hash: Buffer containing the hashed message or the raw data.
1757 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001758 * - dst: Buffer to hold the encoded message.
1759 *
1760 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001761 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001762 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001763 *
1764 */
1765static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1766 unsigned int hashlen,
1767 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001768 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001769 unsigned char *dst )
1770{
1771 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001772 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001773 unsigned char *p = dst;
1774 const char *oid = NULL;
1775
1776 /* Are we signing hashed or raw data? */
1777 if( md_alg != MBEDTLS_MD_NONE )
1778 {
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +02001779 unsigned char md_size = mbedtls_hash_info_get_size( md_alg );
Manuel Pégourié-Gonnardf493f2a2022-07-05 17:41:05 +02001780 if( md_size == 0 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001781 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1782
1783 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1784 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1785
Manuel Pégourié-Gonnardf493f2a2022-07-05 17:41:05 +02001786 if( hashlen != md_size )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001787 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001788
1789 /* Double-check that 8 + hashlen + oid_size can be used as a
1790 * 1-byte ASN.1 length encoding and that there's no overflow. */
1791 if( 8 + hashlen + oid_size >= 0x80 ||
1792 10 + hashlen < hashlen ||
1793 10 + hashlen + oid_size < 10 + hashlen )
1794 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1795
1796 /*
1797 * Static bounds check:
1798 * - Need 10 bytes for five tag-length pairs.
1799 * (Insist on 1-byte length encodings to protect against variants of
1800 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1801 * - Need hashlen bytes for hash
1802 * - Need oid_size bytes for hash alg OID.
1803 */
1804 if( nb_pad < 10 + hashlen + oid_size )
1805 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1806 nb_pad -= 10 + hashlen + oid_size;
1807 }
1808 else
1809 {
1810 if( nb_pad < hashlen )
1811 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1812
1813 nb_pad -= hashlen;
1814 }
1815
Hanno Becker2b2f8982017-09-27 17:10:03 +01001816 /* Need space for signature header and padding delimiter (3 bytes),
1817 * and 8 bytes for the minimal padding */
1818 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001819 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1820 nb_pad -= 3;
1821
1822 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001823 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001824
1825 /* Write signature header and padding */
1826 *p++ = 0;
1827 *p++ = MBEDTLS_RSA_SIGN;
1828 memset( p, 0xFF, nb_pad );
1829 p += nb_pad;
1830 *p++ = 0;
1831
1832 /* Are we signing raw data? */
1833 if( md_alg == MBEDTLS_MD_NONE )
1834 {
1835 memcpy( p, hash, hashlen );
1836 return( 0 );
1837 }
1838
1839 /* Signing hashed data, add corresponding ASN.1 structure
1840 *
1841 * DigestInfo ::= SEQUENCE {
1842 * digestAlgorithm DigestAlgorithmIdentifier,
1843 * digest Digest }
1844 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1845 * Digest ::= OCTET STRING
1846 *
1847 * Schematic:
1848 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1849 * TAG-NULL + LEN [ NULL ] ]
1850 * TAG-OCTET + LEN [ HASH ] ]
1851 */
1852 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001853 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001854 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001855 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001856 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001857 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001858 memcpy( p, oid, oid_size );
1859 p += oid_size;
1860 *p++ = MBEDTLS_ASN1_NULL;
1861 *p++ = 0x00;
1862 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001863 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001864 memcpy( p, hash, hashlen );
1865 p += hashlen;
1866
1867 /* Just a sanity-check, should be automatic
1868 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001869 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001870 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001871 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001872 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1873 }
1874
1875 return( 0 );
1876}
1877
Paul Bakkerb3869132013-02-28 17:21:01 +01001878/*
1879 * Do an RSA operation to sign the message digest
1880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001882 int (*f_rng)(void *, unsigned char *, size_t),
1883 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001885 unsigned int hashlen,
1886 const unsigned char *hash,
1887 unsigned char *sig )
1888{
Janos Follath24eed8d2019-11-22 13:21:35 +00001889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001890 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001891
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001892 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001893 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1894 hashlen == 0 ) ||
1895 hash != NULL );
1896 RSA_VALIDATE_RET( sig != NULL );
1897
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001898 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1899 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1900
Hanno Beckerfdf38032017-09-06 12:35:55 +01001901 /*
1902 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1903 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001904
Hanno Beckerfdf38032017-09-06 12:35:55 +01001905 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1906 ctx->len, sig ) ) != 0 )
1907 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001908
Hanno Beckerfdf38032017-09-06 12:35:55 +01001909 /* Private key operation
1910 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001911 * In order to prevent Lenstra's attack, make the signature in a
1912 * temporary buffer and check it before returning it.
1913 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001914
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001915 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001916 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001917 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1918
Hanno Beckerfdf38032017-09-06 12:35:55 +01001919 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001920 if( verif == NULL )
1921 {
1922 mbedtls_free( sig_try );
1923 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1924 }
1925
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001926 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1927 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1928
Gabor Mezei90437e32021-10-20 11:59:27 +02001929 if( mbedtls_ct_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001930 {
1931 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1932 goto cleanup;
1933 }
1934
1935 memcpy( sig, sig_try, ctx->len );
1936
1937cleanup:
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001938 mbedtls_platform_zeroize( sig_try, ctx->len );
1939 mbedtls_platform_zeroize( verif, ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001940 mbedtls_free( sig_try );
1941 mbedtls_free( verif );
1942
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001943 if( ret != 0 )
1944 memset( sig, '!', ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001945 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001946}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001948
1949/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 * Do an RSA operation to sign the message digest
1951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001953 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001954 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001956 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001957 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 unsigned char *sig )
1959{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001960 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001961 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1962 hashlen == 0 ) ||
1963 hash != NULL );
1964 RSA_VALIDATE_RET( sig != NULL );
1965
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 switch( ctx->padding )
1967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968#if defined(MBEDTLS_PKCS1_V15)
1969 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney52654982021-05-18 16:54:00 +01001970 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng,
Thomas Daubney140184d2021-05-18 16:04:07 +01001971 md_alg, hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001972#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974#if defined(MBEDTLS_PKCS1_V21)
1975 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyde9fdc42021-05-18 17:10:04 +01001976 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
1977 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001978#endif
1979
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001983}
1984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001986/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001987 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001991 unsigned int hashlen,
1992 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001994 int expected_salt_len,
1995 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001996{
Janos Follath24eed8d2019-11-22 13:21:35 +00001997 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001998 size_t siglen;
1999 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002000 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002002 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002003 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 const mbedtls_md_info_t *md_info;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07002005 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = {0};
Paul Bakkerb3869132013-02-28 17:21:01 +01002006
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002007 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002008 RSA_VALIDATE_RET( sig != NULL );
2009 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2010 hashlen == 0 ) ||
2011 hash != NULL );
2012
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 siglen = ctx->len;
2014
Paul Bakker27fdf462011-06-09 13:55:13 +00002015 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002017
Thomas Daubney782a7f52021-05-19 12:27:35 +01002018 ret = mbedtls_rsa_public( ctx, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
2020 if( ret != 0 )
2021 return( ret );
2022
2023 p = buf;
2024
Paul Bakkerb3869132013-02-28 17:21:01 +01002025 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 {
Simon Butcher02037452016-03-01 21:19:12 +00002030 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002032 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002034
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002035 if( hashlen != mbedtls_md_get_size( md_info ) )
2036 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002037 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002040 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002044
Simon Butcher02037452016-03-01 21:19:12 +00002045 /*
2046 * Note: EMSA-PSS verification is over the length of N - 1 bits
2047 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002048 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002049
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002050 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2051 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2052
Simon Butcher02037452016-03-01 21:19:12 +00002053 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002054 if( msb % 8 == 0 )
2055 {
2056 p++;
2057 siglen -= 1;
2058 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002059
Gilles Peskine139108a2017-10-18 19:03:42 +02002060 if( siglen < hlen + 2 )
2061 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2062 hash_start = p + siglen - hlen - 1;
2063
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02002064 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id );
Jaeden Amero66954e12018-01-25 16:05:54 +00002065 if( ret != 0 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002066 return( ret );
Paul Bakker02303e82013-01-03 11:08:31 +01002067
Paul Bakkerb3869132013-02-28 17:21:01 +01002068 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002069
Gilles Peskine6a54b022017-10-17 19:02:13 +02002070 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002071 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002072
Gilles Peskine91048a32017-10-19 17:46:14 +02002073 if( *p++ != 0x01 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002074 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002075
Gilles Peskine6a54b022017-10-17 19:02:13 +02002076 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002079 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002080 {
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002081 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002082 }
2083
Simon Butcher02037452016-03-01 21:19:12 +00002084 /*
2085 * Generate H = Hash( M' )
2086 */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002087 ret = hash_mprime( hash, hashlen, p, observed_salt_len,
2088 result, mgf1_hash_id );
2089 if( ret != 0 )
2090 return( ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002091
Jaeden Amero66954e12018-01-25 16:05:54 +00002092 if( memcmp( hash_start, result, hlen ) != 0 )
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002093 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002094
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002095 return( 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01002096}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002097
2098/*
2099 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002103 unsigned int hashlen,
2104 const unsigned char *hash,
2105 const unsigned char *sig )
2106{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002107 mbedtls_md_type_t mgf1_hash_id;
2108 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002109 RSA_VALIDATE_RET( sig != NULL );
2110 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2111 hashlen == 0 ) ||
2112 hash != NULL );
2113
2114 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002116 : md_alg;
2117
Thomas Daubney9e65f792021-05-19 12:18:58 +01002118 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx,
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002119 md_alg, hashlen, hash,
2120 mgf1_hash_id,
2121 MBEDTLS_RSA_SALT_LEN_ANY,
2122 sig ) );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002123
2124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002128/*
2129 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002133 unsigned int hashlen,
2134 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002135 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002136{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002137 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002138 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002139 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002140
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002141 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002142 RSA_VALIDATE_RET( sig != NULL );
2143 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2144 hashlen == 0 ) ||
2145 hash != NULL );
2146
2147 sig_len = ctx->len;
2148
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002149 /*
2150 * Prepare expected PKCS1 v1.5 encoding of hash.
2151 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002152
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002153 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2154 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2155 {
2156 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2157 goto cleanup;
2158 }
2159
2160 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2161 encoded_expected ) ) != 0 )
2162 goto cleanup;
2163
2164 /*
2165 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2166 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002167
Thomas Daubney41e4ce42021-05-19 15:10:05 +01002168 ret = mbedtls_rsa_public( ctx, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002169 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002170 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002171
Simon Butcher02037452016-03-01 21:19:12 +00002172 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002173 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002174 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002175
Gabor Mezei90437e32021-10-20 11:59:27 +02002176 if( ( ret = mbedtls_ct_memcmp( encoded, encoded_expected,
gabor-mezei-arm46025642021-07-19 15:19:19 +02002177 sig_len ) ) != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002178 {
2179 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2180 goto cleanup;
2181 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002182
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002183cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002184
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002185 if( encoded != NULL )
2186 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002187 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002188 mbedtls_free( encoded );
2189 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002190
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002191 if( encoded_expected != NULL )
2192 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002193 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002194 mbedtls_free( encoded_expected );
2195 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002196
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002197 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002200
2201/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002202 * Do an RSA operation and check the message digest
2203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002206 unsigned int hashlen,
2207 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002208 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002209{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002210 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002211 RSA_VALIDATE_RET( sig != NULL );
2212 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2213 hashlen == 0 ) ||
2214 hash != NULL );
2215
Paul Bakkerb3869132013-02-28 17:21:01 +01002216 switch( ctx->padding )
2217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218#if defined(MBEDTLS_PKCS1_V15)
2219 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney2e126252021-05-19 11:48:53 +01002220 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, md_alg,
2221 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002222#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224#if defined(MBEDTLS_PKCS1_V21)
2225 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002226 return mbedtls_rsa_rsassa_pss_verify( ctx, md_alg,
2227 hashlen, hash, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01002228#endif
2229
2230 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002232 }
2233}
2234
2235/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002236 * Copy the components of an RSA key
2237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002239{
Janos Follath24eed8d2019-11-22 13:21:35 +00002240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002241 RSA_VALIDATE_RET( dst != NULL );
2242 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002243
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002244 dst->len = src->len;
2245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2247 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2250 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2251 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002252
2253#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2255 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2256 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2258 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002259#endif
2260
2261 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2264 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002265
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002266 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002267 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002268
2269cleanup:
2270 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002272
2273 return( ret );
2274}
2275
2276/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 * Free the components of an RSA key
2278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002280{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002281 if( ctx == NULL )
2282 return;
2283
2284 mbedtls_mpi_free( &ctx->Vi );
2285 mbedtls_mpi_free( &ctx->Vf );
2286 mbedtls_mpi_free( &ctx->RN );
2287 mbedtls_mpi_free( &ctx->D );
2288 mbedtls_mpi_free( &ctx->Q );
2289 mbedtls_mpi_free( &ctx->P );
2290 mbedtls_mpi_free( &ctx->E );
2291 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002292
Hanno Becker33c30a02017-08-23 07:00:22 +01002293#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002294 mbedtls_mpi_free( &ctx->RQ );
2295 mbedtls_mpi_free( &ctx->RP );
2296 mbedtls_mpi_free( &ctx->QP );
2297 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002298 mbedtls_mpi_free( &ctx->DP );
2299#endif /* MBEDTLS_RSA_NO_CRT */
2300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002302 /* Free the mutex, but only if it hasn't been freed already. */
2303 if( ctx->ver != 0 )
2304 {
2305 mbedtls_mutex_free( &ctx->mutex );
2306 ctx->ver = 0;
2307 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002308#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002309}
2310
Hanno Beckerab377312017-08-23 16:24:51 +01002311#endif /* !MBEDTLS_RSA_ALT */
2312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002314
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002315#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
2317/*
2318 * Example RSA-1024 keypair, for test purposes
2319 */
2320#define KEY_LEN 128
2321
2322#define RSA_N "9292758453063D803DD603D5E777D788" \
2323 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2324 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2325 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2326 "93A89813FBF3C4F8066D2D800F7C38A8" \
2327 "1AE31942917403FF4946B0A83D3D3E05" \
2328 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2329 "5E94BB77B07507233A0BC7BAC8F90F79"
2330
2331#define RSA_E "10001"
2332
2333#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2334 "66CA472BC44D253102F8B4A9D3BFA750" \
2335 "91386C0077937FE33FA3252D28855837" \
2336 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2337 "DF79C5CE07EE72C7F123142198164234" \
2338 "CABB724CF78B8173B9F880FC86322407" \
2339 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2340 "071513A1E85B5DFA031F21ECAE91A34D"
2341
2342#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2343 "2C01CAD19EA484A87EA4377637E75500" \
2344 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2345 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2346
2347#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2348 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2349 "910E4168387E3C30AA1E00C339A79508" \
2350 "8452DD96A9A5EA5D9DCA68DA636032AF"
2351
Paul Bakker5121ce52009-01-03 21:22:43 +00002352#define PT_LEN 24
2353#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2354 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002357static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002358{
gufe44c2620da2020-08-03 17:56:50 +02002359#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002360 size_t i;
2361
Paul Bakker545570e2010-07-18 09:00:25 +00002362 if( rng_state != NULL )
2363 rng_state = NULL;
2364
Paul Bakkera3d195c2011-11-27 21:07:34 +00002365 for( i = 0; i < len; ++i )
2366 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002367#else
2368 if( rng_state != NULL )
2369 rng_state = NULL;
2370
2371 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002372#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002373
Paul Bakkera3d195c2011-11-27 21:07:34 +00002374 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002375}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002377
Paul Bakker5121ce52009-01-03 21:22:43 +00002378/*
2379 * Checkup routine
2380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002382{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002383 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002385 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 unsigned char rsa_plaintext[PT_LEN];
2388 unsigned char rsa_decrypted[PT_LEN];
2389 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002391 unsigned char sha1sum[20];
2392#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002393
Hanno Becker3a701162017-08-22 13:52:43 +01002394 mbedtls_mpi K;
2395
2396 mbedtls_mpi_init( &K );
Ronald Cronc1905a12021-06-05 11:11:14 +02002397 mbedtls_rsa_init( &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
Hanno Becker3a701162017-08-22 13:52:43 +01002399 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2400 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2401 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2402 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2403 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2404 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2405 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2406 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2407 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2408 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2409
Hanno Becker7f25f852017-10-10 16:56:22 +01002410 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002411
2412 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2416 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 {
2418 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
Hanno Becker5bc87292017-05-03 15:09:31 +01002421 ret = 1;
2422 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 }
2424
2425 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427
2428 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2429
Thomas Daubney21772772021-05-13 17:30:32 +01002430 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002431 PT_LEN, rsa_plaintext,
2432 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 {
2434 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
Hanno Becker5bc87292017-05-03 15:09:31 +01002437 ret = 1;
2438 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 }
2440
2441 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002444 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002445 &len, rsa_ciphertext, rsa_decrypted,
2446 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 {
2448 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
Hanno Becker5bc87292017-05-03 15:09:31 +01002451 ret = 1;
2452 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 }
2454
2455 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2456 {
2457 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002459
Hanno Becker5bc87292017-05-03 15:09:31 +01002460 ret = 1;
2461 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 }
2463
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002464 if( verbose != 0 )
2465 mbedtls_printf( "passed\n" );
2466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002469 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
TRodziewicz26371e42021-06-08 16:45:41 +02002471 if( mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002472 {
2473 if( verbose != 0 )
2474 mbedtls_printf( "failed\n" );
2475
2476 return( 1 );
2477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
Hanno Becker98838b02017-10-02 13:16:10 +01002479 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002480 MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002481 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 {
2483 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Hanno Becker5bc87292017-05-03 15:09:31 +01002486 ret = 1;
2487 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 }
2489
2490 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002493 if( mbedtls_rsa_pkcs1_verify( &rsa, MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002494 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 {
2496 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498
Hanno Becker5bc87292017-05-03 15:09:31 +01002499 ret = 1;
2500 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 }
2502
2503 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002504 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002506
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002507 if( verbose != 0 )
2508 mbedtls_printf( "\n" );
2509
Paul Bakker3d8fb632014-04-17 12:42:41 +02002510cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002511 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 mbedtls_rsa_free( &rsa );
2513#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002514 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002516 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002517}
2518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#endif /* MBEDTLS_RSA_C */