blob: 26a93c1b94b04054566727d969c947f86934248f [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 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200480void mbedtls_rsa_init( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000481{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500482 RSA_VALIDATE( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
Ronald Cronc1905a12021-06-05 11:11:14 +0200486 ctx->padding = MBEDTLS_RSA_PKCS_V15;
487 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100490 /* Set ctx->ver to nonzero to indicate that the mutex has been
491 * initialized and will need to be freed. */
492 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200494#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000495}
496
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100497/*
498 * Set padding for an existing RSA context
499 */
Ronald Cronea7631b2021-06-03 18:51:59 +0200500int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
501 mbedtls_md_type_t hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100502{
Ronald Cronea7631b2021-06-03 18:51:59 +0200503 if( ( padding != MBEDTLS_RSA_PKCS_V15 ) &&
504 ( padding != MBEDTLS_RSA_PKCS_V21 ) )
505 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
506
507 if( ( padding == MBEDTLS_RSA_PKCS_V21 ) &&
508 ( hash_id != MBEDTLS_MD_NONE ) )
509 {
510 const mbedtls_md_info_t *md_info;
511
512 md_info = mbedtls_md_info_from_type( hash_id );
513 if( md_info == NULL )
514 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
515 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500516
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100517 ctx->padding = padding;
518 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200519
520 return( 0 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100521}
522
Hanno Becker617c1ae2017-08-23 14:11:24 +0100523/*
524 * Get length in bytes of RSA modulus
525 */
526
527size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
528{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100529 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100530}
531
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
535/*
536 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800537 *
538 * This generation method follows the RSA key pair generation procedure of
539 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000542 int (*f_rng)(void *, unsigned char *, size_t),
543 void *p_rng,
544 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000545{
Janos Follath24eed8d2019-11-22 13:21:35 +0000546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800547 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100548 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500549 RSA_VALIDATE_RET( ctx != NULL );
550 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
Janos Follathb8fc1b02018-09-03 15:37:01 +0100552 /*
553 * If the modulus is 1024 bit long or shorter, then the security strength of
554 * the RSA algorithm is less than or equal to 80 bits and therefore an error
555 * rate of 2^-80 is sufficient.
556 */
557 if( nbits > 1024 )
558 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
559
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100560 mbedtls_mpi_init( &H );
561 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800562 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100564 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
565 {
566 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
567 goto cleanup;
568 }
569
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 /*
571 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800572 * 1. |P-Q| > 2^( nbits / 2 - 100 )
573 * 2. GCD( E, (P-1)*(Q-1) ) == 1
574 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578 do
579 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100580 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
581 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
Janos Follathb8fc1b02018-09-03 15:37:01 +0100583 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
584 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800586 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
587 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
588 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 continue;
590
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800591 /* not required by any standards, but some users rely on the fact that P > Q */
592 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100593 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100594
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100595 /* Temporarily replace P,Q by P-1, Q-1 */
596 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
597 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
598 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800599
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800600 /* 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 +0200601 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800602 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
603 continue;
604
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800605 /* 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 -0800606 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
607 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
608 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
609
610 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
611 continue;
612
613 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800615 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100617 /* Restore P,Q */
618 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
619 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
620
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800621 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
622
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100623 ctx->len = mbedtls_mpi_size( &ctx->N );
624
Jethro Beekman97f95c92018-02-13 15:50:36 -0800625#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000626 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 * DP = D mod (P - 1)
628 * DQ = D mod (Q - 1)
629 * QP = Q^-1 mod P
630 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100631 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
632 &ctx->DP, &ctx->DQ, &ctx->QP ) );
633#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
Hanno Becker83aad1f2017-08-23 06:45:10 +0100635 /* Double-check */
636 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
638cleanup:
639
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100640 mbedtls_mpi_free( &H );
641 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800642 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
644 if( ret != 0 )
645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100647
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100648 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100649 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100650 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 }
652
Paul Bakker48377d92013-08-30 12:06:24 +0200653 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000654}
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
658/*
659 * Check a public RSA key
660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000662{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500663 RSA_VALIDATE_RET( ctx != NULL );
664
Hanno Beckerebd2c022017-10-12 10:54:53 +0100665 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000667
Hanno Becker3a760a12018-01-05 08:14:49 +0000668 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100671 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
Hanno Becker705fc682017-10-10 17:57:02 +0100673 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
674 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100678 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
680 return( 0 );
681}
682
683/*
Hanno Becker705fc682017-10-10 17:57:02 +0100684 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000687{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500688 RSA_VALIDATE_RET( ctx != NULL );
689
Hanno Becker705fc682017-10-10 17:57:02 +0100690 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100691 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000692 {
Hanno Becker98838b02017-10-02 13:16:10 +0100693 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 }
Paul Bakker48377d92013-08-30 12:06:24 +0200695
Hanno Becker98838b02017-10-02 13:16:10 +0100696 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100697 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100699 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000701
Hanno Beckerb269a852017-08-25 08:03:21 +0100702#if !defined(MBEDTLS_RSA_NO_CRT)
703 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
704 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
705 {
706 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
707 }
708#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000709
710 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711}
712
713/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100714 * Check if contexts holding a public and private key match
715 */
Hanno Becker98838b02017-10-02 13:16:10 +0100716int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
717 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100718{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500719 RSA_VALIDATE_RET( pub != NULL );
720 RSA_VALIDATE_RET( prv != NULL );
721
Hanno Becker98838b02017-10-02 13:16:10 +0100722 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100726 }
727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
729 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100732 }
733
734 return( 0 );
735}
736
737/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 * Do an RSA public key operation
739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000741 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 unsigned char *output )
743{
Janos Follath24eed8d2019-11-22 13:21:35 +0000744 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000745 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500747 RSA_VALIDATE_RET( ctx != NULL );
748 RSA_VALIDATE_RET( input != NULL );
749 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Hanno Beckerebd2c022017-10-12 10:54:53 +0100751 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100752 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200756#if defined(MBEDTLS_THREADING_C)
757 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
758 return( ret );
759#endif
760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200765 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
766 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 }
768
769 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
771 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
773cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200775 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
776 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100777#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000780
781 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100782 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000783
784 return( 0 );
785}
786
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200787/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200788 * Generate or update blinding values, see section 10 of:
789 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200790 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200791 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200792 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200793static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200794 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
795{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200796 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200797 mbedtls_mpi R;
798
799 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200800
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200801 if( ctx->Vf.p != NULL )
802 {
803 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
805 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
806 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
807 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200808
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200809 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200810 }
811
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200812 /* Unblinding value: Vf = random number, invertible mod N */
813 do {
814 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200815 {
816 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
817 goto cleanup;
818 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 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 +0200821
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200822 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200823 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
824 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
825 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
826
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200827 /* At this point, Vi is invertible mod N if and only if both Vf and R
828 * are invertible mod N. If one of them isn't, we don't need to know
829 * which one, we just loop and choose new values for both of them.
830 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200831 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500832 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200833 goto cleanup;
834
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500835 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
836
837 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
838 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
839 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200840
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200841 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200842 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 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 +0200844
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200845
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200846cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200847 mbedtls_mpi_free( &R );
848
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200849 return( ret );
850}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200851
Paul Bakker5121ce52009-01-03 21:22:43 +0000852/*
Janos Follathe81102e2017-03-22 13:38:28 +0000853 * Exponent blinding supposed to prevent side-channel attacks using multiple
854 * traces of measurements to recover the RSA key. The more collisions are there,
855 * the more bits of the key can be recovered. See [3].
856 *
857 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
858 * observations on avarage.
859 *
860 * For example with 28 byte blinding to achieve 2 collisions the adversary has
861 * to make 2^112 observations on avarage.
862 *
863 * (With the currently (as of 2017 April) known best algorithms breaking 2048
864 * bit RSA requires approximately as much time as trying out 2^112 random keys.
865 * Thus in this sense with 28 byte blinding the security is not reduced by
866 * side-channel attacks like the one in [3])
867 *
868 * This countermeasure does not help if the key recovery is possible with a
869 * single trace.
870 */
871#define RSA_EXPONENT_BLINDING 28
872
873/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000874 * Do an RSA private key operation
875 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200877 int (*f_rng)(void *, unsigned char *, size_t),
878 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000879 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000880 unsigned char *output )
881{
Janos Follath24eed8d2019-11-22 13:21:35 +0000882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000883 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100884
885 /* Temporary holding the result */
886 mbedtls_mpi T;
887
888 /* Temporaries holding P-1, Q-1 and the
889 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000890 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100891
892#if !defined(MBEDTLS_RSA_NO_CRT)
893 /* Temporaries holding the results mod p resp. mod q. */
894 mbedtls_mpi TP, TQ;
895
896 /* Temporaries holding the blinded exponents for
897 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000898 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100899
900 /* Pointers to actual exponents to be used - either the unblinded
901 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000902 mbedtls_mpi *DP = &ctx->DP;
903 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100904#else
905 /* Temporary holding the blinded exponent (if used). */
906 mbedtls_mpi D_blind;
907
908 /* Pointer to actual exponent to be used - either the unblinded
909 * or the blinded one, depending on the presence of a PRNG. */
910 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100911#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100912
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100913 /* Temporaries holding the initial input and the double
914 * checked result; should be the same in the end. */
915 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500917 RSA_VALIDATE_RET( ctx != NULL );
918 RSA_VALIDATE_RET( input != NULL );
919 RSA_VALIDATE_RET( output != NULL );
920
Hanno Beckerebd2c022017-10-12 10:54:53 +0100921 if( rsa_check_context( ctx, 1 /* private key checks */,
922 f_rng != NULL /* blinding y/n */ ) != 0 )
923 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100924 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100925 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100926
Hanno Becker06811ce2017-05-03 15:10:34 +0100927#if defined(MBEDTLS_THREADING_C)
928 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
929 return( ret );
930#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000931
Hanno Becker06811ce2017-05-03 15:10:34 +0100932 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100933 mbedtls_mpi_init( &T );
934
935 mbedtls_mpi_init( &P1 );
936 mbedtls_mpi_init( &Q1 );
937 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000938
Janos Follathf9203b42017-03-22 15:13:15 +0000939 if( f_rng != NULL )
940 {
Janos Follathe81102e2017-03-22 13:38:28 +0000941#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000942 mbedtls_mpi_init( &D_blind );
943#else
944 mbedtls_mpi_init( &DP_blind );
945 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000946#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000947 }
Janos Follathe81102e2017-03-22 13:38:28 +0000948
Hanno Becker06811ce2017-05-03 15:10:34 +0100949#if !defined(MBEDTLS_RSA_NO_CRT)
950 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200951#endif
952
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100953 mbedtls_mpi_init( &I );
954 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100955
956 /* End of MPI initialization */
957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
959 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200961 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
962 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 }
964
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100965 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100966
Paul Bakkerf451bac2013-08-30 15:37:02 +0200967 if( f_rng != NULL )
968 {
969 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200970 * Blinding
971 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200972 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200973 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
974 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000976
Janos Follathe81102e2017-03-22 13:38:28 +0000977 /*
978 * Exponent blinding
979 */
980 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
981 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
982
Janos Follathf9203b42017-03-22 15:13:15 +0000983#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000984 /*
985 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
986 */
987 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
988 f_rng, p_rng ) );
989 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
990 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
991 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
992
993 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000994#else
995 /*
996 * DP_blind = ( P - 1 ) * R + DP
997 */
998 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
999 f_rng, p_rng ) );
1000 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1001 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1002 &ctx->DP ) );
1003
1004 DP = &DP_blind;
1005
1006 /*
1007 * DQ_blind = ( Q - 1 ) * R + DQ
1008 */
1009 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1010 f_rng, p_rng ) );
1011 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1012 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1013 &ctx->DQ ) );
1014
1015 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001016#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001017 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001020 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001021#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001022 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001023 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001024 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001025 * TP = input ^ dP mod P
1026 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001028
1029 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1030 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
1032 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001033 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001035 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1036 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1037 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001038
1039 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001040 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001042 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1043 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001045
Paul Bakkerf451bac2013-08-30 15:37:02 +02001046 if( f_rng != NULL )
1047 {
1048 /*
1049 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001050 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001051 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001052 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001054 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
Hanno Becker2dec5e82017-10-03 07:49:52 +01001056 /* Verify the result to prevent glitching attacks. */
1057 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1058 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001059 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001060 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001061 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1062 goto cleanup;
1063 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001064
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
1068cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001070 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1071 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001072#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001073
Hanno Becker06811ce2017-05-03 15:10:34 +01001074 mbedtls_mpi_free( &P1 );
1075 mbedtls_mpi_free( &Q1 );
1076 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001077
1078 if( f_rng != NULL )
1079 {
Janos Follathe81102e2017-03-22 13:38:28 +00001080#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001081 mbedtls_mpi_free( &D_blind );
1082#else
1083 mbedtls_mpi_free( &DP_blind );
1084 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001085#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001086 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Hanno Becker06811ce2017-05-03 15:10:34 +01001088 mbedtls_mpi_free( &T );
1089
1090#if !defined(MBEDTLS_RSA_NO_CRT)
1091 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1092#endif
1093
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001094 mbedtls_mpi_free( &C );
1095 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001096
Gilles Peskineae3741e2020-11-25 00:10:31 +01001097 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001098 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001099
Gilles Peskineae3741e2020-11-25 00:10:31 +01001100 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001101}
1102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104/**
1105 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1106 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001107 * \param dst buffer to mask
1108 * \param dlen length of destination buffer
1109 * \param src source of the mask generation
1110 * \param slen length of the source buffer
1111 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001112 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001113static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001117 unsigned char counter[4];
1118 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001119 unsigned int hlen;
1120 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001121 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001124 memset( counter, 0, 4 );
1125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001127
Simon Butcher02037452016-03-01 21:19:12 +00001128 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001129 p = dst;
1130
1131 while( dlen > 0 )
1132 {
1133 use_len = hlen;
1134 if( dlen < hlen )
1135 use_len = dlen;
1136
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001137 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1138 goto exit;
1139 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1140 goto exit;
1141 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1142 goto exit;
1143 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1144 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001145
1146 for( i = 0; i < use_len; ++i )
1147 *p++ ^= mask[i];
1148
1149 counter[3]++;
1150
1151 dlen -= use_len;
1152 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001153
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001154exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001155 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001156
1157 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001162/*
1163 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001166 int (*f_rng)(void *, unsigned char *, size_t),
1167 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001168 const unsigned char *label, size_t label_len,
1169 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001170 const unsigned char *input,
1171 unsigned char *output )
1172{
1173 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001175 unsigned char *p = output;
1176 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 const mbedtls_md_info_t *md_info;
1178 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001179
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001180 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001181 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001182 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001183 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1184
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001185 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001189 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001191
1192 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001194
Simon Butcher02037452016-03-01 21:19:12 +00001195 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001196 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001198
1199 memset( output, 0, olen );
1200
1201 *p++ = 0;
1202
Simon Butcher02037452016-03-01 21:19:12 +00001203 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001204 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001205 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001206
1207 p += hlen;
1208
Simon Butcher02037452016-03-01 21:19:12 +00001209 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001210 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1211 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001212 p += hlen;
1213 p += olen - 2 * hlen - 2 - ilen;
1214 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001215 if( ilen != 0 )
1216 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001219 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001220 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001221
Simon Butcher02037452016-03-01 21:19:12 +00001222 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001223 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1224 &md_ctx ) ) != 0 )
1225 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001226
Simon Butcher02037452016-03-01 21:19:12 +00001227 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001228 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1229 &md_ctx ) ) != 0 )
1230 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001231
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001232exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001234
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001235 if( ret != 0 )
1236 return( ret );
1237
Thomas Daubney141700f2021-05-13 19:06:10 +01001238 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001239}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001243/*
1244 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001247 int (*f_rng)(void *, unsigned char *, size_t),
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001248 void *p_rng, size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001249 const unsigned char *input,
1250 unsigned char *output )
1251{
1252 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001254 unsigned char *p = output;
1255
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001256 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001257 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001258 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001259
Paul Bakkerb3869132013-02-28 17:21:01 +01001260 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001261
Simon Butcher02037452016-03-01 21:19:12 +00001262 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001263 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001265
1266 nb_pad = olen - 3 - ilen;
1267
1268 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001269
1270 if( f_rng == NULL )
1271 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1272
1273 *p++ = MBEDTLS_RSA_CRYPT;
1274
1275 while( nb_pad-- > 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001276 {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001277 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001278
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001279 do {
1280 ret = f_rng( p_rng, p, 1 );
1281 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001282
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001283 /* Check if RNG failed to generate data */
1284 if( rng_dl == 0 || ret != 0 )
1285 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001286
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001287 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001288 }
1289
1290 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001291 if( ilen != 0 )
1292 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001293
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001294 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001295}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001297
Paul Bakker5121ce52009-01-03 21:22:43 +00001298/*
1299 * Add the message padding, then do an RSA operation
1300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001302 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001303 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +01001304 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001305 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 unsigned char *output )
1307{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001308 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001309 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001310 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001311
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 switch( ctx->padding )
1313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#if defined(MBEDTLS_PKCS1_V15)
1315 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney21772772021-05-13 17:30:32 +01001316 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001317 ilen, input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001318#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#if defined(MBEDTLS_PKCS1_V21)
1321 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney141700f2021-05-13 19:06:10 +01001322 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, NULL, 0,
Thomas Daubney21772772021-05-13 17:30:32 +01001323 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001324#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
1326 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001329}
1330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001332/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001333 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001336 int (*f_rng)(void *, unsigned char *, size_t),
1337 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001338 const unsigned char *label, size_t label_len,
1339 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001340 const unsigned char *input,
1341 unsigned char *output,
1342 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001343{
Janos Follath24eed8d2019-11-22 13:21:35 +00001344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001345 size_t ilen, i, pad_len;
1346 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1348 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001349 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 const mbedtls_md_info_t *md_info;
1351 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001352
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001353 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001354 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1355 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1356 RSA_VALIDATE_RET( input != NULL );
1357 RSA_VALIDATE_RET( olen != NULL );
1358
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001359 /*
1360 * Parameters sanity checks
1361 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001362 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
1365 ilen = ctx->len;
1366
Paul Bakker27fdf462011-06-09 13:55:13 +00001367 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001371 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001373
Janos Follathc17cda12016-02-11 11:08:18 +00001374 hlen = mbedtls_md_get_size( md_info );
1375
1376 // checking for integer underflow
1377 if( 2 * hlen + 2 > ilen )
1378 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1379
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001380 /*
1381 * RSA operation
1382 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001383 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
1385 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001386 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001388 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001389 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001392 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1393 {
1394 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001395 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001396 }
1397
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001398 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001399 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1400 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001401 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001402 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1403 &md_ctx ) ) != 0 )
1404 {
1405 mbedtls_md_free( &md_ctx );
1406 goto cleanup;
1407 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001410
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001411 /* Generate lHash */
1412 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1413 goto cleanup;
1414
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001415 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001416 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001417 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001418 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001419 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001421 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001422
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001423 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001424
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001425 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001426 for( i = 0; i < hlen; i++ )
1427 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001428
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001429 /* Get zero-padding len, but always read till end of buffer
1430 * (minus one, for the 01 byte) */
1431 pad_len = 0;
1432 pad_done = 0;
1433 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1434 {
1435 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001436 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001437 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001438
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001439 p += pad_len;
1440 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001441
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001442 /*
1443 * The only information "leaked" is whether the padding was correct or not
1444 * (eg, no data is copied if it was not correct). This meets the
1445 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1446 * the different error conditions.
1447 */
1448 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001449 {
1450 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1451 goto cleanup;
1452 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001453
Paul Bakker66d5d072014-06-17 16:39:18 +02001454 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001455 {
1456 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1457 goto cleanup;
1458 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001459
1460 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001461 if( *olen != 0 )
1462 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001463 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001464
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001465cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001466 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1467 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001468
1469 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001474/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1475 *
1476 * \param value The value to analyze.
1477 * \return Zero if \p value is zero, otherwise all-bits-one.
1478 */
1479static unsigned all_or_nothing_int( unsigned value )
1480{
1481 /* MSVC has a warning about unary minus on unsigned, but this is
1482 * well-defined and precisely what we want to do here */
1483#if defined(_MSC_VER)
1484#pragma warning( push )
1485#pragma warning( disable : 4146 )
1486#endif
1487 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1488#if defined(_MSC_VER)
1489#pragma warning( pop )
1490#endif
1491}
1492
1493/** Check whether a size is out of bounds, without branches.
1494 *
1495 * This is equivalent to `size > max`, but is likely to be compiled to
1496 * to code using bitwise operation rather than a branch.
1497 *
1498 * \param size Size to check.
1499 * \param max Maximum desired value for \p size.
1500 * \return \c 0 if `size <= max`.
1501 * \return \c 1 if `size > max`.
1502 */
1503static unsigned size_greater_than( size_t size, size_t max )
1504{
1505 /* Return the sign bit (1 for negative) of (max - size). */
1506 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1507}
1508
1509/** Choose between two integer values, without branches.
1510 *
1511 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1512 * to code using bitwise operation rather than a branch.
1513 *
1514 * \param cond Condition to test.
1515 * \param if1 Value to use if \p cond is nonzero.
1516 * \param if0 Value to use if \p cond is zero.
1517 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1518 */
1519static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
1520{
1521 unsigned mask = all_or_nothing_int( cond );
1522 return( ( mask & if1 ) | (~mask & if0 ) );
1523}
1524
1525/** Shift some data towards the left inside a buffer without leaking
1526 * the length of the data through side channels.
1527 *
1528 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1529 * ```
1530 * memmove(start, start + offset, total - offset);
1531 * memset(start + offset, 0, total - offset);
1532 * ```
1533 * but it strives to use a memory access pattern (and thus total timing)
1534 * that does not depend on \p offset. This timing independence comes at
1535 * the expense of performance.
1536 *
1537 * \param start Pointer to the start of the buffer.
1538 * \param total Total size of the buffer.
1539 * \param offset Offset from which to copy \p total - \p offset bytes.
1540 */
1541static void mem_move_to_left( void *start,
1542 size_t total,
1543 size_t offset )
1544{
1545 volatile unsigned char *buf = start;
1546 size_t i, n;
1547 if( total == 0 )
1548 return;
1549 for( i = 0; i < total; i++ )
1550 {
1551 unsigned no_op = size_greater_than( total - offset, i );
1552 /* The first `total - offset` passes are a no-op. The last
1553 * `offset` passes shift the data one byte to the left and
1554 * zero out the last byte. */
1555 for( n = 0; n < total - 1; n++ )
1556 {
1557 unsigned char current = buf[n];
1558 unsigned char next = buf[n+1];
1559 buf[n] = if_int( no_op, current, next );
1560 }
1561 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1562 }
1563}
1564
Paul Bakkerb3869132013-02-28 17:21:01 +01001565/*
1566 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001569 int (*f_rng)(void *, unsigned char *, size_t),
1570 void *p_rng,
Thomas Daubney34733082021-05-12 09:24:29 +01001571 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001572 const unsigned char *input,
1573 unsigned char *output,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001574 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001575{
Janos Follath24eed8d2019-11-22 13:21:35 +00001576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001577 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001579 /* The following variables take sensitive values: their value must
1580 * not leak into the observable behavior of the function other than
1581 * the designated outputs (output, olen, return value). Otherwise
1582 * this would open the execution of the function to
1583 * side-channel-based variants of the Bleichenbacher padding oracle
1584 * attack. Potential side channels include overall timing, memory
1585 * access patterns (especially visible to an adversary who has access
1586 * to a shared memory cache), and branches (especially visible to
1587 * an adversary who has access to a shared code cache or to a shared
1588 * branch predictor). */
1589 size_t pad_count = 0;
1590 unsigned bad = 0;
1591 unsigned char pad_done = 0;
1592 size_t plaintext_size = 0;
1593 unsigned output_too_large;
1594
1595 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001596 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1597 RSA_VALIDATE_RET( input != NULL );
1598 RSA_VALIDATE_RET( olen != NULL );
1599
1600 ilen = ctx->len;
1601 plaintext_max_size = ( output_max_len > ilen - 11 ?
1602 ilen - 11 :
1603 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001604
Thomas Daubney34733082021-05-12 09:24:29 +01001605 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001607
Paul Bakkerb3869132013-02-28 17:21:01 +01001608 if( ilen < 16 || ilen > sizeof( buf ) )
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
Thomas Daubney34733082021-05-12 09:24:29 +01001611 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001612
1613 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001614 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001615
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001616 /* Check and get padding length in constant time and constant
1617 * memory trace. The first byte must be 0. */
1618 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001619
Paul Bakker5121ce52009-01-03 21:22:43 +00001620
Thomas Daubney34733082021-05-12 09:24:29 +01001621 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1622 * where PS must be at least 8 nonzero bytes. */
1623 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001624
Thomas Daubney34733082021-05-12 09:24:29 +01001625 /* Read the whole buffer. Set pad_done to nonzero if we find
1626 * the 0x00 byte and remember the padding length in pad_count. */
1627 for( i = 2; i < ilen; i++ )
1628 {
1629 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
1630 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001631 }
1632
Thomas Daubney34733082021-05-12 09:24:29 +01001633
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001634 /* If pad_done is still zero, there's no data, only unfinished padding. */
1635 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001636
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001637 /* There must be at least 8 bytes of padding. */
1638 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001639
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001640 /* If the padding is valid, set plaintext_size to the number of
1641 * remaining bytes after stripping the padding. If the padding
1642 * is invalid, avoid leaking this fact through the size of the
1643 * output: use the maximum message size that fits in the output
1644 * buffer. Do it without branches to avoid leaking the padding
1645 * validity through timing. RSA keys are small enough that all the
1646 * size_t values involved fit in unsigned int. */
1647 plaintext_size = if_int( bad,
1648 (unsigned) plaintext_max_size,
1649 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001650
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001651 /* Set output_too_large to 0 if the plaintext fits in the output
1652 * buffer and to 1 otherwise. */
1653 output_too_large = size_greater_than( plaintext_size,
1654 plaintext_max_size );
1655
1656 /* Set ret without branches to avoid timing attacks. Return:
1657 * - INVALID_PADDING if the padding is bad (bad != 0).
1658 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1659 * plaintext does not fit in the output buffer.
1660 * - 0 if the padding is correct. */
1661 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1662 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1663 0 ) );
1664
1665 /* If the padding is bad or the plaintext is too large, zero the
1666 * data that we're about to copy to the output buffer.
1667 * We need to copy the same amount of data
1668 * from the same buffer whether the padding is good or not to
1669 * avoid leaking the padding validity through overall timing or
1670 * through memory or cache access patterns. */
1671 bad = all_or_nothing_int( bad | output_too_large );
1672 for( i = 11; i < ilen; i++ )
1673 buf[i] &= ~bad;
1674
1675 /* If the plaintext is too large, truncate it to the buffer size.
1676 * Copy anyway to avoid revealing the length through timing, because
1677 * revealing the length is as bad as revealing the padding validity
1678 * for a Bleichenbacher attack. */
1679 plaintext_size = if_int( output_too_large,
1680 (unsigned) plaintext_max_size,
1681 (unsigned) plaintext_size );
1682
1683 /* Move the plaintext to the leftmost position where it can start in
1684 * the working buffer, i.e. make it start plaintext_max_size from
1685 * the end of the buffer. Do this with a memory access trace that
1686 * does not depend on the plaintext size. After this move, the
1687 * starting location of the plaintext is no longer sensitive
1688 * information. */
1689 mem_move_to_left( buf + ilen - plaintext_max_size,
1690 plaintext_max_size,
1691 plaintext_max_size - plaintext_size );
1692
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001693 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1694 * buffer. If output_max_len is 0, then output may be an invalid pointer
1695 * and the result of memcpy() would be undefined; prevent undefined
1696 * behavior making sure to depend only on output_max_len (the size of the
1697 * user-provided output buffer), which is independent from plaintext
1698 * length, validity of padding, success of the decryption, and other
1699 * secrets. */
1700 if( output_max_len != 0 )
1701 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001702
1703 /* Report the amount of data we copied to the output buffer. In case
1704 * of errors (bad padding or output too large), the value of *olen
1705 * when this function returns is not specified. Making it equivalent
1706 * to the good case limits the risks of leaking the padding validity. */
1707 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001708
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001709cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001710 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001711
1712 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001713}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
1716/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001717 * Do an RSA operation, then remove the message padding
1718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001720 int (*f_rng)(void *, unsigned char *, size_t),
1721 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001722 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001723 const unsigned char *input,
1724 unsigned char *output,
1725 size_t output_max_len)
1726{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001727 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001728 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1729 RSA_VALIDATE_RET( input != NULL );
1730 RSA_VALIDATE_RET( olen != NULL );
1731
Paul Bakkerb3869132013-02-28 17:21:01 +01001732 switch( ctx->padding )
1733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#if defined(MBEDTLS_PKCS1_V15)
1735 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney34733082021-05-12 09:24:29 +01001736 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001737 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001738#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740#if defined(MBEDTLS_PKCS1_V21)
1741 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001742 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001743 olen, input, output,
1744 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001745#endif
1746
1747 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001749 }
1750}
1751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001753static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001754 int (*f_rng)(void *, unsigned char *, size_t),
1755 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001757 unsigned int hashlen,
1758 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001759 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001760 unsigned char *sig )
1761{
1762 size_t olen;
1763 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001764 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001765 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001767 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 const mbedtls_md_info_t *md_info;
1769 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001770 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001771 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1772 hashlen == 0 ) ||
1773 hash != NULL );
1774 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001775
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001776 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1777 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1778
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001779 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001781
1782 olen = ctx->len;
1783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001785 {
Simon Butcher02037452016-03-01 21:19:12 +00001786 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001788 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001792 }
1793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001795 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001799
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001800 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1801 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001802 /* Calculate the largest possible salt length, up to the hash size.
1803 * Normally this is the hash length, which is the maximum salt length
1804 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001805 * enough room, use the maximum salt length that fits. The constraint is
1806 * that the hash length plus the salt length plus 2 bytes must be at most
1807 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1808 * (PKCS#1 v2.2) §9.1.1 step 3. */
1809 min_slen = hlen - 2;
1810 if( olen < hlen + min_slen + 2 )
1811 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1812 else if( olen >= hlen + hlen + 2 )
1813 slen = hlen;
1814 else
1815 slen = olen - hlen - 2;
1816 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001817 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001820 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001821 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001822 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001823 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001824 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001825
1826 memset( sig, 0, olen );
1827
Simon Butcher02037452016-03-01 21:19:12 +00001828 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001829 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001830 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001831 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001832
1833 /* Generate salt of length slen in place in the encoded message */
1834 salt = p;
1835 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001836 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001837
Paul Bakkerb3869132013-02-28 17:21:01 +01001838 p += slen;
1839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001841 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001842 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001843
Simon Butcher02037452016-03-01 21:19:12 +00001844 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001845 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1846 goto exit;
1847 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1848 goto exit;
1849 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1850 goto exit;
1851 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1852 goto exit;
1853 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1854 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001855
Simon Butcher02037452016-03-01 21:19:12 +00001856 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001857 if( msb % 8 == 0 )
1858 offset = 1;
1859
Simon Butcher02037452016-03-01 21:19:12 +00001860 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001861 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1862 &md_ctx ) ) != 0 )
1863 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001864
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001865 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001866 sig[0] &= 0xFF >> ( olen * 8 - msb );
1867
1868 p += hlen;
1869 *p++ = 0xBC;
1870
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001871exit:
1872 mbedtls_md_free( &md_ctx );
1873
1874 if( ret != 0 )
1875 return( ret );
1876
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001877 return mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001878}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001879
1880/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001881 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1882 * the option to pass in the salt length.
1883 */
1884int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1885 int (*f_rng)(void *, unsigned char *, size_t),
1886 void *p_rng,
1887 mbedtls_md_type_t md_alg,
1888 unsigned int hashlen,
1889 const unsigned char *hash,
1890 int saltlen,
1891 unsigned char *sig )
1892{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001893 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001894 hashlen, hash, saltlen, sig );
1895}
1896
1897
1898/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001899 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1900 */
1901int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1902 int (*f_rng)(void *, unsigned char *, size_t),
1903 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001904 mbedtls_md_type_t md_alg,
1905 unsigned int hashlen,
1906 const unsigned char *hash,
1907 unsigned char *sig )
1908{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001909 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001910 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001911}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001915/*
1916 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1917 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001918
1919/* Construct a PKCS v1.5 encoding of a hashed message
1920 *
1921 * This is used both for signature generation and verification.
1922 *
1923 * Parameters:
1924 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001925 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001926 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001927 * - hash: Buffer containing the hashed message or the raw data.
1928 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001929 * - dst: Buffer to hold the encoded message.
1930 *
1931 * Assumptions:
1932 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1933 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001934 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001935 *
1936 */
1937static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1938 unsigned int hashlen,
1939 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001940 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001941 unsigned char *dst )
1942{
1943 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001944 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001945 unsigned char *p = dst;
1946 const char *oid = NULL;
1947
1948 /* Are we signing hashed or raw data? */
1949 if( md_alg != MBEDTLS_MD_NONE )
1950 {
1951 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1952 if( md_info == NULL )
1953 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1954
1955 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1956 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1957
1958 hashlen = mbedtls_md_get_size( md_info );
1959
1960 /* Double-check that 8 + hashlen + oid_size can be used as a
1961 * 1-byte ASN.1 length encoding and that there's no overflow. */
1962 if( 8 + hashlen + oid_size >= 0x80 ||
1963 10 + hashlen < hashlen ||
1964 10 + hashlen + oid_size < 10 + hashlen )
1965 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1966
1967 /*
1968 * Static bounds check:
1969 * - Need 10 bytes for five tag-length pairs.
1970 * (Insist on 1-byte length encodings to protect against variants of
1971 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1972 * - Need hashlen bytes for hash
1973 * - Need oid_size bytes for hash alg OID.
1974 */
1975 if( nb_pad < 10 + hashlen + oid_size )
1976 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1977 nb_pad -= 10 + hashlen + oid_size;
1978 }
1979 else
1980 {
1981 if( nb_pad < hashlen )
1982 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1983
1984 nb_pad -= hashlen;
1985 }
1986
Hanno Becker2b2f8982017-09-27 17:10:03 +01001987 /* Need space for signature header and padding delimiter (3 bytes),
1988 * and 8 bytes for the minimal padding */
1989 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001990 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1991 nb_pad -= 3;
1992
1993 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001994 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001995
1996 /* Write signature header and padding */
1997 *p++ = 0;
1998 *p++ = MBEDTLS_RSA_SIGN;
1999 memset( p, 0xFF, nb_pad );
2000 p += nb_pad;
2001 *p++ = 0;
2002
2003 /* Are we signing raw data? */
2004 if( md_alg == MBEDTLS_MD_NONE )
2005 {
2006 memcpy( p, hash, hashlen );
2007 return( 0 );
2008 }
2009
2010 /* Signing hashed data, add corresponding ASN.1 structure
2011 *
2012 * DigestInfo ::= SEQUENCE {
2013 * digestAlgorithm DigestAlgorithmIdentifier,
2014 * digest Digest }
2015 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2016 * Digest ::= OCTET STRING
2017 *
2018 * Schematic:
2019 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2020 * TAG-NULL + LEN [ NULL ] ]
2021 * TAG-OCTET + LEN [ HASH ] ]
2022 */
2023 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002024 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002025 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002026 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002027 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002028 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002029 memcpy( p, oid, oid_size );
2030 p += oid_size;
2031 *p++ = MBEDTLS_ASN1_NULL;
2032 *p++ = 0x00;
2033 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002034 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002035 memcpy( p, hash, hashlen );
2036 p += hashlen;
2037
2038 /* Just a sanity-check, should be automatic
2039 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002040 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002041 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002042 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002043 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2044 }
2045
2046 return( 0 );
2047}
2048
Paul Bakkerb3869132013-02-28 17:21:01 +01002049/*
2050 * Do an RSA operation to sign the message digest
2051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002053 int (*f_rng)(void *, unsigned char *, size_t),
2054 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002056 unsigned int hashlen,
2057 const unsigned char *hash,
2058 unsigned char *sig )
2059{
Janos Follath24eed8d2019-11-22 13:21:35 +00002060 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002061 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002062
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002063 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002064 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2065 hashlen == 0 ) ||
2066 hash != NULL );
2067 RSA_VALIDATE_RET( sig != NULL );
2068
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002069 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2070 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2071
Hanno Beckerfdf38032017-09-06 12:35:55 +01002072 /*
2073 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2074 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002075
Hanno Beckerfdf38032017-09-06 12:35:55 +01002076 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2077 ctx->len, sig ) ) != 0 )
2078 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002079
Hanno Beckerfdf38032017-09-06 12:35:55 +01002080 /* Private key operation
2081 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002082 * In order to prevent Lenstra's attack, make the signature in a
2083 * temporary buffer and check it before returning it.
2084 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002085
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002086 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002087 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002088 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2089
Hanno Beckerfdf38032017-09-06 12:35:55 +01002090 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002091 if( verif == NULL )
2092 {
2093 mbedtls_free( sig_try );
2094 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2095 }
2096
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002097 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2098 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2099
Hanno Becker171a8f12017-09-06 12:32:16 +01002100 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002101 {
2102 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2103 goto cleanup;
2104 }
2105
2106 memcpy( sig, sig_try, ctx->len );
2107
2108cleanup:
2109 mbedtls_free( sig_try );
2110 mbedtls_free( verif );
2111
2112 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002113}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002115
2116/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 * Do an RSA operation to sign the message digest
2118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002120 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002121 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002123 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002124 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 unsigned char *sig )
2126{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002127 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002128 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2129 hashlen == 0 ) ||
2130 hash != NULL );
2131 RSA_VALIDATE_RET( sig != NULL );
2132
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 switch( ctx->padding )
2134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135#if defined(MBEDTLS_PKCS1_V15)
2136 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney52654982021-05-18 16:54:00 +01002137 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng,
Thomas Daubney140184d2021-05-18 16:04:07 +01002138 md_alg, hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002139#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141#if defined(MBEDTLS_PKCS1_V21)
2142 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyde9fdc42021-05-18 17:10:04 +01002143 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
2144 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002145#endif
2146
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002150}
2151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002153/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002154 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002158 unsigned int hashlen,
2159 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002161 int expected_salt_len,
2162 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002163{
Janos Follath24eed8d2019-11-22 13:21:35 +00002164 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002165 size_t siglen;
2166 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002167 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002169 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002170 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002171 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 const mbedtls_md_info_t *md_info;
2173 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002174 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002175
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002176 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002177 RSA_VALIDATE_RET( sig != NULL );
2178 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2179 hashlen == 0 ) ||
2180 hash != NULL );
2181
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 siglen = ctx->len;
2183
Paul Bakker27fdf462011-06-09 13:55:13 +00002184 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Thomas Daubney782a7f52021-05-19 12:27:35 +01002187 ret = mbedtls_rsa_public( ctx, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002188
2189 if( ret != 0 )
2190 return( ret );
2191
2192 p = buf;
2193
Paul Bakkerb3869132013-02-28 17:21:01 +01002194 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 {
Simon Butcher02037452016-03-01 21:19:12 +00002199 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002201 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002205 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002208 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002212
Paul Bakkerb3869132013-02-28 17:21:01 +01002213 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002214
Simon Butcher02037452016-03-01 21:19:12 +00002215 /*
2216 * Note: EMSA-PSS verification is over the length of N - 1 bits
2217 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002218 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002219
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002220 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2221 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2222
Simon Butcher02037452016-03-01 21:19:12 +00002223 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002224 if( msb % 8 == 0 )
2225 {
2226 p++;
2227 siglen -= 1;
2228 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002229
Gilles Peskine139108a2017-10-18 19:03:42 +02002230 if( siglen < hlen + 2 )
2231 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2232 hash_start = p + siglen - hlen - 1;
2233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002235 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002236 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002237
Jaeden Amero66954e12018-01-25 16:05:54 +00002238 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2239 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002240 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002241
Paul Bakkerb3869132013-02-28 17:21:01 +01002242 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002243
Gilles Peskine6a54b022017-10-17 19:02:13 +02002244 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002245 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002246
Gilles Peskine91048a32017-10-19 17:46:14 +02002247 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002248 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002249 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2250 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002251 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002252
Gilles Peskine6a54b022017-10-17 19:02:13 +02002253 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002256 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002257 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002258 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2259 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002260 }
2261
Simon Butcher02037452016-03-01 21:19:12 +00002262 /*
2263 * Generate H = Hash( M' )
2264 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002265 ret = mbedtls_md_starts( &md_ctx );
2266 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002267 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002268 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
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, hash, hashlen );
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, p, observed_salt_len );
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_finish( &md_ctx, result );
2278 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002279 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002280
Jaeden Amero66954e12018-01-25 16:05:54 +00002281 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002282 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002283 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002284 goto exit;
2285 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002286
2287exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002289
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002290 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002291}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002292
2293/*
2294 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002298 unsigned int hashlen,
2299 const unsigned char *hash,
2300 const unsigned char *sig )
2301{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002302 mbedtls_md_type_t mgf1_hash_id;
2303 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002304 RSA_VALIDATE_RET( sig != NULL );
2305 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2306 hashlen == 0 ) ||
2307 hash != NULL );
2308
2309 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002311 : md_alg;
2312
Thomas Daubney9e65f792021-05-19 12:18:58 +01002313 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx,
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002314 md_alg, hashlen, hash,
2315 mgf1_hash_id,
2316 MBEDTLS_RSA_SALT_LEN_ANY,
2317 sig ) );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002318
2319}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002323/*
2324 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002328 unsigned int hashlen,
2329 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002330 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002331{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002332 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002333 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002334 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002335
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002336 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002337 RSA_VALIDATE_RET( sig != NULL );
2338 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2339 hashlen == 0 ) ||
2340 hash != NULL );
2341
2342 sig_len = ctx->len;
2343
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002344 /*
2345 * Prepare expected PKCS1 v1.5 encoding of hash.
2346 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002347
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002348 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2349 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2350 {
2351 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2352 goto cleanup;
2353 }
2354
2355 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2356 encoded_expected ) ) != 0 )
2357 goto cleanup;
2358
2359 /*
2360 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2361 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002362
Thomas Daubney41e4ce42021-05-19 15:10:05 +01002363 ret = mbedtls_rsa_public( ctx, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002364 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002365 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002366
Simon Butcher02037452016-03-01 21:19:12 +00002367 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002368 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002369 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002370
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002371 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2372 sig_len ) ) != 0 )
2373 {
2374 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2375 goto cleanup;
2376 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002377
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002378cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002379
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002380 if( encoded != NULL )
2381 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002382 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002383 mbedtls_free( encoded );
2384 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002385
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002386 if( encoded_expected != NULL )
2387 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002388 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002389 mbedtls_free( encoded_expected );
2390 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002391
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002392 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002393}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002395
2396/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002397 * Do an RSA operation and check the message digest
2398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002401 unsigned int hashlen,
2402 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002403 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002404{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002405 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002406 RSA_VALIDATE_RET( sig != NULL );
2407 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2408 hashlen == 0 ) ||
2409 hash != NULL );
2410
Paul Bakkerb3869132013-02-28 17:21:01 +01002411 switch( ctx->padding )
2412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413#if defined(MBEDTLS_PKCS1_V15)
2414 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney2e126252021-05-19 11:48:53 +01002415 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, md_alg,
2416 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002417#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419#if defined(MBEDTLS_PKCS1_V21)
2420 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002421 return mbedtls_rsa_rsassa_pss_verify( ctx, md_alg,
2422 hashlen, hash, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01002423#endif
2424
2425 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002427 }
2428}
2429
2430/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002431 * Copy the components of an RSA key
2432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002434{
Janos Follath24eed8d2019-11-22 13:21:35 +00002435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002436 RSA_VALIDATE_RET( dst != NULL );
2437 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002438
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002439 dst->len = src->len;
2440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2442 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2445 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2446 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002447
2448#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2450 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2451 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2453 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002454#endif
2455
2456 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2459 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002460
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002461 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002462 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002463
2464cleanup:
2465 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002467
2468 return( ret );
2469}
2470
2471/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 * Free the components of an RSA key
2473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002475{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002476 if( ctx == NULL )
2477 return;
2478
2479 mbedtls_mpi_free( &ctx->Vi );
2480 mbedtls_mpi_free( &ctx->Vf );
2481 mbedtls_mpi_free( &ctx->RN );
2482 mbedtls_mpi_free( &ctx->D );
2483 mbedtls_mpi_free( &ctx->Q );
2484 mbedtls_mpi_free( &ctx->P );
2485 mbedtls_mpi_free( &ctx->E );
2486 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002487
Hanno Becker33c30a02017-08-23 07:00:22 +01002488#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002489 mbedtls_mpi_free( &ctx->RQ );
2490 mbedtls_mpi_free( &ctx->RP );
2491 mbedtls_mpi_free( &ctx->QP );
2492 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002493 mbedtls_mpi_free( &ctx->DP );
2494#endif /* MBEDTLS_RSA_NO_CRT */
2495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002497 /* Free the mutex, but only if it hasn't been freed already. */
2498 if( ctx->ver != 0 )
2499 {
2500 mbedtls_mutex_free( &ctx->mutex );
2501 ctx->ver = 0;
2502 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002503#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002504}
2505
Hanno Beckerab377312017-08-23 16:24:51 +01002506#endif /* !MBEDTLS_RSA_ALT */
2507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002510#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
2512/*
2513 * Example RSA-1024 keypair, for test purposes
2514 */
2515#define KEY_LEN 128
2516
2517#define RSA_N "9292758453063D803DD603D5E777D788" \
2518 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2519 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2520 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2521 "93A89813FBF3C4F8066D2D800F7C38A8" \
2522 "1AE31942917403FF4946B0A83D3D3E05" \
2523 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2524 "5E94BB77B07507233A0BC7BAC8F90F79"
2525
2526#define RSA_E "10001"
2527
2528#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2529 "66CA472BC44D253102F8B4A9D3BFA750" \
2530 "91386C0077937FE33FA3252D28855837" \
2531 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2532 "DF79C5CE07EE72C7F123142198164234" \
2533 "CABB724CF78B8173B9F880FC86322407" \
2534 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2535 "071513A1E85B5DFA031F21ECAE91A34D"
2536
2537#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2538 "2C01CAD19EA484A87EA4377637E75500" \
2539 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2540 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2541
2542#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2543 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2544 "910E4168387E3C30AA1E00C339A79508" \
2545 "8452DD96A9A5EA5D9DCA68DA636032AF"
2546
Paul Bakker5121ce52009-01-03 21:22:43 +00002547#define PT_LEN 24
2548#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2549 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002552static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002553{
gufe44c2620da2020-08-03 17:56:50 +02002554#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002555 size_t i;
2556
Paul Bakker545570e2010-07-18 09:00:25 +00002557 if( rng_state != NULL )
2558 rng_state = NULL;
2559
Paul Bakkera3d195c2011-11-27 21:07:34 +00002560 for( i = 0; i < len; ++i )
2561 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002562#else
2563 if( rng_state != NULL )
2564 rng_state = NULL;
2565
2566 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002567#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002568
Paul Bakkera3d195c2011-11-27 21:07:34 +00002569 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002572
Paul Bakker5121ce52009-01-03 21:22:43 +00002573/*
2574 * Checkup routine
2575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002577{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002578 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002580 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 unsigned char rsa_plaintext[PT_LEN];
2583 unsigned char rsa_decrypted[PT_LEN];
2584 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002586 unsigned char sha1sum[20];
2587#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Hanno Becker3a701162017-08-22 13:52:43 +01002589 mbedtls_mpi K;
2590
2591 mbedtls_mpi_init( &K );
Ronald Cronc1905a12021-06-05 11:11:14 +02002592 mbedtls_rsa_init( &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
Hanno Becker3a701162017-08-22 13:52:43 +01002594 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2595 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2596 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2597 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2598 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2599 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2600 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2601 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2602 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2603 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2604
Hanno Becker7f25f852017-10-10 16:56:22 +01002605 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
2607 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2611 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 {
2613 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Hanno Becker5bc87292017-05-03 15:09:31 +01002616 ret = 1;
2617 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
2619
2620 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
2623 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2624
Thomas Daubney21772772021-05-13 17:30:32 +01002625 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002626 PT_LEN, rsa_plaintext,
2627 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 {
2629 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Hanno Becker5bc87292017-05-03 15:09:31 +01002632 ret = 1;
2633 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 }
2635
2636 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002639 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002640 &len, rsa_ciphertext, rsa_decrypted,
2641 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 {
2643 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Hanno Becker5bc87292017-05-03 15:09:31 +01002646 ret = 1;
2647 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002648 }
2649
2650 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2651 {
2652 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Hanno Becker5bc87292017-05-03 15:09:31 +01002655 ret = 1;
2656 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 }
2658
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002659 if( verbose != 0 )
2660 mbedtls_printf( "passed\n" );
2661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002664 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002666 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002667 {
2668 if( verbose != 0 )
2669 mbedtls_printf( "failed\n" );
2670
2671 return( 1 );
2672 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Hanno Becker98838b02017-10-02 13:16:10 +01002674 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
Thomas Daubney140184d2021-05-18 16:04:07 +01002675 MBEDTLS_MD_SHA1, 0,
Hanno Becker98838b02017-10-02 13:16:10 +01002676 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 {
2678 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Hanno Becker5bc87292017-05-03 15:09:31 +01002681 ret = 1;
2682 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 }
2684
2685 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Thomas Daubney68d9cbc2021-05-18 18:45:09 +01002688 if( mbedtls_rsa_pkcs1_verify( &rsa, MBEDTLS_MD_SHA1, 0,
Hanno Becker98838b02017-10-02 13:16:10 +01002689 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 {
2691 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Hanno Becker5bc87292017-05-03 15:09:31 +01002694 ret = 1;
2695 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 }
2697
2698 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002699 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002702 if( verbose != 0 )
2703 mbedtls_printf( "\n" );
2704
Paul Bakker3d8fb632014-04-17 12:42:41 +02002705cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002706 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 mbedtls_rsa_free( &rsa );
2708#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002709 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002711 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712}
2713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716#endif /* MBEDTLS_RSA_C */