blob: a395542c3add99a297f62c28ba24482b7f177915 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#else
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020065#define mbedtls_calloc calloc
66#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#endif
68
Hanno Beckera565f542017-10-11 11:00:19 +010069#if !defined(MBEDTLS_RSA_ALT)
70
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050071/* Parameter validation macros */
72#define RSA_VALIDATE_RET( cond ) \
73 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
74#define RSA_VALIDATE( cond ) \
75 MBEDTLS_INTERNAL_VALIDATE( cond )
76
Hanno Becker617c1ae2017-08-23 14:11:24 +010077int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
78 const mbedtls_mpi *N,
79 const mbedtls_mpi *P, const mbedtls_mpi *Q,
80 const mbedtls_mpi *D, const mbedtls_mpi *E )
81{
Janos Follath24eed8d2019-11-22 13:21:35 +000082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050083 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +010084
85 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
86 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
87 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
88 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
89 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
90 {
Chris Jonesb7d02e02021-04-01 17:40:03 +010091 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +010092 }
93
94 if( N != NULL )
95 ctx->len = mbedtls_mpi_size( &ctx->N );
96
97 return( 0 );
98}
99
100int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100101 unsigned char const *N, size_t N_len,
102 unsigned char const *P, size_t P_len,
103 unsigned char const *Q, size_t Q_len,
104 unsigned char const *D, size_t D_len,
105 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100106{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000107 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500108 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100109
110 if( N != NULL )
111 {
112 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
113 ctx->len = mbedtls_mpi_size( &ctx->N );
114 }
115
116 if( P != NULL )
117 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
118
119 if( Q != NULL )
120 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
121
122 if( D != NULL )
123 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
124
125 if( E != NULL )
126 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
127
128cleanup:
129
130 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100131 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100132
133 return( 0 );
134}
135
Hanno Becker705fc682017-10-10 17:57:02 +0100136/*
137 * Checks whether the context fields are set in such a way
138 * that the RSA primitives will be able to execute without error.
139 * It does *not* make guarantees for consistency of the parameters.
140 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100141static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
142 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100143{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100144#if !defined(MBEDTLS_RSA_NO_CRT)
145 /* blinding_needed is only used for NO_CRT to decide whether
146 * P,Q need to be present or not. */
147 ((void) blinding_needed);
148#endif
149
Hanno Becker3a760a12018-01-05 08:14:49 +0000150 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
151 ctx->len > MBEDTLS_MPI_MAX_SIZE )
152 {
Hanno Becker705fc682017-10-10 17:57:02 +0100153 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000154 }
Hanno Becker705fc682017-10-10 17:57:02 +0100155
156 /*
157 * 1. Modular exponentiation needs positive, odd moduli.
158 */
159
160 /* Modular exponentiation wrt. N is always used for
161 * RSA public key operations. */
162 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
163 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
164 {
165 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
166 }
167
168#if !defined(MBEDTLS_RSA_NO_CRT)
169 /* Modular exponentiation for P and Q is only
170 * used for private key operations and if CRT
171 * is used. */
172 if( is_priv &&
173 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
174 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
175 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
176 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
177 {
178 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
179 }
180#endif /* !MBEDTLS_RSA_NO_CRT */
181
182 /*
183 * 2. Exponents must be positive
184 */
185
186 /* Always need E for public key operations */
187 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
188 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
189
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100190#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100191 /* For private key operations, use D or DP & DQ
192 * as (unblinded) exponents. */
193 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
194 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
195#else
196 if( is_priv &&
197 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
198 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
199 {
200 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
201 }
202#endif /* MBEDTLS_RSA_NO_CRT */
203
204 /* Blinding shouldn't make exponents negative either,
205 * so check that P, Q >= 1 if that hasn't yet been
206 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100207#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100208 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100209 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
210 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
211 {
212 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
213 }
214#endif
215
216 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100217 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100218#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100219 if( is_priv &&
220 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
221 {
222 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
223 }
224#endif
225
226 return( 0 );
227}
228
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100229int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100230{
231 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500232 int have_N, have_P, have_Q, have_D, have_E;
233#if !defined(MBEDTLS_RSA_NO_CRT)
234 int have_DP, have_DQ, have_QP;
235#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500236 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100237
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500238 RSA_VALIDATE_RET( ctx != NULL );
239
240 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
241 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
242 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
243 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
244 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500245
246#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500247 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
248 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
249 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500250#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100251
Hanno Becker617c1ae2017-08-23 14:11:24 +0100252 /*
253 * Check whether provided parameters are enough
254 * to deduce all others. The following incomplete
255 * parameter sets for private keys are supported:
256 *
257 * (1) P, Q missing.
258 * (2) D and potentially N missing.
259 *
260 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100261
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500262 n_missing = have_P && have_Q && have_D && have_E;
263 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
264 d_missing = have_P && have_Q && !have_D && have_E;
265 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100266
267 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500268 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100269
Hanno Becker617c1ae2017-08-23 14:11:24 +0100270 if( !is_priv && !is_pub )
271 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
272
273 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100274 * Step 1: Deduce N if P, Q are provided.
275 */
276
277 if( !have_N && have_P && have_Q )
278 {
279 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
280 &ctx->Q ) ) != 0 )
281 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100282 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100283 }
284
285 ctx->len = mbedtls_mpi_size( &ctx->N );
286 }
287
288 /*
289 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100290 */
291
292 if( pq_missing )
293 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100294 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100295 &ctx->P, &ctx->Q );
296 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100297 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100298
299 }
300 else if( d_missing )
301 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100302 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
303 &ctx->Q,
304 &ctx->E,
305 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100306 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100307 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100308 }
309 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100310
Hanno Becker617c1ae2017-08-23 14:11:24 +0100311 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100312 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100313 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100314 */
315
Hanno Becker23344b52017-08-23 07:43:27 +0100316#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500317 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100318 {
319 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
320 &ctx->DP, &ctx->DQ, &ctx->QP );
321 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100322 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100323 }
Hanno Becker23344b52017-08-23 07:43:27 +0100324#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100325
326 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100327 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100328 */
329
Hanno Beckerebd2c022017-10-12 10:54:53 +0100330 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100331}
332
Hanno Becker617c1ae2017-08-23 14:11:24 +0100333int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
334 unsigned char *N, size_t N_len,
335 unsigned char *P, size_t P_len,
336 unsigned char *Q, size_t Q_len,
337 unsigned char *D, size_t D_len,
338 unsigned char *E, size_t E_len )
339{
340 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500341 int is_priv;
342 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100343
344 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500345 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100346 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
347 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
348 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
349 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
350 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
351
352 if( !is_priv )
353 {
354 /* If we're trying to export private parameters for a public key,
355 * something must be wrong. */
356 if( P != NULL || Q != NULL || D != NULL )
357 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
358
359 }
360
361 if( N != NULL )
362 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
363
364 if( P != NULL )
365 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
366
367 if( Q != NULL )
368 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
369
370 if( D != NULL )
371 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
372
373 if( E != NULL )
374 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100375
376cleanup:
377
378 return( ret );
379}
380
Hanno Becker617c1ae2017-08-23 14:11:24 +0100381int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
382 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
383 mbedtls_mpi *D, mbedtls_mpi *E )
384{
Janos Follath24eed8d2019-11-22 13:21:35 +0000385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500386 int is_priv;
387 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100388
389 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500390 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100391 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
392 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
393 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
394 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
395 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
396
397 if( !is_priv )
398 {
399 /* If we're trying to export private parameters for a public key,
400 * something must be wrong. */
401 if( P != NULL || Q != NULL || D != NULL )
402 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
403
404 }
405
406 /* Export all requested core parameters. */
407
408 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
409 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
410 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
411 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
412 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
413 {
414 return( ret );
415 }
416
417 return( 0 );
418}
419
420/*
421 * Export CRT parameters
422 * This must also be implemented if CRT is not used, for being able to
423 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
424 * can be used in this case.
425 */
426int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
427 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
428{
Janos Follath24eed8d2019-11-22 13:21:35 +0000429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500430 int is_priv;
431 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100432
433 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500434 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100435 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
436 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
437 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
438 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
439 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
440
441 if( !is_priv )
442 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
443
Hanno Beckerdc95c892017-08-23 06:57:02 +0100444#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100445 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100446 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
447 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
448 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
449 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100450 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100451 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100452#else
453 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
454 DP, DQ, QP ) ) != 0 )
455 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100456 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100457 }
458#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100459
460 return( 0 );
461}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100462
Paul Bakker5121ce52009-01-03 21:22:43 +0000463/*
464 * Initialize an RSA context
465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000468 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500470 RSA_VALIDATE( ctx != NULL );
471 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
472 padding == MBEDTLS_RSA_PKCS_V21 );
473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100479 /* Set ctx->ver to nonzero to indicate that the mutex has been
480 * initialized and will need to be freed. */
481 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200483#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000484}
485
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100486/*
487 * Set padding for an existing RSA context
488 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500489void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
490 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100491{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500492 RSA_VALIDATE( ctx != NULL );
493 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
494 padding == MBEDTLS_RSA_PKCS_V21 );
495
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100496 ctx->padding = padding;
497 ctx->hash_id = hash_id;
498}
499
Hanno Becker617c1ae2017-08-23 14:11:24 +0100500/*
501 * Get length in bytes of RSA modulus
502 */
503
504size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
505{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100506 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100507}
508
509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512/*
513 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800514 *
515 * This generation method follows the RSA key pair generation procedure of
516 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000519 int (*f_rng)(void *, unsigned char *, size_t),
520 void *p_rng,
521 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000522{
Janos Follath24eed8d2019-11-22 13:21:35 +0000523 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800524 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100525 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500526 RSA_VALIDATE_RET( ctx != NULL );
527 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
Janos Follathb8fc1b02018-09-03 15:37:01 +0100529 /*
530 * If the modulus is 1024 bit long or shorter, then the security strength of
531 * the RSA algorithm is less than or equal to 80 bits and therefore an error
532 * rate of 2^-80 is sufficient.
533 */
534 if( nbits > 1024 )
535 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
536
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100537 mbedtls_mpi_init( &H );
538 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800539 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100541 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
542 {
543 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
544 goto cleanup;
545 }
546
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 /*
548 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800549 * 1. |P-Q| > 2^( nbits / 2 - 100 )
550 * 2. GCD( E, (P-1)*(Q-1) ) == 1
551 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555 do
556 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100557 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
558 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Janos Follathb8fc1b02018-09-03 15:37:01 +0100560 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
561 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800563 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
564 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
565 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 continue;
567
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800568 /* not required by any standards, but some users rely on the fact that P > Q */
569 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100570 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100571
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100572 /* Temporarily replace P,Q by P-1, Q-1 */
573 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
574 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
575 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800576
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800577 /* 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 +0200578 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800579 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
580 continue;
581
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800582 /* 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 -0800583 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
584 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
585 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
586
587 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
588 continue;
589
590 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800592 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100594 /* Restore P,Q */
595 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
596 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
597
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800598 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
599
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100600 ctx->len = mbedtls_mpi_size( &ctx->N );
601
Jethro Beekman97f95c92018-02-13 15:50:36 -0800602#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 * DP = D mod (P - 1)
605 * DQ = D mod (Q - 1)
606 * QP = Q^-1 mod P
607 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100608 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
609 &ctx->DP, &ctx->DQ, &ctx->QP ) );
610#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
Hanno Becker83aad1f2017-08-23 06:45:10 +0100612 /* Double-check */
613 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000614
615cleanup:
616
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100617 mbedtls_mpi_free( &H );
618 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800619 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
621 if( ret != 0 )
622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100624
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100625 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100626 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100627 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 }
629
Paul Bakker48377d92013-08-30 12:06:24 +0200630 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000631}
632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
635/*
636 * Check a public RSA key
637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000639{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500640 RSA_VALIDATE_RET( ctx != NULL );
641
Hanno Beckerebd2c022017-10-12 10:54:53 +0100642 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000644
Hanno Becker3a760a12018-01-05 08:14:49 +0000645 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
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
Hanno Becker705fc682017-10-10 17:57:02 +0100650 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
651 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100655 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
657 return( 0 );
658}
659
660/*
Hanno Becker705fc682017-10-10 17:57:02 +0100661 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000664{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500665 RSA_VALIDATE_RET( ctx != NULL );
666
Hanno Becker705fc682017-10-10 17:57:02 +0100667 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100668 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 {
Hanno Becker98838b02017-10-02 13:16:10 +0100670 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 }
Paul Bakker48377d92013-08-30 12:06:24 +0200672
Hanno Becker98838b02017-10-02 13:16:10 +0100673 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100674 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100676 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000678
Hanno Beckerb269a852017-08-25 08:03:21 +0100679#if !defined(MBEDTLS_RSA_NO_CRT)
680 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
681 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
682 {
683 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
684 }
685#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000686
687 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688}
689
690/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100691 * Check if contexts holding a public and private key match
692 */
Hanno Becker98838b02017-10-02 13:16:10 +0100693int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
694 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100695{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500696 RSA_VALIDATE_RET( pub != NULL );
697 RSA_VALIDATE_RET( prv != NULL );
698
Hanno Becker98838b02017-10-02 13:16:10 +0100699 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100703 }
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
706 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100709 }
710
711 return( 0 );
712}
713
714/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 * Do an RSA public key operation
716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000718 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 unsigned char *output )
720{
Janos Follath24eed8d2019-11-22 13:21:35 +0000721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000722 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500724 RSA_VALIDATE_RET( ctx != NULL );
725 RSA_VALIDATE_RET( input != NULL );
726 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000727
Hanno Beckerebd2c022017-10-12 10:54:53 +0100728 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100729 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200733#if defined(MBEDTLS_THREADING_C)
734 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
735 return( ret );
736#endif
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000741 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200742 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
743 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 }
745
746 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
748 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
750cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200752 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
753 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100754#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
758 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100759 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
761 return( 0 );
762}
763
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200764/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200765 * Generate or update blinding values, see section 10 of:
766 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200767 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200768 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200769 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200770static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200771 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
772{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200773 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200774 mbedtls_mpi R;
775
776 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200777
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200778 if( ctx->Vf.p != NULL )
779 {
780 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
782 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
783 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
784 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200785
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200786 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200787 }
788
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200789 /* Unblinding value: Vf = random number, invertible mod N */
790 do {
791 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200792 {
793 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
794 goto cleanup;
795 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 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 +0200798
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200799 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200800 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
801 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
802 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
803
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200804 /* At this point, Vi is invertible mod N if and only if both Vf and R
805 * are invertible mod N. If one of them isn't, we don't need to know
806 * which one, we just loop and choose new values for both of them.
807 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200808 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500809 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200810 goto cleanup;
811
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500812 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
813
814 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
815 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
816 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200817
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200818 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200819 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 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 +0200821
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200822
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200823cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200824 mbedtls_mpi_free( &R );
825
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200826 return( ret );
827}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200828
Paul Bakker5121ce52009-01-03 21:22:43 +0000829/*
Janos Follathe81102e2017-03-22 13:38:28 +0000830 * Exponent blinding supposed to prevent side-channel attacks using multiple
831 * traces of measurements to recover the RSA key. The more collisions are there,
832 * the more bits of the key can be recovered. See [3].
833 *
834 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
835 * observations on avarage.
836 *
837 * For example with 28 byte blinding to achieve 2 collisions the adversary has
838 * to make 2^112 observations on avarage.
839 *
840 * (With the currently (as of 2017 April) known best algorithms breaking 2048
841 * bit RSA requires approximately as much time as trying out 2^112 random keys.
842 * Thus in this sense with 28 byte blinding the security is not reduced by
843 * side-channel attacks like the one in [3])
844 *
845 * This countermeasure does not help if the key recovery is possible with a
846 * single trace.
847 */
848#define RSA_EXPONENT_BLINDING 28
849
850/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000851 * Do an RSA private key operation
852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200854 int (*f_rng)(void *, unsigned char *, size_t),
855 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000856 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 unsigned char *output )
858{
Janos Follath24eed8d2019-11-22 13:21:35 +0000859 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000860 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100861
862 /* Temporary holding the result */
863 mbedtls_mpi T;
864
865 /* Temporaries holding P-1, Q-1 and the
866 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000867 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100868
869#if !defined(MBEDTLS_RSA_NO_CRT)
870 /* Temporaries holding the results mod p resp. mod q. */
871 mbedtls_mpi TP, TQ;
872
873 /* Temporaries holding the blinded exponents for
874 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000875 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100876
877 /* Pointers to actual exponents to be used - either the unblinded
878 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000879 mbedtls_mpi *DP = &ctx->DP;
880 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100881#else
882 /* Temporary holding the blinded exponent (if used). */
883 mbedtls_mpi D_blind;
884
885 /* Pointer to actual exponent to be used - either the unblinded
886 * or the blinded one, depending on the presence of a PRNG. */
887 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100888#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100889
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100890 /* Temporaries holding the initial input and the double
891 * checked result; should be the same in the end. */
892 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500894 RSA_VALIDATE_RET( ctx != NULL );
895 RSA_VALIDATE_RET( input != NULL );
896 RSA_VALIDATE_RET( output != NULL );
897
Hanno Beckerebd2c022017-10-12 10:54:53 +0100898 if( rsa_check_context( ctx, 1 /* private key checks */,
899 f_rng != NULL /* blinding y/n */ ) != 0 )
900 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100901 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100902 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100903
Hanno Becker06811ce2017-05-03 15:10:34 +0100904#if defined(MBEDTLS_THREADING_C)
905 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
906 return( ret );
907#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000908
Hanno Becker06811ce2017-05-03 15:10:34 +0100909 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100910 mbedtls_mpi_init( &T );
911
912 mbedtls_mpi_init( &P1 );
913 mbedtls_mpi_init( &Q1 );
914 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000915
Janos Follathf9203b42017-03-22 15:13:15 +0000916 if( f_rng != NULL )
917 {
Janos Follathe81102e2017-03-22 13:38:28 +0000918#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000919 mbedtls_mpi_init( &D_blind );
920#else
921 mbedtls_mpi_init( &DP_blind );
922 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000923#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000924 }
Janos Follathe81102e2017-03-22 13:38:28 +0000925
Hanno Becker06811ce2017-05-03 15:10:34 +0100926#if !defined(MBEDTLS_RSA_NO_CRT)
927 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200928#endif
929
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100930 mbedtls_mpi_init( &I );
931 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100932
933 /* End of MPI initialization */
934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
936 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000937 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200938 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
939 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 }
941
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100942 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100943
Paul Bakkerf451bac2013-08-30 15:37:02 +0200944 if( f_rng != NULL )
945 {
946 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200947 * Blinding
948 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200949 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200950 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
951 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000953
Janos Follathe81102e2017-03-22 13:38:28 +0000954 /*
955 * Exponent blinding
956 */
957 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
958 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
959
Janos Follathf9203b42017-03-22 15:13:15 +0000960#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000961 /*
962 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
963 */
964 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
965 f_rng, p_rng ) );
966 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
967 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
968 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
969
970 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000971#else
972 /*
973 * DP_blind = ( P - 1 ) * R + DP
974 */
975 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
976 f_rng, p_rng ) );
977 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
978 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
979 &ctx->DP ) );
980
981 DP = &DP_blind;
982
983 /*
984 * DQ_blind = ( Q - 1 ) * R + DQ
985 */
986 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
987 f_rng, p_rng ) );
988 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
989 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
990 &ctx->DQ ) );
991
992 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000993#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200994 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000997 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100998#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200999 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001000 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001001 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001002 * TP = input ^ dP mod P
1003 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001004 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001005
1006 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1007 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
1009 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001010 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001012 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1013 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1014 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
1016 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001017 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001019 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1020 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001022
Paul Bakkerf451bac2013-08-30 15:37:02 +02001023 if( f_rng != NULL )
1024 {
1025 /*
1026 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001027 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001028 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001029 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001031 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
Hanno Becker2dec5e82017-10-03 07:49:52 +01001033 /* Verify the result to prevent glitching attacks. */
1034 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1035 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001036 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001037 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001038 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1039 goto cleanup;
1040 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001041
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
1045cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001047 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1048 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001049#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001050
Hanno Becker06811ce2017-05-03 15:10:34 +01001051 mbedtls_mpi_free( &P1 );
1052 mbedtls_mpi_free( &Q1 );
1053 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001054
1055 if( f_rng != NULL )
1056 {
Janos Follathe81102e2017-03-22 13:38:28 +00001057#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001058 mbedtls_mpi_free( &D_blind );
1059#else
1060 mbedtls_mpi_free( &DP_blind );
1061 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001062#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001063 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001064
Hanno Becker06811ce2017-05-03 15:10:34 +01001065 mbedtls_mpi_free( &T );
1066
1067#if !defined(MBEDTLS_RSA_NO_CRT)
1068 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1069#endif
1070
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001071 mbedtls_mpi_free( &C );
1072 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001073
Gilles Peskineae3741e2020-11-25 00:10:31 +01001074 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001075 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Gilles Peskineae3741e2020-11-25 00:10:31 +01001077 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001078}
1079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001081/**
1082 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1083 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001084 * \param dst buffer to mask
1085 * \param dlen length of destination buffer
1086 * \param src source of the mask generation
1087 * \param slen length of the source buffer
1088 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001089 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001090static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001094 unsigned char counter[4];
1095 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001096 unsigned int hlen;
1097 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001098 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001101 memset( counter, 0, 4 );
1102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104
Simon Butcher02037452016-03-01 21:19:12 +00001105 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001106 p = dst;
1107
1108 while( dlen > 0 )
1109 {
1110 use_len = hlen;
1111 if( dlen < hlen )
1112 use_len = dlen;
1113
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001114 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1115 goto exit;
1116 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1117 goto exit;
1118 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1119 goto exit;
1120 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1121 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001122
1123 for( i = 0; i < use_len; ++i )
1124 *p++ ^= mask[i];
1125
1126 counter[3]++;
1127
1128 dlen -= use_len;
1129 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001130
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001131exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001132 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001133
1134 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001139/*
1140 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001143 int (*f_rng)(void *, unsigned char *, size_t),
1144 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001145 int mode,
1146 const unsigned char *label, size_t label_len,
1147 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001148 const unsigned char *input,
1149 unsigned char *output )
1150{
1151 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001153 unsigned char *p = output;
1154 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 const mbedtls_md_info_t *md_info;
1156 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001157
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001158 RSA_VALIDATE_RET( ctx != NULL );
1159 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1160 mode == MBEDTLS_RSA_PUBLIC );
1161 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001162 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001163 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1166 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001167
1168 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001172 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001174
1175 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001177
Simon Butcher02037452016-03-01 21:19:12 +00001178 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001179 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001181
1182 memset( output, 0, olen );
1183
1184 *p++ = 0;
1185
Simon Butcher02037452016-03-01 21:19:12 +00001186 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001187 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001188 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001189
1190 p += hlen;
1191
Simon Butcher02037452016-03-01 21:19:12 +00001192 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001193 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1194 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001195 p += hlen;
1196 p += olen - 2 * hlen - 2 - ilen;
1197 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001198 if( ilen != 0 )
1199 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001202 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001203 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001204
Simon Butcher02037452016-03-01 21:19:12 +00001205 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001206 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1207 &md_ctx ) ) != 0 )
1208 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001209
Simon Butcher02037452016-03-01 21:19:12 +00001210 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001211 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1212 &md_ctx ) ) != 0 )
1213 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001214
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001215exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001217
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001218 if( ret != 0 )
1219 return( ret );
1220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 return( ( mode == MBEDTLS_RSA_PUBLIC )
1222 ? mbedtls_rsa_public( ctx, output, output )
1223 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001224}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001228/*
1229 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001232 int (*f_rng)(void *, unsigned char *, size_t),
1233 void *p_rng,
1234 int mode, size_t ilen,
1235 const unsigned char *input,
1236 unsigned char *output )
1237{
1238 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001240 unsigned char *p = output;
1241
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001242 RSA_VALIDATE_RET( ctx != NULL );
1243 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1244 mode == MBEDTLS_RSA_PUBLIC );
1245 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001246 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001247
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001248 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001250
1251 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001252
Simon Butcher02037452016-03-01 21:19:12 +00001253 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001254 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001256
1257 nb_pad = olen - 3 - ilen;
1258
1259 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001261 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001262 if( f_rng == NULL )
1263 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001266
1267 while( nb_pad-- > 0 )
1268 {
1269 int rng_dl = 100;
1270
1271 do {
1272 ret = f_rng( p_rng, p, 1 );
1273 } while( *p == 0 && --rng_dl && ret == 0 );
1274
Simon Butcher02037452016-03-01 21:19:12 +00001275 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001276 if( rng_dl == 0 || ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001277 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001278
1279 p++;
1280 }
1281 }
1282 else
1283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001285
1286 while( nb_pad-- > 0 )
1287 *p++ = 0xFF;
1288 }
1289
1290 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001291 if( ilen != 0 )
1292 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 return( ( mode == MBEDTLS_RSA_PUBLIC )
1295 ? mbedtls_rsa_public( ctx, output, output )
1296 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001299
Paul Bakker5121ce52009-01-03 21:22:43 +00001300/*
1301 * Add the message padding, then do an RSA operation
1302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001304 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001305 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001306 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001307 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 unsigned char *output )
1309{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001310 RSA_VALIDATE_RET( ctx != NULL );
1311 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1312 mode == MBEDTLS_RSA_PUBLIC );
1313 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001314 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001315
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 switch( ctx->padding )
1317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#if defined(MBEDTLS_PKCS1_V15)
1319 case MBEDTLS_RSA_PKCS_V15:
1320 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001321 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001322#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_PKCS1_V21)
1325 case MBEDTLS_RSA_PKCS_V21:
1326 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001327 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001328#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
1330 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001333}
1334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001336/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001337 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001340 int (*f_rng)(void *, unsigned char *, size_t),
1341 void *p_rng,
1342 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001343 const unsigned char *label, size_t label_len,
1344 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001345 const unsigned char *input,
1346 unsigned char *output,
1347 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001348{
Janos Follath24eed8d2019-11-22 13:21:35 +00001349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001350 size_t ilen, i, pad_len;
1351 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1353 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001354 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 const mbedtls_md_info_t *md_info;
1356 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001357
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001358 RSA_VALIDATE_RET( ctx != NULL );
1359 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1360 mode == MBEDTLS_RSA_PUBLIC );
1361 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1362 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1363 RSA_VALIDATE_RET( input != NULL );
1364 RSA_VALIDATE_RET( olen != NULL );
1365
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001366 /*
1367 * Parameters sanity checks
1368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1370 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
1372 ilen = ctx->len;
1373
Paul Bakker27fdf462011-06-09 13:55:13 +00001374 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001378 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001380
Janos Follathc17cda12016-02-11 11:08:18 +00001381 hlen = mbedtls_md_get_size( md_info );
1382
1383 // checking for integer underflow
1384 if( 2 * hlen + 2 > ilen )
1385 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1386
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001387 /*
1388 * RSA operation
1389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1391 ? mbedtls_rsa_public( ctx, input, buf )
1392 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
1394 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001395 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001397 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001398 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001401 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1402 {
1403 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001404 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001405 }
1406
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001407 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001408 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1409 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001410 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001411 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1412 &md_ctx ) ) != 0 )
1413 {
1414 mbedtls_md_free( &md_ctx );
1415 goto cleanup;
1416 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001419
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001420 /* Generate lHash */
1421 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1422 goto cleanup;
1423
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001424 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001425 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001426 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001428 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001429
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001430 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001431
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001432 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001433
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001434 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001435 for( i = 0; i < hlen; i++ )
1436 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001437
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001438 /* Get zero-padding len, but always read till end of buffer
1439 * (minus one, for the 01 byte) */
1440 pad_len = 0;
1441 pad_done = 0;
1442 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1443 {
1444 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001445 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001446 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001447
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001448 p += pad_len;
1449 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001450
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001451 /*
1452 * The only information "leaked" is whether the padding was correct or not
1453 * (eg, no data is copied if it was not correct). This meets the
1454 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1455 * the different error conditions.
1456 */
1457 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001458 {
1459 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1460 goto cleanup;
1461 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001462
Paul Bakker66d5d072014-06-17 16:39:18 +02001463 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001464 {
1465 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1466 goto cleanup;
1467 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001468
1469 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001470 if( *olen != 0 )
1471 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001472 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001473
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001474cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001475 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1476 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001477
1478 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001483/*
1484 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1485 */
1486int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
1487 int (*f_rng)(void *, unsigned char *, size_t),
1488 void *p_rng,
1489 int mode,
1490 size_t *olen,
1491 const unsigned char *input,
1492 unsigned char *output,
1493 size_t output_max_len )
1494{
1495 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1496 size_t ilen;
1497 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1498
1499 RSA_VALIDATE_RET( ctx != NULL );
1500 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1501 mode == MBEDTLS_RSA_PUBLIC );
1502 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1503 RSA_VALIDATE_RET( input != NULL );
1504 RSA_VALIDATE_RET( olen != NULL );
1505
1506 ilen = ctx->len;
1507
1508 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1509 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1510
1511 if( ilen < 16 || ilen > sizeof( buf ) )
1512 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1513
1514 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1515 ? mbedtls_rsa_public( ctx, input, buf )
1516 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
1517
1518 if( ret != 0 )
1519 goto cleanup;
1520
Gabor Mezei18a44942021-10-20 11:59:27 +02001521 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding( mode, buf, ilen,
Gabor Mezei91deea72021-10-18 16:17:57 +02001522 output, output_max_len, olen );
gabor-mezei-armc2aee6f2021-09-26 15:20:48 +02001523
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001524cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001525 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001526
1527 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001528}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
1531/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001532 * Do an RSA operation, then remove the message padding
1533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001535 int (*f_rng)(void *, unsigned char *, size_t),
1536 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001537 int mode, size_t *olen,
1538 const unsigned char *input,
1539 unsigned char *output,
1540 size_t output_max_len)
1541{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001542 RSA_VALIDATE_RET( ctx != NULL );
1543 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1544 mode == MBEDTLS_RSA_PUBLIC );
1545 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1546 RSA_VALIDATE_RET( input != NULL );
1547 RSA_VALIDATE_RET( olen != NULL );
1548
Paul Bakkerb3869132013-02-28 17:21:01 +01001549 switch( ctx->padding )
1550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#if defined(MBEDTLS_PKCS1_V15)
1552 case MBEDTLS_RSA_PKCS_V15:
1553 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001554 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001555#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#if defined(MBEDTLS_PKCS1_V21)
1558 case MBEDTLS_RSA_PKCS_V21:
1559 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001560 olen, input, output,
1561 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001562#endif
1563
1564 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001566 }
1567}
1568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001570static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001571 int (*f_rng)(void *, unsigned char *, size_t),
1572 void *p_rng,
1573 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001575 unsigned int hashlen,
1576 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001577 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001578 unsigned char *sig )
1579{
1580 size_t olen;
1581 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001582 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001583 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001584 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001585 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 const mbedtls_md_info_t *md_info;
1587 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001588 RSA_VALIDATE_RET( ctx != NULL );
1589 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1590 mode == MBEDTLS_RSA_PUBLIC );
1591 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1592 hashlen == 0 ) ||
1593 hash != NULL );
1594 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1597 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001598
1599 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001601
1602 olen = ctx->len;
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001605 {
Simon Butcher02037452016-03-01 21:19:12 +00001606 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001608 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001612 }
1613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001615 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001619
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001620 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1621 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001622 /* Calculate the largest possible salt length, up to the hash size.
1623 * Normally this is the hash length, which is the maximum salt length
1624 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001625 * enough room, use the maximum salt length that fits. The constraint is
1626 * that the hash length plus the salt length plus 2 bytes must be at most
1627 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1628 * (PKCS#1 v2.2) §9.1.1 step 3. */
1629 min_slen = hlen - 2;
1630 if( olen < hlen + min_slen + 2 )
1631 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1632 else if( olen >= hlen + hlen + 2 )
1633 slen = hlen;
1634 else
1635 slen = olen - hlen - 2;
1636 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001637 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001640 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001641 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001642 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001643 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001644 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001645
1646 memset( sig, 0, olen );
1647
Simon Butcher02037452016-03-01 21:19:12 +00001648 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001649 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001650 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001651 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001652
1653 /* Generate salt of length slen in place in the encoded message */
1654 salt = p;
1655 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001656 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001657
Paul Bakkerb3869132013-02-28 17:21:01 +01001658 p += slen;
1659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001661 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001662 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001663
Simon Butcher02037452016-03-01 21:19:12 +00001664 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001665 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1666 goto exit;
1667 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1668 goto exit;
1669 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1670 goto exit;
1671 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1672 goto exit;
1673 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1674 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001675
Simon Butcher02037452016-03-01 21:19:12 +00001676 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001677 if( msb % 8 == 0 )
1678 offset = 1;
1679
Simon Butcher02037452016-03-01 21:19:12 +00001680 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001681 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1682 &md_ctx ) ) != 0 )
1683 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001684
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001685 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001686 sig[0] &= 0xFF >> ( olen * 8 - msb );
1687
1688 p += hlen;
1689 *p++ = 0xBC;
1690
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001691exit:
1692 mbedtls_md_free( &md_ctx );
1693
1694 if( ret != 0 )
1695 return( ret );
1696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 return( ( mode == MBEDTLS_RSA_PUBLIC )
1698 ? mbedtls_rsa_public( ctx, sig, sig )
1699 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001700}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001701
1702/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001703 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1704 * the option to pass in the salt length.
1705 */
1706int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1707 int (*f_rng)(void *, unsigned char *, size_t),
1708 void *p_rng,
1709 mbedtls_md_type_t md_alg,
1710 unsigned int hashlen,
1711 const unsigned char *hash,
1712 int saltlen,
1713 unsigned char *sig )
1714{
1715 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, md_alg,
1716 hashlen, hash, saltlen, sig );
1717}
1718
1719
1720/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001721 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1722 */
1723int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1724 int (*f_rng)(void *, unsigned char *, size_t),
1725 void *p_rng,
1726 int mode,
1727 mbedtls_md_type_t md_alg,
1728 unsigned int hashlen,
1729 const unsigned char *hash,
1730 unsigned char *sig )
1731{
Cédric Meuterf3fab332020-04-25 11:30:45 +02001732 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
1733 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001734}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001738/*
1739 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1740 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001741
1742/* Construct a PKCS v1.5 encoding of a hashed message
1743 *
1744 * This is used both for signature generation and verification.
1745 *
1746 * Parameters:
1747 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001748 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001749 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001750 * - hash: Buffer containing the hashed message or the raw data.
1751 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001752 * - dst: Buffer to hold the encoded message.
1753 *
1754 * Assumptions:
1755 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1756 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001757 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001758 *
1759 */
1760static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1761 unsigned int hashlen,
1762 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001763 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001764 unsigned char *dst )
1765{
1766 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001767 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001768 unsigned char *p = dst;
1769 const char *oid = NULL;
1770
1771 /* Are we signing hashed or raw data? */
1772 if( md_alg != MBEDTLS_MD_NONE )
1773 {
1774 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1775 if( md_info == NULL )
1776 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1777
1778 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1779 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1780
1781 hashlen = mbedtls_md_get_size( md_info );
1782
1783 /* Double-check that 8 + hashlen + oid_size can be used as a
1784 * 1-byte ASN.1 length encoding and that there's no overflow. */
1785 if( 8 + hashlen + oid_size >= 0x80 ||
1786 10 + hashlen < hashlen ||
1787 10 + hashlen + oid_size < 10 + hashlen )
1788 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1789
1790 /*
1791 * Static bounds check:
1792 * - Need 10 bytes for five tag-length pairs.
1793 * (Insist on 1-byte length encodings to protect against variants of
1794 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1795 * - Need hashlen bytes for hash
1796 * - Need oid_size bytes for hash alg OID.
1797 */
1798 if( nb_pad < 10 + hashlen + oid_size )
1799 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1800 nb_pad -= 10 + hashlen + oid_size;
1801 }
1802 else
1803 {
1804 if( nb_pad < hashlen )
1805 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1806
1807 nb_pad -= hashlen;
1808 }
1809
Hanno Becker2b2f8982017-09-27 17:10:03 +01001810 /* Need space for signature header and padding delimiter (3 bytes),
1811 * and 8 bytes for the minimal padding */
1812 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001813 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1814 nb_pad -= 3;
1815
1816 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001817 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001818
1819 /* Write signature header and padding */
1820 *p++ = 0;
1821 *p++ = MBEDTLS_RSA_SIGN;
1822 memset( p, 0xFF, nb_pad );
1823 p += nb_pad;
1824 *p++ = 0;
1825
1826 /* Are we signing raw data? */
1827 if( md_alg == MBEDTLS_MD_NONE )
1828 {
1829 memcpy( p, hash, hashlen );
1830 return( 0 );
1831 }
1832
1833 /* Signing hashed data, add corresponding ASN.1 structure
1834 *
1835 * DigestInfo ::= SEQUENCE {
1836 * digestAlgorithm DigestAlgorithmIdentifier,
1837 * digest Digest }
1838 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1839 * Digest ::= OCTET STRING
1840 *
1841 * Schematic:
1842 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1843 * TAG-NULL + LEN [ NULL ] ]
1844 * TAG-OCTET + LEN [ HASH ] ]
1845 */
1846 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001847 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001848 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001849 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001850 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001851 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001852 memcpy( p, oid, oid_size );
1853 p += oid_size;
1854 *p++ = MBEDTLS_ASN1_NULL;
1855 *p++ = 0x00;
1856 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001857 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001858 memcpy( p, hash, hashlen );
1859 p += hashlen;
1860
1861 /* Just a sanity-check, should be automatic
1862 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001863 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001864 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001865 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001866 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1867 }
1868
1869 return( 0 );
1870}
1871
Paul Bakkerb3869132013-02-28 17:21:01 +01001872/*
1873 * Do an RSA operation to sign the message digest
1874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001876 int (*f_rng)(void *, unsigned char *, size_t),
1877 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001878 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001880 unsigned int hashlen,
1881 const unsigned char *hash,
1882 unsigned char *sig )
1883{
Janos Follath24eed8d2019-11-22 13:21:35 +00001884 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001885 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001886
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001887 RSA_VALIDATE_RET( ctx != NULL );
1888 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1889 mode == MBEDTLS_RSA_PUBLIC );
1890 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1891 hashlen == 0 ) ||
1892 hash != NULL );
1893 RSA_VALIDATE_RET( sig != NULL );
1894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1896 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001897
Hanno Beckerfdf38032017-09-06 12:35:55 +01001898 /*
1899 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1900 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001901
Hanno Beckerfdf38032017-09-06 12:35:55 +01001902 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1903 ctx->len, sig ) ) != 0 )
1904 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001905
1906 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001907 * Call respective RSA primitive
1908 */
1909
1910 if( mode == MBEDTLS_RSA_PUBLIC )
1911 {
1912 /* Skip verification on a public key operation */
1913 return( mbedtls_rsa_public( ctx, sig, sig ) );
1914 }
1915
1916 /* Private key operation
1917 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001918 * In order to prevent Lenstra's attack, make the signature in a
1919 * temporary buffer and check it before returning it.
1920 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001921
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001922 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001923 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001924 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1925
Hanno Beckerfdf38032017-09-06 12:35:55 +01001926 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001927 if( verif == NULL )
1928 {
1929 mbedtls_free( sig_try );
1930 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1931 }
1932
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001933 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1934 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1935
Gabor Mezei18a44942021-10-20 11:59:27 +02001936 if( mbedtls_ct_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001937 {
1938 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1939 goto cleanup;
1940 }
1941
1942 memcpy( sig, sig_try, ctx->len );
1943
1944cleanup:
1945 mbedtls_free( sig_try );
1946 mbedtls_free( verif );
1947
1948 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001949}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001951
1952/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 * Do an RSA operation to sign the message digest
1954 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001956 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001957 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001960 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001961 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001962 unsigned char *sig )
1963{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001964 RSA_VALIDATE_RET( ctx != NULL );
1965 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1966 mode == MBEDTLS_RSA_PUBLIC );
1967 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1968 hashlen == 0 ) ||
1969 hash != NULL );
1970 RSA_VALIDATE_RET( sig != NULL );
1971
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 switch( ctx->padding )
1973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974#if defined(MBEDTLS_PKCS1_V15)
1975 case MBEDTLS_RSA_PKCS_V15:
1976 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001977 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001978#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980#if defined(MBEDTLS_PKCS1_V21)
1981 case MBEDTLS_RSA_PKCS_V21:
1982 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001983 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001984#endif
1985
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001989}
1990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001992/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001993 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001996 int (*f_rng)(void *, unsigned char *, size_t),
1997 void *p_rng,
1998 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002000 unsigned int hashlen,
2001 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002003 int expected_salt_len,
2004 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002005{
Janos Follath24eed8d2019-11-22 13:21:35 +00002006 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002007 size_t siglen;
2008 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002009 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002011 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002012 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002013 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 const mbedtls_md_info_t *md_info;
2015 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002016 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002017
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002018 RSA_VALIDATE_RET( ctx != NULL );
2019 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2020 mode == MBEDTLS_RSA_PUBLIC );
2021 RSA_VALIDATE_RET( sig != NULL );
2022 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2023 hashlen == 0 ) ||
2024 hash != NULL );
2025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2027 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002028
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 siglen = ctx->len;
2030
Paul Bakker27fdf462011-06-09 13:55:13 +00002031 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2035 ? mbedtls_rsa_public( ctx, sig, buf )
2036 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002037
2038 if( ret != 0 )
2039 return( ret );
2040
2041 p = buf;
2042
Paul Bakkerb3869132013-02-28 17:21:01 +01002043 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 {
Simon Butcher02037452016-03-01 21:19:12 +00002048 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002050 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002054 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002057 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002061
Paul Bakkerb3869132013-02-28 17:21:01 +01002062 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002063
Simon Butcher02037452016-03-01 21:19:12 +00002064 /*
2065 * Note: EMSA-PSS verification is over the length of N - 1 bits
2066 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002067 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002068
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002069 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2070 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2071
Simon Butcher02037452016-03-01 21:19:12 +00002072 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002073 if( msb % 8 == 0 )
2074 {
2075 p++;
2076 siglen -= 1;
2077 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002078
Gilles Peskine139108a2017-10-18 19:03:42 +02002079 if( siglen < hlen + 2 )
2080 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2081 hash_start = p + siglen - hlen - 1;
2082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002084 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002085 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002086
Jaeden Amero66954e12018-01-25 16:05:54 +00002087 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2088 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002089 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002090
Paul Bakkerb3869132013-02-28 17:21:01 +01002091 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002092
Gilles Peskine6a54b022017-10-17 19:02:13 +02002093 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002094 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002095
Gilles Peskine91048a32017-10-19 17:46:14 +02002096 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002097 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002098 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2099 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002100 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002101
Gilles Peskine6a54b022017-10-17 19:02:13 +02002102 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002105 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002106 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002107 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2108 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002109 }
2110
Simon Butcher02037452016-03-01 21:19:12 +00002111 /*
2112 * Generate H = Hash( M' )
2113 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002114 ret = mbedtls_md_starts( &md_ctx );
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, zeros, 8 );
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, hash, hashlen );
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_update( &md_ctx, p, observed_salt_len );
2124 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002125 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002126 ret = mbedtls_md_finish( &md_ctx, result );
2127 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002128 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002129
Jaeden Amero66954e12018-01-25 16:05:54 +00002130 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002131 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002132 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002133 goto exit;
2134 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002135
2136exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002138
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002139 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002140}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002141
2142/*
2143 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002146 int (*f_rng)(void *, unsigned char *, size_t),
2147 void *p_rng,
2148 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002150 unsigned int hashlen,
2151 const unsigned char *hash,
2152 const unsigned char *sig )
2153{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002154 mbedtls_md_type_t mgf1_hash_id;
2155 RSA_VALIDATE_RET( ctx != NULL );
2156 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2157 mode == MBEDTLS_RSA_PUBLIC );
2158 RSA_VALIDATE_RET( sig != NULL );
2159 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2160 hashlen == 0 ) ||
2161 hash != NULL );
2162
2163 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002165 : md_alg;
2166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002168 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002170 sig ) );
2171
2172}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002176/*
2177 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002180 int (*f_rng)(void *, unsigned char *, size_t),
2181 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002182 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002184 unsigned int hashlen,
2185 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002186 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002187{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002188 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002189 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002190 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002191
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002192 RSA_VALIDATE_RET( ctx != NULL );
2193 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2194 mode == MBEDTLS_RSA_PUBLIC );
2195 RSA_VALIDATE_RET( sig != NULL );
2196 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2197 hashlen == 0 ) ||
2198 hash != NULL );
2199
2200 sig_len = ctx->len;
2201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2203 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002204
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002205 /*
2206 * Prepare expected PKCS1 v1.5 encoding of hash.
2207 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002208
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002209 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2210 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2211 {
2212 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2213 goto cleanup;
2214 }
2215
2216 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2217 encoded_expected ) ) != 0 )
2218 goto cleanup;
2219
2220 /*
2221 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2222 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002225 ? mbedtls_rsa_public( ctx, sig, encoded )
2226 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002227 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002228 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002229
Simon Butcher02037452016-03-01 21:19:12 +00002230 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002231 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002232 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002233
Gabor Mezei18a44942021-10-20 11:59:27 +02002234 if( ( ret = mbedtls_ct_memcmp( encoded, encoded_expected,
gabor-mezei-arm378e7eb2021-07-19 15:19:19 +02002235 sig_len ) ) != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002236 {
2237 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2238 goto cleanup;
2239 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002240
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002241cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002242
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002243 if( encoded != NULL )
2244 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002245 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002246 mbedtls_free( encoded );
2247 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002248
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002249 if( encoded_expected != NULL )
2250 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002251 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002252 mbedtls_free( encoded_expected );
2253 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002254
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002255 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002258
2259/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002260 * Do an RSA operation and check the message digest
2261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002263 int (*f_rng)(void *, unsigned char *, size_t),
2264 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002265 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002267 unsigned int hashlen,
2268 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002269 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002270{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002271 RSA_VALIDATE_RET( ctx != NULL );
2272 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2273 mode == MBEDTLS_RSA_PUBLIC );
2274 RSA_VALIDATE_RET( sig != NULL );
2275 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2276 hashlen == 0 ) ||
2277 hash != NULL );
2278
Paul Bakkerb3869132013-02-28 17:21:01 +01002279 switch( ctx->padding )
2280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#if defined(MBEDTLS_PKCS1_V15)
2282 case MBEDTLS_RSA_PKCS_V15:
2283 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002284 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002285#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287#if defined(MBEDTLS_PKCS1_V21)
2288 case MBEDTLS_RSA_PKCS_V21:
2289 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002290 hashlen, hash, sig );
2291#endif
2292
2293 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002295 }
2296}
2297
2298/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002299 * Copy the components of an RSA key
2300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002302{
Janos Follath24eed8d2019-11-22 13:21:35 +00002303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002304 RSA_VALIDATE_RET( dst != NULL );
2305 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002306
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002307 dst->len = src->len;
2308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2310 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2313 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2314 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002315
2316#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2318 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2319 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2321 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002322#endif
2323
2324 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2327 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002328
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002329 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002330 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002331
2332cleanup:
2333 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002335
2336 return( ret );
2337}
2338
2339/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002340 * Free the components of an RSA key
2341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002343{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002344 if( ctx == NULL )
2345 return;
2346
2347 mbedtls_mpi_free( &ctx->Vi );
2348 mbedtls_mpi_free( &ctx->Vf );
2349 mbedtls_mpi_free( &ctx->RN );
2350 mbedtls_mpi_free( &ctx->D );
2351 mbedtls_mpi_free( &ctx->Q );
2352 mbedtls_mpi_free( &ctx->P );
2353 mbedtls_mpi_free( &ctx->E );
2354 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002355
Hanno Becker33c30a02017-08-23 07:00:22 +01002356#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002357 mbedtls_mpi_free( &ctx->RQ );
2358 mbedtls_mpi_free( &ctx->RP );
2359 mbedtls_mpi_free( &ctx->QP );
2360 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002361 mbedtls_mpi_free( &ctx->DP );
2362#endif /* MBEDTLS_RSA_NO_CRT */
2363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002365 /* Free the mutex, but only if it hasn't been freed already. */
2366 if( ctx->ver != 0 )
2367 {
2368 mbedtls_mutex_free( &ctx->mutex );
2369 ctx->ver = 0;
2370 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002371#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002372}
2373
Hanno Beckerab377312017-08-23 16:24:51 +01002374#endif /* !MBEDTLS_RSA_ALT */
2375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002377
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002378#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002379
2380/*
2381 * Example RSA-1024 keypair, for test purposes
2382 */
2383#define KEY_LEN 128
2384
2385#define RSA_N "9292758453063D803DD603D5E777D788" \
2386 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2387 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2388 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2389 "93A89813FBF3C4F8066D2D800F7C38A8" \
2390 "1AE31942917403FF4946B0A83D3D3E05" \
2391 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2392 "5E94BB77B07507233A0BC7BAC8F90F79"
2393
2394#define RSA_E "10001"
2395
2396#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2397 "66CA472BC44D253102F8B4A9D3BFA750" \
2398 "91386C0077937FE33FA3252D28855837" \
2399 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2400 "DF79C5CE07EE72C7F123142198164234" \
2401 "CABB724CF78B8173B9F880FC86322407" \
2402 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2403 "071513A1E85B5DFA031F21ECAE91A34D"
2404
2405#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2406 "2C01CAD19EA484A87EA4377637E75500" \
2407 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2408 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2409
2410#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2411 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2412 "910E4168387E3C30AA1E00C339A79508" \
2413 "8452DD96A9A5EA5D9DCA68DA636032AF"
2414
Paul Bakker5121ce52009-01-03 21:22:43 +00002415#define PT_LEN 24
2416#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2417 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002420static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002421{
gufe44c2620da2020-08-03 17:56:50 +02002422#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002423 size_t i;
2424
Paul Bakker545570e2010-07-18 09:00:25 +00002425 if( rng_state != NULL )
2426 rng_state = NULL;
2427
Paul Bakkera3d195c2011-11-27 21:07:34 +00002428 for( i = 0; i < len; ++i )
2429 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002430#else
2431 if( rng_state != NULL )
2432 rng_state = NULL;
2433
2434 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002435#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002436
Paul Bakkera3d195c2011-11-27 21:07:34 +00002437 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002440
Paul Bakker5121ce52009-01-03 21:22:43 +00002441/*
2442 * Checkup routine
2443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002445{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002446 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002448 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 unsigned char rsa_plaintext[PT_LEN];
2451 unsigned char rsa_decrypted[PT_LEN];
2452 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002454 unsigned char sha1sum[20];
2455#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
Hanno Becker3a701162017-08-22 13:52:43 +01002457 mbedtls_mpi K;
2458
2459 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
Hanno Becker3a701162017-08-22 13:52:43 +01002462 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2463 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2464 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2465 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2466 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2467 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2468 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2469 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2470 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2471 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2472
Hanno Becker7f25f852017-10-10 16:56:22 +01002473 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
2475 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2479 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002480 {
2481 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Hanno Becker5bc87292017-05-03 15:09:31 +01002484 ret = 1;
2485 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 }
2487
2488 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002490
2491 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2492
Hanno Becker98838b02017-10-02 13:16:10 +01002493 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2494 PT_LEN, rsa_plaintext,
2495 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 {
2497 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Hanno Becker5bc87292017-05-03 15:09:31 +01002500 ret = 1;
2501 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 }
2503
2504 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002506
Hanno Becker98838b02017-10-02 13:16:10 +01002507 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2508 &len, rsa_ciphertext, rsa_decrypted,
2509 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 {
2511 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002513
Hanno Becker5bc87292017-05-03 15:09:31 +01002514 ret = 1;
2515 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 }
2517
2518 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2519 {
2520 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Hanno Becker5bc87292017-05-03 15:09:31 +01002523 ret = 1;
2524 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 }
2526
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002527 if( verbose != 0 )
2528 mbedtls_printf( "passed\n" );
2529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002532 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002534 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002535 {
2536 if( verbose != 0 )
2537 mbedtls_printf( "failed\n" );
2538
2539 return( 1 );
2540 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
Hanno Becker98838b02017-10-02 13:16:10 +01002542 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2543 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2544 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 {
2546 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
Hanno Becker5bc87292017-05-03 15:09:31 +01002549 ret = 1;
2550 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 }
2552
2553 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Hanno Becker98838b02017-10-02 13:16:10 +01002556 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2557 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2558 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 {
2560 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
Hanno Becker5bc87292017-05-03 15:09:31 +01002563 ret = 1;
2564 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 }
2566
2567 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002568 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002571 if( verbose != 0 )
2572 mbedtls_printf( "\n" );
2573
Paul Bakker3d8fb632014-04-17 12:42:41 +02002574cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002575 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 mbedtls_rsa_free( &rsa );
2577#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002578 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002580 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002581}
2582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585#endif /* MBEDTLS_RSA_C */