blob: d879a30e3b6453a67834461970c58c845cc9f4d1 [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 {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200505 /* Just make sure this hash is supported in this build. */
506 if( mbedtls_hash_info_get_size( hash_id ) == 0 )
Ronald Cronea7631b2021-06-03 18:51:59 +0200507 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
508 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200509#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500510
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100511 ctx->padding = padding;
512 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200513
514 return( 0 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100515}
516
Hanno Becker617c1ae2017-08-23 14:11:24 +0100517/*
518 * Get length in bytes of RSA modulus
519 */
520
521size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
522{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100523 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100524}
525
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529/*
530 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800531 *
532 * This generation method follows the RSA key pair generation procedure of
533 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000536 int (*f_rng)(void *, unsigned char *, size_t),
537 void *p_rng,
538 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000539{
Janos Follath24eed8d2019-11-22 13:21:35 +0000540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800541 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100542 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500543 RSA_VALIDATE_RET( ctx != NULL );
544 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
Janos Follathb8fc1b02018-09-03 15:37:01 +0100546 /*
547 * If the modulus is 1024 bit long or shorter, then the security strength of
548 * the RSA algorithm is less than or equal to 80 bits and therefore an error
549 * rate of 2^-80 is sufficient.
550 */
551 if( nbits > 1024 )
552 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
553
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100554 mbedtls_mpi_init( &H );
555 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800556 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100558 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
559 {
560 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
561 goto cleanup;
562 }
563
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 /*
565 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800566 * 1. |P-Q| > 2^( nbits / 2 - 100 )
567 * 2. GCD( E, (P-1)*(Q-1) ) == 1
568 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572 do
573 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100574 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
575 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Janos Follathb8fc1b02018-09-03 15:37:01 +0100577 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
578 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800580 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
581 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
582 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 continue;
584
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800585 /* not required by any standards, but some users rely on the fact that P > Q */
586 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100587 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100588
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100589 /* Temporarily replace P,Q by P-1, Q-1 */
590 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
591 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
592 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800593
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800594 /* 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 +0200595 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800596 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
597 continue;
598
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800599 /* 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 -0800600 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
601 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
602 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
603
604 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
605 continue;
606
607 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800609 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000610
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100611 /* Restore P,Q */
612 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
613 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
614
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800615 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
616
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100617 ctx->len = mbedtls_mpi_size( &ctx->N );
618
Jethro Beekman97f95c92018-02-13 15:50:36 -0800619#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 * DP = D mod (P - 1)
622 * DQ = D mod (Q - 1)
623 * QP = Q^-1 mod P
624 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100625 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
626 &ctx->DP, &ctx->DQ, &ctx->QP ) );
627#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
Hanno Becker83aad1f2017-08-23 06:45:10 +0100629 /* Double-check */
630 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
632cleanup:
633
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100634 mbedtls_mpi_free( &H );
635 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800636 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
638 if( ret != 0 )
639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100641
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100642 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100643 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100644 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 }
646
Paul Bakker48377d92013-08-30 12:06:24 +0200647 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000648}
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
652/*
653 * Check a public RSA key
654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000656{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500657 RSA_VALIDATE_RET( ctx != NULL );
658
Hanno Beckerebd2c022017-10-12 10:54:53 +0100659 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000661
Hanno Becker3a760a12018-01-05 08:14:49 +0000662 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100665 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
Hanno Becker705fc682017-10-10 17:57:02 +0100667 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
668 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100672 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
674 return( 0 );
675}
676
677/*
Hanno Becker705fc682017-10-10 17:57:02 +0100678 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000681{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500682 RSA_VALIDATE_RET( ctx != NULL );
683
Hanno Becker705fc682017-10-10 17:57:02 +0100684 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100685 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 {
Hanno Becker98838b02017-10-02 13:16:10 +0100687 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 }
Paul Bakker48377d92013-08-30 12:06:24 +0200689
Hanno Becker98838b02017-10-02 13:16:10 +0100690 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100691 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000692 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100693 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000695
Hanno Beckerb269a852017-08-25 08:03:21 +0100696#if !defined(MBEDTLS_RSA_NO_CRT)
697 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
698 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
699 {
700 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
701 }
702#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000703
704 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000705}
706
707/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708 * Check if contexts holding a public and private key match
709 */
Hanno Becker98838b02017-10-02 13:16:10 +0100710int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
711 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100712{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500713 RSA_VALIDATE_RET( pub != NULL );
714 RSA_VALIDATE_RET( prv != NULL );
715
Hanno Becker98838b02017-10-02 13:16:10 +0100716 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100720 }
721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
723 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100726 }
727
728 return( 0 );
729}
730
731/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 * Do an RSA public key operation
733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000735 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000736 unsigned char *output )
737{
Janos Follath24eed8d2019-11-22 13:21:35 +0000738 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000739 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500741 RSA_VALIDATE_RET( ctx != NULL );
742 RSA_VALIDATE_RET( input != NULL );
743 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Hanno Beckerebd2c022017-10-12 10:54:53 +0100745 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100746 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200750#if defined(MBEDTLS_THREADING_C)
751 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
752 return( ret );
753#endif
754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000758 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200759 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
760 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 }
762
763 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
765 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
767cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200769 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
770 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100771#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000774
775 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100776 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000777
778 return( 0 );
779}
780
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200781/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200782 * Generate or update blinding values, see section 10 of:
783 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200784 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200785 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200786 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200787static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200788 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
789{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200790 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200791 mbedtls_mpi R;
792
793 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200794
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200795 if( ctx->Vf.p != NULL )
796 {
797 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
799 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
800 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
801 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200802
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200803 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200804 }
805
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200806 /* Unblinding value: Vf = random number, invertible mod N */
807 do {
808 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200809 {
810 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
811 goto cleanup;
812 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 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 +0200815
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200816 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200817 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
818 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
819 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
820
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200821 /* At this point, Vi is invertible mod N if and only if both Vf and R
822 * are invertible mod N. If one of them isn't, we don't need to know
823 * which one, we just loop and choose new values for both of them.
824 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200825 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500826 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200827 goto cleanup;
828
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500829 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
830
831 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
832 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
833 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200834
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200835 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200836 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 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 +0200838
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200839
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200840cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200841 mbedtls_mpi_free( &R );
842
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200843 return( ret );
844}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200845
Paul Bakker5121ce52009-01-03 21:22:43 +0000846/*
Janos Follathe81102e2017-03-22 13:38:28 +0000847 * Exponent blinding supposed to prevent side-channel attacks using multiple
848 * traces of measurements to recover the RSA key. The more collisions are there,
849 * the more bits of the key can be recovered. See [3].
850 *
851 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800852 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000853 *
854 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800855 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000856 *
857 * (With the currently (as of 2017 April) known best algorithms breaking 2048
858 * bit RSA requires approximately as much time as trying out 2^112 random keys.
859 * Thus in this sense with 28 byte blinding the security is not reduced by
860 * side-channel attacks like the one in [3])
861 *
862 * This countermeasure does not help if the key recovery is possible with a
863 * single trace.
864 */
865#define RSA_EXPONENT_BLINDING 28
866
867/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000868 * Do an RSA private key operation
869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200871 int (*f_rng)(void *, unsigned char *, size_t),
872 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000873 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000874 unsigned char *output )
875{
Janos Follath24eed8d2019-11-22 13:21:35 +0000876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000877 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100878
879 /* Temporary holding the result */
880 mbedtls_mpi T;
881
882 /* Temporaries holding P-1, Q-1 and the
883 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000884 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100885
886#if !defined(MBEDTLS_RSA_NO_CRT)
887 /* Temporaries holding the results mod p resp. mod q. */
888 mbedtls_mpi TP, TQ;
889
890 /* Temporaries holding the blinded exponents for
891 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000892 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100893
894 /* Pointers to actual exponents to be used - either the unblinded
895 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000896 mbedtls_mpi *DP = &ctx->DP;
897 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100898#else
899 /* Temporary holding the blinded exponent (if used). */
900 mbedtls_mpi D_blind;
901
902 /* Pointer to actual exponent to be used - either the unblinded
903 * or the blinded one, depending on the presence of a PRNG. */
904 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100905#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100906
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100907 /* Temporaries holding the initial input and the double
908 * checked result; should be the same in the end. */
909 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500911 RSA_VALIDATE_RET( ctx != NULL );
912 RSA_VALIDATE_RET( input != NULL );
913 RSA_VALIDATE_RET( output != NULL );
914
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200915 if( f_rng == NULL )
916 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
917
918 if( rsa_check_context( ctx, 1 /* private key checks */,
919 1 /* blinding on */ ) != 0 )
Hanno Beckerebd2c022017-10-12 10:54:53 +0100920 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100921 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100922 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100923
Hanno Becker06811ce2017-05-03 15:10:34 +0100924#if defined(MBEDTLS_THREADING_C)
925 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
926 return( ret );
927#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000928
Hanno Becker06811ce2017-05-03 15:10:34 +0100929 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100930 mbedtls_mpi_init( &T );
931
932 mbedtls_mpi_init( &P1 );
933 mbedtls_mpi_init( &Q1 );
934 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000935
Janos Follathe81102e2017-03-22 13:38:28 +0000936#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200937 mbedtls_mpi_init( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +0000938#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200939 mbedtls_mpi_init( &DP_blind );
940 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000941#endif
942
Hanno Becker06811ce2017-05-03 15:10:34 +0100943#if !defined(MBEDTLS_RSA_NO_CRT)
944 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200945#endif
946
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100947 mbedtls_mpi_init( &I );
948 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100949
950 /* End of MPI initialization */
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
953 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200955 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
956 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000957 }
958
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100959 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100960
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200961 /*
962 * Blinding
963 * T = T * Vi mod N
964 */
965 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
966 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
967 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000968
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200969 /*
970 * Exponent blinding
971 */
972 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
973 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000974
Janos Follathf9203b42017-03-22 15:13:15 +0000975#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200976 /*
977 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
978 */
979 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
980 f_rng, p_rng ) );
981 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
982 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
983 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000984
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200985 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000986#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200987 /*
988 * DP_blind = ( P - 1 ) * R + DP
989 */
990 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
991 f_rng, p_rng ) );
992 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
993 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
994 &ctx->DP ) );
Janos Follathf9203b42017-03-22 15:13:15 +0000995
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200996 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000997
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200998 /*
999 * DQ_blind = ( Q - 1 ) * R + DQ
1000 */
1001 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1002 f_rng, p_rng ) );
1003 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1004 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1005 &ctx->DQ ) );
Janos Follathf9203b42017-03-22 15:13:15 +00001006
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001007 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001008#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001011 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001012#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001013 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001014 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001016 * TP = input ^ dP mod P
1017 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001019
1020 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1021 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
1023 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001024 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001025 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001026 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1027 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1028 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
1030 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001031 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001032 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001033 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1034 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001036
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001037 /*
1038 * Unblind
1039 * T = T * Vf mod N
1040 */
1041 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
1042 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
Hanno Becker2dec5e82017-10-03 07:49:52 +01001044 /* Verify the result to prevent glitching attacks. */
1045 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1046 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001047 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001048 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001049 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1050 goto cleanup;
1051 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001052
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
1056cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001058 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1059 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001060#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001061
Hanno Becker06811ce2017-05-03 15:10:34 +01001062 mbedtls_mpi_free( &P1 );
1063 mbedtls_mpi_free( &Q1 );
1064 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001065
Janos Follathe81102e2017-03-22 13:38:28 +00001066#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001067 mbedtls_mpi_free( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +00001068#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001069 mbedtls_mpi_free( &DP_blind );
1070 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
Hanno Becker06811ce2017-05-03 15:10:34 +01001073 mbedtls_mpi_free( &T );
1074
1075#if !defined(MBEDTLS_RSA_NO_CRT)
1076 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1077#endif
1078
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001079 mbedtls_mpi_free( &C );
1080 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001081
Gilles Peskineae3741e2020-11-25 00:10:31 +01001082 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001083 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Gilles Peskineae3741e2020-11-25 00:10:31 +01001085 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086}
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001089/**
1090 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1091 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001092 * \param dst buffer to mask
1093 * \param dlen length of destination buffer
1094 * \param src source of the mask generation
1095 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001096 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001097 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001098static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001099 size_t slen, mbedtls_md_type_t md_alg )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001100{
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001101 const mbedtls_md_info_t *md_info;
1102 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104 unsigned char counter[4];
1105 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001106 unsigned int hlen;
1107 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001108 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001109
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001110 mbedtls_md_init( &md_ctx );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001112 memset( counter, 0, 4 );
1113
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001114 md_info = mbedtls_md_info_from_type( md_alg );
1115 if( md_info == NULL )
1116 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1117
1118 mbedtls_md_init( &md_ctx );
1119 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1120 goto exit;
1121
1122 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001123
Simon Butcher02037452016-03-01 21:19:12 +00001124 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001125 p = dst;
1126
1127 while( dlen > 0 )
1128 {
1129 use_len = hlen;
1130 if( dlen < hlen )
1131 use_len = dlen;
1132
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001133 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001134 goto exit;
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001135 if( ( ret = mbedtls_md_update( &md_ctx, src, slen ) ) != 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, counter, 4 ) ) != 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_finish( &md_ctx, mask ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001140 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001141
1142 for( i = 0; i < use_len; ++i )
1143 *p++ ^= mask[i];
1144
1145 counter[3]++;
1146
1147 dlen -= use_len;
1148 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001149
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001150exit:
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001151 mbedtls_md_free( &md_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001152 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001153
1154 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001155}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001156
1157/**
1158 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1159 *
1160 * \param hash the input hash
1161 * \param hlen length of the input hash
1162 * \param salt the input salt
1163 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001164 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001165 * \param md_alg message digest to use
1166 */
1167static int hash_mprime( const unsigned char *hash, size_t hlen,
1168 const unsigned char *salt, size_t slen,
1169 unsigned char *out, mbedtls_md_type_t md_alg )
1170{
1171 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1172 mbedtls_md_context_t md_ctx;
1173 int ret;
1174
1175 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1176 if( md_info == NULL )
1177 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1178
1179 mbedtls_md_init( &md_ctx );
1180 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1181 goto exit;
1182 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1183 goto exit;
1184 if( ( ret = mbedtls_md_update( &md_ctx, zeros, sizeof( zeros ) ) ) != 0 )
1185 goto exit;
1186 if( ( ret = mbedtls_md_update( &md_ctx, hash, hlen ) ) != 0 )
1187 goto exit;
1188 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1189 goto exit;
1190 if( ( ret = mbedtls_md_finish( &md_ctx, out ) ) != 0 )
1191 goto exit;
1192
1193exit:
1194 mbedtls_md_free( &md_ctx );
1195
1196 return( ret );
1197}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001198
1199/**
1200 * Compute a hash.
1201 *
1202 * \param md_alg algorithm to use
1203 * \param input input message to hash
1204 * \param ilen input length
1205 * \param output the output buffer - must be large enough for \p md_alg
1206 */
1207static int compute_hash( mbedtls_md_type_t md_alg,
1208 const unsigned char *input, size_t ilen,
1209 unsigned char *output )
1210{
1211 const mbedtls_md_info_t *md_info;
1212
1213 md_info = mbedtls_md_info_from_type( md_alg );
1214 if( md_info == NULL )
1215 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1216
1217 return( mbedtls_md( md_info, input, ilen, output ) );
1218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001222/*
1223 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001226 int (*f_rng)(void *, unsigned char *, size_t),
1227 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001228 const unsigned char *label, size_t label_len,
1229 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001230 const unsigned char *input,
1231 unsigned char *output )
1232{
1233 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001235 unsigned char *p = output;
1236 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001237
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001238 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001239 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001240 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001241 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1242
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001243 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001245
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001246 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1247 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001249
1250 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001251
Simon Butcher02037452016-03-01 21:19:12 +00001252 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001253 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001255
1256 memset( output, 0, olen );
1257
1258 *p++ = 0;
1259
Simon Butcher02037452016-03-01 21:19:12 +00001260 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001261 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001262 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001263
1264 p += hlen;
1265
Simon Butcher02037452016-03-01 21:19:12 +00001266 /* Construct DB */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001267 ret = compute_hash( (mbedtls_md_type_t) ctx->hash_id, label, label_len, p );
1268 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001269 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001270 p += hlen;
1271 p += olen - 2 * hlen - 2 - ilen;
1272 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001273 if( ilen != 0 )
1274 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001275
Simon Butcher02037452016-03-01 21:19:12 +00001276 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001277 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001278 ctx->hash_id ) ) != 0 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02001279 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001280
Simon Butcher02037452016-03-01 21:19:12 +00001281 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001282 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001283 ctx->hash_id ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001284 return( ret );
1285
Thomas Daubney141700f2021-05-13 19:06:10 +01001286 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001287}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001291/*
1292 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001295 int (*f_rng)(void *, unsigned char *, size_t),
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001296 void *p_rng, size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001297 const unsigned char *input,
1298 unsigned char *output )
1299{
1300 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001302 unsigned char *p = output;
1303
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001304 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001305 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001306 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001307
Paul Bakkerb3869132013-02-28 17:21:01 +01001308 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001309
Simon Butcher02037452016-03-01 21:19:12 +00001310 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001311 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001313
1314 nb_pad = olen - 3 - ilen;
1315
1316 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001317
1318 if( f_rng == NULL )
1319 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1320
1321 *p++ = MBEDTLS_RSA_CRYPT;
1322
1323 while( nb_pad-- > 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001324 {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001325 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001326
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001327 do {
1328 ret = f_rng( p_rng, p, 1 );
1329 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001330
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001331 /* Check if RNG failed to generate data */
1332 if( rng_dl == 0 || ret != 0 )
1333 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001334
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001335 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001336 }
1337
1338 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001339 if( ilen != 0 )
1340 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001341
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001342 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001343}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001345
Paul Bakker5121ce52009-01-03 21:22:43 +00001346/*
1347 * Add the message padding, then do an RSA operation
1348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001350 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001351 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +01001352 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001353 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 unsigned char *output )
1355{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001356 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001357 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001358 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001359
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 switch( ctx->padding )
1361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_PKCS1_V15)
1363 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney21772772021-05-13 17:30:32 +01001364 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001365 ilen, input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001366#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_PKCS1_V21)
1369 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney141700f2021-05-13 19:06:10 +01001370 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, NULL, 0,
Thomas Daubney21772772021-05-13 17:30:32 +01001371 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001372#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
1374 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001377}
1378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001380/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001381 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001384 int (*f_rng)(void *, unsigned char *, size_t),
1385 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001386 const unsigned char *label, size_t label_len,
1387 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001388 const unsigned char *input,
1389 unsigned char *output,
1390 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001391{
Janos Follath24eed8d2019-11-22 13:21:35 +00001392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001393 size_t ilen, i, pad_len;
1394 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1396 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001397 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001398
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001399 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001400 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1401 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1402 RSA_VALIDATE_RET( input != NULL );
1403 RSA_VALIDATE_RET( olen != NULL );
1404
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001405 /*
1406 * Parameters sanity checks
1407 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001408 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
1411 ilen = ctx->len;
1412
Paul Bakker27fdf462011-06-09 13:55:13 +00001413 if( ilen < 16 || ilen > sizeof( buf ) )
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
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001416 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1417 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001419
Janos Follathc17cda12016-02-11 11:08:18 +00001420 // checking for integer underflow
1421 if( 2 * hlen + 2 > ilen )
1422 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1423
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001424 /*
1425 * RSA operation
1426 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001427 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
1429 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001430 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001432 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001433 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001434 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001435 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001436 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001437 ctx->hash_id ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001438 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001439 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001440 ctx->hash_id ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001441 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001442 goto cleanup;
1443 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001444
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001445 /* Generate lHash */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001446 ret = compute_hash( (mbedtls_md_type_t) ctx->hash_id,
1447 label, label_len, lhash );
1448 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001449 goto cleanup;
1450
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001451 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001452 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001453 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001455 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001456
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001457 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001458
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001459 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001460
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001461 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001462 for( i = 0; i < hlen; i++ )
1463 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001464
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001465 /* Get zero-padding len, but always read till end of buffer
1466 * (minus one, for the 01 byte) */
1467 pad_len = 0;
1468 pad_done = 0;
1469 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1470 {
1471 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001472 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001473 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001474
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001475 p += pad_len;
1476 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001477
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001478 /*
1479 * The only information "leaked" is whether the padding was correct or not
1480 * (eg, no data is copied if it was not correct). This meets the
1481 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1482 * the different error conditions.
1483 */
1484 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001485 {
1486 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1487 goto cleanup;
1488 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001489
Paul Bakker66d5d072014-06-17 16:39:18 +02001490 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001491 {
1492 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1493 goto cleanup;
1494 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001495
1496 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001497 if( *olen != 0 )
1498 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001499 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001500
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001501cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001502 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1503 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001504
1505 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001510/*
1511 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1512 */
1513int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
1514 int (*f_rng)(void *, unsigned char *, size_t),
1515 void *p_rng,
1516 size_t *olen,
1517 const unsigned char *input,
1518 unsigned char *output,
1519 size_t output_max_len )
1520{
1521 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1522 size_t ilen;
1523 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1524
1525 RSA_VALIDATE_RET( ctx != NULL );
1526 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1527 RSA_VALIDATE_RET( input != NULL );
1528 RSA_VALIDATE_RET( olen != NULL );
1529
1530 ilen = ctx->len;
1531
1532 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1533 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1534
1535 if( ilen < 16 || ilen > sizeof( buf ) )
1536 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1537
1538 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
1539
1540 if( ret != 0 )
1541 goto cleanup;
1542
Gabor Mezei90437e32021-10-20 11:59:27 +02001543 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding( buf, ilen,
Gabor Mezei63bbba52021-10-18 16:17:57 +02001544 output, output_max_len, olen );
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001545
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001546cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001547 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001548
1549 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001552
1553/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001554 * Do an RSA operation, then remove the message padding
1555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001557 int (*f_rng)(void *, unsigned char *, size_t),
1558 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001559 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001560 const unsigned char *input,
1561 unsigned char *output,
1562 size_t output_max_len)
1563{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001564 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001565 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1566 RSA_VALIDATE_RET( input != NULL );
1567 RSA_VALIDATE_RET( olen != NULL );
1568
Paul Bakkerb3869132013-02-28 17:21:01 +01001569 switch( ctx->padding )
1570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#if defined(MBEDTLS_PKCS1_V15)
1572 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney34733082021-05-12 09:24:29 +01001573 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001574 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001575#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#if defined(MBEDTLS_PKCS1_V21)
1578 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001579 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001580 olen, input, output,
1581 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001582#endif
1583
1584 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001586 }
1587}
1588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001590static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001591 int (*f_rng)(void *, unsigned char *, size_t),
1592 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001594 unsigned int hashlen,
1595 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001596 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001597 unsigned char *sig )
1598{
1599 size_t olen;
1600 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001601 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001602 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001603 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001604 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001605
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001606 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001607 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1608 hashlen == 0 ) ||
1609 hash != NULL );
1610 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001611
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001612 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1613 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1614
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001615 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001617
1618 olen = ctx->len;
1619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001621 {
Simon Butcher02037452016-03-01 21:19:12 +00001622 /* Gather length of hash to sign */
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001623 size_t exp_hashlen = mbedtls_hash_info_get_size( md_alg );
1624 if( exp_hashlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001626
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001627 if( hashlen != exp_hashlen )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001628 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001629 }
1630
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02001631 hlen = mbedtls_hash_info_get_size( (mbedtls_md_type_t) ctx->hash_id );
1632 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001634
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001635 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1636 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001637 /* Calculate the largest possible salt length, up to the hash size.
1638 * Normally this is the hash length, which is the maximum salt length
1639 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001640 * enough room, use the maximum salt length that fits. The constraint is
1641 * that the hash length plus the salt length plus 2 bytes must be at most
1642 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1643 * (PKCS#1 v2.2) §9.1.1 step 3. */
1644 min_slen = hlen - 2;
1645 if( olen < hlen + min_slen + 2 )
1646 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1647 else if( olen >= hlen + hlen + 2 )
1648 slen = hlen;
1649 else
1650 slen = olen - hlen - 2;
1651 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001652 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001655 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001656 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001657 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001658 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001659 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001660
1661 memset( sig, 0, olen );
1662
Simon Butcher02037452016-03-01 21:19:12 +00001663 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001664 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001665 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001666 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001667
1668 /* Generate salt of length slen in place in the encoded message */
1669 salt = p;
1670 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001671 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001672
Paul Bakkerb3869132013-02-28 17:21:01 +01001673 p += slen;
1674
Simon Butcher02037452016-03-01 21:19:12 +00001675 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001676 ret = hash_mprime( hash, hashlen, salt, slen, p, ctx->hash_id );
1677 if( ret != 0 )
1678 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001679
Simon Butcher02037452016-03-01 21:19:12 +00001680 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001681 if( msb % 8 == 0 )
1682 offset = 1;
1683
Simon Butcher02037452016-03-01 21:19:12 +00001684 /* maskedDB: Apply dbMask to DB */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001685 ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1686 ctx->hash_id );
1687 if( ret != 0 )
1688 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001689
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001690 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001691 sig[0] &= 0xFF >> ( olen * 8 - msb );
1692
1693 p += hlen;
1694 *p++ = 0xBC;
1695
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001696 return mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001697}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001698
1699/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001700 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1701 * the option to pass in the salt length.
1702 */
1703int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1704 int (*f_rng)(void *, unsigned char *, size_t),
1705 void *p_rng,
1706 mbedtls_md_type_t md_alg,
1707 unsigned int hashlen,
1708 const unsigned char *hash,
1709 int saltlen,
1710 unsigned char *sig )
1711{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001712 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001713 hashlen, hash, saltlen, sig );
1714}
1715
1716
1717/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001718 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1719 */
1720int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1721 int (*f_rng)(void *, unsigned char *, size_t),
1722 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001723 mbedtls_md_type_t md_alg,
1724 unsigned int hashlen,
1725 const unsigned char *hash,
1726 unsigned char *sig )
1727{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001728 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001729 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001730}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001734/*
1735 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1736 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001737
1738/* Construct a PKCS v1.5 encoding of a hashed message
1739 *
1740 * This is used both for signature generation and verification.
1741 *
1742 * Parameters:
1743 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001744 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001745 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001746 * - hash: Buffer containing the hashed message or the raw data.
1747 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001748 * - dst: Buffer to hold the encoded message.
1749 *
1750 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001751 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001752 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001753 *
1754 */
1755static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1756 unsigned int hashlen,
1757 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001758 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001759 unsigned char *dst )
1760{
1761 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001762 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001763 unsigned char *p = dst;
1764 const char *oid = NULL;
1765
1766 /* Are we signing hashed or raw data? */
1767 if( md_alg != MBEDTLS_MD_NONE )
1768 {
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +02001769 unsigned char md_size = mbedtls_hash_info_get_size( md_alg );
Manuel Pégourié-Gonnardf493f2a2022-07-05 17:41:05 +02001770 if( md_size == 0 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001771 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1772
1773 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1774 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1775
Manuel Pégourié-Gonnardf493f2a2022-07-05 17:41:05 +02001776 if( hashlen != md_size )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001777 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001778
1779 /* Double-check that 8 + hashlen + oid_size can be used as a
1780 * 1-byte ASN.1 length encoding and that there's no overflow. */
1781 if( 8 + hashlen + oid_size >= 0x80 ||
1782 10 + hashlen < hashlen ||
1783 10 + hashlen + oid_size < 10 + hashlen )
1784 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1785
1786 /*
1787 * Static bounds check:
1788 * - Need 10 bytes for five tag-length pairs.
1789 * (Insist on 1-byte length encodings to protect against variants of
1790 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1791 * - Need hashlen bytes for hash
1792 * - Need oid_size bytes for hash alg OID.
1793 */
1794 if( nb_pad < 10 + hashlen + oid_size )
1795 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1796 nb_pad -= 10 + hashlen + oid_size;
1797 }
1798 else
1799 {
1800 if( nb_pad < hashlen )
1801 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1802
1803 nb_pad -= hashlen;
1804 }
1805
Hanno Becker2b2f8982017-09-27 17:10:03 +01001806 /* Need space for signature header and padding delimiter (3 bytes),
1807 * and 8 bytes for the minimal padding */
1808 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001809 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1810 nb_pad -= 3;
1811
1812 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001813 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001814
1815 /* Write signature header and padding */
1816 *p++ = 0;
1817 *p++ = MBEDTLS_RSA_SIGN;
1818 memset( p, 0xFF, nb_pad );
1819 p += nb_pad;
1820 *p++ = 0;
1821
1822 /* Are we signing raw data? */
1823 if( md_alg == MBEDTLS_MD_NONE )
1824 {
1825 memcpy( p, hash, hashlen );
1826 return( 0 );
1827 }
1828
1829 /* Signing hashed data, add corresponding ASN.1 structure
1830 *
1831 * DigestInfo ::= SEQUENCE {
1832 * digestAlgorithm DigestAlgorithmIdentifier,
1833 * digest Digest }
1834 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1835 * Digest ::= OCTET STRING
1836 *
1837 * Schematic:
1838 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1839 * TAG-NULL + LEN [ NULL ] ]
1840 * TAG-OCTET + LEN [ HASH ] ]
1841 */
1842 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001843 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001844 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001845 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001846 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001847 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001848 memcpy( p, oid, oid_size );
1849 p += oid_size;
1850 *p++ = MBEDTLS_ASN1_NULL;
1851 *p++ = 0x00;
1852 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001853 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001854 memcpy( p, hash, hashlen );
1855 p += hashlen;
1856
1857 /* Just a sanity-check, should be automatic
1858 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001859 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001860 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001861 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001862 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1863 }
1864
1865 return( 0 );
1866}
1867
Paul Bakkerb3869132013-02-28 17:21:01 +01001868/*
1869 * Do an RSA operation to sign the message digest
1870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001872 int (*f_rng)(void *, unsigned char *, size_t),
1873 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001875 unsigned int hashlen,
1876 const unsigned char *hash,
1877 unsigned char *sig )
1878{
Janos Follath24eed8d2019-11-22 13:21:35 +00001879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001880 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001881
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001882 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001883 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1884 hashlen == 0 ) ||
1885 hash != NULL );
1886 RSA_VALIDATE_RET( sig != NULL );
1887
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001888 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1889 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1890
Hanno Beckerfdf38032017-09-06 12:35:55 +01001891 /*
1892 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1893 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001894
Hanno Beckerfdf38032017-09-06 12:35:55 +01001895 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1896 ctx->len, sig ) ) != 0 )
1897 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001898
Hanno Beckerfdf38032017-09-06 12:35:55 +01001899 /* Private key operation
1900 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001901 * In order to prevent Lenstra's attack, make the signature in a
1902 * temporary buffer and check it before returning it.
1903 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001904
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001905 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001906 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001907 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1908
Hanno Beckerfdf38032017-09-06 12:35:55 +01001909 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001910 if( verif == NULL )
1911 {
1912 mbedtls_free( sig_try );
1913 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1914 }
1915
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001916 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1917 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1918
Gabor Mezei90437e32021-10-20 11:59:27 +02001919 if( mbedtls_ct_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001920 {
1921 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1922 goto cleanup;
1923 }
1924
1925 memcpy( sig, sig_try, ctx->len );
1926
1927cleanup:
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001928 mbedtls_platform_zeroize( sig_try, ctx->len );
1929 mbedtls_platform_zeroize( verif, ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001930 mbedtls_free( sig_try );
1931 mbedtls_free( verif );
1932
Gilles Peskine14d5fef2021-12-13 12:37:55 +01001933 if( ret != 0 )
1934 memset( sig, '!', ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001935 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001936}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001938
1939/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 * Do an RSA operation to sign the message digest
1941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001943 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001944 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001946 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001947 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 unsigned char *sig )
1949{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001950 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001951 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1952 hashlen == 0 ) ||
1953 hash != NULL );
1954 RSA_VALIDATE_RET( sig != NULL );
1955
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 switch( ctx->padding )
1957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958#if defined(MBEDTLS_PKCS1_V15)
1959 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney52654982021-05-18 16:54:00 +01001960 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng,
Thomas Daubney140184d2021-05-18 16:04:07 +01001961 md_alg, hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001962#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#if defined(MBEDTLS_PKCS1_V21)
1965 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyde9fdc42021-05-18 17:10:04 +01001966 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
1967 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001968#endif
1969
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001973}
1974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001976/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001977 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001981 unsigned int hashlen,
1982 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001984 int expected_salt_len,
1985 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001986{
Janos Follath24eed8d2019-11-22 13:21:35 +00001987 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001988 size_t siglen;
1989 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001990 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001992 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001993 size_t observed_salt_len, msb;
Leonid Rozenboima3008e72022-04-21 17:28:18 -07001994 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = {0};
Paul Bakkerb3869132013-02-28 17:21:01 +01001995
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001996 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001997 RSA_VALIDATE_RET( sig != NULL );
1998 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1999 hashlen == 0 ) ||
2000 hash != NULL );
2001
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 siglen = ctx->len;
2003
Paul Bakker27fdf462011-06-09 13:55:13 +00002004 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002006
Thomas Daubney782a7f52021-05-19 12:27:35 +01002007 ret = mbedtls_rsa_public( ctx, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
2009 if( ret != 0 )
2010 return( ret );
2011
2012 p = buf;
2013
Paul Bakkerb3869132013-02-28 17:21:01 +01002014 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 {
Simon Butcher02037452016-03-01 21:19:12 +00002019 /* Gather length of hash to sign */
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002020 size_t exp_hashlen = mbedtls_hash_info_get_size( md_alg );
2021 if( exp_hashlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002023
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002024 if( hashlen != exp_hashlen )
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002025 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002026 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002027
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +02002028 hlen = mbedtls_hash_info_get_size( mgf1_hash_id );
2029 if( hlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002031
Simon Butcher02037452016-03-01 21:19:12 +00002032 /*
2033 * Note: EMSA-PSS verification is over the length of N - 1 bits
2034 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002035 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002036
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002037 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2038 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2039
Simon Butcher02037452016-03-01 21:19:12 +00002040 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002041 if( msb % 8 == 0 )
2042 {
2043 p++;
2044 siglen -= 1;
2045 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002046
Gilles Peskine139108a2017-10-18 19:03:42 +02002047 if( siglen < hlen + 2 )
2048 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2049 hash_start = p + siglen - hlen - 1;
2050
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02002051 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id );
Jaeden Amero66954e12018-01-25 16:05:54 +00002052 if( ret != 0 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002053 return( ret );
Paul Bakker02303e82013-01-03 11:08:31 +01002054
Paul Bakkerb3869132013-02-28 17:21:01 +01002055 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002056
Gilles Peskine6a54b022017-10-17 19:02:13 +02002057 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002058 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002059
Gilles Peskine91048a32017-10-19 17:46:14 +02002060 if( *p++ != 0x01 )
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002061 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002062
Gilles Peskine6a54b022017-10-17 19:02:13 +02002063 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002066 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002067 {
Manuel Pégourié-Gonnardf3a67552022-07-15 12:16:42 +02002068 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002069 }
2070
Simon Butcher02037452016-03-01 21:19:12 +00002071 /*
2072 * Generate H = Hash( M' )
2073 */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002074 ret = hash_mprime( hash, hashlen, p, observed_salt_len,
2075 result, mgf1_hash_id );
2076 if( ret != 0 )
2077 return( ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002078
Jaeden Amero66954e12018-01-25 16:05:54 +00002079 if( memcmp( hash_start, result, hlen ) != 0 )
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002080 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002081
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002082 return( 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01002083}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002084
2085/*
2086 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002090 unsigned int hashlen,
2091 const unsigned char *hash,
2092 const unsigned char *sig )
2093{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002094 mbedtls_md_type_t mgf1_hash_id;
2095 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002096 RSA_VALIDATE_RET( sig != NULL );
2097 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2098 hashlen == 0 ) ||
2099 hash != NULL );
2100
2101 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002103 : md_alg;
2104
Thomas Daubney9e65f792021-05-19 12:18:58 +01002105 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx,
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002106 md_alg, hashlen, hash,
2107 mgf1_hash_id,
2108 MBEDTLS_RSA_SALT_LEN_ANY,
2109 sig ) );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002110
2111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002115/*
2116 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002120 unsigned int hashlen,
2121 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002122 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002123{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002124 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002125 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002126 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002127
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002128 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002129 RSA_VALIDATE_RET( sig != NULL );
2130 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2131 hashlen == 0 ) ||
2132 hash != NULL );
2133
2134 sig_len = ctx->len;
2135
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002136 /*
2137 * Prepare expected PKCS1 v1.5 encoding of hash.
2138 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002139
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002140 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2141 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2142 {
2143 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2144 goto cleanup;
2145 }
2146
2147 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2148 encoded_expected ) ) != 0 )
2149 goto cleanup;
2150
2151 /*
2152 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2153 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002154
Thomas Daubney41e4ce42021-05-19 15:10:05 +01002155 ret = mbedtls_rsa_public( ctx, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002156 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002157 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002158
Simon Butcher02037452016-03-01 21:19:12 +00002159 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002160 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002161 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002162
Gabor Mezei90437e32021-10-20 11:59:27 +02002163 if( ( ret = mbedtls_ct_memcmp( encoded, encoded_expected,
gabor-mezei-arm46025642021-07-19 15:19:19 +02002164 sig_len ) ) != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002165 {
2166 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2167 goto cleanup;
2168 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002169
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002170cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002171
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002172 if( encoded != NULL )
2173 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002174 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002175 mbedtls_free( encoded );
2176 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002177
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002178 if( encoded_expected != NULL )
2179 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002180 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002181 mbedtls_free( encoded_expected );
2182 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002183
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002184 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002185}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002187
2188/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002189 * Do an RSA operation and check the message digest
2190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002193 unsigned int hashlen,
2194 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002195 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002196{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002197 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002198 RSA_VALIDATE_RET( sig != NULL );
2199 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2200 hashlen == 0 ) ||
2201 hash != NULL );
2202
Paul Bakkerb3869132013-02-28 17:21:01 +01002203 switch( ctx->padding )
2204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205#if defined(MBEDTLS_PKCS1_V15)
2206 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney2e126252021-05-19 11:48:53 +01002207 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, md_alg,
2208 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002209#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211#if defined(MBEDTLS_PKCS1_V21)
2212 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002213 return mbedtls_rsa_rsassa_pss_verify( ctx, md_alg,
2214 hashlen, hash, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01002215#endif
2216
2217 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002219 }
2220}
2221
2222/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002223 * Copy the components of an RSA key
2224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002226{
Janos Follath24eed8d2019-11-22 13:21:35 +00002227 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002228 RSA_VALIDATE_RET( dst != NULL );
2229 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002230
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002231 dst->len = src->len;
2232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2234 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2237 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2238 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002239
2240#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2242 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2243 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2245 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002246#endif
2247
2248 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2251 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002252
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002253 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002254 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002255
2256cleanup:
2257 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002259
2260 return( ret );
2261}
2262
2263/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 * Free the components of an RSA key
2265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002267{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002268 if( ctx == NULL )
2269 return;
2270
2271 mbedtls_mpi_free( &ctx->Vi );
2272 mbedtls_mpi_free( &ctx->Vf );
2273 mbedtls_mpi_free( &ctx->RN );
2274 mbedtls_mpi_free( &ctx->D );
2275 mbedtls_mpi_free( &ctx->Q );
2276 mbedtls_mpi_free( &ctx->P );
2277 mbedtls_mpi_free( &ctx->E );
2278 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002279
Hanno Becker33c30a02017-08-23 07:00:22 +01002280#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002281 mbedtls_mpi_free( &ctx->RQ );
2282 mbedtls_mpi_free( &ctx->RP );
2283 mbedtls_mpi_free( &ctx->QP );
2284 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002285 mbedtls_mpi_free( &ctx->DP );
2286#endif /* MBEDTLS_RSA_NO_CRT */
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002289 /* Free the mutex, but only if it hasn't been freed already. */
2290 if( ctx->ver != 0 )
2291 {
2292 mbedtls_mutex_free( &ctx->mutex );
2293 ctx->ver = 0;
2294 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002295#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002296}
2297
Hanno Beckerab377312017-08-23 16:24:51 +01002298#endif /* !MBEDTLS_RSA_ALT */
2299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002301
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002302#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
2304/*
2305 * Example RSA-1024 keypair, for test purposes
2306 */
2307#define KEY_LEN 128
2308
2309#define RSA_N "9292758453063D803DD603D5E777D788" \
2310 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2311 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2312 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2313 "93A89813FBF3C4F8066D2D800F7C38A8" \
2314 "1AE31942917403FF4946B0A83D3D3E05" \
2315 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2316 "5E94BB77B07507233A0BC7BAC8F90F79"
2317
2318#define RSA_E "10001"
2319
2320#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2321 "66CA472BC44D253102F8B4A9D3BFA750" \
2322 "91386C0077937FE33FA3252D28855837" \
2323 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2324 "DF79C5CE07EE72C7F123142198164234" \
2325 "CABB724CF78B8173B9F880FC86322407" \
2326 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2327 "071513A1E85B5DFA031F21ECAE91A34D"
2328
2329#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2330 "2C01CAD19EA484A87EA4377637E75500" \
2331 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2332 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2333
2334#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2335 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2336 "910E4168387E3C30AA1E00C339A79508" \
2337 "8452DD96A9A5EA5D9DCA68DA636032AF"
2338
Paul Bakker5121ce52009-01-03 21:22:43 +00002339#define PT_LEN 24
2340#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2341 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002344static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002345{
gufe44c2620da2020-08-03 17:56:50 +02002346#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002347 size_t i;
2348
Paul Bakker545570e2010-07-18 09:00:25 +00002349 if( rng_state != NULL )
2350 rng_state = NULL;
2351
Paul Bakkera3d195c2011-11-27 21:07:34 +00002352 for( i = 0; i < len; ++i )
2353 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002354#else
2355 if( rng_state != NULL )
2356 rng_state = NULL;
2357
2358 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002359#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002360
Paul Bakkera3d195c2011-11-27 21:07:34 +00002361 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002362}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002364
Paul Bakker5121ce52009-01-03 21:22:43 +00002365/*
2366 * Checkup routine
2367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002369{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002370 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002372 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 unsigned char rsa_plaintext[PT_LEN];
2375 unsigned char rsa_decrypted[PT_LEN];
2376 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002378 unsigned char sha1sum[20];
2379#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
Hanno Becker3a701162017-08-22 13:52:43 +01002381 mbedtls_mpi K;
2382
2383 mbedtls_mpi_init( &K );
Ronald Cronc1905a12021-06-05 11:11:14 +02002384 mbedtls_rsa_init( &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
Hanno Becker3a701162017-08-22 13:52:43 +01002386 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2387 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2388 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2389 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2390 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2391 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2392 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2393 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2394 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2395 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2396
Hanno Becker7f25f852017-10-10 16:56:22 +01002397 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
2399 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2403 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 {
2405 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002407
Hanno Becker5bc87292017-05-03 15:09:31 +01002408 ret = 1;
2409 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 }
2411
2412 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
2415 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2416
Thomas Daubney21772772021-05-13 17:30:32 +01002417 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002418 PT_LEN, rsa_plaintext,
2419 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 {
2421 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002423
Hanno Becker5bc87292017-05-03 15:09:31 +01002424 ret = 1;
2425 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 }
2427
2428 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002430
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002431 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002432 &len, rsa_ciphertext, rsa_decrypted,
2433 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 {
2435 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437
Hanno Becker5bc87292017-05-03 15:09:31 +01002438 ret = 1;
2439 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 }
2441
2442 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2443 {
2444 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
Hanno Becker5bc87292017-05-03 15:09:31 +01002447 ret = 1;
2448 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 }
2450
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002451 if( verbose != 0 )
2452 mbedtls_printf( "passed\n" );
2453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002456 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
TRodziewicz26371e42021-06-08 16:45:41 +02002458 if( mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002459 {
2460 if( verbose != 0 )
2461 mbedtls_printf( "failed\n" );
2462
2463 return( 1 );
2464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Hanno Becker98838b02017-10-02 13:16:10 +01002466 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002467 MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002468 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 {
2470 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002472
Hanno Becker5bc87292017-05-03 15:09:31 +01002473 ret = 1;
2474 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 }
2476
2477 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002480 if( mbedtls_rsa_pkcs1_verify( &rsa, 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é-Gonnardd1004f02015-08-07 10:46:54 +02002491 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002493
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002494 if( verbose != 0 )
2495 mbedtls_printf( "\n" );
2496
Paul Bakker3d8fb632014-04-17 12:42:41 +02002497cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002498 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 mbedtls_rsa_free( &rsa );
2500#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002501 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002503 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504}
2505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#endif /* MBEDTLS_RSA_C */