blob: f1b8489eee743a40a2df2532520aeb6c5acc9eaf [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
Hanno Becker74716312017-10-02 10:00:37 +010019
Paul Bakker5121ce52009-01-03 21:22:43 +000020/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000021 * The following sources were referenced in the design of this implementation
22 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000023 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000024 * [1] A method for obtaining digital signatures and public-key cryptosystems
25 * R Rivest, A Shamir, and L Adleman
26 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
27 *
28 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
29 * Menezes, van Oorschot and Vanstone
30 *
Janos Follathe81102e2017-03-22 13:38:28 +000031 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
32 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
33 * Stefan Mangard
34 * https://arxiv.org/abs/1702.08719v2
35 *
Paul Bakker5121ce52009-01-03 21:22:43 +000036 */
37
Gilles Peskinedb09ef62020-06-03 01:43:33 +020038#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/rsa.h"
Chris Jones66a4cd42021-03-09 16:04:12 +000043#include "rsa_alt_helpers.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000046#include "mbedtls/error.h"
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020047#include "constant_time.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000048
Rich Evans00ab4702015-02-06 13:43:58 +000049#include <string.h>
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
gufe44c2620da2020-08-03 17:56:50 +020055#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000056#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010061#else
Rich Evans00ab4702015-02-06 13:43:58 +000062#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020064#define mbedtls_calloc calloc
65#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010066#endif
67
Hanno Beckera565f542017-10-11 11:00:19 +010068#if !defined(MBEDTLS_RSA_ALT)
69
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050070/* Parameter validation macros */
71#define RSA_VALIDATE_RET( cond ) \
72 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
73#define RSA_VALIDATE( cond ) \
74 MBEDTLS_INTERNAL_VALIDATE( cond )
75
Hanno Becker617c1ae2017-08-23 14:11:24 +010076int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
77 const mbedtls_mpi *N,
78 const mbedtls_mpi *P, const mbedtls_mpi *Q,
79 const mbedtls_mpi *D, const mbedtls_mpi *E )
80{
Janos Follath24eed8d2019-11-22 13:21:35 +000081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050082 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +010083
84 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
85 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
86 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
87 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
88 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
89 {
Chris Jonesb7d02e02021-04-01 17:40:03 +010090 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +010091 }
92
93 if( N != NULL )
94 ctx->len = mbedtls_mpi_size( &ctx->N );
95
96 return( 0 );
97}
98
99int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100100 unsigned char const *N, size_t N_len,
101 unsigned char const *P, size_t P_len,
102 unsigned char const *Q, size_t Q_len,
103 unsigned char const *D, size_t D_len,
104 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100105{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000106 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500107 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100108
109 if( N != NULL )
110 {
111 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
112 ctx->len = mbedtls_mpi_size( &ctx->N );
113 }
114
115 if( P != NULL )
116 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
117
118 if( Q != NULL )
119 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
120
121 if( D != NULL )
122 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
123
124 if( E != NULL )
125 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
126
127cleanup:
128
129 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100130 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100131
132 return( 0 );
133}
134
Hanno Becker705fc682017-10-10 17:57:02 +0100135/*
136 * Checks whether the context fields are set in such a way
137 * that the RSA primitives will be able to execute without error.
138 * It does *not* make guarantees for consistency of the parameters.
139 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100140static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
141 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100142{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100143#if !defined(MBEDTLS_RSA_NO_CRT)
144 /* blinding_needed is only used for NO_CRT to decide whether
145 * P,Q need to be present or not. */
146 ((void) blinding_needed);
147#endif
148
Hanno Becker3a760a12018-01-05 08:14:49 +0000149 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
150 ctx->len > MBEDTLS_MPI_MAX_SIZE )
151 {
Hanno Becker705fc682017-10-10 17:57:02 +0100152 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000153 }
Hanno Becker705fc682017-10-10 17:57:02 +0100154
155 /*
156 * 1. Modular exponentiation needs positive, odd moduli.
157 */
158
159 /* Modular exponentiation wrt. N is always used for
160 * RSA public key operations. */
161 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
162 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
163 {
164 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
165 }
166
167#if !defined(MBEDTLS_RSA_NO_CRT)
168 /* Modular exponentiation for P and Q is only
169 * used for private key operations and if CRT
170 * is used. */
171 if( is_priv &&
172 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
173 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
174 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
175 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
176 {
177 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
178 }
179#endif /* !MBEDTLS_RSA_NO_CRT */
180
181 /*
182 * 2. Exponents must be positive
183 */
184
185 /* Always need E for public key operations */
186 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
187 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
188
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100189#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100190 /* For private key operations, use D or DP & DQ
191 * as (unblinded) exponents. */
192 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
194#else
195 if( is_priv &&
196 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
197 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
198 {
199 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
200 }
201#endif /* MBEDTLS_RSA_NO_CRT */
202
203 /* Blinding shouldn't make exponents negative either,
204 * so check that P, Q >= 1 if that hasn't yet been
205 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100206#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100207 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100208 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
209 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
210 {
211 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
212 }
213#endif
214
215 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100216 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100217#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100218 if( is_priv &&
219 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
220 {
221 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
222 }
223#endif
224
225 return( 0 );
226}
227
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100228int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100229{
230 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500231 int have_N, have_P, have_Q, have_D, have_E;
232#if !defined(MBEDTLS_RSA_NO_CRT)
233 int have_DP, have_DQ, have_QP;
234#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500235 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100236
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500237 RSA_VALIDATE_RET( ctx != NULL );
238
239 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
240 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
241 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
242 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
243 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500244
245#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500246 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
247 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
248 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500249#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100250
Hanno Becker617c1ae2017-08-23 14:11:24 +0100251 /*
252 * Check whether provided parameters are enough
253 * to deduce all others. The following incomplete
254 * parameter sets for private keys are supported:
255 *
256 * (1) P, Q missing.
257 * (2) D and potentially N missing.
258 *
259 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100260
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500261 n_missing = have_P && have_Q && have_D && have_E;
262 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
263 d_missing = have_P && have_Q && !have_D && have_E;
264 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100265
266 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500267 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100268
Hanno Becker617c1ae2017-08-23 14:11:24 +0100269 if( !is_priv && !is_pub )
270 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
271
272 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100273 * Step 1: Deduce N if P, Q are provided.
274 */
275
276 if( !have_N && have_P && have_Q )
277 {
278 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
279 &ctx->Q ) ) != 0 )
280 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100281 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100282 }
283
284 ctx->len = mbedtls_mpi_size( &ctx->N );
285 }
286
287 /*
288 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100289 */
290
291 if( pq_missing )
292 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100293 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100294 &ctx->P, &ctx->Q );
295 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100296 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100297
298 }
299 else if( d_missing )
300 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100301 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
302 &ctx->Q,
303 &ctx->E,
304 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100305 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100306 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100307 }
308 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100309
Hanno Becker617c1ae2017-08-23 14:11:24 +0100310 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100311 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100312 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100313 */
314
Hanno Becker23344b52017-08-23 07:43:27 +0100315#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500316 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100317 {
318 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
319 &ctx->DP, &ctx->DQ, &ctx->QP );
320 if( ret != 0 )
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 }
Hanno Becker23344b52017-08-23 07:43:27 +0100323#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324
325 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100326 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327 */
328
Hanno Beckerebd2c022017-10-12 10:54:53 +0100329 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100330}
331
Hanno Becker617c1ae2017-08-23 14:11:24 +0100332int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
333 unsigned char *N, size_t N_len,
334 unsigned char *P, size_t P_len,
335 unsigned char *Q, size_t Q_len,
336 unsigned char *D, size_t D_len,
337 unsigned char *E, size_t E_len )
338{
339 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500340 int is_priv;
341 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100342
343 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500344 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100345 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
346 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
347 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
348 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
349 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
350
351 if( !is_priv )
352 {
353 /* If we're trying to export private parameters for a public key,
354 * something must be wrong. */
355 if( P != NULL || Q != NULL || D != NULL )
356 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
357
358 }
359
360 if( N != NULL )
361 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
362
363 if( P != NULL )
364 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
365
366 if( Q != NULL )
367 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
368
369 if( D != NULL )
370 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
371
372 if( E != NULL )
373 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100374
375cleanup:
376
377 return( ret );
378}
379
Hanno Becker617c1ae2017-08-23 14:11:24 +0100380int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
381 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
382 mbedtls_mpi *D, mbedtls_mpi *E )
383{
Janos Follath24eed8d2019-11-22 13:21:35 +0000384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500385 int is_priv;
386 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100387
388 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500389 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100390 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
391 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
392 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
393 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
394 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
395
396 if( !is_priv )
397 {
398 /* If we're trying to export private parameters for a public key,
399 * something must be wrong. */
400 if( P != NULL || Q != NULL || D != NULL )
401 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
402
403 }
404
405 /* Export all requested core parameters. */
406
407 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
408 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
409 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
410 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
411 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
412 {
413 return( ret );
414 }
415
416 return( 0 );
417}
418
419/*
420 * Export CRT parameters
421 * This must also be implemented if CRT is not used, for being able to
422 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
423 * can be used in this case.
424 */
425int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
426 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
427{
Janos Follath24eed8d2019-11-22 13:21:35 +0000428 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500429 int is_priv;
430 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100431
432 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500433 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100434 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
435 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
436 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
437 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
438 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
439
440 if( !is_priv )
441 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
442
Hanno Beckerdc95c892017-08-23 06:57:02 +0100443#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100445 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
446 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
447 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
448 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100449 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100450 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100451#else
452 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
453 DP, DQ, QP ) ) != 0 )
454 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100455 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100456 }
457#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100458
459 return( 0 );
460}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100461
Paul Bakker5121ce52009-01-03 21:22:43 +0000462/*
463 * Initialize an RSA context
464 */
Ronald Cronc1905a12021-06-05 11:11:14 +0200465void mbedtls_rsa_init( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000466{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500467 RSA_VALIDATE( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
Ronald Cronc1905a12021-06-05 11:11:14 +0200471 ctx->padding = MBEDTLS_RSA_PKCS_V15;
472 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100475 /* Set ctx->ver to nonzero to indicate that the mutex has been
476 * initialized and will need to be freed. */
477 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200479#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000480}
481
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100482/*
483 * Set padding for an existing RSA context
484 */
Ronald Cronea7631b2021-06-03 18:51:59 +0200485int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
486 mbedtls_md_type_t hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100487{
Ronald Cron3a0375f2021-06-08 10:22:28 +0200488 switch( padding )
489 {
490#if defined(MBEDTLS_PKCS1_V15)
491 case MBEDTLS_RSA_PKCS_V15:
492 break;
493#endif
494
495#if defined(MBEDTLS_PKCS1_V21)
496 case MBEDTLS_RSA_PKCS_V21:
497 break;
498#endif
499 default:
500 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
501 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200502
503 if( ( padding == MBEDTLS_RSA_PKCS_V21 ) &&
504 ( hash_id != MBEDTLS_MD_NONE ) )
505 {
506 const mbedtls_md_info_t *md_info;
507
508 md_info = mbedtls_md_info_from_type( hash_id );
509 if( md_info == NULL )
510 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
511 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500512
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100513 ctx->padding = padding;
514 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200515
516 return( 0 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100517}
518
Hanno Becker617c1ae2017-08-23 14:11:24 +0100519/*
520 * Get length in bytes of RSA modulus
521 */
522
523size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
524{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100525 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100526}
527
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
531/*
532 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800533 *
534 * This generation method follows the RSA key pair generation procedure of
535 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000538 int (*f_rng)(void *, unsigned char *, size_t),
539 void *p_rng,
540 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000541{
Janos Follath24eed8d2019-11-22 13:21:35 +0000542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800543 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100544 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500545 RSA_VALIDATE_RET( ctx != NULL );
546 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Janos Follathb8fc1b02018-09-03 15:37:01 +0100548 /*
549 * If the modulus is 1024 bit long or shorter, then the security strength of
550 * the RSA algorithm is less than or equal to 80 bits and therefore an error
551 * rate of 2^-80 is sufficient.
552 */
553 if( nbits > 1024 )
554 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
555
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100556 mbedtls_mpi_init( &H );
557 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800558 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100560 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
561 {
562 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
563 goto cleanup;
564 }
565
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 /*
567 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800568 * 1. |P-Q| > 2^( nbits / 2 - 100 )
569 * 2. GCD( E, (P-1)*(Q-1) ) == 1
570 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 do
575 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100576 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
577 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Janos Follathb8fc1b02018-09-03 15:37:01 +0100579 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
580 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800582 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
583 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
584 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 continue;
586
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800587 /* not required by any standards, but some users rely on the fact that P > Q */
588 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100589 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100590
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100591 /* Temporarily replace P,Q by P-1, Q-1 */
592 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
593 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
594 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800595
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800596 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800598 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
599 continue;
600
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800601 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Jethro Beekman97f95c92018-02-13 15:50:36 -0800602 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
603 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
604 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
605
606 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
607 continue;
608
609 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800611 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100613 /* Restore P,Q */
614 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
615 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
616
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800617 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
618
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100619 ctx->len = mbedtls_mpi_size( &ctx->N );
620
Jethro Beekman97f95c92018-02-13 15:50:36 -0800621#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 * DP = D mod (P - 1)
624 * DQ = D mod (Q - 1)
625 * QP = Q^-1 mod P
626 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100627 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
628 &ctx->DP, &ctx->DQ, &ctx->QP ) );
629#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
Hanno Becker83aad1f2017-08-23 06:45:10 +0100631 /* Double-check */
632 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
634cleanup:
635
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100636 mbedtls_mpi_free( &H );
637 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800638 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640 if( ret != 0 )
641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100643
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100644 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100645 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100646 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 }
648
Paul Bakker48377d92013-08-30 12:06:24 +0200649 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000650}
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
654/*
655 * Check a public RSA key
656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000658{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500659 RSA_VALIDATE_RET( ctx != NULL );
660
Hanno Beckerebd2c022017-10-12 10:54:53 +0100661 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000663
Hanno Becker3a760a12018-01-05 08:14:49 +0000664 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100667 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
Hanno Becker705fc682017-10-10 17:57:02 +0100669 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
670 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100674 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676 return( 0 );
677}
678
679/*
Hanno Becker705fc682017-10-10 17:57:02 +0100680 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500684 RSA_VALIDATE_RET( ctx != NULL );
685
Hanno Becker705fc682017-10-10 17:57:02 +0100686 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100687 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 {
Hanno Becker98838b02017-10-02 13:16:10 +0100689 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 }
Paul Bakker48377d92013-08-30 12:06:24 +0200691
Hanno Becker98838b02017-10-02 13:16:10 +0100692 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100693 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100695 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000697
Hanno Beckerb269a852017-08-25 08:03:21 +0100698#if !defined(MBEDTLS_RSA_NO_CRT)
699 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
700 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
701 {
702 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
703 }
704#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000705
706 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707}
708
709/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100710 * Check if contexts holding a public and private key match
711 */
Hanno Becker98838b02017-10-02 13:16:10 +0100712int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
713 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100714{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500715 RSA_VALIDATE_RET( pub != NULL );
716 RSA_VALIDATE_RET( prv != NULL );
717
Hanno Becker98838b02017-10-02 13:16:10 +0100718 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100722 }
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
725 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100728 }
729
730 return( 0 );
731}
732
733/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 * Do an RSA public key operation
735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000737 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 unsigned char *output )
739{
Janos Follath24eed8d2019-11-22 13:21:35 +0000740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000741 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500743 RSA_VALIDATE_RET( ctx != NULL );
744 RSA_VALIDATE_RET( input != NULL );
745 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Hanno Beckerebd2c022017-10-12 10:54:53 +0100747 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100748 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200752#if defined(MBEDTLS_THREADING_C)
753 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
754 return( ret );
755#endif
756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200761 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
762 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000763 }
764
765 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
767 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
769cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200771 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
772 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100773#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000776
777 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100778 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
780 return( 0 );
781}
782
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200783/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200784 * Generate or update blinding values, see section 10 of:
785 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200786 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200787 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200788 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200789static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200790 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
791{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200792 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200793 mbedtls_mpi R;
794
795 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200796
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200797 if( ctx->Vf.p != NULL )
798 {
799 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
801 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
802 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
803 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200804
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200805 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200806 }
807
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200808 /* Unblinding value: Vf = random number, invertible mod N */
809 do {
810 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200811 {
812 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
813 goto cleanup;
814 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200817
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200818 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200819 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
820 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
821 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
822
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200823 /* At this point, Vi is invertible mod N if and only if both Vf and R
824 * are invertible mod N. If one of them isn't, we don't need to know
825 * which one, we just loop and choose new values for both of them.
826 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200827 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500828 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200829 goto cleanup;
830
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500831 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
832
833 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
834 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
835 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200836
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200837 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200838 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200840
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200841
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200842cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200843 mbedtls_mpi_free( &R );
844
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200845 return( ret );
846}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200847
Paul Bakker5121ce52009-01-03 21:22:43 +0000848/*
Janos Follathe81102e2017-03-22 13:38:28 +0000849 * Exponent blinding supposed to prevent side-channel attacks using multiple
850 * traces of measurements to recover the RSA key. The more collisions are there,
851 * the more bits of the key can be recovered. See [3].
852 *
853 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
854 * observations on avarage.
855 *
856 * For example with 28 byte blinding to achieve 2 collisions the adversary has
857 * to make 2^112 observations on avarage.
858 *
859 * (With the currently (as of 2017 April) known best algorithms breaking 2048
860 * bit RSA requires approximately as much time as trying out 2^112 random keys.
861 * Thus in this sense with 28 byte blinding the security is not reduced by
862 * side-channel attacks like the one in [3])
863 *
864 * This countermeasure does not help if the key recovery is possible with a
865 * single trace.
866 */
867#define RSA_EXPONENT_BLINDING 28
868
869/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000870 * Do an RSA private key operation
871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200873 int (*f_rng)(void *, unsigned char *, size_t),
874 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000875 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 unsigned char *output )
877{
Janos Follath24eed8d2019-11-22 13:21:35 +0000878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000879 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100880
881 /* Temporary holding the result */
882 mbedtls_mpi T;
883
884 /* Temporaries holding P-1, Q-1 and the
885 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000886 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100887
888#if !defined(MBEDTLS_RSA_NO_CRT)
889 /* Temporaries holding the results mod p resp. mod q. */
890 mbedtls_mpi TP, TQ;
891
892 /* Temporaries holding the blinded exponents for
893 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000894 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100895
896 /* Pointers to actual exponents to be used - either the unblinded
897 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000898 mbedtls_mpi *DP = &ctx->DP;
899 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100900#else
901 /* Temporary holding the blinded exponent (if used). */
902 mbedtls_mpi D_blind;
903
904 /* Pointer to actual exponent to be used - either the unblinded
905 * or the blinded one, depending on the presence of a PRNG. */
906 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100907#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100908
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100909 /* Temporaries holding the initial input and the double
910 * checked result; should be the same in the end. */
911 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500913 RSA_VALIDATE_RET( ctx != NULL );
914 RSA_VALIDATE_RET( input != NULL );
915 RSA_VALIDATE_RET( output != NULL );
916
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200917 if( f_rng == NULL )
918 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
919
920 if( rsa_check_context( ctx, 1 /* private key checks */,
921 1 /* blinding on */ ) != 0 )
Hanno Beckerebd2c022017-10-12 10:54:53 +0100922 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100923 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100924 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100925
Hanno Becker06811ce2017-05-03 15:10:34 +0100926#if defined(MBEDTLS_THREADING_C)
927 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
928 return( ret );
929#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000930
Hanno Becker06811ce2017-05-03 15:10:34 +0100931 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100932 mbedtls_mpi_init( &T );
933
934 mbedtls_mpi_init( &P1 );
935 mbedtls_mpi_init( &Q1 );
936 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000937
Janos Follathe81102e2017-03-22 13:38:28 +0000938#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200939 mbedtls_mpi_init( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +0000940#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200941 mbedtls_mpi_init( &DP_blind );
942 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000943#endif
944
Hanno Becker06811ce2017-05-03 15:10:34 +0100945#if !defined(MBEDTLS_RSA_NO_CRT)
946 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200947#endif
948
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100949 mbedtls_mpi_init( &I );
950 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100951
952 /* End of MPI initialization */
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
955 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200957 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
958 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000959 }
960
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100961 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100962
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200963 /*
964 * Blinding
965 * T = T * Vi mod N
966 */
967 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
968 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
969 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000970
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200971 /*
972 * Exponent blinding
973 */
974 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
975 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000976
Janos Follathf9203b42017-03-22 15:13:15 +0000977#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200978 /*
979 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
980 */
981 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
982 f_rng, p_rng ) );
983 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
984 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
985 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000986
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200987 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000988#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200989 /*
990 * DP_blind = ( P - 1 ) * R + DP
991 */
992 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
993 f_rng, p_rng ) );
994 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
995 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
996 &ctx->DP ) );
Janos Follathf9203b42017-03-22 15:13:15 +0000997
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200998 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000999
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001000 /*
1001 * DQ_blind = ( Q - 1 ) * R + DQ
1002 */
1003 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1004 f_rng, p_rng ) );
1005 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1006 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1007 &ctx->DQ ) );
Janos Follathf9203b42017-03-22 15:13:15 +00001008
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001009 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001010#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001013 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001014#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001015 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001016 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001017 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001018 * TP = input ^ dP mod P
1019 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001021
1022 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1023 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
1025 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001026 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001028 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1029 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1030 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
1032 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001033 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001035 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1036 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001038
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001039 /*
1040 * Unblind
1041 * T = T * Vf mod N
1042 */
1043 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
1044 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
Hanno Becker2dec5e82017-10-03 07:49:52 +01001046 /* Verify the result to prevent glitching attacks. */
1047 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1048 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001049 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001050 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001051 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1052 goto cleanup;
1053 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001054
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
1058cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001060 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1061 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001062#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001063
Hanno Becker06811ce2017-05-03 15:10:34 +01001064 mbedtls_mpi_free( &P1 );
1065 mbedtls_mpi_free( &Q1 );
1066 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001067
Janos Follathe81102e2017-03-22 13:38:28 +00001068#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001069 mbedtls_mpi_free( &D_blind );
Janos Follathf9203b42017-03-22 15:13:15 +00001070#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001071 mbedtls_mpi_free( &DP_blind );
1072 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001073#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Hanno Becker06811ce2017-05-03 15:10:34 +01001075 mbedtls_mpi_free( &T );
1076
1077#if !defined(MBEDTLS_RSA_NO_CRT)
1078 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1079#endif
1080
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001081 mbedtls_mpi_free( &C );
1082 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001083
Gilles Peskineae3741e2020-11-25 00:10:31 +01001084 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001085 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
Gilles Peskineae3741e2020-11-25 00:10:31 +01001087 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088}
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001091/**
1092 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1093 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001094 * \param dst buffer to mask
1095 * \param dlen length of destination buffer
1096 * \param src source of the mask generation
1097 * \param slen length of the source buffer
1098 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001099 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001100static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104 unsigned char counter[4];
1105 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001106 unsigned int hlen;
1107 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001108 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001111 memset( counter, 0, 4 );
1112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114
Simon Butcher02037452016-03-01 21:19:12 +00001115 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001116 p = dst;
1117
1118 while( dlen > 0 )
1119 {
1120 use_len = hlen;
1121 if( dlen < hlen )
1122 use_len = dlen;
1123
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001124 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1125 goto exit;
1126 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1127 goto exit;
1128 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1129 goto exit;
1130 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1131 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001132
1133 for( i = 0; i < use_len; ++i )
1134 *p++ ^= mask[i];
1135
1136 counter[3]++;
1137
1138 dlen -= use_len;
1139 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001140
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001141exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001142 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001143
1144 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001149/*
1150 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001153 int (*f_rng)(void *, unsigned char *, size_t),
1154 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001155 const unsigned char *label, size_t label_len,
1156 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001157 const unsigned char *input,
1158 unsigned char *output )
1159{
1160 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001162 unsigned char *p = output;
1163 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 const mbedtls_md_info_t *md_info;
1165 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001166
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001167 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001168 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001169 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001170 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1171
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001172 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001176 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001178
1179 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001181
Simon Butcher02037452016-03-01 21:19:12 +00001182 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001183 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001185
1186 memset( output, 0, olen );
1187
1188 *p++ = 0;
1189
Simon Butcher02037452016-03-01 21:19:12 +00001190 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001191 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001192 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001193
1194 p += hlen;
1195
Simon Butcher02037452016-03-01 21:19:12 +00001196 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001197 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1198 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001199 p += hlen;
1200 p += olen - 2 * hlen - 2 - ilen;
1201 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001202 if( ilen != 0 )
1203 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001206 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001207 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001208
Simon Butcher02037452016-03-01 21:19:12 +00001209 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001210 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1211 &md_ctx ) ) != 0 )
1212 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001213
Simon Butcher02037452016-03-01 21:19:12 +00001214 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001215 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1216 &md_ctx ) ) != 0 )
1217 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001218
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001219exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001221
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001222 if( ret != 0 )
1223 return( ret );
1224
Thomas Daubney141700f2021-05-13 19:06:10 +01001225 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001230/*
1231 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001234 int (*f_rng)(void *, unsigned char *, size_t),
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001235 void *p_rng, size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001236 const unsigned char *input,
1237 unsigned char *output )
1238{
1239 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001241 unsigned char *p = output;
1242
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001243 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001244 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001245 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001246
Paul Bakkerb3869132013-02-28 17:21:01 +01001247 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001248
Simon Butcher02037452016-03-01 21:19:12 +00001249 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001250 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001252
1253 nb_pad = olen - 3 - ilen;
1254
1255 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001256
1257 if( f_rng == NULL )
1258 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1259
1260 *p++ = MBEDTLS_RSA_CRYPT;
1261
1262 while( nb_pad-- > 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001263 {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001264 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001265
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001266 do {
1267 ret = f_rng( p_rng, p, 1 );
1268 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +01001269
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001270 /* Check if RNG failed to generate data */
1271 if( rng_dl == 0 || ret != 0 )
1272 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001273
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001274 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001275 }
1276
1277 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001278 if( ilen != 0 )
1279 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001280
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001281 return( mbedtls_rsa_public( ctx, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001282}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001284
Paul Bakker5121ce52009-01-03 21:22:43 +00001285/*
1286 * Add the message padding, then do an RSA operation
1287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001289 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001290 void *p_rng,
Thomas Daubney21772772021-05-13 17:30:32 +01001291 size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001292 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 unsigned char *output )
1294{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001295 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001296 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001297 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001298
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 switch( ctx->padding )
1300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_PKCS1_V15)
1302 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney21772772021-05-13 17:30:32 +01001303 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng,
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001304 ilen, input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001305#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#if defined(MBEDTLS_PKCS1_V21)
1308 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney141700f2021-05-13 19:06:10 +01001309 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, NULL, 0,
Thomas Daubney21772772021-05-13 17:30:32 +01001310 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001311#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001312
1313 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001316}
1317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001319/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001320 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001323 int (*f_rng)(void *, unsigned char *, size_t),
1324 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001325 const unsigned char *label, size_t label_len,
1326 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001327 const unsigned char *input,
1328 unsigned char *output,
1329 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001330{
Janos Follath24eed8d2019-11-22 13:21:35 +00001331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001332 size_t ilen, i, pad_len;
1333 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1335 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001336 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 const mbedtls_md_info_t *md_info;
1338 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001339
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001340 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001341 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1342 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1343 RSA_VALIDATE_RET( input != NULL );
1344 RSA_VALIDATE_RET( olen != NULL );
1345
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001346 /*
1347 * Parameters sanity checks
1348 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001349 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001351
1352 ilen = ctx->len;
1353
Paul Bakker27fdf462011-06-09 13:55:13 +00001354 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001358 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001360
Janos Follathc17cda12016-02-11 11:08:18 +00001361 hlen = mbedtls_md_get_size( md_info );
1362
1363 // checking for integer underflow
1364 if( 2 * hlen + 2 > ilen )
1365 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1366
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001367 /*
1368 * RSA operation
1369 */
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001370 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
1372 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001373 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001374
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001375 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001376 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001379 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1380 {
1381 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001382 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001383 }
1384
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001385 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001386 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1387 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001388 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001389 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1390 &md_ctx ) ) != 0 )
1391 {
1392 mbedtls_md_free( &md_ctx );
1393 goto cleanup;
1394 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001397
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001398 /* Generate lHash */
1399 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1400 goto cleanup;
1401
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001402 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001403 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001404 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001406 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001408 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001409
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001410 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001411
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001412 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001413 for( i = 0; i < hlen; i++ )
1414 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001415
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001416 /* Get zero-padding len, but always read till end of buffer
1417 * (minus one, for the 01 byte) */
1418 pad_len = 0;
1419 pad_done = 0;
1420 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1421 {
1422 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001423 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001424 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001425
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001426 p += pad_len;
1427 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001428
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001429 /*
1430 * The only information "leaked" is whether the padding was correct or not
1431 * (eg, no data is copied if it was not correct). This meets the
1432 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1433 * the different error conditions.
1434 */
1435 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001436 {
1437 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1438 goto cleanup;
1439 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001440
Paul Bakker66d5d072014-06-17 16:39:18 +02001441 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001442 {
1443 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1444 goto cleanup;
1445 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001446
1447 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001448 if( *olen != 0 )
1449 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001450 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001451
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001452cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001453 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1454 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001455
1456 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001461/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1462 *
1463 * \param value The value to analyze.
1464 * \return Zero if \p value is zero, otherwise all-bits-one.
1465 */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001466static unsigned mbedtls_cf_uint_mask( unsigned value )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001467{
1468 /* MSVC has a warning about unary minus on unsigned, but this is
1469 * well-defined and precisely what we want to do here */
1470#if defined(_MSC_VER)
1471#pragma warning( push )
1472#pragma warning( disable : 4146 )
1473#endif
1474 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1475#if defined(_MSC_VER)
1476#pragma warning( pop )
1477#endif
1478}
1479
1480/** Check whether a size is out of bounds, without branches.
1481 *
1482 * This is equivalent to `size > max`, but is likely to be compiled to
1483 * to code using bitwise operation rather than a branch.
1484 *
1485 * \param size Size to check.
1486 * \param max Maximum desired value for \p size.
1487 * \return \c 0 if `size <= max`.
1488 * \return \c 1 if `size > max`.
1489 */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001490static unsigned mbedtls_cf_size_gt( size_t size, size_t max )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001491{
1492 /* Return the sign bit (1 for negative) of (max - size). */
1493 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1494}
1495
1496/** Choose between two integer values, without branches.
1497 *
1498 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1499 * to code using bitwise operation rather than a branch.
1500 *
1501 * \param cond Condition to test.
1502 * \param if1 Value to use if \p cond is nonzero.
1503 * \param if0 Value to use if \p cond is zero.
1504 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1505 */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001506static unsigned mbedtls_cf_uint_if( unsigned cond, unsigned if1, unsigned if0 )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001507{
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001508 unsigned mask = mbedtls_cf_uint_mask( cond );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001509 return( ( mask & if1 ) | (~mask & if0 ) );
1510}
1511
1512/** Shift some data towards the left inside a buffer without leaking
1513 * the length of the data through side channels.
1514 *
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001515 * `mbedtls_cf_mem_move_to_left(start, total, offset)` is functionally
1516 * equivalent to
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001517 * ```
1518 * memmove(start, start + offset, total - offset);
1519 * memset(start + offset, 0, total - offset);
1520 * ```
1521 * but it strives to use a memory access pattern (and thus total timing)
1522 * that does not depend on \p offset. This timing independence comes at
1523 * the expense of performance.
1524 *
1525 * \param start Pointer to the start of the buffer.
1526 * \param total Total size of the buffer.
1527 * \param offset Offset from which to copy \p total - \p offset bytes.
1528 */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001529static void mbedtls_cf_mem_move_to_left( void *start,
1530 size_t total,
1531 size_t offset )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001532{
1533 volatile unsigned char *buf = start;
1534 size_t i, n;
1535 if( total == 0 )
1536 return;
1537 for( i = 0; i < total; i++ )
1538 {
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001539 unsigned no_op = mbedtls_cf_size_gt( total - offset, i );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001540 /* The first `total - offset` passes are a no-op. The last
1541 * `offset` passes shift the data one byte to the left and
1542 * zero out the last byte. */
1543 for( n = 0; n < total - 1; n++ )
1544 {
1545 unsigned char current = buf[n];
1546 unsigned char next = buf[n+1];
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001547 buf[n] = mbedtls_cf_uint_if( no_op, current, next );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001548 }
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001549 buf[total-1] = mbedtls_cf_uint_if( no_op, buf[total-1], 0 );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001550 }
1551}
1552
Paul Bakkerb3869132013-02-28 17:21:01 +01001553/*
1554 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001557 int (*f_rng)(void *, unsigned char *, size_t),
1558 void *p_rng,
Thomas Daubney34733082021-05-12 09:24:29 +01001559 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001560 const unsigned char *input,
1561 unsigned char *output,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001562 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001563{
Janos Follath24eed8d2019-11-22 13:21:35 +00001564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001565 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001567 /* The following variables take sensitive values: their value must
1568 * not leak into the observable behavior of the function other than
1569 * the designated outputs (output, olen, return value). Otherwise
1570 * this would open the execution of the function to
1571 * side-channel-based variants of the Bleichenbacher padding oracle
1572 * attack. Potential side channels include overall timing, memory
1573 * access patterns (especially visible to an adversary who has access
1574 * to a shared memory cache), and branches (especially visible to
1575 * an adversary who has access to a shared code cache or to a shared
1576 * branch predictor). */
1577 size_t pad_count = 0;
1578 unsigned bad = 0;
1579 unsigned char pad_done = 0;
1580 size_t plaintext_size = 0;
1581 unsigned output_too_large;
1582
1583 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001584 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1585 RSA_VALIDATE_RET( input != NULL );
1586 RSA_VALIDATE_RET( olen != NULL );
1587
1588 ilen = ctx->len;
1589 plaintext_max_size = ( output_max_len > ilen - 11 ?
1590 ilen - 11 :
1591 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001592
Thomas Daubney34733082021-05-12 09:24:29 +01001593 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001595
Paul Bakkerb3869132013-02-28 17:21:01 +01001596 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001598
Thomas Daubney34733082021-05-12 09:24:29 +01001599 ret = mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001600
1601 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001602 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001603
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001604 /* Check and get padding length in constant time and constant
1605 * memory trace. The first byte must be 0. */
1606 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001607
Paul Bakker5121ce52009-01-03 21:22:43 +00001608
Thomas Daubney34733082021-05-12 09:24:29 +01001609 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1610 * where PS must be at least 8 nonzero bytes. */
1611 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001612
Thomas Daubney34733082021-05-12 09:24:29 +01001613 /* Read the whole buffer. Set pad_done to nonzero if we find
1614 * the 0x00 byte and remember the padding length in pad_count. */
1615 for( i = 2; i < ilen; i++ )
1616 {
1617 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
1618 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001619 }
1620
Thomas Daubney34733082021-05-12 09:24:29 +01001621
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001622 /* If pad_done is still zero, there's no data, only unfinished padding. */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001623 bad |= mbedtls_cf_uint_if( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001624
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001625 /* There must be at least 8 bytes of padding. */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001626 bad |= mbedtls_cf_size_gt( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001627
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001628 /* If the padding is valid, set plaintext_size to the number of
1629 * remaining bytes after stripping the padding. If the padding
1630 * is invalid, avoid leaking this fact through the size of the
1631 * output: use the maximum message size that fits in the output
1632 * buffer. Do it without branches to avoid leaking the padding
1633 * validity through timing. RSA keys are small enough that all the
1634 * size_t values involved fit in unsigned int. */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001635 plaintext_size = mbedtls_cf_uint_if(
1636 bad, (unsigned) plaintext_max_size,
1637 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001638
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001639 /* Set output_too_large to 0 if the plaintext fits in the output
1640 * buffer and to 1 otherwise. */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001641 output_too_large = mbedtls_cf_size_gt( plaintext_size,
1642 plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001643
1644 /* Set ret without branches to avoid timing attacks. Return:
1645 * - INVALID_PADDING if the padding is bad (bad != 0).
1646 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1647 * plaintext does not fit in the output buffer.
1648 * - 0 if the padding is correct. */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001649 ret = - (int) mbedtls_cf_uint_if(
1650 bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1651 mbedtls_cf_uint_if( output_too_large,
1652 - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1653 0 ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001654
1655 /* If the padding is bad or the plaintext is too large, zero the
1656 * data that we're about to copy to the output buffer.
1657 * We need to copy the same amount of data
1658 * from the same buffer whether the padding is good or not to
1659 * avoid leaking the padding validity through overall timing or
1660 * through memory or cache access patterns. */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001661 bad = mbedtls_cf_uint_mask( bad | output_too_large );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001662 for( i = 11; i < ilen; i++ )
1663 buf[i] &= ~bad;
1664
1665 /* If the plaintext is too large, truncate it to the buffer size.
1666 * Copy anyway to avoid revealing the length through timing, because
1667 * revealing the length is as bad as revealing the padding validity
1668 * for a Bleichenbacher attack. */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001669 plaintext_size = mbedtls_cf_uint_if( output_too_large,
1670 (unsigned) plaintext_max_size,
1671 (unsigned) plaintext_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001672
1673 /* Move the plaintext to the leftmost position where it can start in
1674 * the working buffer, i.e. make it start plaintext_max_size from
1675 * the end of the buffer. Do this with a memory access trace that
1676 * does not depend on the plaintext size. After this move, the
1677 * starting location of the plaintext is no longer sensitive
1678 * information. */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001679 mbedtls_cf_mem_move_to_left( buf + ilen - plaintext_max_size,
1680 plaintext_max_size,
1681 plaintext_max_size - plaintext_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001682
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001683 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1684 * buffer. If output_max_len is 0, then output may be an invalid pointer
1685 * and the result of memcpy() would be undefined; prevent undefined
1686 * behavior making sure to depend only on output_max_len (the size of the
1687 * user-provided output buffer), which is independent from plaintext
1688 * length, validity of padding, success of the decryption, and other
1689 * secrets. */
1690 if( output_max_len != 0 )
1691 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001692
1693 /* Report the amount of data we copied to the output buffer. In case
1694 * of errors (bad padding or output too large), the value of *olen
1695 * when this function returns is not specified. Making it equivalent
1696 * to the good case limits the risks of leaking the padding validity. */
1697 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001698
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001699cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001700 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001701
1702 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001705
1706/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001707 * Do an RSA operation, then remove the message padding
1708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001710 int (*f_rng)(void *, unsigned char *, size_t),
1711 void *p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01001712 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001713 const unsigned char *input,
1714 unsigned char *output,
1715 size_t output_max_len)
1716{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001717 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001718 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1719 RSA_VALIDATE_RET( input != NULL );
1720 RSA_VALIDATE_RET( olen != NULL );
1721
Paul Bakkerb3869132013-02-28 17:21:01 +01001722 switch( ctx->padding )
1723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#if defined(MBEDTLS_PKCS1_V15)
1725 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney34733082021-05-12 09:24:29 +01001726 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001727 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001728#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#if defined(MBEDTLS_PKCS1_V21)
1731 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyd21e0b72021-05-06 11:41:09 +01001732 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001733 olen, input, output,
1734 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001735#endif
1736
1737 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001739 }
1740}
1741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001743static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001744 int (*f_rng)(void *, unsigned char *, size_t),
1745 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001747 unsigned int hashlen,
1748 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001749 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001750 unsigned char *sig )
1751{
1752 size_t olen;
1753 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001754 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001755 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001756 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001757 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 const mbedtls_md_info_t *md_info;
1759 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001760 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001761 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1762 hashlen == 0 ) ||
1763 hash != NULL );
1764 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001765
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001766 if( ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1767 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1768
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001769 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001771
1772 olen = ctx->len;
1773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001775 {
Simon Butcher02037452016-03-01 21:19:12 +00001776 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001778 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001780
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001781 if( hashlen != mbedtls_md_get_size( md_info ) )
1782 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001783 }
1784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001786 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001790
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001791 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1792 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001793 /* Calculate the largest possible salt length, up to the hash size.
1794 * Normally this is the hash length, which is the maximum salt length
1795 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001796 * enough room, use the maximum salt length that fits. The constraint is
1797 * that the hash length plus the salt length plus 2 bytes must be at most
1798 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1799 * (PKCS#1 v2.2) §9.1.1 step 3. */
1800 min_slen = hlen - 2;
1801 if( olen < hlen + min_slen + 2 )
1802 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1803 else if( olen >= hlen + hlen + 2 )
1804 slen = hlen;
1805 else
1806 slen = olen - hlen - 2;
1807 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001808 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001811 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001812 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001813 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001814 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001815 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001816
1817 memset( sig, 0, olen );
1818
Simon Butcher02037452016-03-01 21:19:12 +00001819 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001820 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001821 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001822 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001823
1824 /* Generate salt of length slen in place in the encoded message */
1825 salt = p;
1826 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001827 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001828
Paul Bakkerb3869132013-02-28 17:21:01 +01001829 p += slen;
1830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001832 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001833 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001834
Simon Butcher02037452016-03-01 21:19:12 +00001835 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001836 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1837 goto exit;
1838 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1839 goto exit;
1840 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1841 goto exit;
1842 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1843 goto exit;
1844 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1845 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001846
Simon Butcher02037452016-03-01 21:19:12 +00001847 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001848 if( msb % 8 == 0 )
1849 offset = 1;
1850
Simon Butcher02037452016-03-01 21:19:12 +00001851 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001852 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1853 &md_ctx ) ) != 0 )
1854 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001855
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001856 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001857 sig[0] &= 0xFF >> ( olen * 8 - msb );
1858
1859 p += hlen;
1860 *p++ = 0xBC;
1861
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001862exit:
1863 mbedtls_md_free( &md_ctx );
1864
1865 if( ret != 0 )
1866 return( ret );
1867
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001868 return mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01001869}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001870
1871/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001872 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1873 * the option to pass in the salt length.
1874 */
1875int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1876 int (*f_rng)(void *, unsigned char *, size_t),
1877 void *p_rng,
1878 mbedtls_md_type_t md_alg,
1879 unsigned int hashlen,
1880 const unsigned char *hash,
1881 int saltlen,
1882 unsigned char *sig )
1883{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001884 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001885 hashlen, hash, saltlen, sig );
1886}
1887
1888
1889/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001890 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1891 */
1892int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1893 int (*f_rng)(void *, unsigned char *, size_t),
1894 void *p_rng,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001895 mbedtls_md_type_t md_alg,
1896 unsigned int hashlen,
1897 const unsigned char *hash,
1898 unsigned char *sig )
1899{
Thomas Daubneycad59ed2021-05-19 15:04:08 +01001900 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
Cédric Meuterf3fab332020-04-25 11:30:45 +02001901 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001902}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001906/*
1907 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1908 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001909
1910/* Construct a PKCS v1.5 encoding of a hashed message
1911 *
1912 * This is used both for signature generation and verification.
1913 *
1914 * Parameters:
1915 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001916 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001917 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001918 * - hash: Buffer containing the hashed message or the raw data.
1919 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001920 * - dst: Buffer to hold the encoded message.
1921 *
1922 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001923 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001924 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001925 *
1926 */
1927static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1928 unsigned int hashlen,
1929 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001930 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001931 unsigned char *dst )
1932{
1933 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001934 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001935 unsigned char *p = dst;
1936 const char *oid = NULL;
1937
1938 /* Are we signing hashed or raw data? */
1939 if( md_alg != MBEDTLS_MD_NONE )
1940 {
1941 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1942 if( md_info == NULL )
1943 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1944
1945 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1946 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1947
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001948 if( hashlen != mbedtls_md_get_size( md_info ) )
1949 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001950
1951 /* Double-check that 8 + hashlen + oid_size can be used as a
1952 * 1-byte ASN.1 length encoding and that there's no overflow. */
1953 if( 8 + hashlen + oid_size >= 0x80 ||
1954 10 + hashlen < hashlen ||
1955 10 + hashlen + oid_size < 10 + hashlen )
1956 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1957
1958 /*
1959 * Static bounds check:
1960 * - Need 10 bytes for five tag-length pairs.
1961 * (Insist on 1-byte length encodings to protect against variants of
1962 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1963 * - Need hashlen bytes for hash
1964 * - Need oid_size bytes for hash alg OID.
1965 */
1966 if( nb_pad < 10 + hashlen + oid_size )
1967 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1968 nb_pad -= 10 + hashlen + oid_size;
1969 }
1970 else
1971 {
1972 if( nb_pad < hashlen )
1973 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1974
1975 nb_pad -= hashlen;
1976 }
1977
Hanno Becker2b2f8982017-09-27 17:10:03 +01001978 /* Need space for signature header and padding delimiter (3 bytes),
1979 * and 8 bytes for the minimal padding */
1980 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001981 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1982 nb_pad -= 3;
1983
1984 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001985 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001986
1987 /* Write signature header and padding */
1988 *p++ = 0;
1989 *p++ = MBEDTLS_RSA_SIGN;
1990 memset( p, 0xFF, nb_pad );
1991 p += nb_pad;
1992 *p++ = 0;
1993
1994 /* Are we signing raw data? */
1995 if( md_alg == MBEDTLS_MD_NONE )
1996 {
1997 memcpy( p, hash, hashlen );
1998 return( 0 );
1999 }
2000
2001 /* Signing hashed data, add corresponding ASN.1 structure
2002 *
2003 * DigestInfo ::= SEQUENCE {
2004 * digestAlgorithm DigestAlgorithmIdentifier,
2005 * digest Digest }
2006 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2007 * Digest ::= OCTET STRING
2008 *
2009 * Schematic:
2010 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2011 * TAG-NULL + LEN [ NULL ] ]
2012 * TAG-OCTET + LEN [ HASH ] ]
2013 */
2014 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002015 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002016 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002017 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002018 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002019 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002020 memcpy( p, oid, oid_size );
2021 p += oid_size;
2022 *p++ = MBEDTLS_ASN1_NULL;
2023 *p++ = 0x00;
2024 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002025 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002026 memcpy( p, hash, hashlen );
2027 p += hashlen;
2028
2029 /* Just a sanity-check, should be automatic
2030 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002031 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002032 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002033 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002034 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2035 }
2036
2037 return( 0 );
2038}
2039
Paul Bakkerb3869132013-02-28 17:21:01 +01002040/*
2041 * Do an RSA operation to sign the message digest
2042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002044 int (*f_rng)(void *, unsigned char *, size_t),
2045 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002047 unsigned int hashlen,
2048 const unsigned char *hash,
2049 unsigned char *sig )
2050{
Janos Follath24eed8d2019-11-22 13:21:35 +00002051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002052 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002053
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002054 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002055 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2056 hashlen == 0 ) ||
2057 hash != NULL );
2058 RSA_VALIDATE_RET( sig != NULL );
2059
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002060 if( ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2061 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2062
Hanno Beckerfdf38032017-09-06 12:35:55 +01002063 /*
2064 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2065 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002066
Hanno Beckerfdf38032017-09-06 12:35:55 +01002067 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2068 ctx->len, sig ) ) != 0 )
2069 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002070
Hanno Beckerfdf38032017-09-06 12:35:55 +01002071 /* Private key operation
2072 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002073 * In order to prevent Lenstra's attack, make the signature in a
2074 * temporary buffer and check it before returning it.
2075 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002076
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002077 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002078 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002079 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2080
Hanno Beckerfdf38032017-09-06 12:35:55 +01002081 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002082 if( verif == NULL )
2083 {
2084 mbedtls_free( sig_try );
2085 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2086 }
2087
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002088 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2089 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2090
Hanno Becker171a8f12017-09-06 12:32:16 +01002091 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002092 {
2093 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2094 goto cleanup;
2095 }
2096
2097 memcpy( sig, sig_try, ctx->len );
2098
2099cleanup:
2100 mbedtls_free( sig_try );
2101 mbedtls_free( verif );
2102
2103 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002104}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002106
2107/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 * Do an RSA operation to sign the message digest
2109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002111 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002112 void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002114 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002115 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 unsigned char *sig )
2117{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002118 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002119 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2120 hashlen == 0 ) ||
2121 hash != NULL );
2122 RSA_VALIDATE_RET( sig != NULL );
2123
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 switch( ctx->padding )
2125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126#if defined(MBEDTLS_PKCS1_V15)
2127 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney52654982021-05-18 16:54:00 +01002128 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng,
Thomas Daubney140184d2021-05-18 16:04:07 +01002129 md_alg, hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002130#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132#if defined(MBEDTLS_PKCS1_V21)
2133 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubneyde9fdc42021-05-18 17:10:04 +01002134 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, md_alg,
2135 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002136#endif
2137
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002141}
2142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002144/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002145 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002149 unsigned int hashlen,
2150 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002152 int expected_salt_len,
2153 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002154{
Janos Follath24eed8d2019-11-22 13:21:35 +00002155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002156 size_t siglen;
2157 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002158 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002160 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002161 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002162 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 const mbedtls_md_info_t *md_info;
2164 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002165 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002166
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002167 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002168 RSA_VALIDATE_RET( sig != NULL );
2169 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2170 hashlen == 0 ) ||
2171 hash != NULL );
2172
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 siglen = ctx->len;
2174
Paul Bakker27fdf462011-06-09 13:55:13 +00002175 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
Thomas Daubney782a7f52021-05-19 12:27:35 +01002178 ret = mbedtls_rsa_public( ctx, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002179
2180 if( ret != 0 )
2181 return( ret );
2182
2183 p = buf;
2184
Paul Bakkerb3869132013-02-28 17:21:01 +01002185 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 {
Simon Butcher02037452016-03-01 21:19:12 +00002190 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002192 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002194
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002195 if( hashlen != mbedtls_md_get_size( md_info ) )
2196 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002197 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002200 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002204
Paul Bakkerb3869132013-02-28 17:21:01 +01002205 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002206
Simon Butcher02037452016-03-01 21:19:12 +00002207 /*
2208 * Note: EMSA-PSS verification is over the length of N - 1 bits
2209 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002210 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002211
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002212 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2213 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2214
Simon Butcher02037452016-03-01 21:19:12 +00002215 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002216 if( msb % 8 == 0 )
2217 {
2218 p++;
2219 siglen -= 1;
2220 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002221
Gilles Peskine139108a2017-10-18 19:03:42 +02002222 if( siglen < hlen + 2 )
2223 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2224 hash_start = p + siglen - hlen - 1;
2225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002227 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002228 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002229
Jaeden Amero66954e12018-01-25 16:05:54 +00002230 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2231 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002232 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002233
Paul Bakkerb3869132013-02-28 17:21:01 +01002234 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002235
Gilles Peskine6a54b022017-10-17 19:02:13 +02002236 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002237 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002238
Gilles Peskine91048a32017-10-19 17:46:14 +02002239 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002240 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002241 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2242 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002243 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002244
Gilles Peskine6a54b022017-10-17 19:02:13 +02002245 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002248 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002249 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002250 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2251 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002252 }
2253
Simon Butcher02037452016-03-01 21:19:12 +00002254 /*
2255 * Generate H = Hash( M' )
2256 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002257 ret = mbedtls_md_starts( &md_ctx );
2258 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002259 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002260 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2261 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002262 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002263 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2264 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002265 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002266 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2267 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002268 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002269 ret = mbedtls_md_finish( &md_ctx, result );
2270 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002271 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002272
Jaeden Amero66954e12018-01-25 16:05:54 +00002273 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002274 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002275 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002276 goto exit;
2277 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002278
2279exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002281
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002282 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002283}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002284
2285/*
2286 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002290 unsigned int hashlen,
2291 const unsigned char *hash,
2292 const unsigned char *sig )
2293{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002294 mbedtls_md_type_t mgf1_hash_id;
2295 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002296 RSA_VALIDATE_RET( sig != NULL );
2297 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2298 hashlen == 0 ) ||
2299 hash != NULL );
2300
2301 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002303 : md_alg;
2304
Thomas Daubney9e65f792021-05-19 12:18:58 +01002305 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx,
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002306 md_alg, hashlen, hash,
2307 mgf1_hash_id,
2308 MBEDTLS_RSA_SALT_LEN_ANY,
2309 sig ) );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002310
2311}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002315/*
2316 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002320 unsigned int hashlen,
2321 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002322 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002323{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002324 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002325 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002326 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002327
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002328 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002329 RSA_VALIDATE_RET( sig != NULL );
2330 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2331 hashlen == 0 ) ||
2332 hash != NULL );
2333
2334 sig_len = ctx->len;
2335
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002336 /*
2337 * Prepare expected PKCS1 v1.5 encoding of hash.
2338 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002339
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002340 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2341 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2342 {
2343 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2344 goto cleanup;
2345 }
2346
2347 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2348 encoded_expected ) ) != 0 )
2349 goto cleanup;
2350
2351 /*
2352 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2353 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002354
Thomas Daubney41e4ce42021-05-19 15:10:05 +01002355 ret = mbedtls_rsa_public( ctx, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002356 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002357 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002358
Simon Butcher02037452016-03-01 21:19:12 +00002359 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002360 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002361 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002362
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002363 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2364 sig_len ) ) != 0 )
2365 {
2366 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2367 goto cleanup;
2368 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002369
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002370cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002371
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002372 if( encoded != NULL )
2373 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002374 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002375 mbedtls_free( encoded );
2376 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002377
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002378 if( encoded_expected != NULL )
2379 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002380 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002381 mbedtls_free( encoded_expected );
2382 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002383
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002384 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
2388/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002389 * Do an RSA operation and check the message digest
2390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002393 unsigned int hashlen,
2394 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002395 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002396{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002397 RSA_VALIDATE_RET( ctx != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002398 RSA_VALIDATE_RET( sig != NULL );
2399 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2400 hashlen == 0 ) ||
2401 hash != NULL );
2402
Paul Bakkerb3869132013-02-28 17:21:01 +01002403 switch( ctx->padding )
2404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405#if defined(MBEDTLS_PKCS1_V15)
2406 case MBEDTLS_RSA_PKCS_V15:
Thomas Daubney2e126252021-05-19 11:48:53 +01002407 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, md_alg,
2408 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002409#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411#if defined(MBEDTLS_PKCS1_V21)
2412 case MBEDTLS_RSA_PKCS_V21:
Thomas Daubney5ee4cc02021-05-19 12:07:42 +01002413 return mbedtls_rsa_rsassa_pss_verify( ctx, md_alg,
2414 hashlen, hash, sig );
Paul Bakkerb3869132013-02-28 17:21:01 +01002415#endif
2416
2417 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002419 }
2420}
2421
2422/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002423 * Copy the components of an RSA key
2424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002426{
Janos Follath24eed8d2019-11-22 13:21:35 +00002427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002428 RSA_VALIDATE_RET( dst != NULL );
2429 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002430
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002431 dst->len = src->len;
2432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2434 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2437 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2438 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002439
2440#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2442 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2443 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2445 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002446#endif
2447
2448 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2451 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002452
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002453 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002454 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002455
2456cleanup:
2457 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002459
2460 return( ret );
2461}
2462
2463/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 * Free the components of an RSA key
2465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002467{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002468 if( ctx == NULL )
2469 return;
2470
2471 mbedtls_mpi_free( &ctx->Vi );
2472 mbedtls_mpi_free( &ctx->Vf );
2473 mbedtls_mpi_free( &ctx->RN );
2474 mbedtls_mpi_free( &ctx->D );
2475 mbedtls_mpi_free( &ctx->Q );
2476 mbedtls_mpi_free( &ctx->P );
2477 mbedtls_mpi_free( &ctx->E );
2478 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002479
Hanno Becker33c30a02017-08-23 07:00:22 +01002480#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002481 mbedtls_mpi_free( &ctx->RQ );
2482 mbedtls_mpi_free( &ctx->RP );
2483 mbedtls_mpi_free( &ctx->QP );
2484 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002485 mbedtls_mpi_free( &ctx->DP );
2486#endif /* MBEDTLS_RSA_NO_CRT */
2487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002489 /* Free the mutex, but only if it hasn't been freed already. */
2490 if( ctx->ver != 0 )
2491 {
2492 mbedtls_mutex_free( &ctx->mutex );
2493 ctx->ver = 0;
2494 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002495#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002496}
2497
Hanno Beckerab377312017-08-23 16:24:51 +01002498#endif /* !MBEDTLS_RSA_ALT */
2499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002501
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002502#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
2504/*
2505 * Example RSA-1024 keypair, for test purposes
2506 */
2507#define KEY_LEN 128
2508
2509#define RSA_N "9292758453063D803DD603D5E777D788" \
2510 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2511 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2512 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2513 "93A89813FBF3C4F8066D2D800F7C38A8" \
2514 "1AE31942917403FF4946B0A83D3D3E05" \
2515 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2516 "5E94BB77B07507233A0BC7BAC8F90F79"
2517
2518#define RSA_E "10001"
2519
2520#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2521 "66CA472BC44D253102F8B4A9D3BFA750" \
2522 "91386C0077937FE33FA3252D28855837" \
2523 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2524 "DF79C5CE07EE72C7F123142198164234" \
2525 "CABB724CF78B8173B9F880FC86322407" \
2526 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2527 "071513A1E85B5DFA031F21ECAE91A34D"
2528
2529#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2530 "2C01CAD19EA484A87EA4377637E75500" \
2531 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2532 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2533
2534#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2535 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2536 "910E4168387E3C30AA1E00C339A79508" \
2537 "8452DD96A9A5EA5D9DCA68DA636032AF"
2538
Paul Bakker5121ce52009-01-03 21:22:43 +00002539#define PT_LEN 24
2540#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2541 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002544static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002545{
gufe44c2620da2020-08-03 17:56:50 +02002546#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002547 size_t i;
2548
Paul Bakker545570e2010-07-18 09:00:25 +00002549 if( rng_state != NULL )
2550 rng_state = NULL;
2551
Paul Bakkera3d195c2011-11-27 21:07:34 +00002552 for( i = 0; i < len; ++i )
2553 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002554#else
2555 if( rng_state != NULL )
2556 rng_state = NULL;
2557
2558 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002559#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002560
Paul Bakkera3d195c2011-11-27 21:07:34 +00002561 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002564
Paul Bakker5121ce52009-01-03 21:22:43 +00002565/*
2566 * Checkup routine
2567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002569{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002570 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002572 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 unsigned char rsa_plaintext[PT_LEN];
2575 unsigned char rsa_decrypted[PT_LEN];
2576 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002578 unsigned char sha1sum[20];
2579#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
Hanno Becker3a701162017-08-22 13:52:43 +01002581 mbedtls_mpi K;
2582
2583 mbedtls_mpi_init( &K );
Ronald Cronc1905a12021-06-05 11:11:14 +02002584 mbedtls_rsa_init( &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Hanno Becker3a701162017-08-22 13:52:43 +01002586 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2587 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2588 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2589 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2590 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2591 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2592 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2593 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2594 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2595 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2596
Hanno Becker7f25f852017-10-10 16:56:22 +01002597 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598
2599 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2603 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 {
2605 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
Hanno Becker5bc87292017-05-03 15:09:31 +01002608 ret = 1;
2609 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 }
2611
2612 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
2615 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2616
Thomas Daubney21772772021-05-13 17:30:32 +01002617 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002618 PT_LEN, rsa_plaintext,
2619 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 {
2621 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Hanno Becker5bc87292017-05-03 15:09:31 +01002624 ret = 1;
2625 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 }
2627
2628 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
Thomas Daubneyc7feaf32021-05-07 14:02:43 +01002631 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL,
Hanno Becker98838b02017-10-02 13:16:10 +01002632 &len, rsa_ciphertext, rsa_decrypted,
2633 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 {
2635 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Hanno Becker5bc87292017-05-03 15:09:31 +01002638 ret = 1;
2639 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 }
2641
2642 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2643 {
2644 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
Hanno Becker5bc87292017-05-03 15:09:31 +01002647 ret = 1;
2648 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 }
2650
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002651 if( verbose != 0 )
2652 mbedtls_printf( "passed\n" );
2653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002656 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
TRodziewicz26371e42021-06-08 16:45:41 +02002658 if( mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002659 {
2660 if( verbose != 0 )
2661 mbedtls_printf( "failed\n" );
2662
2663 return( 1 );
2664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Hanno Becker98838b02017-10-02 13:16:10 +01002666 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002667 MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002668 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 {
2670 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Hanno Becker5bc87292017-05-03 15:09:31 +01002673 ret = 1;
2674 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 }
2676
2677 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002680 if( mbedtls_rsa_pkcs1_verify( &rsa, MBEDTLS_MD_SHA1, 20,
Hanno Becker98838b02017-10-02 13:16:10 +01002681 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 {
2683 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Hanno Becker5bc87292017-05-03 15:09:31 +01002686 ret = 1;
2687 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 }
2689
2690 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002691 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002694 if( verbose != 0 )
2695 mbedtls_printf( "\n" );
2696
Paul Bakker3d8fb632014-04-17 12:42:41 +02002697cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002698 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 mbedtls_rsa_free( &rsa );
2700#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002701 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002703 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002704}
2705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708#endif /* MBEDTLS_RSA_C */