blob: 9c39fa5d91485df4631e37bf989839abb357f6b4 [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"
Hanno Beckera565f542017-10-11 11:00:19 +010043#include "mbedtls/rsa_internal.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 Mezeic0ae1cf2021-10-20 12:09:35 +020047#include "constant_time_internal.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020048#include "mbedtls/constant_time.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
gufe44c2620da2020-08-03 17:56:50 +020056#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010061
Hanno Beckera565f542017-10-11 11:00:19 +010062#if !defined(MBEDTLS_RSA_ALT)
63
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050064/* Parameter validation macros */
65#define RSA_VALIDATE_RET( cond ) \
66 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
67#define RSA_VALIDATE( cond ) \
68 MBEDTLS_INTERNAL_VALIDATE( cond )
69
Hanno Becker617c1ae2017-08-23 14:11:24 +010070int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
71 const mbedtls_mpi *N,
72 const mbedtls_mpi *P, const mbedtls_mpi *Q,
73 const mbedtls_mpi *D, const mbedtls_mpi *E )
74{
Janos Follath24eed8d2019-11-22 13:21:35 +000075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050076 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +010077
78 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
79 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
80 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
81 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
82 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
83 {
Chris Jonesb7d02e02021-04-01 17:40:03 +010084 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +010085 }
86
87 if( N != NULL )
88 ctx->len = mbedtls_mpi_size( &ctx->N );
89
90 return( 0 );
91}
92
93int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +010094 unsigned char const *N, size_t N_len,
95 unsigned char const *P, size_t P_len,
96 unsigned char const *Q, size_t Q_len,
97 unsigned char const *D, size_t D_len,
98 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +010099{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000100 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100102
103 if( N != NULL )
104 {
105 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
106 ctx->len = mbedtls_mpi_size( &ctx->N );
107 }
108
109 if( P != NULL )
110 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
111
112 if( Q != NULL )
113 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
114
115 if( D != NULL )
116 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
117
118 if( E != NULL )
119 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
120
121cleanup:
122
123 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100124 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100125
126 return( 0 );
127}
128
Hanno Becker705fc682017-10-10 17:57:02 +0100129/*
130 * Checks whether the context fields are set in such a way
131 * that the RSA primitives will be able to execute without error.
132 * It does *not* make guarantees for consistency of the parameters.
133 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100134static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
135 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100136{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100137#if !defined(MBEDTLS_RSA_NO_CRT)
138 /* blinding_needed is only used for NO_CRT to decide whether
139 * P,Q need to be present or not. */
140 ((void) blinding_needed);
141#endif
142
Hanno Becker3a760a12018-01-05 08:14:49 +0000143 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
144 ctx->len > MBEDTLS_MPI_MAX_SIZE )
145 {
Hanno Becker705fc682017-10-10 17:57:02 +0100146 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000147 }
Hanno Becker705fc682017-10-10 17:57:02 +0100148
149 /*
150 * 1. Modular exponentiation needs positive, odd moduli.
151 */
152
153 /* Modular exponentiation wrt. N is always used for
154 * RSA public key operations. */
155 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
156 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
157 {
158 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
159 }
160
161#if !defined(MBEDTLS_RSA_NO_CRT)
162 /* Modular exponentiation for P and Q is only
163 * used for private key operations and if CRT
164 * is used. */
165 if( is_priv &&
166 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
167 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
168 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
169 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
170 {
171 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
172 }
173#endif /* !MBEDTLS_RSA_NO_CRT */
174
175 /*
176 * 2. Exponents must be positive
177 */
178
179 /* Always need E for public key operations */
180 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
181 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
182
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100183#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100184 /* For private key operations, use D or DP & DQ
185 * as (unblinded) exponents. */
186 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
187 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
188#else
189 if( is_priv &&
190 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
191 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
192 {
193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
194 }
195#endif /* MBEDTLS_RSA_NO_CRT */
196
197 /* Blinding shouldn't make exponents negative either,
198 * so check that P, Q >= 1 if that hasn't yet been
199 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100200#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100201 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100202 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
203 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
204 {
205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
206 }
207#endif
208
209 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100210 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100211#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100212 if( is_priv &&
213 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
214 {
215 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
216 }
217#endif
218
219 return( 0 );
220}
221
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100222int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100223{
224 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500225 int have_N, have_P, have_Q, have_D, have_E;
226#if !defined(MBEDTLS_RSA_NO_CRT)
227 int have_DP, have_DQ, have_QP;
228#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500229 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100230
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500231 RSA_VALIDATE_RET( ctx != NULL );
232
233 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
234 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
235 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
236 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
237 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500238
239#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500240 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
241 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
242 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500243#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100244
Hanno Becker617c1ae2017-08-23 14:11:24 +0100245 /*
246 * Check whether provided parameters are enough
247 * to deduce all others. The following incomplete
248 * parameter sets for private keys are supported:
249 *
250 * (1) P, Q missing.
251 * (2) D and potentially N missing.
252 *
253 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100254
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 n_missing = have_P && have_Q && have_D && have_E;
256 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
257 d_missing = have_P && have_Q && !have_D && have_E;
258 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100259
260 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500261 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100262
Hanno Becker617c1ae2017-08-23 14:11:24 +0100263 if( !is_priv && !is_pub )
264 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
265
266 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100267 * Step 1: Deduce N if P, Q are provided.
268 */
269
270 if( !have_N && have_P && have_Q )
271 {
272 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
273 &ctx->Q ) ) != 0 )
274 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100275 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100276 }
277
278 ctx->len = mbedtls_mpi_size( &ctx->N );
279 }
280
281 /*
282 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100283 */
284
285 if( pq_missing )
286 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100287 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100288 &ctx->P, &ctx->Q );
289 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100290 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100291
292 }
293 else if( d_missing )
294 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100295 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
296 &ctx->Q,
297 &ctx->E,
298 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100299 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100300 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100301 }
302 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100303
Hanno Becker617c1ae2017-08-23 14:11:24 +0100304 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100305 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100306 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100307 */
308
Hanno Becker23344b52017-08-23 07:43:27 +0100309#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500310 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100311 {
312 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
313 &ctx->DP, &ctx->DQ, &ctx->QP );
314 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100315 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100316 }
Hanno Becker23344b52017-08-23 07:43:27 +0100317#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100318
319 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100320 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100321 */
322
Hanno Beckerebd2c022017-10-12 10:54:53 +0100323 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324}
325
Hanno Becker617c1ae2017-08-23 14:11:24 +0100326int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
327 unsigned char *N, size_t N_len,
328 unsigned char *P, size_t P_len,
329 unsigned char *Q, size_t Q_len,
330 unsigned char *D, size_t D_len,
331 unsigned char *E, size_t E_len )
332{
333 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500334 int is_priv;
335 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100336
337 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500338 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100339 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
340 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
341 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
342 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
343 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
344
345 if( !is_priv )
346 {
347 /* If we're trying to export private parameters for a public key,
348 * something must be wrong. */
349 if( P != NULL || Q != NULL || D != NULL )
350 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
351
352 }
353
354 if( N != NULL )
355 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
356
357 if( P != NULL )
358 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
359
360 if( Q != NULL )
361 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
362
363 if( D != NULL )
364 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
365
366 if( E != NULL )
367 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100368
369cleanup:
370
371 return( ret );
372}
373
Hanno Becker617c1ae2017-08-23 14:11:24 +0100374int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
375 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
376 mbedtls_mpi *D, mbedtls_mpi *E )
377{
Janos Follath24eed8d2019-11-22 13:21:35 +0000378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500379 int is_priv;
380 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100381
382 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500383 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100384 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
385 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
386 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
387 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
388 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
389
390 if( !is_priv )
391 {
392 /* If we're trying to export private parameters for a public key,
393 * something must be wrong. */
394 if( P != NULL || Q != NULL || D != NULL )
395 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
396
397 }
398
399 /* Export all requested core parameters. */
400
401 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
402 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
403 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
404 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
405 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
406 {
407 return( ret );
408 }
409
410 return( 0 );
411}
412
413/*
414 * Export CRT parameters
415 * This must also be implemented if CRT is not used, for being able to
416 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
417 * can be used in this case.
418 */
419int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
420 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
421{
Janos Follath24eed8d2019-11-22 13:21:35 +0000422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500423 int is_priv;
424 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100425
426 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500427 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100428 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
429 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
430 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
431 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
432 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
433
434 if( !is_priv )
435 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
436
Hanno Beckerdc95c892017-08-23 06:57:02 +0100437#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100438 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100439 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
440 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
441 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
442 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100443 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100445#else
446 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
447 DP, DQ, QP ) ) != 0 )
448 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100449 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100450 }
451#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100452
453 return( 0 );
454}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100455
Paul Bakker5121ce52009-01-03 21:22:43 +0000456/*
457 * Initialize an RSA context
458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000461 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000462{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500463 RSA_VALIDATE( ctx != NULL );
464 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
465 padding == MBEDTLS_RSA_PKCS_V21 );
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100472 /* Set ctx->ver to nonzero to indicate that the mutex has been
473 * initialized and will need to be freed. */
474 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200476#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000477}
478
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100479/*
480 * Set padding for an existing RSA context
481 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500482void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
483 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100484{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500485 RSA_VALIDATE( ctx != NULL );
486 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
487 padding == MBEDTLS_RSA_PKCS_V21 );
488
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100489 ctx->padding = padding;
490 ctx->hash_id = hash_id;
491}
492
Hanno Becker617c1ae2017-08-23 14:11:24 +0100493/*
494 * Get length in bytes of RSA modulus
495 */
496
497size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
498{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100499 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100500}
501
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
505/*
506 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800507 *
508 * This generation method follows the RSA key pair generation procedure of
509 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000512 int (*f_rng)(void *, unsigned char *, size_t),
513 void *p_rng,
514 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000515{
Janos Follath24eed8d2019-11-22 13:21:35 +0000516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800517 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100518 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500519 RSA_VALIDATE_RET( ctx != NULL );
520 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Janos Follathb8fc1b02018-09-03 15:37:01 +0100522 /*
523 * If the modulus is 1024 bit long or shorter, then the security strength of
524 * the RSA algorithm is less than or equal to 80 bits and therefore an error
525 * rate of 2^-80 is sufficient.
526 */
527 if( nbits > 1024 )
528 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
529
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100530 mbedtls_mpi_init( &H );
531 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800532 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100534 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
535 {
536 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
537 goto cleanup;
538 }
539
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 /*
541 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800542 * 1. |P-Q| > 2^( nbits / 2 - 100 )
543 * 2. GCD( E, (P-1)*(Q-1) ) == 1
544 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548 do
549 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100550 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
551 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Janos Follathb8fc1b02018-09-03 15:37:01 +0100553 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
554 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800556 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
557 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
558 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 continue;
560
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800561 /* not required by any standards, but some users rely on the fact that P > Q */
562 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100563 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100564
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100565 /* Temporarily replace P,Q by P-1, Q-1 */
566 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
567 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
568 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800569
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800570 /* 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 +0200571 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800572 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
573 continue;
574
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800575 /* 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 -0800576 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
577 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
578 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
579
580 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
581 continue;
582
583 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800585 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100587 /* Restore P,Q */
588 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
589 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
590
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800591 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
592
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100593 ctx->len = mbedtls_mpi_size( &ctx->N );
594
Jethro Beekman97f95c92018-02-13 15:50:36 -0800595#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 * DP = D mod (P - 1)
598 * DQ = D mod (Q - 1)
599 * QP = Q^-1 mod P
600 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100601 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
602 &ctx->DP, &ctx->DQ, &ctx->QP ) );
603#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Hanno Becker83aad1f2017-08-23 06:45:10 +0100605 /* Double-check */
606 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
608cleanup:
609
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100610 mbedtls_mpi_free( &H );
611 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800612 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614 if( ret != 0 )
615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100617
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100618 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100619 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100620 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 }
622
Paul Bakker48377d92013-08-30 12:06:24 +0200623 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000624}
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
628/*
629 * Check a public RSA key
630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000632{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500633 RSA_VALIDATE_RET( ctx != NULL );
634
Hanno Beckerebd2c022017-10-12 10:54:53 +0100635 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000637
Hanno Becker3a760a12018-01-05 08:14:49 +0000638 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100641 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
Hanno Becker705fc682017-10-10 17:57:02 +0100643 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
644 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100648 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
650 return( 0 );
651}
652
653/*
Hanno Becker705fc682017-10-10 17:57:02 +0100654 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500658 RSA_VALIDATE_RET( ctx != NULL );
659
Hanno Becker705fc682017-10-10 17:57:02 +0100660 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100661 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 {
Hanno Becker98838b02017-10-02 13:16:10 +0100663 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 }
Paul Bakker48377d92013-08-30 12:06:24 +0200665
Hanno Becker98838b02017-10-02 13:16:10 +0100666 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100667 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100669 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000671
Hanno Beckerb269a852017-08-25 08:03:21 +0100672#if !defined(MBEDTLS_RSA_NO_CRT)
673 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
674 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
675 {
676 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
677 }
678#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000679
680 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681}
682
683/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100684 * Check if contexts holding a public and private key match
685 */
Hanno Becker98838b02017-10-02 13:16:10 +0100686int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
687 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100688{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500689 RSA_VALIDATE_RET( pub != NULL );
690 RSA_VALIDATE_RET( prv != NULL );
691
Hanno Becker98838b02017-10-02 13:16:10 +0100692 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696 }
697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
699 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100702 }
703
704 return( 0 );
705}
706
707/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 * Do an RSA public key operation
709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000711 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 unsigned char *output )
713{
Janos Follath24eed8d2019-11-22 13:21:35 +0000714 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000715 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500717 RSA_VALIDATE_RET( ctx != NULL );
718 RSA_VALIDATE_RET( input != NULL );
719 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
Hanno Beckerebd2c022017-10-12 10:54:53 +0100721 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100722 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200726#if defined(MBEDTLS_THREADING_C)
727 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
728 return( ret );
729#endif
730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200735 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
736 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 }
738
739 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
741 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
743cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200745 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
746 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100747#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
751 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100752 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000753
754 return( 0 );
755}
756
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200757/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200758 * Generate or update blinding values, see section 10 of:
759 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200760 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200761 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200762 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200763static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200764 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
765{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200766 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200767 mbedtls_mpi R;
768
769 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200770
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200771 if( ctx->Vf.p != NULL )
772 {
773 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
775 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
776 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
777 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200778
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200779 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200780 }
781
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200782 /* Unblinding value: Vf = random number, invertible mod N */
783 do {
784 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200785 {
786 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
787 goto cleanup;
788 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 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 +0200791
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200792 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200793 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
794 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
795 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
796
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200797 /* At this point, Vi is invertible mod N if and only if both Vf and R
798 * are invertible mod N. If one of them isn't, we don't need to know
799 * which one, we just loop and choose new values for both of them.
800 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200801 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500802 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200803 goto cleanup;
804
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500805 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
806
807 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
808 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
809 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200810
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200811 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200812 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 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 +0200814
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200815
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200816cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200817 mbedtls_mpi_free( &R );
818
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200819 return( ret );
820}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200821
Paul Bakker5121ce52009-01-03 21:22:43 +0000822/*
Janos Follathe81102e2017-03-22 13:38:28 +0000823 * Exponent blinding supposed to prevent side-channel attacks using multiple
824 * traces of measurements to recover the RSA key. The more collisions are there,
825 * the more bits of the key can be recovered. See [3].
826 *
827 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case0e7791f2021-12-20 21:14:10 -0800828 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000829 *
830 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case0e7791f2021-12-20 21:14:10 -0800831 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000832 *
833 * (With the currently (as of 2017 April) known best algorithms breaking 2048
834 * bit RSA requires approximately as much time as trying out 2^112 random keys.
835 * Thus in this sense with 28 byte blinding the security is not reduced by
836 * side-channel attacks like the one in [3])
837 *
838 * This countermeasure does not help if the key recovery is possible with a
839 * single trace.
840 */
841#define RSA_EXPONENT_BLINDING 28
842
843/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000844 * Do an RSA private key operation
845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200847 int (*f_rng)(void *, unsigned char *, size_t),
848 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000849 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000850 unsigned char *output )
851{
Janos Follath24eed8d2019-11-22 13:21:35 +0000852 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000853 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100854
855 /* Temporary holding the result */
856 mbedtls_mpi T;
857
858 /* Temporaries holding P-1, Q-1 and the
859 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000860 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100861
862#if !defined(MBEDTLS_RSA_NO_CRT)
863 /* Temporaries holding the results mod p resp. mod q. */
864 mbedtls_mpi TP, TQ;
865
866 /* Temporaries holding the blinded exponents for
867 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000868 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100869
870 /* Pointers to actual exponents to be used - either the unblinded
871 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000872 mbedtls_mpi *DP = &ctx->DP;
873 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100874#else
875 /* Temporary holding the blinded exponent (if used). */
876 mbedtls_mpi D_blind;
877
878 /* Pointer to actual exponent to be used - either the unblinded
879 * or the blinded one, depending on the presence of a PRNG. */
880 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100881#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100882
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100883 /* Temporaries holding the initial input and the double
884 * checked result; should be the same in the end. */
885 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000886
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500887 RSA_VALIDATE_RET( ctx != NULL );
888 RSA_VALIDATE_RET( input != NULL );
889 RSA_VALIDATE_RET( output != NULL );
890
Hanno Beckerebd2c022017-10-12 10:54:53 +0100891 if( rsa_check_context( ctx, 1 /* private key checks */,
892 f_rng != NULL /* blinding y/n */ ) != 0 )
893 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100894 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100895 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100896
Hanno Becker06811ce2017-05-03 15:10:34 +0100897#if defined(MBEDTLS_THREADING_C)
898 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
899 return( ret );
900#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000901
Hanno Becker06811ce2017-05-03 15:10:34 +0100902 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100903 mbedtls_mpi_init( &T );
904
905 mbedtls_mpi_init( &P1 );
906 mbedtls_mpi_init( &Q1 );
907 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000908
Janos Follathf9203b42017-03-22 15:13:15 +0000909 if( f_rng != NULL )
910 {
Janos Follathe81102e2017-03-22 13:38:28 +0000911#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000912 mbedtls_mpi_init( &D_blind );
913#else
914 mbedtls_mpi_init( &DP_blind );
915 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000916#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000917 }
Janos Follathe81102e2017-03-22 13:38:28 +0000918
Hanno Becker06811ce2017-05-03 15:10:34 +0100919#if !defined(MBEDTLS_RSA_NO_CRT)
920 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200921#endif
922
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100923 mbedtls_mpi_init( &I );
924 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100925
926 /* End of MPI initialization */
927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
929 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200931 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
932 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 }
934
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100935 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100936
Paul Bakkerf451bac2013-08-30 15:37:02 +0200937 if( f_rng != NULL )
938 {
939 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200940 * Blinding
941 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200942 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200943 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
944 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000946
Janos Follathe81102e2017-03-22 13:38:28 +0000947 /*
948 * Exponent blinding
949 */
950 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
951 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
952
Janos Follathf9203b42017-03-22 15:13:15 +0000953#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000954 /*
955 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
956 */
957 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
958 f_rng, p_rng ) );
959 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
960 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
961 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
962
963 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000964#else
965 /*
966 * DP_blind = ( P - 1 ) * R + DP
967 */
968 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
969 f_rng, p_rng ) );
970 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
971 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
972 &ctx->DP ) );
973
974 DP = &DP_blind;
975
976 /*
977 * DQ_blind = ( Q - 1 ) * R + DQ
978 */
979 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
980 f_rng, p_rng ) );
981 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
982 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
983 &ctx->DQ ) );
984
985 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000986#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200987 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000990 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100991#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200992 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000993 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100995 * TP = input ^ dP mod P
996 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000997 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100998
999 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1000 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
1002 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001003 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001004 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001005 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1006 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1007 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
1009 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001010 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001012 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1013 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001015
Paul Bakkerf451bac2013-08-30 15:37:02 +02001016 if( f_rng != NULL )
1017 {
1018 /*
1019 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001020 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001021 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001022 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001024 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
Hanno Becker2dec5e82017-10-03 07:49:52 +01001026 /* Verify the result to prevent glitching attacks. */
1027 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1028 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001029 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001030 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001031 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1032 goto cleanup;
1033 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001034
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
1038cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001040 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1041 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001042#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001043
Hanno Becker06811ce2017-05-03 15:10:34 +01001044 mbedtls_mpi_free( &P1 );
1045 mbedtls_mpi_free( &Q1 );
1046 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001047
1048 if( f_rng != NULL )
1049 {
Janos Follathe81102e2017-03-22 13:38:28 +00001050#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001051 mbedtls_mpi_free( &D_blind );
1052#else
1053 mbedtls_mpi_free( &DP_blind );
1054 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001055#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
Hanno Becker06811ce2017-05-03 15:10:34 +01001058 mbedtls_mpi_free( &T );
1059
1060#if !defined(MBEDTLS_RSA_NO_CRT)
1061 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1062#endif
1063
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001064 mbedtls_mpi_free( &C );
1065 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001066
Gilles Peskineae3741e2020-11-25 00:10:31 +01001067 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001068 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001069
Gilles Peskineae3741e2020-11-25 00:10:31 +01001070 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001071}
1072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001074/**
1075 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1076 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001077 * \param dst buffer to mask
1078 * \param dlen length of destination buffer
1079 * \param src source of the mask generation
1080 * \param slen length of the source buffer
1081 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001082 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001083static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001085{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001087 unsigned char counter[4];
1088 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001089 unsigned int hlen;
1090 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001091 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001094 memset( counter, 0, 4 );
1095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001097
Simon Butcher02037452016-03-01 21:19:12 +00001098 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001099 p = dst;
1100
1101 while( dlen > 0 )
1102 {
1103 use_len = hlen;
1104 if( dlen < hlen )
1105 use_len = dlen;
1106
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001107 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1108 goto exit;
1109 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1110 goto exit;
1111 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1112 goto exit;
1113 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1114 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001115
1116 for( i = 0; i < use_len; ++i )
1117 *p++ ^= mask[i];
1118
1119 counter[3]++;
1120
1121 dlen -= use_len;
1122 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001123
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001124exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001125 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001126
1127 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001132/*
1133 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001136 int (*f_rng)(void *, unsigned char *, size_t),
1137 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001138 int mode,
1139 const unsigned char *label, size_t label_len,
1140 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001141 const unsigned char *input,
1142 unsigned char *output )
1143{
1144 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001145 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001146 unsigned char *p = output;
1147 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 const mbedtls_md_info_t *md_info;
1149 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001150
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001151 RSA_VALIDATE_RET( ctx != NULL );
1152 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1153 mode == MBEDTLS_RSA_PUBLIC );
1154 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001155 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001156 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1159 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001160
1161 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001165 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001167
1168 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001170
Simon Butcher02037452016-03-01 21:19:12 +00001171 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001172 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
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
1175 memset( output, 0, olen );
1176
1177 *p++ = 0;
1178
Simon Butcher02037452016-03-01 21:19:12 +00001179 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001180 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001181 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001182
1183 p += hlen;
1184
Simon Butcher02037452016-03-01 21:19:12 +00001185 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001186 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1187 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001188 p += hlen;
1189 p += olen - 2 * hlen - 2 - ilen;
1190 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001191 if( ilen != 0 )
1192 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001195 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001196 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001197
Simon Butcher02037452016-03-01 21:19:12 +00001198 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001199 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1200 &md_ctx ) ) != 0 )
1201 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001202
Simon Butcher02037452016-03-01 21:19:12 +00001203 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001204 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1205 &md_ctx ) ) != 0 )
1206 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001207
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001208exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001210
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001211 if( ret != 0 )
1212 return( ret );
1213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 return( ( mode == MBEDTLS_RSA_PUBLIC )
1215 ? mbedtls_rsa_public( ctx, output, output )
1216 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001217}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001221/*
1222 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001225 int (*f_rng)(void *, unsigned char *, size_t),
1226 void *p_rng,
1227 int mode, size_t ilen,
1228 const unsigned char *input,
1229 unsigned char *output )
1230{
1231 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001233 unsigned char *p = output;
1234
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001235 RSA_VALIDATE_RET( ctx != NULL );
1236 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1237 mode == MBEDTLS_RSA_PUBLIC );
1238 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001239 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001240
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001241 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001243
1244 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001245
Simon Butcher02037452016-03-01 21:19:12 +00001246 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001247 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001249
1250 nb_pad = olen - 3 - ilen;
1251
1252 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001254 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001255 if( f_rng == NULL )
1256 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001259
1260 while( nb_pad-- > 0 )
1261 {
1262 int rng_dl = 100;
1263
1264 do {
1265 ret = f_rng( p_rng, p, 1 );
1266 } while( *p == 0 && --rng_dl && ret == 0 );
1267
Simon Butcher02037452016-03-01 21:19:12 +00001268 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001269 if( rng_dl == 0 || ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001270 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001271
1272 p++;
1273 }
1274 }
1275 else
1276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001278
1279 while( nb_pad-- > 0 )
1280 *p++ = 0xFF;
1281 }
1282
1283 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001284 if( ilen != 0 )
1285 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 return( ( mode == MBEDTLS_RSA_PUBLIC )
1288 ? mbedtls_rsa_public( ctx, output, output )
1289 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001290}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001292
Paul Bakker5121ce52009-01-03 21:22:43 +00001293/*
1294 * Add the message padding, then do an RSA operation
1295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001297 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001298 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001299 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001300 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001301 unsigned char *output )
1302{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001303 RSA_VALIDATE_RET( ctx != NULL );
1304 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1305 mode == MBEDTLS_RSA_PUBLIC );
1306 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001307 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001308
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 switch( ctx->padding )
1310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#if defined(MBEDTLS_PKCS1_V15)
1312 case MBEDTLS_RSA_PKCS_V15:
1313 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001314 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001315#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#if defined(MBEDTLS_PKCS1_V21)
1318 case MBEDTLS_RSA_PKCS_V21:
1319 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001320 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001321#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
1323 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001326}
1327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001329/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001330 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001333 int (*f_rng)(void *, unsigned char *, size_t),
1334 void *p_rng,
1335 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001336 const unsigned char *label, size_t label_len,
1337 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001338 const unsigned char *input,
1339 unsigned char *output,
1340 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001341{
Janos Follath24eed8d2019-11-22 13:21:35 +00001342 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001343 size_t ilen, i, pad_len;
1344 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1346 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001347 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 const mbedtls_md_info_t *md_info;
1349 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001350
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001351 RSA_VALIDATE_RET( ctx != NULL );
1352 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1353 mode == MBEDTLS_RSA_PUBLIC );
1354 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1355 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1356 RSA_VALIDATE_RET( input != NULL );
1357 RSA_VALIDATE_RET( olen != NULL );
1358
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001359 /*
1360 * Parameters sanity checks
1361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1363 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
1365 ilen = ctx->len;
1366
Paul Bakker27fdf462011-06-09 13:55:13 +00001367 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001371 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001373
Janos Follathc17cda12016-02-11 11:08:18 +00001374 hlen = mbedtls_md_get_size( md_info );
1375
1376 // checking for integer underflow
1377 if( 2 * hlen + 2 > ilen )
1378 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1379
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001380 /*
1381 * RSA operation
1382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1384 ? mbedtls_rsa_public( ctx, input, buf )
1385 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
1387 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001388 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001390 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001391 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001394 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1395 {
1396 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001397 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001398 }
1399
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001400 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001401 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1402 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001403 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001404 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1405 &md_ctx ) ) != 0 )
1406 {
1407 mbedtls_md_free( &md_ctx );
1408 goto cleanup;
1409 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001412
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001413 /* Generate lHash */
1414 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1415 goto cleanup;
1416
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001417 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001418 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001419 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001421 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001422
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001423 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001424
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001425 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001426
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001427 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001428 for( i = 0; i < hlen; i++ )
1429 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001430
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001431 /* Get zero-padding len, but always read till end of buffer
1432 * (minus one, for the 01 byte) */
1433 pad_len = 0;
1434 pad_done = 0;
1435 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1436 {
1437 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001438 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001439 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001440
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001441 p += pad_len;
1442 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001443
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001444 /*
1445 * The only information "leaked" is whether the padding was correct or not
1446 * (eg, no data is copied if it was not correct). This meets the
1447 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1448 * the different error conditions.
1449 */
1450 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001451 {
1452 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1453 goto cleanup;
1454 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001455
Paul Bakker66d5d072014-06-17 16:39:18 +02001456 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001457 {
1458 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1459 goto cleanup;
1460 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001461
1462 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001463 if( *olen != 0 )
1464 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001465 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001466
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001467cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001468 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1469 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001470
1471 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001472}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001476/*
1477 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1478 */
1479int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
1480 int (*f_rng)(void *, unsigned char *, size_t),
1481 void *p_rng,
1482 int mode,
1483 size_t *olen,
1484 const unsigned char *input,
1485 unsigned char *output,
1486 size_t output_max_len )
1487{
1488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1489 size_t ilen;
1490 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1491
1492 RSA_VALIDATE_RET( ctx != NULL );
1493 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1494 mode == MBEDTLS_RSA_PUBLIC );
1495 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1496 RSA_VALIDATE_RET( input != NULL );
1497 RSA_VALIDATE_RET( olen != NULL );
1498
1499 ilen = ctx->len;
1500
1501 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1502 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1503
1504 if( ilen < 16 || ilen > sizeof( buf ) )
1505 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1506
1507 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1508 ? mbedtls_rsa_public( ctx, input, buf )
1509 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
1510
1511 if( ret != 0 )
1512 goto cleanup;
1513
Gabor Mezei18a44942021-10-20 11:59:27 +02001514 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding( mode, buf, ilen,
Gabor Mezei91deea72021-10-18 16:17:57 +02001515 output, output_max_len, olen );
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001516
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001517cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001518 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001519
1520 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001523
1524/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001525 * Do an RSA operation, then remove the message padding
1526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001528 int (*f_rng)(void *, unsigned char *, size_t),
1529 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001530 int mode, size_t *olen,
1531 const unsigned char *input,
1532 unsigned char *output,
1533 size_t output_max_len)
1534{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001535 RSA_VALIDATE_RET( ctx != NULL );
1536 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1537 mode == MBEDTLS_RSA_PUBLIC );
1538 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1539 RSA_VALIDATE_RET( input != NULL );
1540 RSA_VALIDATE_RET( olen != NULL );
1541
Paul Bakkerb3869132013-02-28 17:21:01 +01001542 switch( ctx->padding )
1543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_PKCS1_V15)
1545 case MBEDTLS_RSA_PKCS_V15:
1546 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001547 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001548#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#if defined(MBEDTLS_PKCS1_V21)
1551 case MBEDTLS_RSA_PKCS_V21:
1552 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001553 olen, input, output,
1554 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001555#endif
1556
1557 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001559 }
1560}
1561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001563static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001564 int (*f_rng)(void *, unsigned char *, size_t),
1565 void *p_rng,
1566 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001568 unsigned int hashlen,
1569 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001570 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001571 unsigned char *sig )
1572{
1573 size_t olen;
1574 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001575 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001576 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001578 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 const mbedtls_md_info_t *md_info;
1580 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001581 RSA_VALIDATE_RET( ctx != NULL );
1582 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1583 mode == MBEDTLS_RSA_PUBLIC );
1584 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1585 hashlen == 0 ) ||
1586 hash != NULL );
1587 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1590 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001591
1592 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001594
1595 olen = ctx->len;
1596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001598 {
Simon Butcher02037452016-03-01 21:19:12 +00001599 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001601 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001605 }
1606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001608 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001612
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001613 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1614 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001615 /* Calculate the largest possible salt length, up to the hash size.
1616 * Normally this is the hash length, which is the maximum salt length
1617 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001618 * enough room, use the maximum salt length that fits. The constraint is
1619 * that the hash length plus the salt length plus 2 bytes must be at most
1620 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1621 * (PKCS#1 v2.2) §9.1.1 step 3. */
1622 min_slen = hlen - 2;
1623 if( olen < hlen + min_slen + 2 )
1624 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1625 else if( olen >= hlen + hlen + 2 )
1626 slen = hlen;
1627 else
1628 slen = olen - hlen - 2;
1629 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001630 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001633 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001634 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001635 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001636 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001637 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001638
1639 memset( sig, 0, olen );
1640
Simon Butcher02037452016-03-01 21:19:12 +00001641 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001642 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001643 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001644 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001645
1646 /* Generate salt of length slen in place in the encoded message */
1647 salt = p;
1648 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001649 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001650
Paul Bakkerb3869132013-02-28 17:21:01 +01001651 p += slen;
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001654 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001655 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001656
Simon Butcher02037452016-03-01 21:19:12 +00001657 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001658 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1659 goto exit;
1660 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1661 goto exit;
1662 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1663 goto exit;
1664 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1665 goto exit;
1666 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1667 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001668
Simon Butcher02037452016-03-01 21:19:12 +00001669 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001670 if( msb % 8 == 0 )
1671 offset = 1;
1672
Simon Butcher02037452016-03-01 21:19:12 +00001673 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001674 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1675 &md_ctx ) ) != 0 )
1676 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001677
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001678 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001679 sig[0] &= 0xFF >> ( olen * 8 - msb );
1680
1681 p += hlen;
1682 *p++ = 0xBC;
1683
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001684exit:
1685 mbedtls_md_free( &md_ctx );
1686
1687 if( ret != 0 )
1688 return( ret );
1689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 return( ( mode == MBEDTLS_RSA_PUBLIC )
1691 ? mbedtls_rsa_public( ctx, sig, sig )
1692 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001693}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001694
1695/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001696 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1697 * the option to pass in the salt length.
1698 */
1699int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1700 int (*f_rng)(void *, unsigned char *, size_t),
1701 void *p_rng,
1702 mbedtls_md_type_t md_alg,
1703 unsigned int hashlen,
1704 const unsigned char *hash,
1705 int saltlen,
1706 unsigned char *sig )
1707{
1708 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, md_alg,
1709 hashlen, hash, saltlen, sig );
1710}
1711
1712
1713/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001714 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1715 */
1716int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1717 int (*f_rng)(void *, unsigned char *, size_t),
1718 void *p_rng,
1719 int mode,
1720 mbedtls_md_type_t md_alg,
1721 unsigned int hashlen,
1722 const unsigned char *hash,
1723 unsigned char *sig )
1724{
Cédric Meuterf3fab332020-04-25 11:30:45 +02001725 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
1726 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001727}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001731/*
1732 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1733 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001734
1735/* Construct a PKCS v1.5 encoding of a hashed message
1736 *
1737 * This is used both for signature generation and verification.
1738 *
1739 * Parameters:
1740 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001741 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001742 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001743 * - hash: Buffer containing the hashed message or the raw data.
1744 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001745 * - dst: Buffer to hold the encoded message.
1746 *
1747 * Assumptions:
1748 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1749 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001750 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001751 *
1752 */
1753static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1754 unsigned int hashlen,
1755 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001756 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001757 unsigned char *dst )
1758{
1759 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001760 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001761 unsigned char *p = dst;
1762 const char *oid = NULL;
1763
1764 /* Are we signing hashed or raw data? */
1765 if( md_alg != MBEDTLS_MD_NONE )
1766 {
1767 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1768 if( md_info == NULL )
1769 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1770
1771 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1772 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1773
1774 hashlen = mbedtls_md_get_size( md_info );
1775
1776 /* Double-check that 8 + hashlen + oid_size can be used as a
1777 * 1-byte ASN.1 length encoding and that there's no overflow. */
1778 if( 8 + hashlen + oid_size >= 0x80 ||
1779 10 + hashlen < hashlen ||
1780 10 + hashlen + oid_size < 10 + hashlen )
1781 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1782
1783 /*
1784 * Static bounds check:
1785 * - Need 10 bytes for five tag-length pairs.
1786 * (Insist on 1-byte length encodings to protect against variants of
1787 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1788 * - Need hashlen bytes for hash
1789 * - Need oid_size bytes for hash alg OID.
1790 */
1791 if( nb_pad < 10 + hashlen + oid_size )
1792 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1793 nb_pad -= 10 + hashlen + oid_size;
1794 }
1795 else
1796 {
1797 if( nb_pad < hashlen )
1798 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1799
1800 nb_pad -= hashlen;
1801 }
1802
Hanno Becker2b2f8982017-09-27 17:10:03 +01001803 /* Need space for signature header and padding delimiter (3 bytes),
1804 * and 8 bytes for the minimal padding */
1805 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001806 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1807 nb_pad -= 3;
1808
1809 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001810 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001811
1812 /* Write signature header and padding */
1813 *p++ = 0;
1814 *p++ = MBEDTLS_RSA_SIGN;
1815 memset( p, 0xFF, nb_pad );
1816 p += nb_pad;
1817 *p++ = 0;
1818
1819 /* Are we signing raw data? */
1820 if( md_alg == MBEDTLS_MD_NONE )
1821 {
1822 memcpy( p, hash, hashlen );
1823 return( 0 );
1824 }
1825
1826 /* Signing hashed data, add corresponding ASN.1 structure
1827 *
1828 * DigestInfo ::= SEQUENCE {
1829 * digestAlgorithm DigestAlgorithmIdentifier,
1830 * digest Digest }
1831 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1832 * Digest ::= OCTET STRING
1833 *
1834 * Schematic:
1835 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1836 * TAG-NULL + LEN [ NULL ] ]
1837 * TAG-OCTET + LEN [ HASH ] ]
1838 */
1839 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001840 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001841 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001842 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001843 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001844 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001845 memcpy( p, oid, oid_size );
1846 p += oid_size;
1847 *p++ = MBEDTLS_ASN1_NULL;
1848 *p++ = 0x00;
1849 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001850 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001851 memcpy( p, hash, hashlen );
1852 p += hashlen;
1853
1854 /* Just a sanity-check, should be automatic
1855 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001856 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001857 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001858 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001859 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1860 }
1861
1862 return( 0 );
1863}
1864
Paul Bakkerb3869132013-02-28 17:21:01 +01001865/*
1866 * Do an RSA operation to sign the message digest
1867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001869 int (*f_rng)(void *, unsigned char *, size_t),
1870 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001871 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001873 unsigned int hashlen,
1874 const unsigned char *hash,
1875 unsigned char *sig )
1876{
Janos Follath24eed8d2019-11-22 13:21:35 +00001877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001878 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001879
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001880 RSA_VALIDATE_RET( ctx != NULL );
1881 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1882 mode == MBEDTLS_RSA_PUBLIC );
1883 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1884 hashlen == 0 ) ||
1885 hash != NULL );
1886 RSA_VALIDATE_RET( sig != NULL );
1887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1889 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001890
Hanno Beckerfdf38032017-09-06 12:35:55 +01001891 /*
1892 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1893 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001894
Hanno Beckerfdf38032017-09-06 12:35:55 +01001895 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1896 ctx->len, sig ) ) != 0 )
1897 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001898
1899 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001900 * Call respective RSA primitive
1901 */
1902
1903 if( mode == MBEDTLS_RSA_PUBLIC )
1904 {
1905 /* Skip verification on a public key operation */
1906 return( mbedtls_rsa_public( ctx, sig, sig ) );
1907 }
1908
1909 /* Private key operation
1910 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001911 * In order to prevent Lenstra's attack, make the signature in a
1912 * temporary buffer and check it before returning it.
1913 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001914
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001915 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001916 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001917 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1918
Hanno Beckerfdf38032017-09-06 12:35:55 +01001919 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001920 if( verif == NULL )
1921 {
1922 mbedtls_free( sig_try );
1923 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1924 }
1925
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001926 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1927 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1928
Gabor Mezei18a44942021-10-20 11:59:27 +02001929 if( mbedtls_ct_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001930 {
1931 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1932 goto cleanup;
1933 }
1934
1935 memcpy( sig, sig_try, ctx->len );
1936
1937cleanup:
Gilles Peskine8c99a762021-12-13 12:37:55 +01001938 mbedtls_platform_zeroize( sig_try, ctx->len );
1939 mbedtls_platform_zeroize( verif, ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001940 mbedtls_free( sig_try );
1941 mbedtls_free( verif );
1942
Gilles Peskine8c99a762021-12-13 12:37:55 +01001943 if( ret != 0 )
1944 memset( sig, '!', ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001945 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001946}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001948
1949/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 * Do an RSA operation to sign the message digest
1951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001953 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001954 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001957 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001958 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 unsigned char *sig )
1960{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001961 RSA_VALIDATE_RET( ctx != NULL );
1962 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1963 mode == MBEDTLS_RSA_PUBLIC );
1964 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1965 hashlen == 0 ) ||
1966 hash != NULL );
1967 RSA_VALIDATE_RET( sig != NULL );
1968
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 switch( ctx->padding )
1970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971#if defined(MBEDTLS_PKCS1_V15)
1972 case MBEDTLS_RSA_PKCS_V15:
1973 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001974 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001975#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977#if defined(MBEDTLS_PKCS1_V21)
1978 case MBEDTLS_RSA_PKCS_V21:
1979 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001980 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001981#endif
1982
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001985 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001986}
1987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001989/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001990 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001993 int (*f_rng)(void *, unsigned char *, size_t),
1994 void *p_rng,
1995 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001997 unsigned int hashlen,
1998 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002000 int expected_salt_len,
2001 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002002{
Janos Follath24eed8d2019-11-22 13:21:35 +00002003 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002004 size_t siglen;
2005 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002006 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002008 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002009 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002010 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 const mbedtls_md_info_t *md_info;
2012 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002013 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002014
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002015 RSA_VALIDATE_RET( ctx != NULL );
2016 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2017 mode == MBEDTLS_RSA_PUBLIC );
2018 RSA_VALIDATE_RET( sig != NULL );
2019 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2020 hashlen == 0 ) ||
2021 hash != NULL );
2022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2024 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002025
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 siglen = ctx->len;
2027
Paul Bakker27fdf462011-06-09 13:55:13 +00002028 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2032 ? mbedtls_rsa_public( ctx, sig, buf )
2033 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002034
2035 if( ret != 0 )
2036 return( ret );
2037
2038 p = buf;
2039
Paul Bakkerb3869132013-02-28 17:21:01 +01002040 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 {
Simon Butcher02037452016-03-01 21:19:12 +00002045 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002047 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002051 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002054 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002058
Paul Bakkerb3869132013-02-28 17:21:01 +01002059 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002060
Simon Butcher02037452016-03-01 21:19:12 +00002061 /*
2062 * Note: EMSA-PSS verification is over the length of N - 1 bits
2063 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002064 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002065
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002066 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2067 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2068
Simon Butcher02037452016-03-01 21:19:12 +00002069 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002070 if( msb % 8 == 0 )
2071 {
2072 p++;
2073 siglen -= 1;
2074 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002075
Gilles Peskine139108a2017-10-18 19:03:42 +02002076 if( siglen < hlen + 2 )
2077 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2078 hash_start = p + siglen - hlen - 1;
2079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002081 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002082 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002083
Jaeden Amero66954e12018-01-25 16:05:54 +00002084 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2085 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002086 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002087
Paul Bakkerb3869132013-02-28 17:21:01 +01002088 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002089
Gilles Peskine6a54b022017-10-17 19:02:13 +02002090 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002091 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002092
Gilles Peskine91048a32017-10-19 17:46:14 +02002093 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002094 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002095 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2096 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002097 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002098
Gilles Peskine6a54b022017-10-17 19:02:13 +02002099 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002102 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002103 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002104 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2105 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002106 }
2107
Simon Butcher02037452016-03-01 21:19:12 +00002108 /*
2109 * Generate H = Hash( M' )
2110 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002111 ret = mbedtls_md_starts( &md_ctx );
2112 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002113 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002114 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2115 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002116 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002117 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2118 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002119 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002120 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2121 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002122 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002123 ret = mbedtls_md_finish( &md_ctx, result );
2124 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002125 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002126
Jaeden Amero66954e12018-01-25 16:05:54 +00002127 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002128 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002129 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002130 goto exit;
2131 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002132
2133exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002135
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002136 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002137}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002138
2139/*
2140 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002143 int (*f_rng)(void *, unsigned char *, size_t),
2144 void *p_rng,
2145 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002147 unsigned int hashlen,
2148 const unsigned char *hash,
2149 const unsigned char *sig )
2150{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002151 mbedtls_md_type_t mgf1_hash_id;
2152 RSA_VALIDATE_RET( ctx != NULL );
2153 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2154 mode == MBEDTLS_RSA_PUBLIC );
2155 RSA_VALIDATE_RET( sig != NULL );
2156 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2157 hashlen == 0 ) ||
2158 hash != NULL );
2159
2160 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002162 : md_alg;
2163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002165 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002167 sig ) );
2168
2169}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002173/*
2174 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002177 int (*f_rng)(void *, unsigned char *, size_t),
2178 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002179 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002181 unsigned int hashlen,
2182 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002183 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002184{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002185 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002186 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002187 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002188
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002189 RSA_VALIDATE_RET( ctx != NULL );
2190 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2191 mode == MBEDTLS_RSA_PUBLIC );
2192 RSA_VALIDATE_RET( sig != NULL );
2193 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2194 hashlen == 0 ) ||
2195 hash != NULL );
2196
2197 sig_len = ctx->len;
2198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2200 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002201
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002202 /*
2203 * Prepare expected PKCS1 v1.5 encoding of hash.
2204 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002205
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002206 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2207 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2208 {
2209 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2210 goto cleanup;
2211 }
2212
2213 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2214 encoded_expected ) ) != 0 )
2215 goto cleanup;
2216
2217 /*
2218 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2219 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002222 ? mbedtls_rsa_public( ctx, sig, encoded )
2223 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002224 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002225 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002226
Simon Butcher02037452016-03-01 21:19:12 +00002227 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002228 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002229 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002230
Gabor Mezei18a44942021-10-20 11:59:27 +02002231 if( ( ret = mbedtls_ct_memcmp( encoded, encoded_expected,
gabor-mezei-arm378e7eb2021-07-19 15:19:19 +02002232 sig_len ) ) != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002233 {
2234 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2235 goto cleanup;
2236 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002237
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002238cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002239
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002240 if( encoded != NULL )
2241 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002242 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002243 mbedtls_free( encoded );
2244 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002245
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002246 if( encoded_expected != NULL )
2247 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002248 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002249 mbedtls_free( encoded_expected );
2250 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002251
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002252 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
2256/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002257 * Do an RSA operation and check the message digest
2258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002260 int (*f_rng)(void *, unsigned char *, size_t),
2261 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002262 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002264 unsigned int hashlen,
2265 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002266 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002267{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002268 RSA_VALIDATE_RET( ctx != NULL );
2269 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2270 mode == MBEDTLS_RSA_PUBLIC );
2271 RSA_VALIDATE_RET( sig != NULL );
2272 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2273 hashlen == 0 ) ||
2274 hash != NULL );
2275
Paul Bakkerb3869132013-02-28 17:21:01 +01002276 switch( ctx->padding )
2277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#if defined(MBEDTLS_PKCS1_V15)
2279 case MBEDTLS_RSA_PKCS_V15:
2280 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002281 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002282#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#if defined(MBEDTLS_PKCS1_V21)
2285 case MBEDTLS_RSA_PKCS_V21:
2286 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002287 hashlen, hash, sig );
2288#endif
2289
2290 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002292 }
2293}
2294
2295/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002296 * Copy the components of an RSA key
2297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002299{
Janos Follath24eed8d2019-11-22 13:21:35 +00002300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002301 RSA_VALIDATE_RET( dst != NULL );
2302 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002303
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002304 dst->len = src->len;
2305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2307 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2310 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2311 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002312
2313#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2315 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2316 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2318 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002319#endif
2320
2321 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2324 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002325
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002326 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002327 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002328
2329cleanup:
2330 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002332
2333 return( ret );
2334}
2335
2336/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002337 * Free the components of an RSA key
2338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002340{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002341 if( ctx == NULL )
2342 return;
2343
2344 mbedtls_mpi_free( &ctx->Vi );
2345 mbedtls_mpi_free( &ctx->Vf );
2346 mbedtls_mpi_free( &ctx->RN );
2347 mbedtls_mpi_free( &ctx->D );
2348 mbedtls_mpi_free( &ctx->Q );
2349 mbedtls_mpi_free( &ctx->P );
2350 mbedtls_mpi_free( &ctx->E );
2351 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002352
Hanno Becker33c30a02017-08-23 07:00:22 +01002353#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002354 mbedtls_mpi_free( &ctx->RQ );
2355 mbedtls_mpi_free( &ctx->RP );
2356 mbedtls_mpi_free( &ctx->QP );
2357 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002358 mbedtls_mpi_free( &ctx->DP );
2359#endif /* MBEDTLS_RSA_NO_CRT */
2360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002362 /* Free the mutex, but only if it hasn't been freed already. */
2363 if( ctx->ver != 0 )
2364 {
2365 mbedtls_mutex_free( &ctx->mutex );
2366 ctx->ver = 0;
2367 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002368#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002369}
2370
Hanno Beckerab377312017-08-23 16:24:51 +01002371#endif /* !MBEDTLS_RSA_ALT */
2372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002374
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002375#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002376
2377/*
2378 * Example RSA-1024 keypair, for test purposes
2379 */
2380#define KEY_LEN 128
2381
2382#define RSA_N "9292758453063D803DD603D5E777D788" \
2383 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2384 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2385 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2386 "93A89813FBF3C4F8066D2D800F7C38A8" \
2387 "1AE31942917403FF4946B0A83D3D3E05" \
2388 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2389 "5E94BB77B07507233A0BC7BAC8F90F79"
2390
2391#define RSA_E "10001"
2392
2393#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2394 "66CA472BC44D253102F8B4A9D3BFA750" \
2395 "91386C0077937FE33FA3252D28855837" \
2396 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2397 "DF79C5CE07EE72C7F123142198164234" \
2398 "CABB724CF78B8173B9F880FC86322407" \
2399 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2400 "071513A1E85B5DFA031F21ECAE91A34D"
2401
2402#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2403 "2C01CAD19EA484A87EA4377637E75500" \
2404 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2405 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2406
2407#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2408 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2409 "910E4168387E3C30AA1E00C339A79508" \
2410 "8452DD96A9A5EA5D9DCA68DA636032AF"
2411
Paul Bakker5121ce52009-01-03 21:22:43 +00002412#define PT_LEN 24
2413#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2414 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002417static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002418{
gufe44c2620da2020-08-03 17:56:50 +02002419#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002420 size_t i;
2421
Paul Bakker545570e2010-07-18 09:00:25 +00002422 if( rng_state != NULL )
2423 rng_state = NULL;
2424
Paul Bakkera3d195c2011-11-27 21:07:34 +00002425 for( i = 0; i < len; ++i )
2426 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002427#else
2428 if( rng_state != NULL )
2429 rng_state = NULL;
2430
2431 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002432#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002433
Paul Bakkera3d195c2011-11-27 21:07:34 +00002434 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002437
Paul Bakker5121ce52009-01-03 21:22:43 +00002438/*
2439 * Checkup routine
2440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002442{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002443 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002445 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 unsigned char rsa_plaintext[PT_LEN];
2448 unsigned char rsa_decrypted[PT_LEN];
2449 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002451 unsigned char sha1sum[20];
2452#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
Hanno Becker3a701162017-08-22 13:52:43 +01002454 mbedtls_mpi K;
2455
2456 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
Hanno Becker3a701162017-08-22 13:52:43 +01002459 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2460 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2461 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2462 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2463 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2464 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2465 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2466 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2467 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2468 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2469
Hanno Becker7f25f852017-10-10 16:56:22 +01002470 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
2472 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2476 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 {
2478 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Hanno Becker5bc87292017-05-03 15:09:31 +01002481 ret = 1;
2482 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 }
2484
2485 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
2488 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2489
Hanno Becker98838b02017-10-02 13:16:10 +01002490 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2491 PT_LEN, rsa_plaintext,
2492 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 {
2494 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Hanno Becker5bc87292017-05-03 15:09:31 +01002497 ret = 1;
2498 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 }
2500
2501 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Hanno Becker98838b02017-10-02 13:16:10 +01002504 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2505 &len, rsa_ciphertext, rsa_decrypted,
2506 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 {
2508 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Hanno Becker5bc87292017-05-03 15:09:31 +01002511 ret = 1;
2512 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 }
2514
2515 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2516 {
2517 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Hanno Becker5bc87292017-05-03 15:09:31 +01002520 ret = 1;
2521 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 }
2523
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002524 if( verbose != 0 )
2525 mbedtls_printf( "passed\n" );
2526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002529 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002531 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002532 {
2533 if( verbose != 0 )
2534 mbedtls_printf( "failed\n" );
2535
2536 return( 1 );
2537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002538
Hanno Becker98838b02017-10-02 13:16:10 +01002539 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2540 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2541 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 {
2543 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Hanno Becker5bc87292017-05-03 15:09:31 +01002546 ret = 1;
2547 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 }
2549
2550 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
Hanno Becker98838b02017-10-02 13:16:10 +01002553 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2554 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2555 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 {
2557 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Hanno Becker5bc87292017-05-03 15:09:31 +01002560 ret = 1;
2561 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 }
2563
2564 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002565 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002568 if( verbose != 0 )
2569 mbedtls_printf( "\n" );
2570
Paul Bakker3d8fb632014-04-17 12:42:41 +02002571cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002572 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 mbedtls_rsa_free( &rsa );
2574#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002575 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002577 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578}
2579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582#endif /* MBEDTLS_RSA_C */