blob: 0b3011f14a0135f7c6d399bfc59b4edd7d5a087e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
Hanno Becker74716312017-10-02 10:00:37 +010046
Paul Bakker5121ce52009-01-03 21:22:43 +000047/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000048 * The following sources were referenced in the design of this implementation
49 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000050 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000051 * [1] A method for obtaining digital signatures and public-key cryptosystems
52 * R Rivest, A Shamir, and L Adleman
53 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
54 *
55 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
56 * Menezes, van Oorschot and Vanstone
57 *
Janos Follathe81102e2017-03-22 13:38:28 +000058 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
59 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
60 * Stefan Mangard
61 * https://arxiv.org/abs/1702.08719v2
62 *
Paul Bakker5121ce52009-01-03 21:22:43 +000063 */
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020067#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020069#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010074#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050076#include "mbedtls/platform_util.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000077
Rich Evans00ab4702015-02-06 13:43:58 +000078#include <string.h>
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000081#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000083
gufe443fa7c642020-08-03 17:56:50 +020084#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000085#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000086#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000089#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010090#else
Rich Evans00ab4702015-02-06 13:43:58 +000091#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020093#define mbedtls_calloc calloc
94#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010095#endif
96
Hanno Beckera565f542017-10-11 11:00:19 +010097#if !defined(MBEDTLS_RSA_ALT)
98
Hanno Beckerddeeed72018-12-13 18:07:00 +000099/* Parameter validation macros */
100#define RSA_VALIDATE_RET( cond ) \
101 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
102#define RSA_VALIDATE( cond ) \
103 MBEDTLS_INTERNAL_VALIDATE( cond )
104
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100105#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +0100106/* constant-time buffer comparison */
107static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
108{
109 size_t i;
110 const unsigned char *A = (const unsigned char *) a;
111 const unsigned char *B = (const unsigned char *) b;
112 unsigned char diff = 0;
113
114 for( i = 0; i < n; i++ )
115 diff |= A[i] ^ B[i];
116
117 return( diff );
118}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100119#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +0100120
Hanno Becker617c1ae2017-08-23 14:11:24 +0100121int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
122 const mbedtls_mpi *N,
123 const mbedtls_mpi *P, const mbedtls_mpi *Q,
124 const mbedtls_mpi *D, const mbedtls_mpi *E )
125{
126 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000127 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100128
129 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
130 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
131 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
132 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
133 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
134 {
135 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
136 }
137
138 if( N != NULL )
139 ctx->len = mbedtls_mpi_size( &ctx->N );
140
141 return( 0 );
142}
143
144int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100145 unsigned char const *N, size_t N_len,
146 unsigned char const *P, size_t P_len,
147 unsigned char const *Q, size_t Q_len,
148 unsigned char const *D, size_t D_len,
149 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100150{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000151 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000152 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100153
154 if( N != NULL )
155 {
156 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
157 ctx->len = mbedtls_mpi_size( &ctx->N );
158 }
159
160 if( P != NULL )
161 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
162
163 if( Q != NULL )
164 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
165
166 if( D != NULL )
167 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
168
169 if( E != NULL )
170 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
171
172cleanup:
173
174 if( ret != 0 )
175 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
176
177 return( 0 );
178}
179
Hanno Becker705fc682017-10-10 17:57:02 +0100180/*
181 * Checks whether the context fields are set in such a way
182 * that the RSA primitives will be able to execute without error.
183 * It does *not* make guarantees for consistency of the parameters.
184 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100185static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
186 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100187{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100188#if !defined(MBEDTLS_RSA_NO_CRT)
189 /* blinding_needed is only used for NO_CRT to decide whether
190 * P,Q need to be present or not. */
191 ((void) blinding_needed);
192#endif
193
Hanno Becker3a760a12018-01-05 08:14:49 +0000194 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
195 ctx->len > MBEDTLS_MPI_MAX_SIZE )
196 {
Hanno Becker705fc682017-10-10 17:57:02 +0100197 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000198 }
Hanno Becker705fc682017-10-10 17:57:02 +0100199
200 /*
201 * 1. Modular exponentiation needs positive, odd moduli.
202 */
203
204 /* Modular exponentiation wrt. N is always used for
205 * RSA public key operations. */
206 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
207 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
208 {
209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
210 }
211
212#if !defined(MBEDTLS_RSA_NO_CRT)
213 /* Modular exponentiation for P and Q is only
214 * used for private key operations and if CRT
215 * is used. */
216 if( is_priv &&
217 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
218 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
219 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
220 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
221 {
222 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
223 }
224#endif /* !MBEDTLS_RSA_NO_CRT */
225
226 /*
227 * 2. Exponents must be positive
228 */
229
230 /* Always need E for public key operations */
231 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
232 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
233
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100234#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100235 /* For private key operations, use D or DP & DQ
236 * as (unblinded) exponents. */
237 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
238 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
239#else
240 if( is_priv &&
241 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
242 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
243 {
244 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
245 }
246#endif /* MBEDTLS_RSA_NO_CRT */
247
248 /* Blinding shouldn't make exponents negative either,
249 * so check that P, Q >= 1 if that hasn't yet been
250 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100251#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100252 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100253 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
254 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
255 {
256 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
257 }
258#endif
259
260 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100261 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100262#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100263 if( is_priv &&
264 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
265 {
266 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
267 }
268#endif
269
270 return( 0 );
271}
272
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100273int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100274{
275 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000276 int have_N, have_P, have_Q, have_D, have_E;
Jack Lloydb10fd062020-01-29 13:09:55 -0500277#if !defined(MBEDTLS_RSA_NO_CRT)
278 int have_DP, have_DQ, have_QP;
279#endif
Hanno Beckerddeeed72018-12-13 18:07:00 +0000280 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100281
Hanno Beckerddeeed72018-12-13 18:07:00 +0000282 RSA_VALIDATE_RET( ctx != NULL );
283
284 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
285 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
286 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
287 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
288 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100289
Jack Lloydb10fd062020-01-29 13:09:55 -0500290#if !defined(MBEDTLS_RSA_NO_CRT)
291 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
292 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
293 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
294#endif
295
Hanno Becker617c1ae2017-08-23 14:11:24 +0100296 /*
297 * Check whether provided parameters are enough
298 * to deduce all others. The following incomplete
299 * parameter sets for private keys are supported:
300 *
301 * (1) P, Q missing.
302 * (2) D and potentially N missing.
303 *
304 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100305
Hanno Beckerddeeed72018-12-13 18:07:00 +0000306 n_missing = have_P && have_Q && have_D && have_E;
307 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
308 d_missing = have_P && have_Q && !have_D && have_E;
309 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100310
311 /* These three alternatives are mutually exclusive */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000312 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100313
Hanno Becker617c1ae2017-08-23 14:11:24 +0100314 if( !is_priv && !is_pub )
315 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
316
317 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100318 * Step 1: Deduce N if P, Q are provided.
319 */
320
321 if( !have_N && have_P && have_Q )
322 {
323 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
324 &ctx->Q ) ) != 0 )
325 {
326 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
327 }
328
329 ctx->len = mbedtls_mpi_size( &ctx->N );
330 }
331
332 /*
333 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100334 */
335
336 if( pq_missing )
337 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100338 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100339 &ctx->P, &ctx->Q );
340 if( ret != 0 )
341 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
342
343 }
344 else if( d_missing )
345 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100346 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
347 &ctx->Q,
348 &ctx->E,
349 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100350 {
351 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
352 }
353 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100354
Hanno Becker617c1ae2017-08-23 14:11:24 +0100355 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100356 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100357 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100358 */
359
Hanno Becker23344b52017-08-23 07:43:27 +0100360#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloydb10fd062020-01-29 13:09:55 -0500361 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100362 {
363 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
364 &ctx->DP, &ctx->DQ, &ctx->QP );
365 if( ret != 0 )
366 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
367 }
Hanno Becker23344b52017-08-23 07:43:27 +0100368#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100369
370 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100371 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100372 */
373
Hanno Beckerebd2c022017-10-12 10:54:53 +0100374 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100375}
376
Hanno Becker617c1ae2017-08-23 14:11:24 +0100377int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
378 unsigned char *N, size_t N_len,
379 unsigned char *P, size_t P_len,
380 unsigned char *Q, size_t Q_len,
381 unsigned char *D, size_t D_len,
382 unsigned char *E, size_t E_len )
383{
384 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000385 int is_priv;
386 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100387
388 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000389 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100390 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
391 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
392 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
393 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
394 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
395
396 if( !is_priv )
397 {
398 /* If we're trying to export private parameters for a public key,
399 * something must be wrong. */
400 if( P != NULL || Q != NULL || D != NULL )
401 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
402
403 }
404
405 if( N != NULL )
406 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
407
408 if( P != NULL )
409 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
410
411 if( Q != NULL )
412 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
413
414 if( D != NULL )
415 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
416
417 if( E != NULL )
418 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100419
420cleanup:
421
422 return( ret );
423}
424
Hanno Becker617c1ae2017-08-23 14:11:24 +0100425int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
426 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
427 mbedtls_mpi *D, mbedtls_mpi *E )
428{
429 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000430 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 */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000434 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 {
443 /* If we're trying to export private parameters for a public key,
444 * something must be wrong. */
445 if( P != NULL || Q != NULL || D != NULL )
446 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
447
448 }
449
450 /* Export all requested core parameters. */
451
452 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
453 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
454 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
455 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
456 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
457 {
458 return( ret );
459 }
460
461 return( 0 );
462}
463
464/*
465 * Export CRT parameters
466 * This must also be implemented if CRT is not used, for being able to
467 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
468 * can be used in this case.
469 */
470int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
471 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
472{
473 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000474 int is_priv;
475 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100476
477 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000478 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100479 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
480 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
481 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
482 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
483 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
484
485 if( !is_priv )
486 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
487
Hanno Beckerdc95c892017-08-23 06:57:02 +0100488#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100489 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100490 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
491 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
492 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
493 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100494 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100495 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100496#else
497 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
498 DP, DQ, QP ) ) != 0 )
499 {
500 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
501 }
502#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100503
504 return( 0 );
505}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100506
Paul Bakker5121ce52009-01-03 21:22:43 +0000507/*
508 * Initialize an RSA context
509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000512 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000513{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000514 RSA_VALIDATE( ctx != NULL );
515 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
516 padding == MBEDTLS_RSA_PKCS_V21 );
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#if defined(MBEDTLS_THREADING_C)
Gilles Peskineb9fce3c2021-02-01 17:57:41 +0100523 /* Set ctx->ver to nonzero to indicate that the mutex has been
524 * initialized and will need to be freed. */
525 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200527#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000528}
529
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100530/*
531 * Set padding for an existing RSA context
532 */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000533void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
534 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100535{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000536 RSA_VALIDATE( ctx != NULL );
537 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
538 padding == MBEDTLS_RSA_PKCS_V21 );
539
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100540 ctx->padding = padding;
541 ctx->hash_id = hash_id;
542}
543
Hanno Becker617c1ae2017-08-23 14:11:24 +0100544/*
545 * Get length in bytes of RSA modulus
546 */
547
548size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
549{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100550 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100551}
552
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556/*
557 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800558 *
559 * This generation method follows the RSA key pair generation procedure of
560 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000563 int (*f_rng)(void *, unsigned char *, size_t),
564 void *p_rng,
565 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566{
567 int ret;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800568 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100569 int prime_quality = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000570 RSA_VALIDATE_RET( ctx != NULL );
571 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Hanno Beckerddeeed72018-12-13 18:07:00 +0000573 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
Janos Follathef441782016-09-21 13:18:12 +0100574 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
575
Janos Follathb8fc1b02018-09-03 15:37:01 +0100576 /*
577 * If the modulus is 1024 bit long or shorter, then the security strength of
578 * the RSA algorithm is less than or equal to 80 bits and therefore an error
579 * rate of 2^-80 is sufficient.
580 */
581 if( nbits > 1024 )
582 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
583
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100584 mbedtls_mpi_init( &H );
585 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800586 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
588 /*
589 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800590 * 1. |P-Q| > 2^( nbits / 2 - 100 )
591 * 2. GCD( E, (P-1)*(Q-1) ) == 1
592 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
596 do
597 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100598 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
599 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Janos Follathb8fc1b02018-09-03 15:37:01 +0100601 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
602 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800604 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
605 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
606 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 continue;
608
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800609 /* not required by any standards, but some users rely on the fact that P > Q */
610 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100611 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100612
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100613 /* Temporarily replace P,Q by P-1, Q-1 */
614 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
615 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
616 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800617
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800618 /* 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 +0200619 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800620 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
621 continue;
622
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800623 /* 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 -0800624 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
625 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
626 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
627
628 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
629 continue;
630
631 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800633 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100635 /* Restore P,Q */
636 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
637 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
638
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800639 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
640
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100641 ctx->len = mbedtls_mpi_size( &ctx->N );
642
Jethro Beekman97f95c92018-02-13 15:50:36 -0800643#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 * DP = D mod (P - 1)
646 * DQ = D mod (Q - 1)
647 * QP = Q^-1 mod P
648 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100649 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
650 &ctx->DP, &ctx->DQ, &ctx->QP ) );
651#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Hanno Becker83aad1f2017-08-23 06:45:10 +0100653 /* Double-check */
654 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
656cleanup:
657
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100658 mbedtls_mpi_free( &H );
659 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800660 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
662 if( ret != 0 )
663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_rsa_free( ctx );
665 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 }
667
Paul Bakker48377d92013-08-30 12:06:24 +0200668 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000669}
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673/*
674 * Check a public RSA key
675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000677{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000678 RSA_VALIDATE_RET( ctx != NULL );
679
Hanno Beckerebd2c022017-10-12 10:54:53 +0100680 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000682
Hanno Becker3a760a12018-01-05 08:14:49 +0000683 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100686 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000687
Hanno Becker705fc682017-10-10 17:57:02 +0100688 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
689 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100693 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
695 return( 0 );
696}
697
698/*
Hanno Becker705fc682017-10-10 17:57:02 +0100699 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000702{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000703 RSA_VALIDATE_RET( ctx != NULL );
704
Hanno Becker705fc682017-10-10 17:57:02 +0100705 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100706 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 {
Hanno Becker98838b02017-10-02 13:16:10 +0100708 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 }
Paul Bakker48377d92013-08-30 12:06:24 +0200710
Hanno Becker98838b02017-10-02 13:16:10 +0100711 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100712 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100714 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000716
Hanno Beckerb269a852017-08-25 08:03:21 +0100717#if !defined(MBEDTLS_RSA_NO_CRT)
718 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
719 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
720 {
721 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
722 }
723#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000724
725 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000726}
727
728/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100729 * Check if contexts holding a public and private key match
730 */
Hanno Becker98838b02017-10-02 13:16:10 +0100731int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
732 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100733{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000734 RSA_VALIDATE_RET( pub != NULL );
735 RSA_VALIDATE_RET( prv != NULL );
736
Hanno Becker98838b02017-10-02 13:16:10 +0100737 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100741 }
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
744 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100747 }
748
749 return( 0 );
750}
751
752/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000753 * Do an RSA public key operation
754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000756 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000757 unsigned char *output )
758{
Paul Bakker23986e52011-04-24 08:57:21 +0000759 int ret;
760 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_mpi T;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000762 RSA_VALIDATE_RET( ctx != NULL );
763 RSA_VALIDATE_RET( input != NULL );
764 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Hanno Beckerebd2c022017-10-12 10:54:53 +0100766 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100767 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200771#if defined(MBEDTLS_THREADING_C)
772 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
773 return( ret );
774#endif
775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200780 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
781 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000782 }
783
784 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
786 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000787
788cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200790 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
791 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100792#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000795
796 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000798
799 return( 0 );
800}
801
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200802/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200803 * Generate or update blinding values, see section 10 of:
804 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200805 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200806 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200807 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200808static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200809 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
810{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200811 int ret, count = 0;
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200812 mbedtls_mpi R;
813
814 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200815
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200816 if( ctx->Vf.p != NULL )
817 {
818 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
820 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
821 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
822 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200823
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200824 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200825 }
826
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200827 /* Unblinding value: Vf = random number, invertible mod N */
828 do {
829 if( count++ > 10 )
Manuel Pégourié-Gonnardcadcf4c2020-07-16 09:23:30 +0200830 {
831 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
832 goto cleanup;
833 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
Manuel Pégourié-Gonnard86ad5be2020-06-26 11:03:19 +0200836
Manuel Pégourié-Gonnard87a602d2020-07-16 09:48:54 +0200837 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200838 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
839 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
840 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
841
Manuel Pégourié-Gonnard87a602d2020-07-16 09:48:54 +0200842 /* At this point, Vi is invertible mod N if and only if both Vf and R
843 * are invertible mod N. If one of them isn't, we don't need to know
844 * which one, we just loop and choose new values for both of them.
845 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200846 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusb2aeb752020-09-24 11:11:50 -0500847 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnard86ad5be2020-06-26 11:03:19 +0200848 goto cleanup;
849
Peter Kolbusb2aeb752020-09-24 11:11:50 -0500850 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
851
852 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
853 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
854 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200855
Manuel Pégourié-Gonnard87a602d2020-07-16 09:48:54 +0200856 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200857 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 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 +0200859
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200860
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200861cleanup:
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200862 mbedtls_mpi_free( &R );
863
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200864 return( ret );
865}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200866
Paul Bakker5121ce52009-01-03 21:22:43 +0000867/*
Janos Follathe81102e2017-03-22 13:38:28 +0000868 * Exponent blinding supposed to prevent side-channel attacks using multiple
869 * traces of measurements to recover the RSA key. The more collisions are there,
870 * the more bits of the key can be recovered. See [3].
871 *
872 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
873 * observations on avarage.
874 *
875 * For example with 28 byte blinding to achieve 2 collisions the adversary has
876 * to make 2^112 observations on avarage.
877 *
878 * (With the currently (as of 2017 April) known best algorithms breaking 2048
879 * bit RSA requires approximately as much time as trying out 2^112 random keys.
880 * Thus in this sense with 28 byte blinding the security is not reduced by
881 * side-channel attacks like the one in [3])
882 *
883 * This countermeasure does not help if the key recovery is possible with a
884 * single trace.
885 */
886#define RSA_EXPONENT_BLINDING 28
887
888/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 * Do an RSA private key operation
890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200892 int (*f_rng)(void *, unsigned char *, size_t),
893 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000894 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000895 unsigned char *output )
896{
Paul Bakker23986e52011-04-24 08:57:21 +0000897 int ret;
898 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100899
900 /* Temporary holding the result */
901 mbedtls_mpi T;
902
903 /* Temporaries holding P-1, Q-1 and the
904 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000905 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100906
907#if !defined(MBEDTLS_RSA_NO_CRT)
908 /* Temporaries holding the results mod p resp. mod q. */
909 mbedtls_mpi TP, TQ;
910
911 /* Temporaries holding the blinded exponents for
912 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000913 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100914
915 /* Pointers to actual exponents to be used - either the unblinded
916 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000917 mbedtls_mpi *DP = &ctx->DP;
918 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100919#else
920 /* Temporary holding the blinded exponent (if used). */
921 mbedtls_mpi D_blind;
922
923 /* Pointer to actual exponent to be used - either the unblinded
924 * or the blinded one, depending on the presence of a PRNG. */
925 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100926#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100927
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100928 /* Temporaries holding the initial input and the double
929 * checked result; should be the same in the end. */
930 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Hanno Beckerddeeed72018-12-13 18:07:00 +0000932 RSA_VALIDATE_RET( ctx != NULL );
933 RSA_VALIDATE_RET( input != NULL );
934 RSA_VALIDATE_RET( output != NULL );
935
Hanno Beckerebd2c022017-10-12 10:54:53 +0100936 if( rsa_check_context( ctx, 1 /* private key checks */,
937 f_rng != NULL /* blinding y/n */ ) != 0 )
938 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100939 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100940 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100941
Hanno Becker06811ce2017-05-03 15:10:34 +0100942#if defined(MBEDTLS_THREADING_C)
943 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
944 return( ret );
945#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000946
Hanno Becker06811ce2017-05-03 15:10:34 +0100947 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100948 mbedtls_mpi_init( &T );
949
950 mbedtls_mpi_init( &P1 );
951 mbedtls_mpi_init( &Q1 );
952 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000953
Janos Follathf9203b42017-03-22 15:13:15 +0000954 if( f_rng != NULL )
955 {
Janos Follathe81102e2017-03-22 13:38:28 +0000956#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000957 mbedtls_mpi_init( &D_blind );
958#else
959 mbedtls_mpi_init( &DP_blind );
960 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000961#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000962 }
Janos Follathe81102e2017-03-22 13:38:28 +0000963
Hanno Becker06811ce2017-05-03 15:10:34 +0100964#if !defined(MBEDTLS_RSA_NO_CRT)
965 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200966#endif
967
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100968 mbedtls_mpi_init( &I );
969 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100970
971 /* End of MPI initialization */
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
974 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200976 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
977 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 }
979
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100980 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100981
Paul Bakkerf451bac2013-08-30 15:37:02 +0200982 if( f_rng != NULL )
983 {
984 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200985 * Blinding
986 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200987 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200988 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
989 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000991
Janos Follathe81102e2017-03-22 13:38:28 +0000992 /*
993 * Exponent blinding
994 */
995 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
996 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
997
Janos Follathf9203b42017-03-22 15:13:15 +0000998#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000999 /*
1000 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1001 */
1002 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1003 f_rng, p_rng ) );
1004 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1005 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1006 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1007
1008 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001009#else
1010 /*
1011 * DP_blind = ( P - 1 ) * R + DP
1012 */
1013 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1014 f_rng, p_rng ) );
1015 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1016 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1017 &ctx->DP ) );
1018
1019 DP = &DP_blind;
1020
1021 /*
1022 * DQ_blind = ( Q - 1 ) * R + DQ
1023 */
1024 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1025 f_rng, p_rng ) );
1026 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1027 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1028 &ctx->DQ ) );
1029
1030 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001031#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001032 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001035 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001036#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001037 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001038 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001040 * TP = input ^ dP mod P
1041 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001043
1044 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1045 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
1047 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001048 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001049 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001050 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1051 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1052 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
1054 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001055 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001057 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1058 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001060
Paul Bakkerf451bac2013-08-30 15:37:02 +02001061 if( f_rng != NULL )
1062 {
1063 /*
1064 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001065 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001066 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001067 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Hanno Becker2dec5e82017-10-03 07:49:52 +01001071 /* Verify the result to prevent glitching attacks. */
1072 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1073 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001074 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001075 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001076 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1077 goto cleanup;
1078 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001079
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001082
1083cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001085 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1086 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001087#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001088
Hanno Becker06811ce2017-05-03 15:10:34 +01001089 mbedtls_mpi_free( &P1 );
1090 mbedtls_mpi_free( &Q1 );
1091 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001092
1093 if( f_rng != NULL )
1094 {
Janos Follathe81102e2017-03-22 13:38:28 +00001095#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001096 mbedtls_mpi_free( &D_blind );
1097#else
1098 mbedtls_mpi_free( &DP_blind );
1099 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001100#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001101 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
Hanno Becker06811ce2017-05-03 15:10:34 +01001103 mbedtls_mpi_free( &T );
1104
1105#if !defined(MBEDTLS_RSA_NO_CRT)
1106 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1107#endif
1108
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001109 mbedtls_mpi_free( &C );
1110 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001111
Gilles Peskine3b7523e2020-11-25 00:10:31 +01001112 if( ret != 0 && ret >= -0x007f )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
Gilles Peskine3b7523e2020-11-25 00:10:31 +01001115 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001116}
1117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001119/**
1120 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1121 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001122 * \param dst buffer to mask
1123 * \param dlen length of destination buffer
1124 * \param src source of the mask generation
1125 * \param slen length of the source buffer
1126 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001127 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001128static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001130{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001132 unsigned char counter[4];
1133 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001134 unsigned int hlen;
1135 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001136 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001139 memset( counter, 0, 4 );
1140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001142
Simon Butcher02037452016-03-01 21:19:12 +00001143 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001144 p = dst;
1145
1146 while( dlen > 0 )
1147 {
1148 use_len = hlen;
1149 if( dlen < hlen )
1150 use_len = dlen;
1151
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001152 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1153 goto exit;
1154 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1155 goto exit;
1156 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1157 goto exit;
1158 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1159 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001160
1161 for( i = 0; i < use_len; ++i )
1162 *p++ ^= mask[i];
1163
1164 counter[3]++;
1165
1166 dlen -= use_len;
1167 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001168
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001169exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001170 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001171
1172 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001177/*
1178 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001181 int (*f_rng)(void *, unsigned char *, size_t),
1182 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001183 int mode,
1184 const unsigned char *label, size_t label_len,
1185 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001186 const unsigned char *input,
1187 unsigned char *output )
1188{
1189 size_t olen;
1190 int ret;
1191 unsigned char *p = output;
1192 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 const mbedtls_md_info_t *md_info;
1194 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001195
Hanno Beckerddeeed72018-12-13 18:07:00 +00001196 RSA_VALIDATE_RET( ctx != NULL );
1197 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1198 mode == MBEDTLS_RSA_PUBLIC );
1199 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001200 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001201 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1204 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001205
1206 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001210 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001212
1213 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001215
Simon Butcher02037452016-03-01 21:19:12 +00001216 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001217 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001219
1220 memset( output, 0, olen );
1221
1222 *p++ = 0;
1223
Simon Butcher02037452016-03-01 21:19:12 +00001224 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001225 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001227
1228 p += hlen;
1229
Simon Butcher02037452016-03-01 21:19:12 +00001230 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001231 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1232 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001233 p += hlen;
1234 p += olen - 2 * hlen - 2 - ilen;
1235 *p++ = 1;
1236 memcpy( p, input, ilen );
1237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001239 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001240 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001241
Simon Butcher02037452016-03-01 21:19:12 +00001242 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001243 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1244 &md_ctx ) ) != 0 )
1245 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001246
Simon Butcher02037452016-03-01 21:19:12 +00001247 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001248 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1249 &md_ctx ) ) != 0 )
1250 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001251
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001252exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001254
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001255 if( ret != 0 )
1256 return( ret );
1257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 return( ( mode == MBEDTLS_RSA_PUBLIC )
1259 ? mbedtls_rsa_public( ctx, output, output )
1260 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001265/*
1266 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001269 int (*f_rng)(void *, unsigned char *, size_t),
1270 void *p_rng,
1271 int mode, size_t ilen,
1272 const unsigned char *input,
1273 unsigned char *output )
1274{
1275 size_t nb_pad, olen;
1276 int ret;
1277 unsigned char *p = output;
1278
Hanno Beckerddeeed72018-12-13 18:07:00 +00001279 RSA_VALIDATE_RET( ctx != NULL );
1280 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1281 mode == MBEDTLS_RSA_PUBLIC );
1282 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001283 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1286 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001287
Paul Bakkerb3869132013-02-28 17:21:01 +01001288 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001289
Simon Butcher02037452016-03-01 21:19:12 +00001290 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001291 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001293
1294 nb_pad = olen - 3 - ilen;
1295
1296 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001298 {
Hanno Beckerb86e6842018-12-18 14:46:04 +00001299 if( f_rng == NULL )
1300 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001303
1304 while( nb_pad-- > 0 )
1305 {
1306 int rng_dl = 100;
1307
1308 do {
1309 ret = f_rng( p_rng, p, 1 );
1310 } while( *p == 0 && --rng_dl && ret == 0 );
1311
Simon Butcher02037452016-03-01 21:19:12 +00001312 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001313 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001315
1316 p++;
1317 }
1318 }
1319 else
1320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001322
1323 while( nb_pad-- > 0 )
1324 *p++ = 0xFF;
1325 }
1326
1327 *p++ = 0;
1328 memcpy( p, input, ilen );
1329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 return( ( mode == MBEDTLS_RSA_PUBLIC )
1331 ? mbedtls_rsa_public( ctx, output, output )
1332 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001335
Paul Bakker5121ce52009-01-03 21:22:43 +00001336/*
1337 * Add the message padding, then do an RSA operation
1338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001340 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001341 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001342 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001343 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 unsigned char *output )
1345{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001346 RSA_VALIDATE_RET( ctx != NULL );
1347 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1348 mode == MBEDTLS_RSA_PUBLIC );
1349 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001350 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001351
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 switch( ctx->padding )
1353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#if defined(MBEDTLS_PKCS1_V15)
1355 case MBEDTLS_RSA_PKCS_V15:
1356 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001357 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001358#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#if defined(MBEDTLS_PKCS1_V21)
1361 case MBEDTLS_RSA_PKCS_V21:
1362 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001363 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001364#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
1366 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001369}
1370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001372/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001373 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001376 int (*f_rng)(void *, unsigned char *, size_t),
1377 void *p_rng,
1378 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001379 const unsigned char *label, size_t label_len,
1380 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001381 const unsigned char *input,
1382 unsigned char *output,
1383 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001384{
Paul Bakker23986e52011-04-24 08:57:21 +00001385 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001386 size_t ilen, i, pad_len;
1387 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1389 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001390 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 const mbedtls_md_info_t *md_info;
1392 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001393
Hanno Beckerddeeed72018-12-13 18:07:00 +00001394 RSA_VALIDATE_RET( ctx != NULL );
1395 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1396 mode == MBEDTLS_RSA_PUBLIC );
1397 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1398 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1399 RSA_VALIDATE_RET( input != NULL );
1400 RSA_VALIDATE_RET( olen != NULL );
1401
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001402 /*
1403 * Parameters sanity checks
1404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1406 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
1408 ilen = ctx->len;
1409
Paul Bakker27fdf462011-06-09 13:55:13 +00001410 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001414 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001416
Janos Follathc17cda12016-02-11 11:08:18 +00001417 hlen = mbedtls_md_get_size( md_info );
1418
1419 // checking for integer underflow
1420 if( 2 * hlen + 2 > ilen )
1421 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1422
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001423 /*
1424 * RSA operation
1425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1427 ? mbedtls_rsa_public( ctx, input, buf )
1428 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429
1430 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001431 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001433 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001434 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001437 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1438 {
1439 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001440 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001441 }
1442
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001443 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001444 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1445 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001446 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001447 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1448 &md_ctx ) ) != 0 )
1449 {
1450 mbedtls_md_free( &md_ctx );
1451 goto cleanup;
1452 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001455
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001456 /* Generate lHash */
1457 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1458 goto cleanup;
1459
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001460 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001461 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001462 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001463 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001464 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001465
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001466 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001467
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001468 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001469
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001470 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001471 for( i = 0; i < hlen; i++ )
1472 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001473
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001474 /* Get zero-padding len, but always read till end of buffer
1475 * (minus one, for the 01 byte) */
1476 pad_len = 0;
1477 pad_done = 0;
1478 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1479 {
1480 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001481 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001482 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001483
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001484 p += pad_len;
1485 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001486
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001487 /*
1488 * The only information "leaked" is whether the padding was correct or not
1489 * (eg, no data is copied if it was not correct). This meets the
1490 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1491 * the different error conditions.
1492 */
1493 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001494 {
1495 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1496 goto cleanup;
1497 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001498
Paul Bakker66d5d072014-06-17 16:39:18 +02001499 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001500 {
1501 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1502 goto cleanup;
1503 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001504
1505 *olen = ilen - (p - buf);
1506 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001507 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001508
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001509cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001510 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1511 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001512
1513 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001518/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1519 *
1520 * \param value The value to analyze.
Gilles Peskine331d80e2018-10-04 18:32:29 +02001521 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001522 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001523static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001524{
1525 /* MSVC has a warning about unary minus on unsigned, but this is
1526 * well-defined and precisely what we want to do here */
1527#if defined(_MSC_VER)
1528#pragma warning( push )
1529#pragma warning( disable : 4146 )
1530#endif
1531 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1532#if defined(_MSC_VER)
1533#pragma warning( pop )
1534#endif
1535}
1536
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001537/** Check whether a size is out of bounds, without branches.
1538 *
1539 * This is equivalent to `size > max`, but is likely to be compiled to
1540 * to code using bitwise operation rather than a branch.
1541 *
1542 * \param size Size to check.
1543 * \param max Maximum desired value for \p size.
1544 * \return \c 0 if `size <= max`.
1545 * \return \c 1 if `size > max`.
1546 */
1547static unsigned size_greater_than( size_t size, size_t max )
1548{
1549 /* Return the sign bit (1 for negative) of (max - size). */
1550 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1551}
1552
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001553/** Choose between two integer values, without branches.
1554 *
Gilles Peskine331d80e2018-10-04 18:32:29 +02001555 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1556 * to code using bitwise operation rather than a branch.
1557 *
1558 * \param cond Condition to test.
1559 * \param if1 Value to use if \p cond is nonzero.
1560 * \param if0 Value to use if \p cond is zero.
1561 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001562 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001563static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001564{
Gilles Peskine331d80e2018-10-04 18:32:29 +02001565 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001566 return( ( mask & if1 ) | (~mask & if0 ) );
1567}
1568
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001569/** Shift some data towards the left inside a buffer without leaking
1570 * the length of the data through side channels.
1571 *
1572 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1573 * ```
1574 * memmove(start, start + offset, total - offset);
1575 * memset(start + offset, 0, total - offset);
1576 * ```
1577 * but it strives to use a memory access pattern (and thus total timing)
1578 * that does not depend on \p offset. This timing independence comes at
1579 * the expense of performance.
1580 *
1581 * \param start Pointer to the start of the buffer.
1582 * \param total Total size of the buffer.
1583 * \param offset Offset from which to copy \p total - \p offset bytes.
1584 */
1585static void mem_move_to_left( void *start,
1586 size_t total,
1587 size_t offset )
1588{
1589 volatile unsigned char *buf = start;
1590 size_t i, n;
1591 if( total == 0 )
1592 return;
1593 for( i = 0; i < total; i++ )
1594 {
1595 unsigned no_op = size_greater_than( total - offset, i );
1596 /* The first `total - offset` passes are a no-op. The last
1597 * `offset` passes shift the data one byte to the left and
1598 * zero out the last byte. */
1599 for( n = 0; n < total - 1; n++ )
Gilles Peskine9b430702018-10-12 19:15:34 +02001600 {
1601 unsigned char current = buf[n];
1602 unsigned char next = buf[n+1];
1603 buf[n] = if_int( no_op, current, next );
1604 }
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001605 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1606 }
1607}
1608
Paul Bakkerb3869132013-02-28 17:21:01 +01001609/*
1610 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001613 int (*f_rng)(void *, unsigned char *, size_t),
1614 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001615 int mode, size_t *olen,
1616 const unsigned char *input,
1617 unsigned char *output,
Gilles Peskine5908dd42018-10-02 22:43:06 +02001618 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001619{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001620 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001621 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine85a74422018-10-05 14:50:21 +02001623 /* The following variables take sensitive values: their value must
1624 * not leak into the observable behavior of the function other than
1625 * the designated outputs (output, olen, return value). Otherwise
1626 * this would open the execution of the function to
1627 * side-channel-based variants of the Bleichenbacher padding oracle
1628 * attack. Potential side channels include overall timing, memory
1629 * access patterns (especially visible to an adversary who has access
1630 * to a shared memory cache), and branches (especially visible to
1631 * an adversary who has access to a shared code cache or to a shared
1632 * branch predictor). */
1633 size_t pad_count = 0;
1634 unsigned bad = 0;
1635 unsigned char pad_done = 0;
1636 size_t plaintext_size = 0;
1637 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001638
Hanno Beckerddeeed72018-12-13 18:07:00 +00001639 RSA_VALIDATE_RET( ctx != NULL );
1640 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1641 mode == MBEDTLS_RSA_PUBLIC );
1642 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1643 RSA_VALIDATE_RET( input != NULL );
1644 RSA_VALIDATE_RET( olen != NULL );
1645
1646 ilen = ctx->len;
1647 plaintext_max_size = ( output_max_len > ilen - 11 ?
1648 ilen - 11 :
1649 output_max_len );
1650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1652 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001653
Paul Bakkerb3869132013-02-28 17:21:01 +01001654 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1658 ? mbedtls_rsa_public( ctx, input, buf )
1659 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001660
1661 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001662 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001663
Gilles Peskine40b57f42018-10-05 15:06:12 +02001664 /* Check and get padding length in constant time and constant
1665 * memory trace. The first byte must be 0. */
1666 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001670 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1671 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001672 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001673
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001674 /* Read the whole buffer. Set pad_done to nonzero if we find
1675 * the 0x00 byte and remember the padding length in pad_count. */
1676 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001677 {
Gilles Peskine85a74422018-10-05 14:50:21 +02001678 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001679 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001680 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001681 }
1682 else
1683 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001684 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1685 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001686 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001687
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001688 /* Read the whole buffer. Set pad_done to nonzero if we find
1689 * the 0x00 byte and remember the padding length in pad_count.
1690 * If there's a non-0xff byte in the padding, the padding is bad. */
1691 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001692 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001693 pad_done |= if_int( buf[i], 0, 1 );
1694 pad_count += if_int( pad_done, 0, 1 );
1695 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001696 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 }
1698
Gilles Peskine40b57f42018-10-05 15:06:12 +02001699 /* If pad_done is still zero, there's no data, only unfinished padding. */
1700 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001701
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001702 /* There must be at least 8 bytes of padding. */
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001703 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001704
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001705 /* If the padding is valid, set plaintext_size to the number of
1706 * remaining bytes after stripping the padding. If the padding
1707 * is invalid, avoid leaking this fact through the size of the
1708 * output: use the maximum message size that fits in the output
1709 * buffer. Do it without branches to avoid leaking the padding
1710 * validity through timing. RSA keys are small enough that all the
1711 * size_t values involved fit in unsigned int. */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001712 plaintext_size = if_int( bad,
1713 (unsigned) plaintext_max_size,
Gilles Peskine85a74422018-10-05 14:50:21 +02001714 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001715
Gilles Peskine9265ff42018-10-04 19:13:43 +02001716 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001717 * buffer and to 1 otherwise. */
1718 output_too_large = size_greater_than( plaintext_size,
1719 plaintext_max_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Gilles Peskine9265ff42018-10-04 19:13:43 +02001721 /* Set ret without branches to avoid timing attacks. Return:
1722 * - INVALID_PADDING if the padding is bad (bad != 0).
1723 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1724 * plaintext does not fit in the output buffer.
1725 * - 0 if the padding is correct. */
Gilles Peskine48992472018-10-12 19:19:12 +02001726 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1727 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1728 0 ) );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001729
Gilles Peskine9265ff42018-10-04 19:13:43 +02001730 /* If the padding is bad or the plaintext is too large, zero the
1731 * data that we're about to copy to the output buffer.
1732 * We need to copy the same amount of data
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001733 * from the same buffer whether the padding is good or not to
1734 * avoid leaking the padding validity through overall timing or
1735 * through memory or cache access patterns. */
Gilles Peskine40b57f42018-10-05 15:06:12 +02001736 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001737 for( i = 11; i < ilen; i++ )
Gilles Peskine40b57f42018-10-05 15:06:12 +02001738 buf[i] &= ~bad;
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001739
Gilles Peskine9265ff42018-10-04 19:13:43 +02001740 /* If the plaintext is too large, truncate it to the buffer size.
1741 * Copy anyway to avoid revealing the length through timing, because
1742 * revealing the length is as bad as revealing the padding validity
1743 * for a Bleichenbacher attack. */
1744 plaintext_size = if_int( output_too_large,
1745 (unsigned) plaintext_max_size,
1746 (unsigned) plaintext_size );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001747
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001748 /* Move the plaintext to the leftmost position where it can start in
1749 * the working buffer, i.e. make it start plaintext_max_size from
1750 * the end of the buffer. Do this with a memory access trace that
1751 * does not depend on the plaintext size. After this move, the
1752 * starting location of the plaintext is no longer sensitive
1753 * information. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001754 mem_move_to_left( buf + ilen - plaintext_max_size,
1755 plaintext_max_size,
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001756 plaintext_max_size - plaintext_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001757
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001758 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskine9265ff42018-10-04 19:13:43 +02001759 * into the output buffer. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001760 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001761
1762 /* Report the amount of data we copied to the output buffer. In case
1763 * of errors (bad padding or output too large), the value of *olen
1764 * when this function returns is not specified. Making it equivalent
1765 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001766 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001768cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001769 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001770
1771 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001772}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
1775/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001776 * Do an RSA operation, then remove the message padding
1777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001779 int (*f_rng)(void *, unsigned char *, size_t),
1780 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001781 int mode, size_t *olen,
1782 const unsigned char *input,
1783 unsigned char *output,
1784 size_t output_max_len)
1785{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001786 RSA_VALIDATE_RET( ctx != NULL );
1787 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1788 mode == MBEDTLS_RSA_PUBLIC );
1789 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1790 RSA_VALIDATE_RET( input != NULL );
1791 RSA_VALIDATE_RET( olen != NULL );
1792
Paul Bakkerb3869132013-02-28 17:21:01 +01001793 switch( ctx->padding )
1794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795#if defined(MBEDTLS_PKCS1_V15)
1796 case MBEDTLS_RSA_PKCS_V15:
1797 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001798 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001799#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801#if defined(MBEDTLS_PKCS1_V21)
1802 case MBEDTLS_RSA_PKCS_V21:
1803 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001804 olen, input, output,
1805 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001806#endif
1807
1808 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001810 }
1811}
1812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001814/*
1815 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001818 int (*f_rng)(void *, unsigned char *, size_t),
1819 void *p_rng,
1820 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001822 unsigned int hashlen,
1823 const unsigned char *hash,
1824 unsigned char *sig )
1825{
1826 size_t olen;
1827 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001829 size_t slen, min_slen, hlen, offset = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001830 int ret;
1831 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 const mbedtls_md_info_t *md_info;
1833 mbedtls_md_context_t md_ctx;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001834 RSA_VALIDATE_RET( ctx != NULL );
1835 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1836 mode == MBEDTLS_RSA_PUBLIC );
1837 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1838 hashlen == 0 ) ||
1839 hash != NULL );
1840 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1843 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001844
1845 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001847
1848 olen = ctx->len;
1849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001851 {
Simon Butcher02037452016-03-01 21:19:12 +00001852 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001854 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001858 }
1859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001861 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001865
Jaeden Amero3725bb22018-09-07 19:12:36 +01001866 /* Calculate the largest possible salt length. Normally this is the hash
1867 * length, which is the maximum length the salt can have. If there is not
1868 * enough room, use the maximum salt length that fits. The constraint is
1869 * that the hash length plus the salt length plus 2 bytes must be at most
1870 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1871 * (PKCS#1 v2.2) §9.1.1 step 3. */
1872 min_slen = hlen - 2;
1873 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001875 else if( olen >= hlen + hlen + 2 )
1876 slen = hlen;
1877 else
1878 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001879
1880 memset( sig, 0, olen );
1881
Simon Butcher02037452016-03-01 21:19:12 +00001882 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001883 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001885
Simon Butcher02037452016-03-01 21:19:12 +00001886 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001887 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001888 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001889 *p++ = 0x01;
1890 memcpy( p, salt, slen );
1891 p += slen;
1892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001894 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001895 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001896
Simon Butcher02037452016-03-01 21:19:12 +00001897 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001898 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1899 goto exit;
1900 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1901 goto exit;
1902 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1903 goto exit;
1904 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1905 goto exit;
1906 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1907 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001908
Simon Butcher02037452016-03-01 21:19:12 +00001909 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001910 if( msb % 8 == 0 )
1911 offset = 1;
1912
Simon Butcher02037452016-03-01 21:19:12 +00001913 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001914 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1915 &md_ctx ) ) != 0 )
1916 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001917
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001918 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001919 sig[0] &= 0xFF >> ( olen * 8 - msb );
1920
1921 p += hlen;
1922 *p++ = 0xBC;
1923
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001924 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001925
1926exit:
1927 mbedtls_md_free( &md_ctx );
1928
1929 if( ret != 0 )
1930 return( ret );
1931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 return( ( mode == MBEDTLS_RSA_PUBLIC )
1933 ? mbedtls_rsa_public( ctx, sig, sig )
1934 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001935}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001939/*
1940 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1941 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001942
1943/* Construct a PKCS v1.5 encoding of a hashed message
1944 *
1945 * This is used both for signature generation and verification.
1946 *
1947 * Parameters:
1948 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001949 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001950 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001951 * - hash: Buffer containing the hashed message or the raw data.
1952 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001953 * - dst: Buffer to hold the encoded message.
1954 *
1955 * Assumptions:
1956 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1957 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001958 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001959 *
1960 */
1961static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1962 unsigned int hashlen,
1963 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001964 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001965 unsigned char *dst )
1966{
1967 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001968 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001969 unsigned char *p = dst;
1970 const char *oid = NULL;
1971
1972 /* Are we signing hashed or raw data? */
1973 if( md_alg != MBEDTLS_MD_NONE )
1974 {
1975 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1976 if( md_info == NULL )
1977 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1978
1979 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1980 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1981
1982 hashlen = mbedtls_md_get_size( md_info );
1983
1984 /* Double-check that 8 + hashlen + oid_size can be used as a
1985 * 1-byte ASN.1 length encoding and that there's no overflow. */
1986 if( 8 + hashlen + oid_size >= 0x80 ||
1987 10 + hashlen < hashlen ||
1988 10 + hashlen + oid_size < 10 + hashlen )
1989 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1990
1991 /*
1992 * Static bounds check:
1993 * - Need 10 bytes for five tag-length pairs.
1994 * (Insist on 1-byte length encodings to protect against variants of
1995 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1996 * - Need hashlen bytes for hash
1997 * - Need oid_size bytes for hash alg OID.
1998 */
1999 if( nb_pad < 10 + hashlen + oid_size )
2000 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2001 nb_pad -= 10 + hashlen + oid_size;
2002 }
2003 else
2004 {
2005 if( nb_pad < hashlen )
2006 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2007
2008 nb_pad -= hashlen;
2009 }
2010
Hanno Becker2b2f8982017-09-27 17:10:03 +01002011 /* Need space for signature header and padding delimiter (3 bytes),
2012 * and 8 bytes for the minimal padding */
2013 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002014 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2015 nb_pad -= 3;
2016
2017 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002018 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002019
2020 /* Write signature header and padding */
2021 *p++ = 0;
2022 *p++ = MBEDTLS_RSA_SIGN;
2023 memset( p, 0xFF, nb_pad );
2024 p += nb_pad;
2025 *p++ = 0;
2026
2027 /* Are we signing raw data? */
2028 if( md_alg == MBEDTLS_MD_NONE )
2029 {
2030 memcpy( p, hash, hashlen );
2031 return( 0 );
2032 }
2033
2034 /* Signing hashed data, add corresponding ASN.1 structure
2035 *
2036 * DigestInfo ::= SEQUENCE {
2037 * digestAlgorithm DigestAlgorithmIdentifier,
2038 * digest Digest }
2039 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2040 * Digest ::= OCTET STRING
2041 *
2042 * Schematic:
2043 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2044 * TAG-NULL + LEN [ NULL ] ]
2045 * TAG-OCTET + LEN [ HASH ] ]
2046 */
2047 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002048 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002049 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002050 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002051 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002052 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002053 memcpy( p, oid, oid_size );
2054 p += oid_size;
2055 *p++ = MBEDTLS_ASN1_NULL;
2056 *p++ = 0x00;
2057 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002058 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002059 memcpy( p, hash, hashlen );
2060 p += hashlen;
2061
2062 /* Just a sanity-check, should be automatic
2063 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002064 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002065 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002066 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002067 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2068 }
2069
2070 return( 0 );
2071}
2072
Paul Bakkerb3869132013-02-28 17:21:01 +01002073/*
2074 * Do an RSA operation to sign the message digest
2075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002077 int (*f_rng)(void *, unsigned char *, size_t),
2078 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002079 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002081 unsigned int hashlen,
2082 const unsigned char *hash,
2083 unsigned char *sig )
2084{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002085 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002086 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002087
Hanno Beckerddeeed72018-12-13 18:07:00 +00002088 RSA_VALIDATE_RET( ctx != NULL );
2089 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2090 mode == MBEDTLS_RSA_PUBLIC );
2091 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2092 hashlen == 0 ) ||
2093 hash != NULL );
2094 RSA_VALIDATE_RET( sig != NULL );
2095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2097 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002098
Hanno Beckerfdf38032017-09-06 12:35:55 +01002099 /*
2100 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2101 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002102
Hanno Beckerfdf38032017-09-06 12:35:55 +01002103 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2104 ctx->len, sig ) ) != 0 )
2105 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002106
2107 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002108 * Call respective RSA primitive
2109 */
2110
2111 if( mode == MBEDTLS_RSA_PUBLIC )
2112 {
2113 /* Skip verification on a public key operation */
2114 return( mbedtls_rsa_public( ctx, sig, sig ) );
2115 }
2116
2117 /* Private key operation
2118 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002119 * In order to prevent Lenstra's attack, make the signature in a
2120 * temporary buffer and check it before returning it.
2121 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002122
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002123 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002124 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002125 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2126
Hanno Beckerfdf38032017-09-06 12:35:55 +01002127 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002128 if( verif == NULL )
2129 {
2130 mbedtls_free( sig_try );
2131 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2132 }
2133
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002134 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2135 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2136
Hanno Becker171a8f12017-09-06 12:32:16 +01002137 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002138 {
2139 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2140 goto cleanup;
2141 }
2142
2143 memcpy( sig, sig_try, ctx->len );
2144
2145cleanup:
2146 mbedtls_free( sig_try );
2147 mbedtls_free( verif );
2148
2149 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002150}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002152
2153/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 * Do an RSA operation to sign the message digest
2155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002157 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002158 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002161 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002162 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 unsigned char *sig )
2164{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002165 RSA_VALIDATE_RET( ctx != NULL );
2166 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2167 mode == MBEDTLS_RSA_PUBLIC );
2168 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2169 hashlen == 0 ) ||
2170 hash != NULL );
2171 RSA_VALIDATE_RET( sig != NULL );
2172
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 switch( ctx->padding )
2174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#if defined(MBEDTLS_PKCS1_V15)
2176 case MBEDTLS_RSA_PKCS_V15:
2177 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002178 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002179#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181#if defined(MBEDTLS_PKCS1_V21)
2182 case MBEDTLS_RSA_PKCS_V21:
2183 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002184 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002185#endif
2186
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002190}
2191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002193/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002194 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002197 int (*f_rng)(void *, unsigned char *, size_t),
2198 void *p_rng,
2199 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002201 unsigned int hashlen,
2202 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002204 int expected_salt_len,
2205 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002206{
Paul Bakker23986e52011-04-24 08:57:21 +00002207 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002208 size_t siglen;
2209 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002210 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002212 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002213 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002214 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 const mbedtls_md_info_t *md_info;
2216 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002217 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002218
Hanno Beckerddeeed72018-12-13 18:07:00 +00002219 RSA_VALIDATE_RET( ctx != NULL );
2220 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2221 mode == MBEDTLS_RSA_PUBLIC );
2222 RSA_VALIDATE_RET( sig != NULL );
2223 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2224 hashlen == 0 ) ||
2225 hash != NULL );
2226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2228 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002229
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 siglen = ctx->len;
2231
Paul Bakker27fdf462011-06-09 13:55:13 +00002232 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2236 ? mbedtls_rsa_public( ctx, sig, buf )
2237 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238
2239 if( ret != 0 )
2240 return( ret );
2241
2242 p = buf;
2243
Paul Bakkerb3869132013-02-28 17:21:01 +01002244 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 {
Simon Butcher02037452016-03-01 21:19:12 +00002249 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002251 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002255 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002258 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002262
Paul Bakkerb3869132013-02-28 17:21:01 +01002263 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002264
Simon Butcher02037452016-03-01 21:19:12 +00002265 /*
2266 * Note: EMSA-PSS verification is over the length of N - 1 bits
2267 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002268 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002269
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002270 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2271 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2272
Simon Butcher02037452016-03-01 21:19:12 +00002273 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002274 if( msb % 8 == 0 )
2275 {
2276 p++;
2277 siglen -= 1;
2278 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002279
Gilles Peskine139108a2017-10-18 19:03:42 +02002280 if( siglen < hlen + 2 )
2281 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2282 hash_start = p + siglen - hlen - 1;
2283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002285 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002286 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002287
Jaeden Amero66954e12018-01-25 16:05:54 +00002288 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2289 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002290 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002291
Paul Bakkerb3869132013-02-28 17:21:01 +01002292 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002293
Gilles Peskine6a54b022017-10-17 19:02:13 +02002294 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002295 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002296
Gilles Peskine91048a32017-10-19 17:46:14 +02002297 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002298 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002299 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2300 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002301 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002302
Gilles Peskine6a54b022017-10-17 19:02:13 +02002303 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002306 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002307 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002308 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2309 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002310 }
2311
Simon Butcher02037452016-03-01 21:19:12 +00002312 /*
2313 * Generate H = Hash( M' )
2314 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002315 ret = mbedtls_md_starts( &md_ctx );
2316 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002317 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002318 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2319 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002320 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002321 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2322 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002323 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002324 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2325 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002326 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002327 ret = mbedtls_md_finish( &md_ctx, result );
2328 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002329 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002330
Jaeden Amero66954e12018-01-25 16:05:54 +00002331 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002332 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002333 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002334 goto exit;
2335 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002336
2337exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002339
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002340 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002341}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002342
2343/*
2344 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002347 int (*f_rng)(void *, unsigned char *, size_t),
2348 void *p_rng,
2349 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002351 unsigned int hashlen,
2352 const unsigned char *hash,
2353 const unsigned char *sig )
2354{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002355 mbedtls_md_type_t mgf1_hash_id;
2356 RSA_VALIDATE_RET( ctx != NULL );
2357 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2358 mode == MBEDTLS_RSA_PUBLIC );
2359 RSA_VALIDATE_RET( sig != NULL );
2360 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2361 hashlen == 0 ) ||
2362 hash != NULL );
2363
2364 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002366 : md_alg;
2367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002369 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002371 sig ) );
2372
2373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002377/*
2378 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002381 int (*f_rng)(void *, unsigned char *, size_t),
2382 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002383 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002385 unsigned int hashlen,
2386 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002387 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002388{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002389 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002390 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002391 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002392
Hanno Beckerddeeed72018-12-13 18:07:00 +00002393 RSA_VALIDATE_RET( ctx != NULL );
2394 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2395 mode == MBEDTLS_RSA_PUBLIC );
2396 RSA_VALIDATE_RET( sig != NULL );
2397 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2398 hashlen == 0 ) ||
2399 hash != NULL );
2400
2401 sig_len = ctx->len;
2402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2404 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002405
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002406 /*
2407 * Prepare expected PKCS1 v1.5 encoding of hash.
2408 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002409
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002410 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2411 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2412 {
2413 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2414 goto cleanup;
2415 }
2416
2417 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2418 encoded_expected ) ) != 0 )
2419 goto cleanup;
2420
2421 /*
2422 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2423 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002426 ? mbedtls_rsa_public( ctx, sig, encoded )
2427 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002428 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002429 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002430
Simon Butcher02037452016-03-01 21:19:12 +00002431 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002432 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002433 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002434
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002435 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2436 sig_len ) ) != 0 )
2437 {
2438 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2439 goto cleanup;
2440 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002441
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002442cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002443
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002444 if( encoded != NULL )
2445 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002446 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002447 mbedtls_free( encoded );
2448 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002449
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002450 if( encoded_expected != NULL )
2451 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002452 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002453 mbedtls_free( encoded_expected );
2454 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002455
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002456 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002459
2460/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002461 * Do an RSA operation and check the message digest
2462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002464 int (*f_rng)(void *, unsigned char *, size_t),
2465 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002466 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002468 unsigned int hashlen,
2469 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002470 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002471{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002472 RSA_VALIDATE_RET( ctx != NULL );
2473 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2474 mode == MBEDTLS_RSA_PUBLIC );
2475 RSA_VALIDATE_RET( sig != NULL );
2476 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2477 hashlen == 0 ) ||
2478 hash != NULL );
2479
Paul Bakkerb3869132013-02-28 17:21:01 +01002480 switch( ctx->padding )
2481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#if defined(MBEDTLS_PKCS1_V15)
2483 case MBEDTLS_RSA_PKCS_V15:
2484 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002485 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002486#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#if defined(MBEDTLS_PKCS1_V21)
2489 case MBEDTLS_RSA_PKCS_V21:
2490 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002491 hashlen, hash, sig );
2492#endif
2493
2494 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002496 }
2497}
2498
2499/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002500 * Copy the components of an RSA key
2501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002503{
2504 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002505 RSA_VALIDATE_RET( dst != NULL );
2506 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002507
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002508 dst->len = src->len;
2509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2511 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2514 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2515 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002516
2517#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2519 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2520 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2522 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002523#endif
2524
2525 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2528 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002529
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002530 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002531 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002532
2533cleanup:
2534 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002536
2537 return( ret );
2538}
2539
2540/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 * Free the components of an RSA key
2542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002544{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002545 if( ctx == NULL )
2546 return;
2547
irwir2239a862018-06-12 18:25:09 +03002548 mbedtls_mpi_free( &ctx->Vi );
2549 mbedtls_mpi_free( &ctx->Vf );
2550 mbedtls_mpi_free( &ctx->RN );
2551 mbedtls_mpi_free( &ctx->D );
2552 mbedtls_mpi_free( &ctx->Q );
2553 mbedtls_mpi_free( &ctx->P );
2554 mbedtls_mpi_free( &ctx->E );
2555 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002556
Hanno Becker33c30a02017-08-23 07:00:22 +01002557#if !defined(MBEDTLS_RSA_NO_CRT)
irwir2239a862018-06-12 18:25:09 +03002558 mbedtls_mpi_free( &ctx->RQ );
2559 mbedtls_mpi_free( &ctx->RP );
2560 mbedtls_mpi_free( &ctx->QP );
2561 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002562 mbedtls_mpi_free( &ctx->DP );
2563#endif /* MBEDTLS_RSA_NO_CRT */
2564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565#if defined(MBEDTLS_THREADING_C)
Gilles Peskineb9fce3c2021-02-01 17:57:41 +01002566 /* Free the mutex, but only if it hasn't been freed already. */
2567 if( ctx->ver != 0 )
2568 {
2569 mbedtls_mutex_free( &ctx->mutex );
2570 ctx->ver = 0;
2571 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002572#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002573}
2574
Hanno Beckerab377312017-08-23 16:24:51 +01002575#endif /* !MBEDTLS_RSA_ALT */
2576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002579#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
2581/*
2582 * Example RSA-1024 keypair, for test purposes
2583 */
2584#define KEY_LEN 128
2585
2586#define RSA_N "9292758453063D803DD603D5E777D788" \
2587 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2588 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2589 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2590 "93A89813FBF3C4F8066D2D800F7C38A8" \
2591 "1AE31942917403FF4946B0A83D3D3E05" \
2592 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2593 "5E94BB77B07507233A0BC7BAC8F90F79"
2594
2595#define RSA_E "10001"
2596
2597#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2598 "66CA472BC44D253102F8B4A9D3BFA750" \
2599 "91386C0077937FE33FA3252D28855837" \
2600 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2601 "DF79C5CE07EE72C7F123142198164234" \
2602 "CABB724CF78B8173B9F880FC86322407" \
2603 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2604 "071513A1E85B5DFA031F21ECAE91A34D"
2605
2606#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2607 "2C01CAD19EA484A87EA4377637E75500" \
2608 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2609 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2610
2611#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2612 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2613 "910E4168387E3C30AA1E00C339A79508" \
2614 "8452DD96A9A5EA5D9DCA68DA636032AF"
2615
Paul Bakker5121ce52009-01-03 21:22:43 +00002616#define PT_LEN 24
2617#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2618 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002621static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002622{
gufe443fa7c642020-08-03 17:56:50 +02002623#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002624 size_t i;
2625
Paul Bakker545570e2010-07-18 09:00:25 +00002626 if( rng_state != NULL )
2627 rng_state = NULL;
2628
Paul Bakkera3d195c2011-11-27 21:07:34 +00002629 for( i = 0; i < len; ++i )
2630 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002631#else
2632 if( rng_state != NULL )
2633 rng_state = NULL;
2634
2635 arc4random_buf( output, len );
gufe443fa7c642020-08-03 17:56:50 +02002636#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002637
Paul Bakkera3d195c2011-11-27 21:07:34 +00002638 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002639}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002641
Paul Bakker5121ce52009-01-03 21:22:43 +00002642/*
2643 * Checkup routine
2644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002646{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002647 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002649 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 unsigned char rsa_plaintext[PT_LEN];
2652 unsigned char rsa_decrypted[PT_LEN];
2653 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002655 unsigned char sha1sum[20];
2656#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
Hanno Becker3a701162017-08-22 13:52:43 +01002658 mbedtls_mpi K;
2659
2660 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
Hanno Becker3a701162017-08-22 13:52:43 +01002663 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2664 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2665 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2666 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2667 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2668 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2669 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2670 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2671 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2672 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2673
Hanno Becker7f25f852017-10-10 16:56:22 +01002674 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
2676 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2680 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 {
2682 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Hanno Becker5bc87292017-05-03 15:09:31 +01002685 ret = 1;
2686 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 }
2688
2689 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
2692 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2693
Hanno Becker98838b02017-10-02 13:16:10 +01002694 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2695 PT_LEN, rsa_plaintext,
2696 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 {
2698 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Hanno Becker5bc87292017-05-03 15:09:31 +01002701 ret = 1;
2702 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 }
2704
2705 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Hanno Becker98838b02017-10-02 13:16:10 +01002708 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2709 &len, rsa_ciphertext, rsa_decrypted,
2710 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 {
2712 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
Hanno Becker5bc87292017-05-03 15:09:31 +01002715 ret = 1;
2716 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 }
2718
2719 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2720 {
2721 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Hanno Becker5bc87292017-05-03 15:09:31 +01002724 ret = 1;
2725 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002728 if( verbose != 0 )
2729 mbedtls_printf( "passed\n" );
2730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002733 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002735 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002736 {
2737 if( verbose != 0 )
2738 mbedtls_printf( "failed\n" );
2739
2740 return( 1 );
2741 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Hanno Becker98838b02017-10-02 13:16:10 +01002743 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2744 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2745 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 {
2747 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002749
Hanno Becker5bc87292017-05-03 15:09:31 +01002750 ret = 1;
2751 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 }
2753
2754 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
Hanno Becker98838b02017-10-02 13:16:10 +01002757 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2758 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2759 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 {
2761 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
Hanno Becker5bc87292017-05-03 15:09:31 +01002764 ret = 1;
2765 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 }
2767
2768 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002769 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002772 if( verbose != 0 )
2773 mbedtls_printf( "\n" );
2774
Paul Bakker3d8fb632014-04-17 12:42:41 +02002775cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002776 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 mbedtls_rsa_free( &rsa );
2778#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002779 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002781 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002782}
2783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786#endif /* MBEDTLS_RSA_C */