blob: 47d784c1baa7c8f207ef01dbd7bde0fd9e9696c4 [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
Janos Follathb8fc1b02018-09-03 15:37:01 +0100573 /*
574 * If the modulus is 1024 bit long or shorter, then the security strength of
575 * the RSA algorithm is less than or equal to 80 bits and therefore an error
576 * rate of 2^-80 is sufficient.
577 */
578 if( nbits > 1024 )
579 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
580
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100581 mbedtls_mpi_init( &H );
582 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800583 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Gilles Peskine718972e2021-02-02 21:06:10 +0100585 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
586 {
587 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
588 goto cleanup;
589 }
590
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 /*
592 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800593 * 1. |P-Q| > 2^( nbits / 2 - 100 )
594 * 2. GCD( E, (P-1)*(Q-1) ) == 1
595 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 do
600 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100601 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
602 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
Janos Follathb8fc1b02018-09-03 15:37:01 +0100604 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
605 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800607 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
608 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
609 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 continue;
611
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800612 /* not required by any standards, but some users rely on the fact that P > Q */
613 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100614 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100615
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100616 /* Temporarily replace P,Q by P-1, Q-1 */
617 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
618 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
619 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800620
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800621 /* 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 +0200622 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800623 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
624 continue;
625
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800626 /* 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 -0800627 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
628 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
629 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
630
631 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
632 continue;
633
634 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800636 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100638 /* Restore P,Q */
639 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
640 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
641
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800642 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
643
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100644 ctx->len = mbedtls_mpi_size( &ctx->N );
645
Jethro Beekman97f95c92018-02-13 15:50:36 -0800646#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 * DP = D mod (P - 1)
649 * DQ = D mod (Q - 1)
650 * QP = Q^-1 mod P
651 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100652 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
653 &ctx->DP, &ctx->DQ, &ctx->QP ) );
654#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Hanno Becker83aad1f2017-08-23 06:45:10 +0100656 /* Double-check */
657 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000658
659cleanup:
660
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100661 mbedtls_mpi_free( &H );
662 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800663 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000664
665 if( ret != 0 )
666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_rsa_free( ctx );
Gilles Peskine718972e2021-02-02 21:06:10 +0100668 if( ( -ret & ~0x7f ) == 0 )
669 ret = MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret;
670 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 }
672
Paul Bakker48377d92013-08-30 12:06:24 +0200673 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674}
675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000677
678/*
679 * Check a public RSA key
680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000682{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000683 RSA_VALIDATE_RET( ctx != NULL );
684
Hanno Beckerebd2c022017-10-12 10:54:53 +0100685 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000687
Hanno Becker3a760a12018-01-05 08:14:49 +0000688 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100691 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Hanno Becker705fc682017-10-10 17:57:02 +0100693 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
694 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100698 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
700 return( 0 );
701}
702
703/*
Hanno Becker705fc682017-10-10 17:57:02 +0100704 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000707{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000708 RSA_VALIDATE_RET( ctx != NULL );
709
Hanno Becker705fc682017-10-10 17:57:02 +0100710 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100711 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 {
Hanno Becker98838b02017-10-02 13:16:10 +0100713 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 }
Paul Bakker48377d92013-08-30 12:06:24 +0200715
Hanno Becker98838b02017-10-02 13:16:10 +0100716 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100717 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000718 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100719 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000721
Hanno Beckerb269a852017-08-25 08:03:21 +0100722#if !defined(MBEDTLS_RSA_NO_CRT)
723 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
724 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
725 {
726 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
727 }
728#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000729
730 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731}
732
733/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100734 * Check if contexts holding a public and private key match
735 */
Hanno Becker98838b02017-10-02 13:16:10 +0100736int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
737 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100738{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000739 RSA_VALIDATE_RET( pub != NULL );
740 RSA_VALIDATE_RET( prv != NULL );
741
Hanno Becker98838b02017-10-02 13:16:10 +0100742 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100746 }
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
749 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100752 }
753
754 return( 0 );
755}
756
757/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000758 * Do an RSA public key operation
759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000761 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 unsigned char *output )
763{
Paul Bakker23986e52011-04-24 08:57:21 +0000764 int ret;
765 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_mpi T;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000767 RSA_VALIDATE_RET( ctx != NULL );
768 RSA_VALIDATE_RET( input != NULL );
769 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Hanno Beckerebd2c022017-10-12 10:54:53 +0100771 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100772 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000775
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200776#if defined(MBEDTLS_THREADING_C)
777 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
778 return( ret );
779#endif
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000784 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200785 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
786 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000787 }
788
789 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
791 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
793cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200795 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
796 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100797#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000800
801 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000803
804 return( 0 );
805}
806
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200807/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200808 * Generate or update blinding values, see section 10 of:
809 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200810 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200811 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200812 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200813static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200814 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
815{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200816 int ret, count = 0;
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200817 mbedtls_mpi R;
818
819 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200820
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200821 if( ctx->Vf.p != NULL )
822 {
823 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
825 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
826 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
827 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200828
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200829 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200830 }
831
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200832 /* Unblinding value: Vf = random number, invertible mod N */
833 do {
834 if( count++ > 10 )
Manuel Pégourié-Gonnardcadcf4c2020-07-16 09:23:30 +0200835 {
836 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
837 goto cleanup;
838 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 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 +0200841
Manuel Pégourié-Gonnard87a602d2020-07-16 09:48:54 +0200842 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200843 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
844 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
845 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
846
Manuel Pégourié-Gonnard87a602d2020-07-16 09:48:54 +0200847 /* At this point, Vi is invertible mod N if and only if both Vf and R
848 * are invertible mod N. If one of them isn't, we don't need to know
849 * which one, we just loop and choose new values for both of them.
850 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200851 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusb2aeb752020-09-24 11:11:50 -0500852 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnard86ad5be2020-06-26 11:03:19 +0200853 goto cleanup;
854
Peter Kolbusb2aeb752020-09-24 11:11:50 -0500855 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
856
857 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
858 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
859 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200860
Manuel Pégourié-Gonnard87a602d2020-07-16 09:48:54 +0200861 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200862 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 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 +0200864
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200865
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200866cleanup:
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200867 mbedtls_mpi_free( &R );
868
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200869 return( ret );
870}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200871
Paul Bakker5121ce52009-01-03 21:22:43 +0000872/*
Janos Follathe81102e2017-03-22 13:38:28 +0000873 * Exponent blinding supposed to prevent side-channel attacks using multiple
874 * traces of measurements to recover the RSA key. The more collisions are there,
875 * the more bits of the key can be recovered. See [3].
876 *
877 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
878 * observations on avarage.
879 *
880 * For example with 28 byte blinding to achieve 2 collisions the adversary has
881 * to make 2^112 observations on avarage.
882 *
883 * (With the currently (as of 2017 April) known best algorithms breaking 2048
884 * bit RSA requires approximately as much time as trying out 2^112 random keys.
885 * Thus in this sense with 28 byte blinding the security is not reduced by
886 * side-channel attacks like the one in [3])
887 *
888 * This countermeasure does not help if the key recovery is possible with a
889 * single trace.
890 */
891#define RSA_EXPONENT_BLINDING 28
892
893/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 * Do an RSA private key operation
895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200897 int (*f_rng)(void *, unsigned char *, size_t),
898 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000899 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000900 unsigned char *output )
901{
Paul Bakker23986e52011-04-24 08:57:21 +0000902 int ret;
903 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100904
905 /* Temporary holding the result */
906 mbedtls_mpi T;
907
908 /* Temporaries holding P-1, Q-1 and the
909 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000910 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100911
912#if !defined(MBEDTLS_RSA_NO_CRT)
913 /* Temporaries holding the results mod p resp. mod q. */
914 mbedtls_mpi TP, TQ;
915
916 /* Temporaries holding the blinded exponents for
917 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000918 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100919
920 /* Pointers to actual exponents to be used - either the unblinded
921 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000922 mbedtls_mpi *DP = &ctx->DP;
923 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100924#else
925 /* Temporary holding the blinded exponent (if used). */
926 mbedtls_mpi D_blind;
927
928 /* Pointer to actual exponent to be used - either the unblinded
929 * or the blinded one, depending on the presence of a PRNG. */
930 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100931#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100932
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100933 /* Temporaries holding the initial input and the double
934 * checked result; should be the same in the end. */
935 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Hanno Beckerddeeed72018-12-13 18:07:00 +0000937 RSA_VALIDATE_RET( ctx != NULL );
938 RSA_VALIDATE_RET( input != NULL );
939 RSA_VALIDATE_RET( output != NULL );
940
Hanno Beckerebd2c022017-10-12 10:54:53 +0100941 if( rsa_check_context( ctx, 1 /* private key checks */,
942 f_rng != NULL /* blinding y/n */ ) != 0 )
943 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100944 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100945 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100946
Hanno Becker06811ce2017-05-03 15:10:34 +0100947#if defined(MBEDTLS_THREADING_C)
948 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
949 return( ret );
950#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000951
Hanno Becker06811ce2017-05-03 15:10:34 +0100952 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100953 mbedtls_mpi_init( &T );
954
955 mbedtls_mpi_init( &P1 );
956 mbedtls_mpi_init( &Q1 );
957 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000958
Janos Follathf9203b42017-03-22 15:13:15 +0000959 if( f_rng != NULL )
960 {
Janos Follathe81102e2017-03-22 13:38:28 +0000961#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000962 mbedtls_mpi_init( &D_blind );
963#else
964 mbedtls_mpi_init( &DP_blind );
965 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000966#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000967 }
Janos Follathe81102e2017-03-22 13:38:28 +0000968
Hanno Becker06811ce2017-05-03 15:10:34 +0100969#if !defined(MBEDTLS_RSA_NO_CRT)
970 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200971#endif
972
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100973 mbedtls_mpi_init( &I );
974 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100975
976 /* End of MPI initialization */
977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
979 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200981 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
982 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 }
984
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100985 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100986
Paul Bakkerf451bac2013-08-30 15:37:02 +0200987 if( f_rng != NULL )
988 {
989 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200990 * Blinding
991 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200992 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200993 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
994 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000996
Janos Follathe81102e2017-03-22 13:38:28 +0000997 /*
998 * Exponent blinding
999 */
1000 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1001 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1002
Janos Follathf9203b42017-03-22 15:13:15 +00001003#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001004 /*
1005 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1006 */
1007 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1008 f_rng, p_rng ) );
1009 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1010 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1011 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1012
1013 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001014#else
1015 /*
1016 * DP_blind = ( P - 1 ) * R + DP
1017 */
1018 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1019 f_rng, p_rng ) );
1020 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1021 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1022 &ctx->DP ) );
1023
1024 DP = &DP_blind;
1025
1026 /*
1027 * DQ_blind = ( Q - 1 ) * R + DQ
1028 */
1029 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1030 f_rng, p_rng ) );
1031 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1032 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1033 &ctx->DQ ) );
1034
1035 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001036#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001037 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001040 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001041#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001042 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001043 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001045 * TP = input ^ dP mod P
1046 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001048
1049 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1050 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001051
1052 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001053 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001055 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1056 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1057 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
1059 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001060 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001061 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001062 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1063 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001065
Paul Bakkerf451bac2013-08-30 15:37:02 +02001066 if( f_rng != NULL )
1067 {
1068 /*
1069 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001070 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001071 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001072 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001074 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
Hanno Becker2dec5e82017-10-03 07:49:52 +01001076 /* Verify the result to prevent glitching attacks. */
1077 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1078 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001079 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001080 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001081 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1082 goto cleanup;
1083 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001084
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
1088cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001090 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1091 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001092#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001093
Hanno Becker06811ce2017-05-03 15:10:34 +01001094 mbedtls_mpi_free( &P1 );
1095 mbedtls_mpi_free( &Q1 );
1096 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001097
1098 if( f_rng != NULL )
1099 {
Janos Follathe81102e2017-03-22 13:38:28 +00001100#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001101 mbedtls_mpi_free( &D_blind );
1102#else
1103 mbedtls_mpi_free( &DP_blind );
1104 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001105#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001106 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
Hanno Becker06811ce2017-05-03 15:10:34 +01001108 mbedtls_mpi_free( &T );
1109
1110#if !defined(MBEDTLS_RSA_NO_CRT)
1111 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1112#endif
1113
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001114 mbedtls_mpi_free( &C );
1115 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001116
Gilles Peskine3b7523e2020-11-25 00:10:31 +01001117 if( ret != 0 && ret >= -0x007f )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001119
Gilles Peskine3b7523e2020-11-25 00:10:31 +01001120 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001121}
1122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001124/**
1125 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1126 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001127 * \param dst buffer to mask
1128 * \param dlen length of destination buffer
1129 * \param src source of the mask generation
1130 * \param slen length of the source buffer
1131 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001132 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001133static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001137 unsigned char counter[4];
1138 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001139 unsigned int hlen;
1140 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001141 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001144 memset( counter, 0, 4 );
1145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001147
Simon Butcher02037452016-03-01 21:19:12 +00001148 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001149 p = dst;
1150
1151 while( dlen > 0 )
1152 {
1153 use_len = hlen;
1154 if( dlen < hlen )
1155 use_len = dlen;
1156
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001157 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1158 goto exit;
1159 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1160 goto exit;
1161 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1162 goto exit;
1163 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1164 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001165
1166 for( i = 0; i < use_len; ++i )
1167 *p++ ^= mask[i];
1168
1169 counter[3]++;
1170
1171 dlen -= use_len;
1172 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001173
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001174exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001175 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001176
1177 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001182/*
1183 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001186 int (*f_rng)(void *, unsigned char *, size_t),
1187 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001188 int mode,
1189 const unsigned char *label, size_t label_len,
1190 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001191 const unsigned char *input,
1192 unsigned char *output )
1193{
1194 size_t olen;
1195 int ret;
1196 unsigned char *p = output;
1197 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 const mbedtls_md_info_t *md_info;
1199 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001200
Hanno Beckerddeeed72018-12-13 18:07:00 +00001201 RSA_VALIDATE_RET( ctx != NULL );
1202 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1203 mode == MBEDTLS_RSA_PUBLIC );
1204 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001205 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001206 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001210
1211 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001215 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001217
1218 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001220
Simon Butcher02037452016-03-01 21:19:12 +00001221 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001222 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001224
1225 memset( output, 0, olen );
1226
1227 *p++ = 0;
1228
Simon Butcher02037452016-03-01 21:19:12 +00001229 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001230 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001232
1233 p += hlen;
1234
Simon Butcher02037452016-03-01 21:19:12 +00001235 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001236 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1237 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001238 p += hlen;
1239 p += olen - 2 * hlen - 2 - ilen;
1240 *p++ = 1;
1241 memcpy( p, input, ilen );
1242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001244 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001245 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001246
Simon Butcher02037452016-03-01 21:19:12 +00001247 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001248 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1249 &md_ctx ) ) != 0 )
1250 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001251
Simon Butcher02037452016-03-01 21:19:12 +00001252 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001253 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1254 &md_ctx ) ) != 0 )
1255 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001256
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001257exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001259
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001260 if( ret != 0 )
1261 return( ret );
1262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 return( ( mode == MBEDTLS_RSA_PUBLIC )
1264 ? mbedtls_rsa_public( ctx, output, output )
1265 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001270/*
1271 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001274 int (*f_rng)(void *, unsigned char *, size_t),
1275 void *p_rng,
1276 int mode, size_t ilen,
1277 const unsigned char *input,
1278 unsigned char *output )
1279{
1280 size_t nb_pad, olen;
1281 int ret;
1282 unsigned char *p = output;
1283
Hanno Beckerddeeed72018-12-13 18:07:00 +00001284 RSA_VALIDATE_RET( ctx != NULL );
1285 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1286 mode == MBEDTLS_RSA_PUBLIC );
1287 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001288 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1291 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001292
Paul Bakkerb3869132013-02-28 17:21:01 +01001293 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001294
Simon Butcher02037452016-03-01 21:19:12 +00001295 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001296 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001298
1299 nb_pad = olen - 3 - ilen;
1300
1301 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001303 {
Hanno Beckerb86e6842018-12-18 14:46:04 +00001304 if( f_rng == NULL )
1305 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001308
1309 while( nb_pad-- > 0 )
1310 {
1311 int rng_dl = 100;
1312
1313 do {
1314 ret = f_rng( p_rng, p, 1 );
1315 } while( *p == 0 && --rng_dl && ret == 0 );
1316
Simon Butcher02037452016-03-01 21:19:12 +00001317 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001318 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001320
1321 p++;
1322 }
1323 }
1324 else
1325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001327
1328 while( nb_pad-- > 0 )
1329 *p++ = 0xFF;
1330 }
1331
1332 *p++ = 0;
1333 memcpy( p, input, ilen );
1334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 return( ( mode == MBEDTLS_RSA_PUBLIC )
1336 ? mbedtls_rsa_public( ctx, output, output )
1337 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001338}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001340
Paul Bakker5121ce52009-01-03 21:22:43 +00001341/*
1342 * Add the message padding, then do an RSA operation
1343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001345 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001346 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001347 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001348 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 unsigned char *output )
1350{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001351 RSA_VALIDATE_RET( ctx != NULL );
1352 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1353 mode == MBEDTLS_RSA_PUBLIC );
1354 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001355 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001356
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 switch( ctx->padding )
1358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#if defined(MBEDTLS_PKCS1_V15)
1360 case MBEDTLS_RSA_PKCS_V15:
1361 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001362 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001363#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_PKCS1_V21)
1366 case MBEDTLS_RSA_PKCS_V21:
1367 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001368 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001369#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
1371 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001374}
1375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001377/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001378 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001381 int (*f_rng)(void *, unsigned char *, size_t),
1382 void *p_rng,
1383 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001384 const unsigned char *label, size_t label_len,
1385 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001386 const unsigned char *input,
1387 unsigned char *output,
1388 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001389{
Paul Bakker23986e52011-04-24 08:57:21 +00001390 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001391 size_t ilen, i, pad_len;
1392 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1394 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001395 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 const mbedtls_md_info_t *md_info;
1397 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001398
Hanno Beckerddeeed72018-12-13 18:07:00 +00001399 RSA_VALIDATE_RET( ctx != NULL );
1400 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1401 mode == MBEDTLS_RSA_PUBLIC );
1402 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1403 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1404 RSA_VALIDATE_RET( input != NULL );
1405 RSA_VALIDATE_RET( olen != NULL );
1406
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001407 /*
1408 * Parameters sanity checks
1409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1411 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
1413 ilen = ctx->len;
1414
Paul Bakker27fdf462011-06-09 13:55:13 +00001415 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001419 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001421
Janos Follathc17cda12016-02-11 11:08:18 +00001422 hlen = mbedtls_md_get_size( md_info );
1423
1424 // checking for integer underflow
1425 if( 2 * hlen + 2 > ilen )
1426 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1427
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001428 /*
1429 * RSA operation
1430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1432 ? mbedtls_rsa_public( ctx, input, buf )
1433 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
1435 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001436 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001438 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001439 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001442 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1443 {
1444 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001445 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001446 }
1447
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001448 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001449 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1450 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001451 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001452 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1453 &md_ctx ) ) != 0 )
1454 {
1455 mbedtls_md_free( &md_ctx );
1456 goto cleanup;
1457 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001460
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001461 /* Generate lHash */
1462 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1463 goto cleanup;
1464
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001465 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001466 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001467 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001469 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001470
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001471 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001472
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001473 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001474
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001475 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001476 for( i = 0; i < hlen; i++ )
1477 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001478
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001479 /* Get zero-padding len, but always read till end of buffer
1480 * (minus one, for the 01 byte) */
1481 pad_len = 0;
1482 pad_done = 0;
1483 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1484 {
1485 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001486 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001487 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001488
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001489 p += pad_len;
1490 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001491
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001492 /*
1493 * The only information "leaked" is whether the padding was correct or not
1494 * (eg, no data is copied if it was not correct). This meets the
1495 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1496 * the different error conditions.
1497 */
1498 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001499 {
1500 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1501 goto cleanup;
1502 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001503
Paul Bakker66d5d072014-06-17 16:39:18 +02001504 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001505 {
1506 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1507 goto cleanup;
1508 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001509
1510 *olen = ilen - (p - buf);
1511 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001512 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001513
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001514cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001515 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1516 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001517
1518 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001519}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001523/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1524 *
1525 * \param value The value to analyze.
Gilles Peskine331d80e2018-10-04 18:32:29 +02001526 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001527 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001528static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001529{
1530 /* MSVC has a warning about unary minus on unsigned, but this is
1531 * well-defined and precisely what we want to do here */
1532#if defined(_MSC_VER)
1533#pragma warning( push )
1534#pragma warning( disable : 4146 )
1535#endif
1536 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1537#if defined(_MSC_VER)
1538#pragma warning( pop )
1539#endif
1540}
1541
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001542/** Check whether a size is out of bounds, without branches.
1543 *
1544 * This is equivalent to `size > max`, but is likely to be compiled to
1545 * to code using bitwise operation rather than a branch.
1546 *
1547 * \param size Size to check.
1548 * \param max Maximum desired value for \p size.
1549 * \return \c 0 if `size <= max`.
1550 * \return \c 1 if `size > max`.
1551 */
1552static unsigned size_greater_than( size_t size, size_t max )
1553{
1554 /* Return the sign bit (1 for negative) of (max - size). */
1555 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1556}
1557
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001558/** Choose between two integer values, without branches.
1559 *
Gilles Peskine331d80e2018-10-04 18:32:29 +02001560 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1561 * to code using bitwise operation rather than a branch.
1562 *
1563 * \param cond Condition to test.
1564 * \param if1 Value to use if \p cond is nonzero.
1565 * \param if0 Value to use if \p cond is zero.
1566 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001567 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001568static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001569{
Gilles Peskine331d80e2018-10-04 18:32:29 +02001570 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001571 return( ( mask & if1 ) | (~mask & if0 ) );
1572}
1573
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001574/** Shift some data towards the left inside a buffer without leaking
1575 * the length of the data through side channels.
1576 *
1577 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1578 * ```
1579 * memmove(start, start + offset, total - offset);
1580 * memset(start + offset, 0, total - offset);
1581 * ```
1582 * but it strives to use a memory access pattern (and thus total timing)
1583 * that does not depend on \p offset. This timing independence comes at
1584 * the expense of performance.
1585 *
1586 * \param start Pointer to the start of the buffer.
1587 * \param total Total size of the buffer.
1588 * \param offset Offset from which to copy \p total - \p offset bytes.
1589 */
1590static void mem_move_to_left( void *start,
1591 size_t total,
1592 size_t offset )
1593{
1594 volatile unsigned char *buf = start;
1595 size_t i, n;
1596 if( total == 0 )
1597 return;
1598 for( i = 0; i < total; i++ )
1599 {
1600 unsigned no_op = size_greater_than( total - offset, i );
1601 /* The first `total - offset` passes are a no-op. The last
1602 * `offset` passes shift the data one byte to the left and
1603 * zero out the last byte. */
1604 for( n = 0; n < total - 1; n++ )
Gilles Peskine9b430702018-10-12 19:15:34 +02001605 {
1606 unsigned char current = buf[n];
1607 unsigned char next = buf[n+1];
1608 buf[n] = if_int( no_op, current, next );
1609 }
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001610 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1611 }
1612}
1613
Paul Bakkerb3869132013-02-28 17:21:01 +01001614/*
1615 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001618 int (*f_rng)(void *, unsigned char *, size_t),
1619 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001620 int mode, size_t *olen,
1621 const unsigned char *input,
1622 unsigned char *output,
Gilles Peskine5908dd42018-10-02 22:43:06 +02001623 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001624{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001625 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001626 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine85a74422018-10-05 14:50:21 +02001628 /* The following variables take sensitive values: their value must
1629 * not leak into the observable behavior of the function other than
1630 * the designated outputs (output, olen, return value). Otherwise
1631 * this would open the execution of the function to
1632 * side-channel-based variants of the Bleichenbacher padding oracle
1633 * attack. Potential side channels include overall timing, memory
1634 * access patterns (especially visible to an adversary who has access
1635 * to a shared memory cache), and branches (especially visible to
1636 * an adversary who has access to a shared code cache or to a shared
1637 * branch predictor). */
1638 size_t pad_count = 0;
1639 unsigned bad = 0;
1640 unsigned char pad_done = 0;
1641 size_t plaintext_size = 0;
1642 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001643
Hanno Beckerddeeed72018-12-13 18:07:00 +00001644 RSA_VALIDATE_RET( ctx != NULL );
1645 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1646 mode == MBEDTLS_RSA_PUBLIC );
1647 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1648 RSA_VALIDATE_RET( input != NULL );
1649 RSA_VALIDATE_RET( olen != NULL );
1650
1651 ilen = ctx->len;
1652 plaintext_max_size = ( output_max_len > ilen - 11 ?
1653 ilen - 11 :
1654 output_max_len );
1655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1657 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001658
Paul Bakkerb3869132013-02-28 17:21:01 +01001659 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1663 ? mbedtls_rsa_public( ctx, input, buf )
1664 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001665
1666 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001667 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001668
Gilles Peskine40b57f42018-10-05 15:06:12 +02001669 /* Check and get padding length in constant time and constant
1670 * memory trace. The first byte must be 0. */
1671 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001674 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001675 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1676 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001677 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001679 /* Read the whole buffer. Set pad_done to nonzero if we find
1680 * the 0x00 byte and remember the padding length in pad_count. */
1681 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001682 {
Gilles Peskine85a74422018-10-05 14:50:21 +02001683 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001684 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001685 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001686 }
1687 else
1688 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001689 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1690 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001691 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001692
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001693 /* Read the whole buffer. Set pad_done to nonzero if we find
1694 * the 0x00 byte and remember the padding length in pad_count.
1695 * If there's a non-0xff byte in the padding, the padding is bad. */
1696 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001697 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001698 pad_done |= if_int( buf[i], 0, 1 );
1699 pad_count += if_int( pad_done, 0, 1 );
1700 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 }
1703
Gilles Peskine40b57f42018-10-05 15:06:12 +02001704 /* If pad_done is still zero, there's no data, only unfinished padding. */
1705 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001706
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001707 /* There must be at least 8 bytes of padding. */
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001708 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001709
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001710 /* If the padding is valid, set plaintext_size to the number of
1711 * remaining bytes after stripping the padding. If the padding
1712 * is invalid, avoid leaking this fact through the size of the
1713 * output: use the maximum message size that fits in the output
1714 * buffer. Do it without branches to avoid leaking the padding
1715 * validity through timing. RSA keys are small enough that all the
1716 * size_t values involved fit in unsigned int. */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001717 plaintext_size = if_int( bad,
1718 (unsigned) plaintext_max_size,
Gilles Peskine85a74422018-10-05 14:50:21 +02001719 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001720
Gilles Peskine9265ff42018-10-04 19:13:43 +02001721 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001722 * buffer and to 1 otherwise. */
1723 output_too_large = size_greater_than( plaintext_size,
1724 plaintext_max_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001725
Gilles Peskine9265ff42018-10-04 19:13:43 +02001726 /* Set ret without branches to avoid timing attacks. Return:
1727 * - INVALID_PADDING if the padding is bad (bad != 0).
1728 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1729 * plaintext does not fit in the output buffer.
1730 * - 0 if the padding is correct. */
Gilles Peskine48992472018-10-12 19:19:12 +02001731 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1732 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1733 0 ) );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001734
Gilles Peskine9265ff42018-10-04 19:13:43 +02001735 /* If the padding is bad or the plaintext is too large, zero the
1736 * data that we're about to copy to the output buffer.
1737 * We need to copy the same amount of data
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001738 * from the same buffer whether the padding is good or not to
1739 * avoid leaking the padding validity through overall timing or
1740 * through memory or cache access patterns. */
Gilles Peskine40b57f42018-10-05 15:06:12 +02001741 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001742 for( i = 11; i < ilen; i++ )
Gilles Peskine40b57f42018-10-05 15:06:12 +02001743 buf[i] &= ~bad;
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001744
Gilles Peskine9265ff42018-10-04 19:13:43 +02001745 /* If the plaintext is too large, truncate it to the buffer size.
1746 * Copy anyway to avoid revealing the length through timing, because
1747 * revealing the length is as bad as revealing the padding validity
1748 * for a Bleichenbacher attack. */
1749 plaintext_size = if_int( output_too_large,
1750 (unsigned) plaintext_max_size,
1751 (unsigned) plaintext_size );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001752
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001753 /* Move the plaintext to the leftmost position where it can start in
1754 * the working buffer, i.e. make it start plaintext_max_size from
1755 * the end of the buffer. Do this with a memory access trace that
1756 * does not depend on the plaintext size. After this move, the
1757 * starting location of the plaintext is no longer sensitive
1758 * information. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001759 mem_move_to_left( buf + ilen - plaintext_max_size,
1760 plaintext_max_size,
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001761 plaintext_max_size - plaintext_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001762
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001763 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskine9265ff42018-10-04 19:13:43 +02001764 * into the output buffer. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001765 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001766
1767 /* Report the amount of data we copied to the output buffer. In case
1768 * of errors (bad padding or output too large), the value of *olen
1769 * when this function returns is not specified. Making it equivalent
1770 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001771 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001773cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001774 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001775
1776 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001777}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
1780/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001781 * Do an RSA operation, then remove the message padding
1782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001784 int (*f_rng)(void *, unsigned char *, size_t),
1785 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001786 int mode, size_t *olen,
1787 const unsigned char *input,
1788 unsigned char *output,
1789 size_t output_max_len)
1790{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001791 RSA_VALIDATE_RET( ctx != NULL );
1792 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1793 mode == MBEDTLS_RSA_PUBLIC );
1794 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1795 RSA_VALIDATE_RET( input != NULL );
1796 RSA_VALIDATE_RET( olen != NULL );
1797
Paul Bakkerb3869132013-02-28 17:21:01 +01001798 switch( ctx->padding )
1799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800#if defined(MBEDTLS_PKCS1_V15)
1801 case MBEDTLS_RSA_PKCS_V15:
1802 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001803 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001804#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806#if defined(MBEDTLS_PKCS1_V21)
1807 case MBEDTLS_RSA_PKCS_V21:
1808 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001809 olen, input, output,
1810 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001811#endif
1812
1813 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001815 }
1816}
1817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001819/*
1820 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001823 int (*f_rng)(void *, unsigned char *, size_t),
1824 void *p_rng,
1825 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001827 unsigned int hashlen,
1828 const unsigned char *hash,
1829 unsigned char *sig )
1830{
1831 size_t olen;
1832 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001834 size_t slen, min_slen, hlen, offset = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001835 int ret;
1836 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 const mbedtls_md_info_t *md_info;
1838 mbedtls_md_context_t md_ctx;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001839 RSA_VALIDATE_RET( ctx != NULL );
1840 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1841 mode == MBEDTLS_RSA_PUBLIC );
1842 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1843 hashlen == 0 ) ||
1844 hash != NULL );
1845 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1848 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001849
1850 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001852
1853 olen = ctx->len;
1854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001856 {
Simon Butcher02037452016-03-01 21:19:12 +00001857 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001859 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001863 }
1864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001866 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001870
Jaeden Amero3725bb22018-09-07 19:12:36 +01001871 /* Calculate the largest possible salt length. Normally this is the hash
1872 * length, which is the maximum length the salt can have. If there is not
1873 * enough room, use the maximum salt length that fits. The constraint is
1874 * that the hash length plus the salt length plus 2 bytes must be at most
1875 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1876 * (PKCS#1 v2.2) §9.1.1 step 3. */
1877 min_slen = hlen - 2;
1878 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001880 else if( olen >= hlen + hlen + 2 )
1881 slen = hlen;
1882 else
1883 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001884
1885 memset( sig, 0, olen );
1886
Simon Butcher02037452016-03-01 21:19:12 +00001887 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001888 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001890
Simon Butcher02037452016-03-01 21:19:12 +00001891 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001892 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001893 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001894 *p++ = 0x01;
1895 memcpy( p, salt, slen );
1896 p += slen;
1897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001899 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001900 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001901
Simon Butcher02037452016-03-01 21:19:12 +00001902 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001903 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1904 goto exit;
1905 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1906 goto exit;
1907 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1908 goto exit;
1909 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1910 goto exit;
1911 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1912 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001913
Simon Butcher02037452016-03-01 21:19:12 +00001914 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001915 if( msb % 8 == 0 )
1916 offset = 1;
1917
Simon Butcher02037452016-03-01 21:19:12 +00001918 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001919 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1920 &md_ctx ) ) != 0 )
1921 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001922
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001923 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001924 sig[0] &= 0xFF >> ( olen * 8 - msb );
1925
1926 p += hlen;
1927 *p++ = 0xBC;
1928
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001929 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001930
1931exit:
1932 mbedtls_md_free( &md_ctx );
1933
1934 if( ret != 0 )
1935 return( ret );
1936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 return( ( mode == MBEDTLS_RSA_PUBLIC )
1938 ? mbedtls_rsa_public( ctx, sig, sig )
1939 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001940}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001944/*
1945 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1946 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001947
1948/* Construct a PKCS v1.5 encoding of a hashed message
1949 *
1950 * This is used both for signature generation and verification.
1951 *
1952 * Parameters:
1953 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001954 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001955 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001956 * - hash: Buffer containing the hashed message or the raw data.
1957 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001958 * - dst: Buffer to hold the encoded message.
1959 *
1960 * Assumptions:
1961 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1962 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001963 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001964 *
1965 */
1966static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1967 unsigned int hashlen,
1968 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001969 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001970 unsigned char *dst )
1971{
1972 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001973 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001974 unsigned char *p = dst;
1975 const char *oid = NULL;
1976
1977 /* Are we signing hashed or raw data? */
1978 if( md_alg != MBEDTLS_MD_NONE )
1979 {
1980 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1981 if( md_info == NULL )
1982 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1983
1984 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1985 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1986
1987 hashlen = mbedtls_md_get_size( md_info );
1988
1989 /* Double-check that 8 + hashlen + oid_size can be used as a
1990 * 1-byte ASN.1 length encoding and that there's no overflow. */
1991 if( 8 + hashlen + oid_size >= 0x80 ||
1992 10 + hashlen < hashlen ||
1993 10 + hashlen + oid_size < 10 + hashlen )
1994 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1995
1996 /*
1997 * Static bounds check:
1998 * - Need 10 bytes for five tag-length pairs.
1999 * (Insist on 1-byte length encodings to protect against variants of
2000 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2001 * - Need hashlen bytes for hash
2002 * - Need oid_size bytes for hash alg OID.
2003 */
2004 if( nb_pad < 10 + hashlen + oid_size )
2005 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2006 nb_pad -= 10 + hashlen + oid_size;
2007 }
2008 else
2009 {
2010 if( nb_pad < hashlen )
2011 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2012
2013 nb_pad -= hashlen;
2014 }
2015
Hanno Becker2b2f8982017-09-27 17:10:03 +01002016 /* Need space for signature header and padding delimiter (3 bytes),
2017 * and 8 bytes for the minimal padding */
2018 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002019 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2020 nb_pad -= 3;
2021
2022 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002023 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002024
2025 /* Write signature header and padding */
2026 *p++ = 0;
2027 *p++ = MBEDTLS_RSA_SIGN;
2028 memset( p, 0xFF, nb_pad );
2029 p += nb_pad;
2030 *p++ = 0;
2031
2032 /* Are we signing raw data? */
2033 if( md_alg == MBEDTLS_MD_NONE )
2034 {
2035 memcpy( p, hash, hashlen );
2036 return( 0 );
2037 }
2038
2039 /* Signing hashed data, add corresponding ASN.1 structure
2040 *
2041 * DigestInfo ::= SEQUENCE {
2042 * digestAlgorithm DigestAlgorithmIdentifier,
2043 * digest Digest }
2044 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2045 * Digest ::= OCTET STRING
2046 *
2047 * Schematic:
2048 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2049 * TAG-NULL + LEN [ NULL ] ]
2050 * TAG-OCTET + LEN [ HASH ] ]
2051 */
2052 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002053 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002054 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002055 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002056 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002057 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002058 memcpy( p, oid, oid_size );
2059 p += oid_size;
2060 *p++ = MBEDTLS_ASN1_NULL;
2061 *p++ = 0x00;
2062 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002063 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002064 memcpy( p, hash, hashlen );
2065 p += hashlen;
2066
2067 /* Just a sanity-check, should be automatic
2068 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002069 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002070 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002071 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002072 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2073 }
2074
2075 return( 0 );
2076}
2077
Paul Bakkerb3869132013-02-28 17:21:01 +01002078/*
2079 * Do an RSA operation to sign the message digest
2080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002082 int (*f_rng)(void *, unsigned char *, size_t),
2083 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002084 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002086 unsigned int hashlen,
2087 const unsigned char *hash,
2088 unsigned char *sig )
2089{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002090 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002091 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002092
Hanno Beckerddeeed72018-12-13 18:07:00 +00002093 RSA_VALIDATE_RET( ctx != NULL );
2094 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2095 mode == MBEDTLS_RSA_PUBLIC );
2096 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2097 hashlen == 0 ) ||
2098 hash != NULL );
2099 RSA_VALIDATE_RET( sig != NULL );
2100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2102 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002103
Hanno Beckerfdf38032017-09-06 12:35:55 +01002104 /*
2105 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2106 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002107
Hanno Beckerfdf38032017-09-06 12:35:55 +01002108 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2109 ctx->len, sig ) ) != 0 )
2110 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002111
2112 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002113 * Call respective RSA primitive
2114 */
2115
2116 if( mode == MBEDTLS_RSA_PUBLIC )
2117 {
2118 /* Skip verification on a public key operation */
2119 return( mbedtls_rsa_public( ctx, sig, sig ) );
2120 }
2121
2122 /* Private key operation
2123 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002124 * In order to prevent Lenstra's attack, make the signature in a
2125 * temporary buffer and check it before returning it.
2126 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002127
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002128 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002129 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002130 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2131
Hanno Beckerfdf38032017-09-06 12:35:55 +01002132 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002133 if( verif == NULL )
2134 {
2135 mbedtls_free( sig_try );
2136 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2137 }
2138
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002139 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2140 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2141
Hanno Becker171a8f12017-09-06 12:32:16 +01002142 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002143 {
2144 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2145 goto cleanup;
2146 }
2147
2148 memcpy( sig, sig_try, ctx->len );
2149
2150cleanup:
Gilles Peskine6f8d7f12021-12-13 12:37:55 +01002151 mbedtls_platform_zeroize( sig_try, ctx->len );
2152 mbedtls_platform_zeroize( verif, ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002153 mbedtls_free( sig_try );
2154 mbedtls_free( verif );
2155
Gilles Peskine6f8d7f12021-12-13 12:37:55 +01002156 if( ret != 0 )
2157 memset( sig, '!', ctx->len );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002158 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002161
2162/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 * Do an RSA operation to sign the message digest
2164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002166 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002167 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002170 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002171 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 unsigned char *sig )
2173{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002174 RSA_VALIDATE_RET( ctx != NULL );
2175 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2176 mode == MBEDTLS_RSA_PUBLIC );
2177 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2178 hashlen == 0 ) ||
2179 hash != NULL );
2180 RSA_VALIDATE_RET( sig != NULL );
2181
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 switch( ctx->padding )
2183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184#if defined(MBEDTLS_PKCS1_V15)
2185 case MBEDTLS_RSA_PKCS_V15:
2186 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002187 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002188#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#if defined(MBEDTLS_PKCS1_V21)
2191 case MBEDTLS_RSA_PKCS_V21:
2192 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002193 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002194#endif
2195
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002199}
2200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002202/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002203 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002206 int (*f_rng)(void *, unsigned char *, size_t),
2207 void *p_rng,
2208 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002210 unsigned int hashlen,
2211 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002213 int expected_salt_len,
2214 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002215{
Paul Bakker23986e52011-04-24 08:57:21 +00002216 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002217 size_t siglen;
2218 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002219 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002221 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002222 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002223 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 const mbedtls_md_info_t *md_info;
2225 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002226 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002227
Hanno Beckerddeeed72018-12-13 18:07:00 +00002228 RSA_VALIDATE_RET( ctx != NULL );
2229 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2230 mode == MBEDTLS_RSA_PUBLIC );
2231 RSA_VALIDATE_RET( sig != NULL );
2232 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2233 hashlen == 0 ) ||
2234 hash != NULL );
2235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2237 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002238
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 siglen = ctx->len;
2240
Paul Bakker27fdf462011-06-09 13:55:13 +00002241 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2245 ? mbedtls_rsa_public( ctx, sig, buf )
2246 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002247
2248 if( ret != 0 )
2249 return( ret );
2250
2251 p = buf;
2252
Paul Bakkerb3869132013-02-28 17:21:01 +01002253 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 {
Simon Butcher02037452016-03-01 21:19:12 +00002258 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002260 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002264 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002267 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002271
Paul Bakkerb3869132013-02-28 17:21:01 +01002272 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002273
Simon Butcher02037452016-03-01 21:19:12 +00002274 /*
2275 * Note: EMSA-PSS verification is over the length of N - 1 bits
2276 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002277 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002278
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002279 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2280 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2281
Simon Butcher02037452016-03-01 21:19:12 +00002282 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002283 if( msb % 8 == 0 )
2284 {
2285 p++;
2286 siglen -= 1;
2287 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002288
Gilles Peskine139108a2017-10-18 19:03:42 +02002289 if( siglen < hlen + 2 )
2290 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2291 hash_start = p + siglen - hlen - 1;
2292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002294 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002295 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002296
Jaeden Amero66954e12018-01-25 16:05:54 +00002297 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2298 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002299 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002300
Paul Bakkerb3869132013-02-28 17:21:01 +01002301 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002302
Gilles Peskine6a54b022017-10-17 19:02:13 +02002303 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002304 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002305
Gilles Peskine91048a32017-10-19 17:46:14 +02002306 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002307 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002308 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2309 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002310 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002311
Gilles Peskine6a54b022017-10-17 19:02:13 +02002312 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002315 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002316 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002317 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2318 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002319 }
2320
Simon Butcher02037452016-03-01 21:19:12 +00002321 /*
2322 * Generate H = Hash( M' )
2323 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002324 ret = mbedtls_md_starts( &md_ctx );
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_update( &md_ctx, zeros, 8 );
2328 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002329 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002330 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2331 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002332 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002333 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2334 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002335 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002336 ret = mbedtls_md_finish( &md_ctx, result );
2337 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002338 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002339
Jaeden Amero66954e12018-01-25 16:05:54 +00002340 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002341 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002342 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002343 goto exit;
2344 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002345
2346exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002348
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002349 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002350}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002351
2352/*
2353 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002356 int (*f_rng)(void *, unsigned char *, size_t),
2357 void *p_rng,
2358 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002360 unsigned int hashlen,
2361 const unsigned char *hash,
2362 const unsigned char *sig )
2363{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002364 mbedtls_md_type_t mgf1_hash_id;
2365 RSA_VALIDATE_RET( ctx != NULL );
2366 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2367 mode == MBEDTLS_RSA_PUBLIC );
2368 RSA_VALIDATE_RET( sig != NULL );
2369 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2370 hashlen == 0 ) ||
2371 hash != NULL );
2372
2373 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002375 : md_alg;
2376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002378 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002380 sig ) );
2381
2382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002386/*
2387 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002390 int (*f_rng)(void *, unsigned char *, size_t),
2391 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002392 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002394 unsigned int hashlen,
2395 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002396 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002397{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002398 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002399 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002400 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002401
Hanno Beckerddeeed72018-12-13 18:07:00 +00002402 RSA_VALIDATE_RET( ctx != NULL );
2403 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2404 mode == MBEDTLS_RSA_PUBLIC );
2405 RSA_VALIDATE_RET( sig != NULL );
2406 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2407 hashlen == 0 ) ||
2408 hash != NULL );
2409
2410 sig_len = ctx->len;
2411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2413 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002414
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002415 /*
2416 * Prepare expected PKCS1 v1.5 encoding of hash.
2417 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002418
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002419 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2420 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2421 {
2422 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2423 goto cleanup;
2424 }
2425
2426 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2427 encoded_expected ) ) != 0 )
2428 goto cleanup;
2429
2430 /*
2431 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2432 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002435 ? mbedtls_rsa_public( ctx, sig, encoded )
2436 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002437 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002438 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002439
Simon Butcher02037452016-03-01 21:19:12 +00002440 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002441 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002442 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002443
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002444 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2445 sig_len ) ) != 0 )
2446 {
2447 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2448 goto cleanup;
2449 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002450
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002451cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002452
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002453 if( encoded != NULL )
2454 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002455 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002456 mbedtls_free( encoded );
2457 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002458
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002459 if( encoded_expected != NULL )
2460 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002461 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002462 mbedtls_free( encoded_expected );
2463 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002464
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002465 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002466}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
2469/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002470 * Do an RSA operation and check the message digest
2471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002473 int (*f_rng)(void *, unsigned char *, size_t),
2474 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002475 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002477 unsigned int hashlen,
2478 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002479 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002480{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002481 RSA_VALIDATE_RET( ctx != NULL );
2482 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2483 mode == MBEDTLS_RSA_PUBLIC );
2484 RSA_VALIDATE_RET( sig != NULL );
2485 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2486 hashlen == 0 ) ||
2487 hash != NULL );
2488
Paul Bakkerb3869132013-02-28 17:21:01 +01002489 switch( ctx->padding )
2490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#if defined(MBEDTLS_PKCS1_V15)
2492 case MBEDTLS_RSA_PKCS_V15:
2493 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002494 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002495#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497#if defined(MBEDTLS_PKCS1_V21)
2498 case MBEDTLS_RSA_PKCS_V21:
2499 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002500 hashlen, hash, sig );
2501#endif
2502
2503 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002505 }
2506}
2507
2508/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002509 * Copy the components of an RSA key
2510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002512{
2513 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002514 RSA_VALIDATE_RET( dst != NULL );
2515 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002516
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002517 dst->len = src->len;
2518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2520 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2523 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2524 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002525
2526#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2528 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2529 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2531 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002532#endif
2533
2534 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2537 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002538
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002539 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002540 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002541
2542cleanup:
2543 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002545
2546 return( ret );
2547}
2548
2549/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 * Free the components of an RSA key
2551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002553{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002554 if( ctx == NULL )
2555 return;
2556
irwir2239a862018-06-12 18:25:09 +03002557 mbedtls_mpi_free( &ctx->Vi );
2558 mbedtls_mpi_free( &ctx->Vf );
2559 mbedtls_mpi_free( &ctx->RN );
2560 mbedtls_mpi_free( &ctx->D );
2561 mbedtls_mpi_free( &ctx->Q );
2562 mbedtls_mpi_free( &ctx->P );
2563 mbedtls_mpi_free( &ctx->E );
2564 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002565
Hanno Becker33c30a02017-08-23 07:00:22 +01002566#if !defined(MBEDTLS_RSA_NO_CRT)
irwir2239a862018-06-12 18:25:09 +03002567 mbedtls_mpi_free( &ctx->RQ );
2568 mbedtls_mpi_free( &ctx->RP );
2569 mbedtls_mpi_free( &ctx->QP );
2570 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002571 mbedtls_mpi_free( &ctx->DP );
2572#endif /* MBEDTLS_RSA_NO_CRT */
2573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#if defined(MBEDTLS_THREADING_C)
Gilles Peskineb9fce3c2021-02-01 17:57:41 +01002575 /* Free the mutex, but only if it hasn't been freed already. */
2576 if( ctx->ver != 0 )
2577 {
2578 mbedtls_mutex_free( &ctx->mutex );
2579 ctx->ver = 0;
2580 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002581#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002582}
2583
Hanno Beckerab377312017-08-23 16:24:51 +01002584#endif /* !MBEDTLS_RSA_ALT */
2585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002588#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
2590/*
2591 * Example RSA-1024 keypair, for test purposes
2592 */
2593#define KEY_LEN 128
2594
2595#define RSA_N "9292758453063D803DD603D5E777D788" \
2596 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2597 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2598 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2599 "93A89813FBF3C4F8066D2D800F7C38A8" \
2600 "1AE31942917403FF4946B0A83D3D3E05" \
2601 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2602 "5E94BB77B07507233A0BC7BAC8F90F79"
2603
2604#define RSA_E "10001"
2605
2606#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2607 "66CA472BC44D253102F8B4A9D3BFA750" \
2608 "91386C0077937FE33FA3252D28855837" \
2609 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2610 "DF79C5CE07EE72C7F123142198164234" \
2611 "CABB724CF78B8173B9F880FC86322407" \
2612 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2613 "071513A1E85B5DFA031F21ECAE91A34D"
2614
2615#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2616 "2C01CAD19EA484A87EA4377637E75500" \
2617 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2618 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2619
2620#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2621 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2622 "910E4168387E3C30AA1E00C339A79508" \
2623 "8452DD96A9A5EA5D9DCA68DA636032AF"
2624
Paul Bakker5121ce52009-01-03 21:22:43 +00002625#define PT_LEN 24
2626#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2627 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002630static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002631{
gufe443fa7c642020-08-03 17:56:50 +02002632#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002633 size_t i;
2634
Paul Bakker545570e2010-07-18 09:00:25 +00002635 if( rng_state != NULL )
2636 rng_state = NULL;
2637
Paul Bakkera3d195c2011-11-27 21:07:34 +00002638 for( i = 0; i < len; ++i )
2639 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002640#else
2641 if( rng_state != NULL )
2642 rng_state = NULL;
2643
2644 arc4random_buf( output, len );
gufe443fa7c642020-08-03 17:56:50 +02002645#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002646
Paul Bakkera3d195c2011-11-27 21:07:34 +00002647 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002650
Paul Bakker5121ce52009-01-03 21:22:43 +00002651/*
2652 * Checkup routine
2653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002655{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002656 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002658 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 unsigned char rsa_plaintext[PT_LEN];
2661 unsigned char rsa_decrypted[PT_LEN];
2662 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002664 unsigned char sha1sum[20];
2665#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
Hanno Becker3a701162017-08-22 13:52:43 +01002667 mbedtls_mpi K;
2668
2669 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002671
Hanno Becker3a701162017-08-22 13:52:43 +01002672 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2673 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2674 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2675 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2676 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2677 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2678 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2679 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2680 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2681 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2682
Hanno Becker7f25f852017-10-10 16:56:22 +01002683 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
2685 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2689 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 {
2691 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Hanno Becker5bc87292017-05-03 15:09:31 +01002694 ret = 1;
2695 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 }
2697
2698 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
2701 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2702
Hanno Becker98838b02017-10-02 13:16:10 +01002703 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2704 PT_LEN, rsa_plaintext,
2705 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 {
2707 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Hanno Becker5bc87292017-05-03 15:09:31 +01002710 ret = 1;
2711 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 }
2713
2714 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Hanno Becker98838b02017-10-02 13:16:10 +01002717 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2718 &len, rsa_ciphertext, rsa_decrypted,
2719 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 {
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
2728 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2729 {
2730 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
Hanno Becker5bc87292017-05-03 15:09:31 +01002733 ret = 1;
2734 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 }
2736
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002737 if( verbose != 0 )
2738 mbedtls_printf( "passed\n" );
2739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002742 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002744 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002745 {
2746 if( verbose != 0 )
2747 mbedtls_printf( "failed\n" );
2748
2749 return( 1 );
2750 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
Hanno Becker98838b02017-10-02 13:16:10 +01002752 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2753 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2754 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 {
2756 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002758
Hanno Becker5bc87292017-05-03 15:09:31 +01002759 ret = 1;
2760 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002761 }
2762
2763 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002765
Hanno Becker98838b02017-10-02 13:16:10 +01002766 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2767 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2768 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 {
2770 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002772
Hanno Becker5bc87292017-05-03 15:09:31 +01002773 ret = 1;
2774 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002775 }
2776
2777 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002778 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002781 if( verbose != 0 )
2782 mbedtls_printf( "\n" );
2783
Paul Bakker3d8fb632014-04-17 12:42:41 +02002784cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002785 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 mbedtls_rsa_free( &rsa );
2787#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002788 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002790 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791}
2792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795#endif /* MBEDTLS_RSA_C */