blob: 5a1ae79bcec952d77bebe3fa2657efa15fe40978 [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"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000047
Rich Evans00ab4702015-02-06 13:43:58 +000048#include <string.h>
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
gufe44c2620da2020-08-03 17:56:50 +020054#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010060#else
Rich Evans00ab4702015-02-06 13:43:58 +000061#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020063#define mbedtls_calloc calloc
64#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010065#endif
66
Hanno Beckera565f542017-10-11 11:00:19 +010067#if !defined(MBEDTLS_RSA_ALT)
68
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050069/* Parameter validation macros */
70#define RSA_VALIDATE_RET( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
72#define RSA_VALIDATE( cond ) \
73 MBEDTLS_INTERNAL_VALIDATE( cond )
74
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010075#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +010076/* constant-time buffer comparison */
77static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
78{
79 size_t i;
80 const unsigned char *A = (const unsigned char *) a;
81 const unsigned char *B = (const unsigned char *) b;
82 unsigned char diff = 0;
83
84 for( i = 0; i < n; i++ )
85 diff |= A[i] ^ B[i];
86
87 return( diff );
88}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010089#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +010090
Hanno Becker617c1ae2017-08-23 14:11:24 +010091int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
92 const mbedtls_mpi *N,
93 const mbedtls_mpi *P, const mbedtls_mpi *Q,
94 const mbedtls_mpi *D, const mbedtls_mpi *E )
95{
Janos Follath24eed8d2019-11-22 13:21:35 +000096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +010098
99 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
100 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
101 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
102 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
103 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
104 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100105 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100106 }
107
108 if( N != NULL )
109 ctx->len = mbedtls_mpi_size( &ctx->N );
110
111 return( 0 );
112}
113
114int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100115 unsigned char const *N, size_t N_len,
116 unsigned char const *P, size_t P_len,
117 unsigned char const *Q, size_t Q_len,
118 unsigned char const *D, size_t D_len,
119 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100120{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000121 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500122 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100123
124 if( N != NULL )
125 {
126 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
127 ctx->len = mbedtls_mpi_size( &ctx->N );
128 }
129
130 if( P != NULL )
131 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
132
133 if( Q != NULL )
134 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
135
136 if( D != NULL )
137 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
138
139 if( E != NULL )
140 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
141
142cleanup:
143
144 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100145 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100146
147 return( 0 );
148}
149
Hanno Becker705fc682017-10-10 17:57:02 +0100150/*
151 * Checks whether the context fields are set in such a way
152 * that the RSA primitives will be able to execute without error.
153 * It does *not* make guarantees for consistency of the parameters.
154 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100155static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
156 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100157{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100158#if !defined(MBEDTLS_RSA_NO_CRT)
159 /* blinding_needed is only used for NO_CRT to decide whether
160 * P,Q need to be present or not. */
161 ((void) blinding_needed);
162#endif
163
Hanno Becker3a760a12018-01-05 08:14:49 +0000164 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
165 ctx->len > MBEDTLS_MPI_MAX_SIZE )
166 {
Hanno Becker705fc682017-10-10 17:57:02 +0100167 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000168 }
Hanno Becker705fc682017-10-10 17:57:02 +0100169
170 /*
171 * 1. Modular exponentiation needs positive, odd moduli.
172 */
173
174 /* Modular exponentiation wrt. N is always used for
175 * RSA public key operations. */
176 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
177 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
178 {
179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
180 }
181
182#if !defined(MBEDTLS_RSA_NO_CRT)
183 /* Modular exponentiation for P and Q is only
184 * used for private key operations and if CRT
185 * is used. */
186 if( is_priv &&
187 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
188 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
189 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
190 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
191 {
192 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
193 }
194#endif /* !MBEDTLS_RSA_NO_CRT */
195
196 /*
197 * 2. Exponents must be positive
198 */
199
200 /* Always need E for public key operations */
201 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
202 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
203
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100204#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100205 /* For private key operations, use D or DP & DQ
206 * as (unblinded) exponents. */
207 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
208 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
209#else
210 if( is_priv &&
211 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
212 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
213 {
214 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
215 }
216#endif /* MBEDTLS_RSA_NO_CRT */
217
218 /* Blinding shouldn't make exponents negative either,
219 * so check that P, Q >= 1 if that hasn't yet been
220 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100221#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100222 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100223 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
224 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
225 {
226 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
227 }
228#endif
229
230 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100231 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100232#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100233 if( is_priv &&
234 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
235 {
236 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
237 }
238#endif
239
240 return( 0 );
241}
242
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100243int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100244{
245 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500246 int have_N, have_P, have_Q, have_D, have_E;
247#if !defined(MBEDTLS_RSA_NO_CRT)
248 int have_DP, have_DQ, have_QP;
249#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500250 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100251
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500252 RSA_VALIDATE_RET( ctx != NULL );
253
254 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
255 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
256 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
257 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
258 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500259
260#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500261 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
262 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
263 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500264#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100265
Hanno Becker617c1ae2017-08-23 14:11:24 +0100266 /*
267 * Check whether provided parameters are enough
268 * to deduce all others. The following incomplete
269 * parameter sets for private keys are supported:
270 *
271 * (1) P, Q missing.
272 * (2) D and potentially N missing.
273 *
274 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100275
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500276 n_missing = have_P && have_Q && have_D && have_E;
277 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
278 d_missing = have_P && have_Q && !have_D && have_E;
279 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100280
281 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100283
Hanno Becker617c1ae2017-08-23 14:11:24 +0100284 if( !is_priv && !is_pub )
285 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
286
287 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100288 * Step 1: Deduce N if P, Q are provided.
289 */
290
291 if( !have_N && have_P && have_Q )
292 {
293 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
294 &ctx->Q ) ) != 0 )
295 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100296 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100297 }
298
299 ctx->len = mbedtls_mpi_size( &ctx->N );
300 }
301
302 /*
303 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100304 */
305
306 if( pq_missing )
307 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100308 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100309 &ctx->P, &ctx->Q );
310 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100311 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100312
313 }
314 else if( d_missing )
315 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100316 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
317 &ctx->Q,
318 &ctx->E,
319 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100320 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100321 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100322 }
323 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324
Hanno Becker617c1ae2017-08-23 14:11:24 +0100325 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100326 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100327 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100328 */
329
Hanno Becker23344b52017-08-23 07:43:27 +0100330#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500331 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100332 {
333 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
334 &ctx->DP, &ctx->DQ, &ctx->QP );
335 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100336 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100337 }
Hanno Becker23344b52017-08-23 07:43:27 +0100338#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100339
340 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100341 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100342 */
343
Hanno Beckerebd2c022017-10-12 10:54:53 +0100344 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100345}
346
Hanno Becker617c1ae2017-08-23 14:11:24 +0100347int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
348 unsigned char *N, size_t N_len,
349 unsigned char *P, size_t P_len,
350 unsigned char *Q, size_t Q_len,
351 unsigned char *D, size_t D_len,
352 unsigned char *E, size_t E_len )
353{
354 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500355 int is_priv;
356 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100357
358 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500359 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100360 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
361 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
362 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
363 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
364 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
365
366 if( !is_priv )
367 {
368 /* If we're trying to export private parameters for a public key,
369 * something must be wrong. */
370 if( P != NULL || Q != NULL || D != NULL )
371 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
372
373 }
374
375 if( N != NULL )
376 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
377
378 if( P != NULL )
379 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
380
381 if( Q != NULL )
382 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
383
384 if( D != NULL )
385 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
386
387 if( E != NULL )
388 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100389
390cleanup:
391
392 return( ret );
393}
394
Hanno Becker617c1ae2017-08-23 14:11:24 +0100395int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
396 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
397 mbedtls_mpi *D, mbedtls_mpi *E )
398{
Janos Follath24eed8d2019-11-22 13:21:35 +0000399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500400 int is_priv;
401 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100402
403 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500404 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100405 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
406 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
407 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
408 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
409 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
410
411 if( !is_priv )
412 {
413 /* If we're trying to export private parameters for a public key,
414 * something must be wrong. */
415 if( P != NULL || Q != NULL || D != NULL )
416 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
417
418 }
419
420 /* Export all requested core parameters. */
421
422 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
423 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
424 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
425 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
426 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
427 {
428 return( ret );
429 }
430
431 return( 0 );
432}
433
434/*
435 * Export CRT parameters
436 * This must also be implemented if CRT is not used, for being able to
437 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
438 * can be used in this case.
439 */
440int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
441 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
442{
Janos Follath24eed8d2019-11-22 13:21:35 +0000443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500444 int is_priv;
445 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100446
447 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500448 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100449 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
450 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
451 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
452 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
453 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
454
455 if( !is_priv )
456 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
457
Hanno Beckerdc95c892017-08-23 06:57:02 +0100458#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100459 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100460 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
461 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
462 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
463 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100464 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100465 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100466#else
467 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
468 DP, DQ, QP ) ) != 0 )
469 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100470 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100471 }
472#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100473
474 return( 0 );
475}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100476
Paul Bakker5121ce52009-01-03 21:22:43 +0000477/*
478 * Initialize an RSA context
479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000482 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000483{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500484 RSA_VALIDATE( ctx != NULL );
485 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
486 padding == MBEDTLS_RSA_PKCS_V21 );
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100493 /* Set ctx->ver to nonzero to indicate that the mutex has been
494 * initialized and will need to be freed. */
495 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000498}
499
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100500/*
501 * Set padding for an existing RSA context
502 */
Ronald Cronea7631b2021-06-03 18:51:59 +0200503int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
504 mbedtls_md_type_t hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100505{
Ronald Cronea7631b2021-06-03 18:51:59 +0200506 if( ( padding != MBEDTLS_RSA_PKCS_V15 ) &&
507 ( padding != MBEDTLS_RSA_PKCS_V21 ) )
508 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
509
510 if( ( padding == MBEDTLS_RSA_PKCS_V21 ) &&
511 ( hash_id != MBEDTLS_MD_NONE ) )
512 {
513 const mbedtls_md_info_t *md_info;
514
515 md_info = mbedtls_md_info_from_type( hash_id );
516 if( md_info == NULL )
517 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
518 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500519
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100520 ctx->padding = padding;
521 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200522
523 return( 0 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100524}
525
Hanno Becker617c1ae2017-08-23 14:11:24 +0100526/*
527 * Get length in bytes of RSA modulus
528 */
529
530size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
531{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100532 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100533}
534
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
538/*
539 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800540 *
541 * This generation method follows the RSA key pair generation procedure of
542 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000545 int (*f_rng)(void *, unsigned char *, size_t),
546 void *p_rng,
547 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000548{
Janos Follath24eed8d2019-11-22 13:21:35 +0000549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800550 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100551 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500552 RSA_VALIDATE_RET( ctx != NULL );
553 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
Janos Follathb8fc1b02018-09-03 15:37:01 +0100555 /*
556 * If the modulus is 1024 bit long or shorter, then the security strength of
557 * the RSA algorithm is less than or equal to 80 bits and therefore an error
558 * rate of 2^-80 is sufficient.
559 */
560 if( nbits > 1024 )
561 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
562
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100563 mbedtls_mpi_init( &H );
564 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800565 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100567 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
568 {
569 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
570 goto cleanup;
571 }
572
Paul Bakker5121ce52009-01-03 21:22:43 +0000573 /*
574 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800575 * 1. |P-Q| > 2^( nbits / 2 - 100 )
576 * 2. GCD( E, (P-1)*(Q-1) ) == 1
577 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 do
582 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100583 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
584 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Janos Follathb8fc1b02018-09-03 15:37:01 +0100586 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
587 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800589 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
590 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
591 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 continue;
593
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800594 /* not required by any standards, but some users rely on the fact that P > Q */
595 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100596 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100597
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100598 /* Temporarily replace P,Q by P-1, Q-1 */
599 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
600 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
601 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800602
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800603 /* 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 +0200604 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800605 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
606 continue;
607
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800608 /* 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 -0800609 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
610 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
611 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
612
613 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
614 continue;
615
616 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800618 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100620 /* Restore P,Q */
621 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
622 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
623
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800624 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
625
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100626 ctx->len = mbedtls_mpi_size( &ctx->N );
627
Jethro Beekman97f95c92018-02-13 15:50:36 -0800628#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 * DP = D mod (P - 1)
631 * DQ = D mod (Q - 1)
632 * QP = Q^-1 mod P
633 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100634 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
635 &ctx->DP, &ctx->DQ, &ctx->QP ) );
636#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
Hanno Becker83aad1f2017-08-23 06:45:10 +0100638 /* Double-check */
639 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
641cleanup:
642
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100643 mbedtls_mpi_free( &H );
644 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800645 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 if( ret != 0 )
648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100650
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100651 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100652 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100653 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 }
655
Paul Bakker48377d92013-08-30 12:06:24 +0200656 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000657}
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
661/*
662 * Check a public RSA key
663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000665{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500666 RSA_VALIDATE_RET( ctx != NULL );
667
Hanno Beckerebd2c022017-10-12 10:54:53 +0100668 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000670
Hanno Becker3a760a12018-01-05 08:14:49 +0000671 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100674 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
Hanno Becker705fc682017-10-10 17:57:02 +0100676 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
677 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100681 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000682
683 return( 0 );
684}
685
686/*
Hanno Becker705fc682017-10-10 17:57:02 +0100687 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000690{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500691 RSA_VALIDATE_RET( ctx != NULL );
692
Hanno Becker705fc682017-10-10 17:57:02 +0100693 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100694 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 {
Hanno Becker98838b02017-10-02 13:16:10 +0100696 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 }
Paul Bakker48377d92013-08-30 12:06:24 +0200698
Hanno Becker98838b02017-10-02 13:16:10 +0100699 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100700 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000701 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100702 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000704
Hanno Beckerb269a852017-08-25 08:03:21 +0100705#if !defined(MBEDTLS_RSA_NO_CRT)
706 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
707 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
708 {
709 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
710 }
711#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000712
713 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000714}
715
716/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100717 * Check if contexts holding a public and private key match
718 */
Hanno Becker98838b02017-10-02 13:16:10 +0100719int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
720 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100721{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500722 RSA_VALIDATE_RET( pub != NULL );
723 RSA_VALIDATE_RET( prv != NULL );
724
Hanno Becker98838b02017-10-02 13:16:10 +0100725 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100729 }
730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
732 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100735 }
736
737 return( 0 );
738}
739
740/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000741 * Do an RSA public key operation
742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000744 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000745 unsigned char *output )
746{
Janos Follath24eed8d2019-11-22 13:21:35 +0000747 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000748 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500750 RSA_VALIDATE_RET( ctx != NULL );
751 RSA_VALIDATE_RET( input != NULL );
752 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000753
Hanno Beckerebd2c022017-10-12 10:54:53 +0100754 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100755 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200759#if defined(MBEDTLS_THREADING_C)
760 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
761 return( ret );
762#endif
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200768 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
769 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000770 }
771
772 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
774 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000775
776cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200778 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
779 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100780#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000783
784 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100785 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
787 return( 0 );
788}
789
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200790/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200791 * Generate or update blinding values, see section 10 of:
792 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200793 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200794 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200795 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200796static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200797 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
798{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200799 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200800 mbedtls_mpi R;
801
802 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200803
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200804 if( ctx->Vf.p != NULL )
805 {
806 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
808 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
809 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
810 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200811
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200812 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200813 }
814
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200815 /* Unblinding value: Vf = random number, invertible mod N */
816 do {
817 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200818 {
819 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
820 goto cleanup;
821 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 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 +0200824
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200825 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200826 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
827 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
828 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
829
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200830 /* At this point, Vi is invertible mod N if and only if both Vf and R
831 * are invertible mod N. If one of them isn't, we don't need to know
832 * which one, we just loop and choose new values for both of them.
833 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200834 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500835 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200836 goto cleanup;
837
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500838 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
839
840 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
841 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
842 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200843
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200844 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200845 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 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 +0200847
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200848
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200849cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200850 mbedtls_mpi_free( &R );
851
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200852 return( ret );
853}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200854
Paul Bakker5121ce52009-01-03 21:22:43 +0000855/*
Janos Follathe81102e2017-03-22 13:38:28 +0000856 * Exponent blinding supposed to prevent side-channel attacks using multiple
857 * traces of measurements to recover the RSA key. The more collisions are there,
858 * the more bits of the key can be recovered. See [3].
859 *
860 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
861 * observations on avarage.
862 *
863 * For example with 28 byte blinding to achieve 2 collisions the adversary has
864 * to make 2^112 observations on avarage.
865 *
866 * (With the currently (as of 2017 April) known best algorithms breaking 2048
867 * bit RSA requires approximately as much time as trying out 2^112 random keys.
868 * Thus in this sense with 28 byte blinding the security is not reduced by
869 * side-channel attacks like the one in [3])
870 *
871 * This countermeasure does not help if the key recovery is possible with a
872 * single trace.
873 */
874#define RSA_EXPONENT_BLINDING 28
875
876/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000877 * Do an RSA private key operation
878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200880 int (*f_rng)(void *, unsigned char *, size_t),
881 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000882 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000883 unsigned char *output )
884{
Janos Follath24eed8d2019-11-22 13:21:35 +0000885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000886 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100887
888 /* Temporary holding the result */
889 mbedtls_mpi T;
890
891 /* Temporaries holding P-1, Q-1 and the
892 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000893 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100894
895#if !defined(MBEDTLS_RSA_NO_CRT)
896 /* Temporaries holding the results mod p resp. mod q. */
897 mbedtls_mpi TP, TQ;
898
899 /* Temporaries holding the blinded exponents for
900 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000901 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100902
903 /* Pointers to actual exponents to be used - either the unblinded
904 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000905 mbedtls_mpi *DP = &ctx->DP;
906 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100907#else
908 /* Temporary holding the blinded exponent (if used). */
909 mbedtls_mpi D_blind;
910
911 /* Pointer to actual exponent to be used - either the unblinded
912 * or the blinded one, depending on the presence of a PRNG. */
913 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100914#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100915
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100916 /* Temporaries holding the initial input and the double
917 * checked result; should be the same in the end. */
918 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500920 RSA_VALIDATE_RET( ctx != NULL );
921 RSA_VALIDATE_RET( input != NULL );
922 RSA_VALIDATE_RET( output != NULL );
923
Hanno Beckerebd2c022017-10-12 10:54:53 +0100924 if( rsa_check_context( ctx, 1 /* private key checks */,
925 f_rng != NULL /* blinding y/n */ ) != 0 )
926 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100927 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100928 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100929
Hanno Becker06811ce2017-05-03 15:10:34 +0100930#if defined(MBEDTLS_THREADING_C)
931 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
932 return( ret );
933#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000934
Hanno Becker06811ce2017-05-03 15:10:34 +0100935 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100936 mbedtls_mpi_init( &T );
937
938 mbedtls_mpi_init( &P1 );
939 mbedtls_mpi_init( &Q1 );
940 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000941
Janos Follathf9203b42017-03-22 15:13:15 +0000942 if( f_rng != NULL )
943 {
Janos Follathe81102e2017-03-22 13:38:28 +0000944#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000945 mbedtls_mpi_init( &D_blind );
946#else
947 mbedtls_mpi_init( &DP_blind );
948 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000949#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000950 }
Janos Follathe81102e2017-03-22 13:38:28 +0000951
Hanno Becker06811ce2017-05-03 15:10:34 +0100952#if !defined(MBEDTLS_RSA_NO_CRT)
953 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200954#endif
955
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100956 mbedtls_mpi_init( &I );
957 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100958
959 /* End of MPI initialization */
960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
962 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200964 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
965 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000966 }
967
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100968 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100969
Paul Bakkerf451bac2013-08-30 15:37:02 +0200970 if( f_rng != NULL )
971 {
972 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200973 * Blinding
974 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200975 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200976 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
977 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000979
Janos Follathe81102e2017-03-22 13:38:28 +0000980 /*
981 * Exponent blinding
982 */
983 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
984 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
985
Janos Follathf9203b42017-03-22 15:13:15 +0000986#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000987 /*
988 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
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( &D_blind, &P1, &Q1 ) );
993 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
994 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
995
996 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000997#else
998 /*
999 * DP_blind = ( P - 1 ) * R + DP
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( &DP_blind, &P1, &R ) );
1004 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1005 &ctx->DP ) );
1006
1007 DP = &DP_blind;
1008
1009 /*
1010 * DQ_blind = ( Q - 1 ) * R + DQ
1011 */
1012 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1013 f_rng, p_rng ) );
1014 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1015 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1016 &ctx->DQ ) );
1017
1018 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001019#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001020 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001023 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001024#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001025 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001026 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001028 * TP = input ^ dP mod P
1029 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001031
1032 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1033 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
1035 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001036 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001038 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1039 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1040 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
1042 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001043 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001045 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1046 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001048
Paul Bakkerf451bac2013-08-30 15:37:02 +02001049 if( f_rng != NULL )
1050 {
1051 /*
1052 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001053 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001054 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001055 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001057 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
Hanno Becker2dec5e82017-10-03 07:49:52 +01001059 /* Verify the result to prevent glitching attacks. */
1060 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1061 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001062 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001063 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001064 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1065 goto cleanup;
1066 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001067
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
1071cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001073 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1074 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001075#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001076
Hanno Becker06811ce2017-05-03 15:10:34 +01001077 mbedtls_mpi_free( &P1 );
1078 mbedtls_mpi_free( &Q1 );
1079 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001080
1081 if( f_rng != NULL )
1082 {
Janos Follathe81102e2017-03-22 13:38:28 +00001083#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001084 mbedtls_mpi_free( &D_blind );
1085#else
1086 mbedtls_mpi_free( &DP_blind );
1087 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001088#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001089 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Hanno Becker06811ce2017-05-03 15:10:34 +01001091 mbedtls_mpi_free( &T );
1092
1093#if !defined(MBEDTLS_RSA_NO_CRT)
1094 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1095#endif
1096
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001097 mbedtls_mpi_free( &C );
1098 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001099
Gilles Peskineae3741e2020-11-25 00:10:31 +01001100 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001101 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
Gilles Peskineae3741e2020-11-25 00:10:31 +01001103 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001104}
1105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001107/**
1108 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1109 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001110 * \param dst buffer to mask
1111 * \param dlen length of destination buffer
1112 * \param src source of the mask generation
1113 * \param slen length of the source buffer
1114 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001115 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001116static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001118{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001120 unsigned char counter[4];
1121 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001122 unsigned int hlen;
1123 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001124 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001127 memset( counter, 0, 4 );
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001130
Simon Butcher02037452016-03-01 21:19:12 +00001131 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001132 p = dst;
1133
1134 while( dlen > 0 )
1135 {
1136 use_len = hlen;
1137 if( dlen < hlen )
1138 use_len = dlen;
1139
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001140 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1141 goto exit;
1142 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1143 goto exit;
1144 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1145 goto exit;
1146 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1147 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001148
1149 for( i = 0; i < use_len; ++i )
1150 *p++ ^= mask[i];
1151
1152 counter[3]++;
1153
1154 dlen -= use_len;
1155 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001156
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001157exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001158 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001159
1160 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001165/*
1166 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001169 int (*f_rng)(void *, unsigned char *, size_t),
1170 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001171 const unsigned char *label, size_t label_len,
1172 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001173 const unsigned char *input,
1174 unsigned char *output )
1175{
1176 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001177 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001178 unsigned char *p = output;
1179 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 const mbedtls_md_info_t *md_info;
1181 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001182
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001183 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001184 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001185 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001186 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1187
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001188 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001192 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001194
1195 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001197
Simon Butcher02037452016-03-01 21:19:12 +00001198 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001199 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001201
1202 memset( output, 0, olen );
1203
1204 *p++ = 0;
1205
Simon Butcher02037452016-03-01 21:19:12 +00001206 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001207 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001208 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001209
1210 p += hlen;
1211
Simon Butcher02037452016-03-01 21:19:12 +00001212 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001213 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1214 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001215 p += hlen;
1216 p += olen - 2 * hlen - 2 - ilen;
1217 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001218 if( ilen != 0 )
1219 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001222 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001223 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001224
Simon Butcher02037452016-03-01 21:19:12 +00001225 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001226 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1227 &md_ctx ) ) != 0 )
1228 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001229
Simon Butcher02037452016-03-01 21:19:12 +00001230 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001231 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1232 &md_ctx ) ) != 0 )
1233 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001234
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001235exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001237
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001238 if( ret != 0 )
1239 return( ret );
1240
Thomas Daubney141700f2021-05-13 19:06:10 +01001241 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001242}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001246/*
1247 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001250 int (*f_rng)(void *, unsigned char *, size_t),
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001251 void *p_rng, size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001252 const unsigned char *input,
1253 unsigned char *output )
1254{
1255 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001257 unsigned char *p = output;
1258
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001259 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001260 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001261 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001262
Paul Bakkerb3869132013-02-28 17:21:01 +01001263 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001264
Simon Butcher02037452016-03-01 21:19:12 +00001265 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001266 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001268
1269 nb_pad = olen - 3 - ilen;
1270
1271 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001272
1273 if( f_rng == NULL )
1274 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1275
1276 *p++ = MBEDTLS_RSA_CRYPT;
1277
1278 while( nb_pad-- > 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001279 {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001280 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001281
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001282 do {
1283 ret = f_rng( p_rng, p, 1 );
1284 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001285
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001286 /* Check if RNG failed to generate data */
1287 if( rng_dl == 0 || ret != 0 )
1288 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001289
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001290 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001291 }
1292
1293 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001294 if( ilen != 0 )
1295 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001296
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001297 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001300
Paul Bakker5121ce52009-01-03 21:22:43 +00001301/*
1302 * Add the message padding, then do an RSA operation
1303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001305 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001306 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +01001307 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001308 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 unsigned char *output )
1310{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001311 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001312 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001313 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001314
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 switch( ctx->padding )
1316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#if defined(MBEDTLS_PKCS1_V15)
1318 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney21772772021-05-13 17:30:32 +01001319 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001320 ilen, input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001321#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_PKCS1_V21)
1324 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney141700f2021-05-13 19:06:10 +01001325 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, NULL, 0,
Thomas Daubney21772772021-05-13 17:30:32 +01001326 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001327#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
1329 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001332}
1333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001335/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001336 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001339 int (*f_rng)(void *, unsigned char *, size_t),
1340 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001341 const unsigned char *label, size_t label_len,
1342 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001343 const unsigned char *input,
1344 unsigned char *output,
1345 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001346{
Janos Follath24eed8d2019-11-22 13:21:35 +00001347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001348 size_t ilen, i, pad_len;
1349 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1351 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001352 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 const mbedtls_md_info_t *md_info;
1354 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001355
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_max_len == 0 || output != NULL );
1358 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1359 RSA_VALIDATE_RET( input != NULL );
1360 RSA_VALIDATE_RET( olen != NULL );
1361
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001362 /*
1363 * Parameters sanity checks
1364 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001365 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
1368 ilen = ctx->len;
1369
Paul Bakker27fdf462011-06-09 13:55:13 +00001370 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001374 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001376
Janos Follathc17cda12016-02-11 11:08:18 +00001377 hlen = mbedtls_md_get_size( md_info );
1378
1379 // checking for integer underflow
1380 if( 2 * hlen + 2 > ilen )
1381 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1382
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001383 /*
1384 * RSA operation
1385 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001386 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
1388 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001389 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001391 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001392 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001395 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1396 {
1397 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001398 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001399 }
1400
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001401 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001402 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1403 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001404 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001405 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1406 &md_ctx ) ) != 0 )
1407 {
1408 mbedtls_md_free( &md_ctx );
1409 goto cleanup;
1410 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001413
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001414 /* Generate lHash */
1415 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1416 goto cleanup;
1417
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001418 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001419 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001420 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001422 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001423
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001424 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001425
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001426 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001427
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001428 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001429 for( i = 0; i < hlen; i++ )
1430 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001431
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001432 /* Get zero-padding len, but always read till end of buffer
1433 * (minus one, for the 01 byte) */
1434 pad_len = 0;
1435 pad_done = 0;
1436 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1437 {
1438 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001439 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001440 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001441
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001442 p += pad_len;
1443 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001444
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001445 /*
1446 * The only information "leaked" is whether the padding was correct or not
1447 * (eg, no data is copied if it was not correct). This meets the
1448 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1449 * the different error conditions.
1450 */
1451 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001452 {
1453 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1454 goto cleanup;
1455 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001456
Paul Bakker66d5d072014-06-17 16:39:18 +02001457 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001458 {
1459 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1460 goto cleanup;
1461 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001462
1463 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001464 if( *olen != 0 )
1465 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001466 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001467
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001468cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001469 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1470 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001471
1472 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001473}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476#if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001477/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1478 *
1479 * \param value The value to analyze.
1480 * \return Zero if \p value is zero, otherwise all-bits-one.
1481 */
1482static unsigned all_or_nothing_int( unsigned value )
1483{
1484 /* MSVC has a warning about unary minus on unsigned, but this is
1485 * well-defined and precisely what we want to do here */
1486#if defined(_MSC_VER)
1487#pragma warning( push )
1488#pragma warning( disable : 4146 )
1489#endif
1490 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1491#if defined(_MSC_VER)
1492#pragma warning( pop )
1493#endif
1494}
1495
1496/** Check whether a size is out of bounds, without branches.
1497 *
1498 * This is equivalent to `size > max`, but is likely to be compiled to
1499 * to code using bitwise operation rather than a branch.
1500 *
1501 * \param size Size to check.
1502 * \param max Maximum desired value for \p size.
1503 * \return \c 0 if `size <= max`.
1504 * \return \c 1 if `size > max`.
1505 */
1506static unsigned size_greater_than( size_t size, size_t max )
1507{
1508 /* Return the sign bit (1 for negative) of (max - size). */
1509 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1510}
1511
1512/** Choose between two integer values, without branches.
1513 *
1514 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1515 * to code using bitwise operation rather than a branch.
1516 *
1517 * \param cond Condition to test.
1518 * \param if1 Value to use if \p cond is nonzero.
1519 * \param if0 Value to use if \p cond is zero.
1520 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1521 */
1522static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
1523{
1524 unsigned mask = all_or_nothing_int( cond );
1525 return( ( mask & if1 ) | (~mask & if0 ) );
1526}
1527
1528/** Shift some data towards the left inside a buffer without leaking
1529 * the length of the data through side channels.
1530 *
1531 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1532 * ```
1533 * memmove(start, start + offset, total - offset);
1534 * memset(start + offset, 0, total - offset);
1535 * ```
1536 * but it strives to use a memory access pattern (and thus total timing)
1537 * that does not depend on \p offset. This timing independence comes at
1538 * the expense of performance.
1539 *
1540 * \param start Pointer to the start of the buffer.
1541 * \param total Total size of the buffer.
1542 * \param offset Offset from which to copy \p total - \p offset bytes.
1543 */
1544static void mem_move_to_left( void *start,
1545 size_t total,
1546 size_t offset )
1547{
1548 volatile unsigned char *buf = start;
1549 size_t i, n;
1550 if( total == 0 )
1551 return;
1552 for( i = 0; i < total; i++ )
1553 {
1554 unsigned no_op = size_greater_than( total - offset, i );
1555 /* The first `total - offset` passes are a no-op. The last
1556 * `offset` passes shift the data one byte to the left and
1557 * zero out the last byte. */
1558 for( n = 0; n < total - 1; n++ )
1559 {
1560 unsigned char current = buf[n];
1561 unsigned char next = buf[n+1];
1562 buf[n] = if_int( no_op, current, next );
1563 }
1564 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1565 }
1566}
1567
Paul Bakkerb3869132013-02-28 17:21:01 +01001568/*
1569 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001572 int (*f_rng)(void *, unsigned char *, size_t),
1573 void *p_rng,
Thomas Daubney34733082021-05-12 09:24:29 +01001574 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001575 const unsigned char *input,
1576 unsigned char *output,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001577 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001578{
Janos Follath24eed8d2019-11-22 13:21:35 +00001579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001580 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001582 /* The following variables take sensitive values: their value must
1583 * not leak into the observable behavior of the function other than
1584 * the designated outputs (output, olen, return value). Otherwise
1585 * this would open the execution of the function to
1586 * side-channel-based variants of the Bleichenbacher padding oracle
1587 * attack. Potential side channels include overall timing, memory
1588 * access patterns (especially visible to an adversary who has access
1589 * to a shared memory cache), and branches (especially visible to
1590 * an adversary who has access to a shared code cache or to a shared
1591 * branch predictor). */
1592 size_t pad_count = 0;
1593 unsigned bad = 0;
1594 unsigned char pad_done = 0;
1595 size_t plaintext_size = 0;
1596 unsigned output_too_large;
1597
1598 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001599 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1600 RSA_VALIDATE_RET( input != NULL );
1601 RSA_VALIDATE_RET( olen != NULL );
1602
1603 ilen = ctx->len;
1604 plaintext_max_size = ( output_max_len > ilen - 11 ?
1605 ilen - 11 :
1606 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001607
Thomas Daubney34733082021-05-12 09:24:29 +01001608 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001610
Paul Bakkerb3869132013-02-28 17:21:01 +01001611 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001613
Thomas Daubney34733082021-05-12 09:24:29 +01001614 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001615
1616 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001617 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001618
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001619 /* Check and get padding length in constant time and constant
1620 * memory trace. The first byte must be 0. */
1621 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001622
Paul Bakker5121ce52009-01-03 21:22:43 +00001623
Thomas Daubney34733082021-05-12 09:24:29 +01001624 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1625 * where PS must be at least 8 nonzero bytes. */
1626 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001627
Thomas Daubney34733082021-05-12 09:24:29 +01001628 /* Read the whole buffer. Set pad_done to nonzero if we find
1629 * the 0x00 byte and remember the padding length in pad_count. */
1630 for( i = 2; i < ilen; i++ )
1631 {
1632 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
1633 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001634 }
1635
Thomas Daubney34733082021-05-12 09:24:29 +01001636
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001637 /* If pad_done is still zero, there's no data, only unfinished padding. */
1638 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001639
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001640 /* There must be at least 8 bytes of padding. */
1641 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001642
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001643 /* If the padding is valid, set plaintext_size to the number of
1644 * remaining bytes after stripping the padding. If the padding
1645 * is invalid, avoid leaking this fact through the size of the
1646 * output: use the maximum message size that fits in the output
1647 * buffer. Do it without branches to avoid leaking the padding
1648 * validity through timing. RSA keys are small enough that all the
1649 * size_t values involved fit in unsigned int. */
1650 plaintext_size = if_int( bad,
1651 (unsigned) plaintext_max_size,
1652 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001653
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001654 /* Set output_too_large to 0 if the plaintext fits in the output
1655 * buffer and to 1 otherwise. */
1656 output_too_large = size_greater_than( plaintext_size,
1657 plaintext_max_size );
1658
1659 /* Set ret without branches to avoid timing attacks. Return:
1660 * - INVALID_PADDING if the padding is bad (bad != 0).
1661 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1662 * plaintext does not fit in the output buffer.
1663 * - 0 if the padding is correct. */
1664 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1665 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1666 0 ) );
1667
1668 /* If the padding is bad or the plaintext is too large, zero the
1669 * data that we're about to copy to the output buffer.
1670 * We need to copy the same amount of data
1671 * from the same buffer whether the padding is good or not to
1672 * avoid leaking the padding validity through overall timing or
1673 * through memory or cache access patterns. */
1674 bad = all_or_nothing_int( bad | output_too_large );
1675 for( i = 11; i < ilen; i++ )
1676 buf[i] &= ~bad;
1677
1678 /* If the plaintext is too large, truncate it to the buffer size.
1679 * Copy anyway to avoid revealing the length through timing, because
1680 * revealing the length is as bad as revealing the padding validity
1681 * for a Bleichenbacher attack. */
1682 plaintext_size = if_int( output_too_large,
1683 (unsigned) plaintext_max_size,
1684 (unsigned) plaintext_size );
1685
1686 /* Move the plaintext to the leftmost position where it can start in
1687 * the working buffer, i.e. make it start plaintext_max_size from
1688 * the end of the buffer. Do this with a memory access trace that
1689 * does not depend on the plaintext size. After this move, the
1690 * starting location of the plaintext is no longer sensitive
1691 * information. */
1692 mem_move_to_left( buf + ilen - plaintext_max_size,
1693 plaintext_max_size,
1694 plaintext_max_size - plaintext_size );
1695
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001696 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1697 * buffer. If output_max_len is 0, then output may be an invalid pointer
1698 * and the result of memcpy() would be undefined; prevent undefined
1699 * behavior making sure to depend only on output_max_len (the size of the
1700 * user-provided output buffer), which is independent from plaintext
1701 * length, validity of padding, success of the decryption, and other
1702 * secrets. */
1703 if( output_max_len != 0 )
1704 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001705
1706 /* Report the amount of data we copied to the output buffer. In case
1707 * of errors (bad padding or output too large), the value of *olen
1708 * when this function returns is not specified. Making it equivalent
1709 * to the good case limits the risks of leaking the padding validity. */
1710 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001711
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001712cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001713 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001714
1715 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001716}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001718
1719/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001720 * Do an RSA operation, then remove the message padding
1721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001723 int (*f_rng)(void *, unsigned char *, size_t),
1724 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001725 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001726 const unsigned char *input,
1727 unsigned char *output,
1728 size_t output_max_len)
1729{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001730 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001731 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1732 RSA_VALIDATE_RET( input != NULL );
1733 RSA_VALIDATE_RET( olen != NULL );
1734
Paul Bakkerb3869132013-02-28 17:21:01 +01001735 switch( ctx->padding )
1736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_PKCS1_V15)
1738 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney34733082021-05-12 09:24:29 +01001739 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001740 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001741#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#if defined(MBEDTLS_PKCS1_V21)
1744 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001745 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001746 olen, input, output,
1747 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001748#endif
1749
1750 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001752 }
1753}
1754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001756static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001757 int (*f_rng)(void *, unsigned char *, size_t),
1758 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001760 unsigned int hashlen,
1761 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001762 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001763 unsigned char *sig )
1764{
1765 size_t olen;
1766 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001767 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001768 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001770 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 const mbedtls_md_info_t *md_info;
1772 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001773 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001774 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1775 hashlen == 0 ) ||
1776 hash != NULL );
1777 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001778
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001779 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1780 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1781
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001782 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001784
1785 olen = ctx->len;
1786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001788 {
Simon Butcher02037452016-03-01 21:19:12 +00001789 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001791 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001795 }
1796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001798 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001802
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001803 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1804 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001805 /* Calculate the largest possible salt length, up to the hash size.
1806 * Normally this is the hash length, which is the maximum salt length
1807 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001808 * enough room, use the maximum salt length that fits. The constraint is
1809 * that the hash length plus the salt length plus 2 bytes must be at most
1810 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1811 * (PKCS#1 v2.2) §9.1.1 step 3. */
1812 min_slen = hlen - 2;
1813 if( olen < hlen + min_slen + 2 )
1814 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1815 else if( olen >= hlen + hlen + 2 )
1816 slen = hlen;
1817 else
1818 slen = olen - hlen - 2;
1819 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001820 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001823 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001824 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001825 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001826 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001827 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001828
1829 memset( sig, 0, olen );
1830
Simon Butcher02037452016-03-01 21:19:12 +00001831 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001832 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001833 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001834 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001835
1836 /* Generate salt of length slen in place in the encoded message */
1837 salt = p;
1838 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001839 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001840
Paul Bakkerb3869132013-02-28 17:21:01 +01001841 p += slen;
1842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001844 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001845 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001846
Simon Butcher02037452016-03-01 21:19:12 +00001847 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001848 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1849 goto exit;
1850 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1851 goto exit;
1852 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1853 goto exit;
1854 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1855 goto exit;
1856 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1857 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001858
Simon Butcher02037452016-03-01 21:19:12 +00001859 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001860 if( msb % 8 == 0 )
1861 offset = 1;
1862
Simon Butcher02037452016-03-01 21:19:12 +00001863 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001864 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1865 &md_ctx ) ) != 0 )
1866 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001867
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001868 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001869 sig[0] &= 0xFF >> ( olen * 8 - msb );
1870
1871 p += hlen;
1872 *p++ = 0xBC;
1873
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001874exit:
1875 mbedtls_md_free( &md_ctx );
1876
1877 if( ret != 0 )
1878 return( ret );
1879
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001880 return mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001881}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001882
1883/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001884 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1885 * the option to pass in the salt length.
1886 */
1887int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1888 int (*f_rng)(void *, unsigned char *, size_t),
1889 void *p_rng,
1890 mbedtls_md_type_t md_alg,
1891 unsigned int hashlen,
1892 const unsigned char *hash,
1893 int saltlen,
1894 unsigned char *sig )
1895{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001896 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001897 hashlen, hash, saltlen, sig );
1898}
1899
1900
1901/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001902 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1903 */
1904int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1905 int (*f_rng)(void *, unsigned char *, size_t),
1906 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001907 mbedtls_md_type_t md_alg,
1908 unsigned int hashlen,
1909 const unsigned char *hash,
1910 unsigned char *sig )
1911{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001912 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001913 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001918/*
1919 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1920 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001921
1922/* Construct a PKCS v1.5 encoding of a hashed message
1923 *
1924 * This is used both for signature generation and verification.
1925 *
1926 * Parameters:
1927 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001928 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001929 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001930 * - hash: Buffer containing the hashed message or the raw data.
1931 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001932 * - dst: Buffer to hold the encoded message.
1933 *
1934 * Assumptions:
1935 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1936 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001937 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001938 *
1939 */
1940static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1941 unsigned int hashlen,
1942 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001943 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001944 unsigned char *dst )
1945{
1946 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001947 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001948 unsigned char *p = dst;
1949 const char *oid = NULL;
1950
1951 /* Are we signing hashed or raw data? */
1952 if( md_alg != MBEDTLS_MD_NONE )
1953 {
1954 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1955 if( md_info == NULL )
1956 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1957
1958 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1959 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1960
1961 hashlen = mbedtls_md_get_size( md_info );
1962
1963 /* Double-check that 8 + hashlen + oid_size can be used as a
1964 * 1-byte ASN.1 length encoding and that there's no overflow. */
1965 if( 8 + hashlen + oid_size >= 0x80 ||
1966 10 + hashlen < hashlen ||
1967 10 + hashlen + oid_size < 10 + hashlen )
1968 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1969
1970 /*
1971 * Static bounds check:
1972 * - Need 10 bytes for five tag-length pairs.
1973 * (Insist on 1-byte length encodings to protect against variants of
1974 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1975 * - Need hashlen bytes for hash
1976 * - Need oid_size bytes for hash alg OID.
1977 */
1978 if( nb_pad < 10 + hashlen + oid_size )
1979 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1980 nb_pad -= 10 + hashlen + oid_size;
1981 }
1982 else
1983 {
1984 if( nb_pad < hashlen )
1985 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1986
1987 nb_pad -= hashlen;
1988 }
1989
Hanno Becker2b2f8982017-09-27 17:10:03 +01001990 /* Need space for signature header and padding delimiter (3 bytes),
1991 * and 8 bytes for the minimal padding */
1992 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001993 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1994 nb_pad -= 3;
1995
1996 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001997 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001998
1999 /* Write signature header and padding */
2000 *p++ = 0;
2001 *p++ = MBEDTLS_RSA_SIGN;
2002 memset( p, 0xFF, nb_pad );
2003 p += nb_pad;
2004 *p++ = 0;
2005
2006 /* Are we signing raw data? */
2007 if( md_alg == MBEDTLS_MD_NONE )
2008 {
2009 memcpy( p, hash, hashlen );
2010 return( 0 );
2011 }
2012
2013 /* Signing hashed data, add corresponding ASN.1 structure
2014 *
2015 * DigestInfo ::= SEQUENCE {
2016 * digestAlgorithm DigestAlgorithmIdentifier,
2017 * digest Digest }
2018 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2019 * Digest ::= OCTET STRING
2020 *
2021 * Schematic:
2022 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2023 * TAG-NULL + LEN [ NULL ] ]
2024 * TAG-OCTET + LEN [ HASH ] ]
2025 */
2026 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002027 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002028 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002029 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002030 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002031 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002032 memcpy( p, oid, oid_size );
2033 p += oid_size;
2034 *p++ = MBEDTLS_ASN1_NULL;
2035 *p++ = 0x00;
2036 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002037 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002038 memcpy( p, hash, hashlen );
2039 p += hashlen;
2040
2041 /* Just a sanity-check, should be automatic
2042 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002043 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002044 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002045 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002046 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2047 }
2048
2049 return( 0 );
2050}
2051
Paul Bakkerb3869132013-02-28 17:21:01 +01002052/*
2053 * Do an RSA operation to sign the message digest
2054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002056 int (*f_rng)(void *, unsigned char *, size_t),
2057 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002059 unsigned int hashlen,
2060 const unsigned char *hash,
2061 unsigned char *sig )
2062{
Janos Follath24eed8d2019-11-22 13:21:35 +00002063 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002064 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002065
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002066 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002067 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2068 hashlen == 0 ) ||
2069 hash != NULL );
2070 RSA_VALIDATE_RET( sig != NULL );
2071
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002072 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2073 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2074
Hanno Beckerfdf38032017-09-06 12:35:55 +01002075 /*
2076 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2077 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002078
Hanno Beckerfdf38032017-09-06 12:35:55 +01002079 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2080 ctx->len, sig ) ) != 0 )
2081 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002082
Hanno Beckerfdf38032017-09-06 12:35:55 +01002083 /* Private key operation
2084 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002085 * In order to prevent Lenstra's attack, make the signature in a
2086 * temporary buffer and check it before returning it.
2087 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002088
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002089 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002090 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002091 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2092
Hanno Beckerfdf38032017-09-06 12:35:55 +01002093 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002094 if( verif == NULL )
2095 {
2096 mbedtls_free( sig_try );
2097 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2098 }
2099
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002100 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2101 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2102
Hanno Becker171a8f12017-09-06 12:32:16 +01002103 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002104 {
2105 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2106 goto cleanup;
2107 }
2108
2109 memcpy( sig, sig_try, ctx->len );
2110
2111cleanup:
2112 mbedtls_free( sig_try );
2113 mbedtls_free( verif );
2114
2115 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002116}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002118
2119/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 * Do an RSA operation to sign the message digest
2121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002123 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002124 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002126 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002127 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 unsigned char *sig )
2129{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002130 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002131 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2132 hashlen == 0 ) ||
2133 hash != NULL );
2134 RSA_VALIDATE_RET( sig != NULL );
2135
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 switch( ctx->padding )
2137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138#if defined(MBEDTLS_PKCS1_V15)
2139 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney52654982021-05-18 16:54:00 +01002140 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng,
Thomas Daubney140184d2021-05-18 16:04:07 +01002141 md_alg, hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002142#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144#if defined(MBEDTLS_PKCS1_V21)
2145 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyde9fdc42021-05-18 17:10:04 +01002146 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
2147 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002148#endif
2149
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002153}
2154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002156/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002157 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002161 unsigned int hashlen,
2162 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002164 int expected_salt_len,
2165 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002166{
Janos Follath24eed8d2019-11-22 13:21:35 +00002167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002168 size_t siglen;
2169 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002170 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002172 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002173 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002174 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 const mbedtls_md_info_t *md_info;
2176 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002177 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002178
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002179 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002180 RSA_VALIDATE_RET( sig != NULL );
2181 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2182 hashlen == 0 ) ||
2183 hash != NULL );
2184
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 siglen = ctx->len;
2186
Paul Bakker27fdf462011-06-09 13:55:13 +00002187 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002189
Thomas Daubney782a7f52021-05-19 12:27:35 +01002190 ret = mbedtls_rsa_public( ctx, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191
2192 if( ret != 0 )
2193 return( ret );
2194
2195 p = buf;
2196
Paul Bakkerb3869132013-02-28 17:21:01 +01002197 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 {
Simon Butcher02037452016-03-01 21:19:12 +00002202 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002204 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002208 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002211 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002215
Paul Bakkerb3869132013-02-28 17:21:01 +01002216 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002217
Simon Butcher02037452016-03-01 21:19:12 +00002218 /*
2219 * Note: EMSA-PSS verification is over the length of N - 1 bits
2220 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002221 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002222
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002223 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2224 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2225
Simon Butcher02037452016-03-01 21:19:12 +00002226 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002227 if( msb % 8 == 0 )
2228 {
2229 p++;
2230 siglen -= 1;
2231 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002232
Gilles Peskine139108a2017-10-18 19:03:42 +02002233 if( siglen < hlen + 2 )
2234 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2235 hash_start = p + siglen - hlen - 1;
2236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002238 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002239 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002240
Jaeden Amero66954e12018-01-25 16:05:54 +00002241 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2242 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002243 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002244
Paul Bakkerb3869132013-02-28 17:21:01 +01002245 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002246
Gilles Peskine6a54b022017-10-17 19:02:13 +02002247 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002248 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002249
Gilles Peskine91048a32017-10-19 17:46:14 +02002250 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002251 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002252 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2253 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002254 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002255
Gilles Peskine6a54b022017-10-17 19:02:13 +02002256 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002259 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002260 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002261 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2262 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002263 }
2264
Simon Butcher02037452016-03-01 21:19:12 +00002265 /*
2266 * Generate H = Hash( M' )
2267 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002268 ret = mbedtls_md_starts( &md_ctx );
2269 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002270 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002271 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2272 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002273 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002274 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2275 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002276 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002277 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2278 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002279 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002280 ret = mbedtls_md_finish( &md_ctx, result );
2281 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002282 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002283
Jaeden Amero66954e12018-01-25 16:05:54 +00002284 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002285 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002286 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002287 goto exit;
2288 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002289
2290exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002292
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002293 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002294}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002295
2296/*
2297 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002301 unsigned int hashlen,
2302 const unsigned char *hash,
2303 const unsigned char *sig )
2304{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002305 mbedtls_md_type_t mgf1_hash_id;
2306 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002307 RSA_VALIDATE_RET( sig != NULL );
2308 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2309 hashlen == 0 ) ||
2310 hash != NULL );
2311
2312 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002314 : md_alg;
2315
Thomas Daubney9e65f792021-05-19 12:18:58 +01002316 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx,
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002317 md_alg, hashlen, hash,
2318 mgf1_hash_id,
2319 MBEDTLS_RSA_SALT_LEN_ANY,
2320 sig ) );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002321
2322}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002326/*
2327 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002331 unsigned int hashlen,
2332 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002333 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002334{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002335 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002336 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002337 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002338
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002339 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002340 RSA_VALIDATE_RET( sig != NULL );
2341 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2342 hashlen == 0 ) ||
2343 hash != NULL );
2344
2345 sig_len = ctx->len;
2346
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002347 /*
2348 * Prepare expected PKCS1 v1.5 encoding of hash.
2349 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002350
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002351 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2352 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2353 {
2354 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2355 goto cleanup;
2356 }
2357
2358 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2359 encoded_expected ) ) != 0 )
2360 goto cleanup;
2361
2362 /*
2363 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2364 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002365
Thomas Daubney41e4ce42021-05-19 15:10:05 +01002366 ret = mbedtls_rsa_public( ctx, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002367 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002368 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002369
Simon Butcher02037452016-03-01 21:19:12 +00002370 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002371 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002372 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002373
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002374 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2375 sig_len ) ) != 0 )
2376 {
2377 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2378 goto cleanup;
2379 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002380
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002381cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002382
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002383 if( encoded != NULL )
2384 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002385 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002386 mbedtls_free( encoded );
2387 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002388
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002389 if( encoded_expected != NULL )
2390 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002391 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002392 mbedtls_free( encoded_expected );
2393 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002394
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002395 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
2399/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002400 * Do an RSA operation and check the message digest
2401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002404 unsigned int hashlen,
2405 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002406 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002407{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002408 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002409 RSA_VALIDATE_RET( sig != NULL );
2410 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2411 hashlen == 0 ) ||
2412 hash != NULL );
2413
Paul Bakkerb3869132013-02-28 17:21:01 +01002414 switch( ctx->padding )
2415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416#if defined(MBEDTLS_PKCS1_V15)
2417 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney2e126252021-05-19 11:48:53 +01002418 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, md_alg,
2419 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002420#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422#if defined(MBEDTLS_PKCS1_V21)
2423 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002424 return mbedtls_rsa_rsassa_pss_verify( ctx, md_alg,
2425 hashlen, hash, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01002426#endif
2427
2428 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002430 }
2431}
2432
2433/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002434 * Copy the components of an RSA key
2435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002437{
Janos Follath24eed8d2019-11-22 13:21:35 +00002438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002439 RSA_VALIDATE_RET( dst != NULL );
2440 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002441
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002442 dst->len = src->len;
2443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2445 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2448 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2449 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002450
2451#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2453 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2454 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2456 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002457#endif
2458
2459 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2462 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002463
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002464 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002465 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002466
2467cleanup:
2468 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002470
2471 return( ret );
2472}
2473
2474/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 * Free the components of an RSA key
2476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002478{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002479 if( ctx == NULL )
2480 return;
2481
2482 mbedtls_mpi_free( &ctx->Vi );
2483 mbedtls_mpi_free( &ctx->Vf );
2484 mbedtls_mpi_free( &ctx->RN );
2485 mbedtls_mpi_free( &ctx->D );
2486 mbedtls_mpi_free( &ctx->Q );
2487 mbedtls_mpi_free( &ctx->P );
2488 mbedtls_mpi_free( &ctx->E );
2489 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002490
Hanno Becker33c30a02017-08-23 07:00:22 +01002491#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002492 mbedtls_mpi_free( &ctx->RQ );
2493 mbedtls_mpi_free( &ctx->RP );
2494 mbedtls_mpi_free( &ctx->QP );
2495 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002496 mbedtls_mpi_free( &ctx->DP );
2497#endif /* MBEDTLS_RSA_NO_CRT */
2498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002500 /* Free the mutex, but only if it hasn't been freed already. */
2501 if( ctx->ver != 0 )
2502 {
2503 mbedtls_mutex_free( &ctx->mutex );
2504 ctx->ver = 0;
2505 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002506#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002507}
2508
Hanno Beckerab377312017-08-23 16:24:51 +01002509#endif /* !MBEDTLS_RSA_ALT */
2510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002513#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002514
2515/*
2516 * Example RSA-1024 keypair, for test purposes
2517 */
2518#define KEY_LEN 128
2519
2520#define RSA_N "9292758453063D803DD603D5E777D788" \
2521 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2522 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2523 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2524 "93A89813FBF3C4F8066D2D800F7C38A8" \
2525 "1AE31942917403FF4946B0A83D3D3E05" \
2526 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2527 "5E94BB77B07507233A0BC7BAC8F90F79"
2528
2529#define RSA_E "10001"
2530
2531#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2532 "66CA472BC44D253102F8B4A9D3BFA750" \
2533 "91386C0077937FE33FA3252D28855837" \
2534 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2535 "DF79C5CE07EE72C7F123142198164234" \
2536 "CABB724CF78B8173B9F880FC86322407" \
2537 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2538 "071513A1E85B5DFA031F21ECAE91A34D"
2539
2540#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2541 "2C01CAD19EA484A87EA4377637E75500" \
2542 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2543 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2544
2545#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2546 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2547 "910E4168387E3C30AA1E00C339A79508" \
2548 "8452DD96A9A5EA5D9DCA68DA636032AF"
2549
Paul Bakker5121ce52009-01-03 21:22:43 +00002550#define PT_LEN 24
2551#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2552 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002555static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002556{
gufe44c2620da2020-08-03 17:56:50 +02002557#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002558 size_t i;
2559
Paul Bakker545570e2010-07-18 09:00:25 +00002560 if( rng_state != NULL )
2561 rng_state = NULL;
2562
Paul Bakkera3d195c2011-11-27 21:07:34 +00002563 for( i = 0; i < len; ++i )
2564 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002565#else
2566 if( rng_state != NULL )
2567 rng_state = NULL;
2568
2569 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002570#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002571
Paul Bakkera3d195c2011-11-27 21:07:34 +00002572 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002575
Paul Bakker5121ce52009-01-03 21:22:43 +00002576/*
2577 * Checkup routine
2578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002580{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002581 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002583 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 unsigned char rsa_plaintext[PT_LEN];
2586 unsigned char rsa_decrypted[PT_LEN];
2587 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002589 unsigned char sha1sum[20];
2590#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Hanno Becker3a701162017-08-22 13:52:43 +01002592 mbedtls_mpi K;
2593
2594 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
Hanno Becker3a701162017-08-22 13:52:43 +01002597 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2598 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2599 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2600 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2601 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2602 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2603 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2604 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2605 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2606 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2607
Hanno Becker7f25f852017-10-10 16:56:22 +01002608 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
2610 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2614 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 {
2616 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
Hanno Becker5bc87292017-05-03 15:09:31 +01002619 ret = 1;
2620 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 }
2622
2623 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
2626 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2627
Thomas Daubney21772772021-05-13 17:30:32 +01002628 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002629 PT_LEN, rsa_plaintext,
2630 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002631 {
2632 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
Hanno Becker5bc87292017-05-03 15:09:31 +01002635 ret = 1;
2636 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 }
2638
2639 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002642 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002643 &len, rsa_ciphertext, rsa_decrypted,
2644 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 {
2646 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Hanno Becker5bc87292017-05-03 15:09:31 +01002649 ret = 1;
2650 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 }
2652
2653 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2654 {
2655 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
Hanno Becker5bc87292017-05-03 15:09:31 +01002658 ret = 1;
2659 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 }
2661
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002662 if( verbose != 0 )
2663 mbedtls_printf( "passed\n" );
2664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002667 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002669 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002670 {
2671 if( verbose != 0 )
2672 mbedtls_printf( "failed\n" );
2673
2674 return( 1 );
2675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Hanno Becker98838b02017-10-02 13:16:10 +01002677 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
Thomas Daubney140184d2021-05-18 16:04:07 +01002678 MBEDTLS_MD_SHA1, 0,
Hanno Becker98838b02017-10-02 13:16:10 +01002679 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 {
2681 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
Hanno Becker5bc87292017-05-03 15:09:31 +01002684 ret = 1;
2685 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 }
2687
2688 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
Thomas Daubney68d9cbc2021-05-18 18:45:09 +01002691 if( mbedtls_rsa_pkcs1_verify( &rsa, MBEDTLS_MD_SHA1, 0,
Hanno Becker98838b02017-10-02 13:16:10 +01002692 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 {
2694 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Hanno Becker5bc87292017-05-03 15:09:31 +01002697 ret = 1;
2698 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
2700
2701 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002702 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002705 if( verbose != 0 )
2706 mbedtls_printf( "\n" );
2707
Paul Bakker3d8fb632014-04-17 12:42:41 +02002708cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002709 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 mbedtls_rsa_free( &rsa );
2711#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002712 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002714 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715}
2716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719#endif /* MBEDTLS_RSA_C */