blob: a400132db6be0d82a392b7de0702aa4d8f6f1588 [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)
523 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200524#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000525}
526
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100527/*
528 * Set padding for an existing RSA context
529 */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000530void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
531 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100532{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000533 RSA_VALIDATE( ctx != NULL );
534 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
535 padding == MBEDTLS_RSA_PKCS_V21 );
536
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100537 ctx->padding = padding;
538 ctx->hash_id = hash_id;
539}
540
Hanno Becker617c1ae2017-08-23 14:11:24 +0100541/*
542 * Get length in bytes of RSA modulus
543 */
544
545size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
546{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100547 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100548}
549
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553/*
554 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800555 *
556 * This generation method follows the RSA key pair generation procedure of
557 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000560 int (*f_rng)(void *, unsigned char *, size_t),
561 void *p_rng,
562 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000563{
564 int ret;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800565 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100566 int prime_quality = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000567 RSA_VALIDATE_RET( ctx != NULL );
568 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Hanno Beckerddeeed72018-12-13 18:07:00 +0000570 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
Janos Follathef441782016-09-21 13:18:12 +0100571 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
572
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
585 /*
586 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800587 * 1. |P-Q| > 2^( nbits / 2 - 100 )
588 * 2. GCD( E, (P-1)*(Q-1) ) == 1
589 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
593 do
594 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100595 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
596 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
Janos Follathb8fc1b02018-09-03 15:37:01 +0100598 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
599 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800601 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
602 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
603 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 continue;
605
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800606 /* not required by any standards, but some users rely on the fact that P > Q */
607 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100608 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100609
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100610 /* Temporarily replace P,Q by P-1, Q-1 */
611 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
612 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
613 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800614
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800615 /* 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 +0200616 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800617 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
618 continue;
619
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800620 /* 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 -0800621 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
622 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
623 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
624
625 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
626 continue;
627
628 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800630 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100632 /* Restore P,Q */
633 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
634 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
635
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800636 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
637
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100638 ctx->len = mbedtls_mpi_size( &ctx->N );
639
Jethro Beekman97f95c92018-02-13 15:50:36 -0800640#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 * DP = D mod (P - 1)
643 * DQ = D mod (Q - 1)
644 * QP = Q^-1 mod P
645 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100646 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
647 &ctx->DP, &ctx->DQ, &ctx->QP ) );
648#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
Hanno Becker83aad1f2017-08-23 06:45:10 +0100650 /* Double-check */
651 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
653cleanup:
654
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100655 mbedtls_mpi_free( &H );
656 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800657 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000658
659 if( ret != 0 )
660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_rsa_free( ctx );
662 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 }
664
Paul Bakker48377d92013-08-30 12:06:24 +0200665 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000666}
667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000669
670/*
671 * Check a public RSA key
672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000674{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000675 RSA_VALIDATE_RET( ctx != NULL );
676
Hanno Beckerebd2c022017-10-12 10:54:53 +0100677 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000679
Hanno Becker3a760a12018-01-05 08:14:49 +0000680 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100683 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
Hanno Becker705fc682017-10-10 17:57:02 +0100685 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
686 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100690 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000691
692 return( 0 );
693}
694
695/*
Hanno Becker705fc682017-10-10 17:57:02 +0100696 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000699{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000700 RSA_VALIDATE_RET( ctx != NULL );
701
Hanno Becker705fc682017-10-10 17:57:02 +0100702 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100703 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 {
Hanno Becker98838b02017-10-02 13:16:10 +0100705 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 }
Paul Bakker48377d92013-08-30 12:06:24 +0200707
Hanno Becker98838b02017-10-02 13:16:10 +0100708 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100709 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000710 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100711 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000713
Hanno Beckerb269a852017-08-25 08:03:21 +0100714#if !defined(MBEDTLS_RSA_NO_CRT)
715 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
716 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
717 {
718 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
719 }
720#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000721
722 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000723}
724
725/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100726 * Check if contexts holding a public and private key match
727 */
Hanno Becker98838b02017-10-02 13:16:10 +0100728int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
729 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100730{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000731 RSA_VALIDATE_RET( pub != NULL );
732 RSA_VALIDATE_RET( prv != NULL );
733
Hanno Becker98838b02017-10-02 13:16:10 +0100734 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100738 }
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
741 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100744 }
745
746 return( 0 );
747}
748
749/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000750 * Do an RSA public key operation
751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000753 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 unsigned char *output )
755{
Paul Bakker23986e52011-04-24 08:57:21 +0000756 int ret;
757 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_mpi T;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000759 RSA_VALIDATE_RET( ctx != NULL );
760 RSA_VALIDATE_RET( input != NULL );
761 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
Hanno Beckerebd2c022017-10-12 10:54:53 +0100763 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100764 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200768#if defined(MBEDTLS_THREADING_C)
769 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
770 return( ret );
771#endif
772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000776 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200777 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
778 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 }
780
781 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
783 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
785cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200787 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
788 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100789#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
793 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000795
796 return( 0 );
797}
798
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200799/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200800 * Generate or update blinding values, see section 10 of:
801 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200802 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200803 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200804 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200805static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200806 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
807{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200808 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200809
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200810 if( ctx->Vf.p != NULL )
811 {
812 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
814 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
815 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
816 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200817
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200818 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200819 }
820
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200821 /* Unblinding value: Vf = random number, invertible mod N */
822 do {
823 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
827 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
828 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200829
830 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
832 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 +0200833
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200834
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200835cleanup:
836 return( ret );
837}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200838
Paul Bakker5121ce52009-01-03 21:22:43 +0000839/*
Janos Follathe81102e2017-03-22 13:38:28 +0000840 * Exponent blinding supposed to prevent side-channel attacks using multiple
841 * traces of measurements to recover the RSA key. The more collisions are there,
842 * the more bits of the key can be recovered. See [3].
843 *
844 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
845 * observations on avarage.
846 *
847 * For example with 28 byte blinding to achieve 2 collisions the adversary has
848 * to make 2^112 observations on avarage.
849 *
850 * (With the currently (as of 2017 April) known best algorithms breaking 2048
851 * bit RSA requires approximately as much time as trying out 2^112 random keys.
852 * Thus in this sense with 28 byte blinding the security is not reduced by
853 * side-channel attacks like the one in [3])
854 *
855 * This countermeasure does not help if the key recovery is possible with a
856 * single trace.
857 */
858#define RSA_EXPONENT_BLINDING 28
859
860/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 * Do an RSA private key operation
862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200864 int (*f_rng)(void *, unsigned char *, size_t),
865 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000866 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 unsigned char *output )
868{
Paul Bakker23986e52011-04-24 08:57:21 +0000869 int ret;
870 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100871
872 /* Temporary holding the result */
873 mbedtls_mpi T;
874
875 /* Temporaries holding P-1, Q-1 and the
876 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000877 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100878
879#if !defined(MBEDTLS_RSA_NO_CRT)
880 /* Temporaries holding the results mod p resp. mod q. */
881 mbedtls_mpi TP, TQ;
882
883 /* Temporaries holding the blinded exponents for
884 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000885 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100886
887 /* Pointers to actual exponents to be used - either the unblinded
888 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000889 mbedtls_mpi *DP = &ctx->DP;
890 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100891#else
892 /* Temporary holding the blinded exponent (if used). */
893 mbedtls_mpi D_blind;
894
895 /* Pointer to actual exponent to be used - either the unblinded
896 * or the blinded one, depending on the presence of a PRNG. */
897 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100898#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100899
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100900 /* Temporaries holding the initial input and the double
901 * checked result; should be the same in the end. */
902 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000903
Hanno Beckerddeeed72018-12-13 18:07:00 +0000904 RSA_VALIDATE_RET( ctx != NULL );
905 RSA_VALIDATE_RET( input != NULL );
906 RSA_VALIDATE_RET( output != NULL );
907
Hanno Beckerebd2c022017-10-12 10:54:53 +0100908 if( rsa_check_context( ctx, 1 /* private key checks */,
909 f_rng != NULL /* blinding y/n */ ) != 0 )
910 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100911 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100912 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100913
Hanno Becker06811ce2017-05-03 15:10:34 +0100914#if defined(MBEDTLS_THREADING_C)
915 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
916 return( ret );
917#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000918
Hanno Becker06811ce2017-05-03 15:10:34 +0100919 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100920 mbedtls_mpi_init( &T );
921
922 mbedtls_mpi_init( &P1 );
923 mbedtls_mpi_init( &Q1 );
924 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000925
Janos Follathf9203b42017-03-22 15:13:15 +0000926 if( f_rng != NULL )
927 {
Janos Follathe81102e2017-03-22 13:38:28 +0000928#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000929 mbedtls_mpi_init( &D_blind );
930#else
931 mbedtls_mpi_init( &DP_blind );
932 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000933#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000934 }
Janos Follathe81102e2017-03-22 13:38:28 +0000935
Hanno Becker06811ce2017-05-03 15:10:34 +0100936#if !defined(MBEDTLS_RSA_NO_CRT)
937 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200938#endif
939
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100940 mbedtls_mpi_init( &I );
941 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100942
943 /* End of MPI initialization */
944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
946 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200948 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
949 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 }
951
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100952 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100953
Paul Bakkerf451bac2013-08-30 15:37:02 +0200954 if( f_rng != NULL )
955 {
956 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200957 * Blinding
958 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200959 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200960 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
961 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000963
Janos Follathe81102e2017-03-22 13:38:28 +0000964 /*
965 * Exponent blinding
966 */
967 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
968 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
969
Janos Follathf9203b42017-03-22 15:13:15 +0000970#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000971 /*
972 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
973 */
974 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
975 f_rng, p_rng ) );
976 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
977 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
978 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
979
980 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000981#else
982 /*
983 * DP_blind = ( P - 1 ) * R + DP
984 */
985 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
986 f_rng, p_rng ) );
987 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
988 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
989 &ctx->DP ) );
990
991 DP = &DP_blind;
992
993 /*
994 * DQ_blind = ( Q - 1 ) * R + DQ
995 */
996 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
997 f_rng, p_rng ) );
998 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
999 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1000 &ctx->DQ ) );
1001
1002 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001003#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001004 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001007 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001008#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001009 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001010 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001012 * TP = input ^ dP mod P
1013 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001015
1016 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1017 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
1019 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001020 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001021 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001022 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1023 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1024 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
1026 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001027 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001029 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1030 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001032
Paul Bakkerf451bac2013-08-30 15:37:02 +02001033 if( f_rng != NULL )
1034 {
1035 /*
1036 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001037 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001038 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001039 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001041 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
Hanno Becker2dec5e82017-10-03 07:49:52 +01001043 /* Verify the result to prevent glitching attacks. */
1044 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1045 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001046 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001047 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001048 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1049 goto cleanup;
1050 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001051
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
1055cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001057 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1058 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001059#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001060
Hanno Becker06811ce2017-05-03 15:10:34 +01001061 mbedtls_mpi_free( &P1 );
1062 mbedtls_mpi_free( &Q1 );
1063 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001064
1065 if( f_rng != NULL )
1066 {
Janos Follathe81102e2017-03-22 13:38:28 +00001067#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001068 mbedtls_mpi_free( &D_blind );
1069#else
1070 mbedtls_mpi_free( &DP_blind );
1071 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001072#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001073 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Hanno Becker06811ce2017-05-03 15:10:34 +01001075 mbedtls_mpi_free( &T );
1076
1077#if !defined(MBEDTLS_RSA_NO_CRT)
1078 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1079#endif
1080
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001081 mbedtls_mpi_free( &C );
1082 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001083
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
1087 return( 0 );
1088}
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001091/**
1092 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1093 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001094 * \param dst buffer to mask
1095 * \param dlen length of destination buffer
1096 * \param src source of the mask generation
1097 * \param slen length of the source buffer
1098 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001099 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001100static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104 unsigned char counter[4];
1105 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001106 unsigned int hlen;
1107 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001108 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001111 memset( counter, 0, 4 );
1112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114
Simon Butcher02037452016-03-01 21:19:12 +00001115 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001116 p = dst;
1117
1118 while( dlen > 0 )
1119 {
1120 use_len = hlen;
1121 if( dlen < hlen )
1122 use_len = dlen;
1123
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001124 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1125 goto exit;
1126 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1127 goto exit;
1128 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1129 goto exit;
1130 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1131 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001132
1133 for( i = 0; i < use_len; ++i )
1134 *p++ ^= mask[i];
1135
1136 counter[3]++;
1137
1138 dlen -= use_len;
1139 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001140
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001141exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001142 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001143
1144 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001149/*
1150 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001153 int (*f_rng)(void *, unsigned char *, size_t),
1154 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001155 int mode,
1156 const unsigned char *label, size_t label_len,
1157 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 const unsigned char *input,
1159 unsigned char *output )
1160{
1161 size_t olen;
1162 int ret;
1163 unsigned char *p = output;
1164 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 const mbedtls_md_info_t *md_info;
1166 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001167
Hanno Beckerddeeed72018-12-13 18:07:00 +00001168 RSA_VALIDATE_RET( ctx != NULL );
1169 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1170 mode == MBEDTLS_RSA_PUBLIC );
1171 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001172 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001173 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001177
1178 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001182 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001184
1185 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001187
Simon Butcher02037452016-03-01 21:19:12 +00001188 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001189 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001191
1192 memset( output, 0, olen );
1193
1194 *p++ = 0;
1195
Simon Butcher02037452016-03-01 21:19:12 +00001196 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001197 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001199
1200 p += hlen;
1201
Simon Butcher02037452016-03-01 21:19:12 +00001202 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001203 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1204 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001205 p += hlen;
1206 p += olen - 2 * hlen - 2 - ilen;
1207 *p++ = 1;
1208 memcpy( p, input, ilen );
1209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001211 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001212 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001213
Simon Butcher02037452016-03-01 21:19:12 +00001214 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001215 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1216 &md_ctx ) ) != 0 )
1217 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001218
Simon Butcher02037452016-03-01 21:19:12 +00001219 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001220 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1221 &md_ctx ) ) != 0 )
1222 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001223
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001224exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001226
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001227 if( ret != 0 )
1228 return( ret );
1229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 return( ( mode == MBEDTLS_RSA_PUBLIC )
1231 ? mbedtls_rsa_public( ctx, output, output )
1232 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001233}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001237/*
1238 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001241 int (*f_rng)(void *, unsigned char *, size_t),
1242 void *p_rng,
1243 int mode, size_t ilen,
1244 const unsigned char *input,
1245 unsigned char *output )
1246{
1247 size_t nb_pad, olen;
1248 int ret;
1249 unsigned char *p = output;
1250
Hanno Beckerddeeed72018-12-13 18:07:00 +00001251 RSA_VALIDATE_RET( ctx != NULL );
1252 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1253 mode == MBEDTLS_RSA_PUBLIC );
1254 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001255 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1258 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001259
Paul Bakkerb3869132013-02-28 17:21:01 +01001260 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001261
Simon Butcher02037452016-03-01 21:19:12 +00001262 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001263 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001265
1266 nb_pad = olen - 3 - ilen;
1267
1268 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001270 {
Hanno Beckerb86e6842018-12-18 14:46:04 +00001271 if( f_rng == NULL )
1272 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001275
1276 while( nb_pad-- > 0 )
1277 {
1278 int rng_dl = 100;
1279
1280 do {
1281 ret = f_rng( p_rng, p, 1 );
1282 } while( *p == 0 && --rng_dl && ret == 0 );
1283
Simon Butcher02037452016-03-01 21:19:12 +00001284 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001285 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001287
1288 p++;
1289 }
1290 }
1291 else
1292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001294
1295 while( nb_pad-- > 0 )
1296 *p++ = 0xFF;
1297 }
1298
1299 *p++ = 0;
1300 memcpy( p, input, ilen );
1301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 return( ( mode == MBEDTLS_RSA_PUBLIC )
1303 ? mbedtls_rsa_public( ctx, output, output )
1304 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001305}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001307
Paul Bakker5121ce52009-01-03 21:22:43 +00001308/*
1309 * Add the message padding, then do an RSA operation
1310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001312 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001313 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001314 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001315 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 unsigned char *output )
1317{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001318 RSA_VALIDATE_RET( ctx != NULL );
1319 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1320 mode == MBEDTLS_RSA_PUBLIC );
1321 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001322 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001323
Paul Bakker5121ce52009-01-03 21:22:43 +00001324 switch( ctx->padding )
1325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#if defined(MBEDTLS_PKCS1_V15)
1327 case MBEDTLS_RSA_PKCS_V15:
1328 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001329 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001330#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#if defined(MBEDTLS_PKCS1_V21)
1333 case MBEDTLS_RSA_PKCS_V21:
1334 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001335 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001336#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
1338 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001341}
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001344/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001345 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001348 int (*f_rng)(void *, unsigned char *, size_t),
1349 void *p_rng,
1350 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001351 const unsigned char *label, size_t label_len,
1352 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001353 const unsigned char *input,
1354 unsigned char *output,
1355 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001356{
Paul Bakker23986e52011-04-24 08:57:21 +00001357 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001358 size_t ilen, i, pad_len;
1359 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1361 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001362 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 const mbedtls_md_info_t *md_info;
1364 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001365
Hanno Beckerddeeed72018-12-13 18:07:00 +00001366 RSA_VALIDATE_RET( ctx != NULL );
1367 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1368 mode == MBEDTLS_RSA_PUBLIC );
1369 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1370 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1371 RSA_VALIDATE_RET( input != NULL );
1372 RSA_VALIDATE_RET( olen != NULL );
1373
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001374 /*
1375 * Parameters sanity checks
1376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1378 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379
1380 ilen = ctx->len;
1381
Paul Bakker27fdf462011-06-09 13:55:13 +00001382 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001386 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001388
Janos Follathc17cda12016-02-11 11:08:18 +00001389 hlen = mbedtls_md_get_size( md_info );
1390
1391 // checking for integer underflow
1392 if( 2 * hlen + 2 > ilen )
1393 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1394
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001395 /*
1396 * RSA operation
1397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1399 ? mbedtls_rsa_public( ctx, input, buf )
1400 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
1402 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001403 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001405 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001406 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001409 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1410 {
1411 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001412 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001413 }
1414
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001415 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001416 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1417 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001418 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001419 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1420 &md_ctx ) ) != 0 )
1421 {
1422 mbedtls_md_free( &md_ctx );
1423 goto cleanup;
1424 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001427
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001428 /* Generate lHash */
1429 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1430 goto cleanup;
1431
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001432 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001433 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001434 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001436 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001438 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001439
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001440 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001441
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001442 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001443 for( i = 0; i < hlen; i++ )
1444 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001446 /* Get zero-padding len, but always read till end of buffer
1447 * (minus one, for the 01 byte) */
1448 pad_len = 0;
1449 pad_done = 0;
1450 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1451 {
1452 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001453 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001454 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001455
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001456 p += pad_len;
1457 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001458
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001459 /*
1460 * The only information "leaked" is whether the padding was correct or not
1461 * (eg, no data is copied if it was not correct). This meets the
1462 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1463 * the different error conditions.
1464 */
1465 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001466 {
1467 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1468 goto cleanup;
1469 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001470
Paul Bakker66d5d072014-06-17 16:39:18 +02001471 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001472 {
1473 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1474 goto cleanup;
1475 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001476
1477 *olen = ilen - (p - buf);
1478 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001479 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001480
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001481cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001482 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1483 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001484
1485 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001490/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1491 *
1492 * \param value The value to analyze.
Gilles Peskine331d80e2018-10-04 18:32:29 +02001493 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001494 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001495static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001496{
1497 /* MSVC has a warning about unary minus on unsigned, but this is
1498 * well-defined and precisely what we want to do here */
1499#if defined(_MSC_VER)
1500#pragma warning( push )
1501#pragma warning( disable : 4146 )
1502#endif
1503 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1504#if defined(_MSC_VER)
1505#pragma warning( pop )
1506#endif
1507}
1508
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001509/** Check whether a size is out of bounds, without branches.
1510 *
1511 * This is equivalent to `size > max`, but is likely to be compiled to
1512 * to code using bitwise operation rather than a branch.
1513 *
1514 * \param size Size to check.
1515 * \param max Maximum desired value for \p size.
1516 * \return \c 0 if `size <= max`.
1517 * \return \c 1 if `size > max`.
1518 */
1519static unsigned size_greater_than( size_t size, size_t max )
1520{
1521 /* Return the sign bit (1 for negative) of (max - size). */
1522 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1523}
1524
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001525/** Choose between two integer values, without branches.
1526 *
Gilles Peskine331d80e2018-10-04 18:32:29 +02001527 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1528 * to code using bitwise operation rather than a branch.
1529 *
1530 * \param cond Condition to test.
1531 * \param if1 Value to use if \p cond is nonzero.
1532 * \param if0 Value to use if \p cond is zero.
1533 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001534 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001535static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001536{
Gilles Peskine331d80e2018-10-04 18:32:29 +02001537 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001538 return( ( mask & if1 ) | (~mask & if0 ) );
1539}
1540
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001541/** Shift some data towards the left inside a buffer without leaking
1542 * the length of the data through side channels.
1543 *
1544 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1545 * ```
1546 * memmove(start, start + offset, total - offset);
1547 * memset(start + offset, 0, total - offset);
1548 * ```
1549 * but it strives to use a memory access pattern (and thus total timing)
1550 * that does not depend on \p offset. This timing independence comes at
1551 * the expense of performance.
1552 *
1553 * \param start Pointer to the start of the buffer.
1554 * \param total Total size of the buffer.
1555 * \param offset Offset from which to copy \p total - \p offset bytes.
1556 */
1557static void mem_move_to_left( void *start,
1558 size_t total,
1559 size_t offset )
1560{
1561 volatile unsigned char *buf = start;
1562 size_t i, n;
1563 if( total == 0 )
1564 return;
1565 for( i = 0; i < total; i++ )
1566 {
1567 unsigned no_op = size_greater_than( total - offset, i );
1568 /* The first `total - offset` passes are a no-op. The last
1569 * `offset` passes shift the data one byte to the left and
1570 * zero out the last byte. */
1571 for( n = 0; n < total - 1; n++ )
Gilles Peskine9b430702018-10-12 19:15:34 +02001572 {
1573 unsigned char current = buf[n];
1574 unsigned char next = buf[n+1];
1575 buf[n] = if_int( no_op, current, next );
1576 }
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001577 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1578 }
1579}
1580
Paul Bakkerb3869132013-02-28 17:21:01 +01001581/*
1582 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001585 int (*f_rng)(void *, unsigned char *, size_t),
1586 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001587 int mode, size_t *olen,
1588 const unsigned char *input,
1589 unsigned char *output,
Gilles Peskine5908dd42018-10-02 22:43:06 +02001590 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001591{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001592 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001593 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine85a74422018-10-05 14:50:21 +02001595 /* The following variables take sensitive values: their value must
1596 * not leak into the observable behavior of the function other than
1597 * the designated outputs (output, olen, return value). Otherwise
1598 * this would open the execution of the function to
1599 * side-channel-based variants of the Bleichenbacher padding oracle
1600 * attack. Potential side channels include overall timing, memory
1601 * access patterns (especially visible to an adversary who has access
1602 * to a shared memory cache), and branches (especially visible to
1603 * an adversary who has access to a shared code cache or to a shared
1604 * branch predictor). */
1605 size_t pad_count = 0;
1606 unsigned bad = 0;
1607 unsigned char pad_done = 0;
1608 size_t plaintext_size = 0;
1609 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001610
Hanno Beckerddeeed72018-12-13 18:07:00 +00001611 RSA_VALIDATE_RET( ctx != NULL );
1612 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1613 mode == MBEDTLS_RSA_PUBLIC );
1614 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1615 RSA_VALIDATE_RET( input != NULL );
1616 RSA_VALIDATE_RET( olen != NULL );
1617
1618 ilen = ctx->len;
1619 plaintext_max_size = ( output_max_len > ilen - 11 ?
1620 ilen - 11 :
1621 output_max_len );
1622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1624 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001625
Paul Bakkerb3869132013-02-28 17:21:01 +01001626 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1630 ? mbedtls_rsa_public( ctx, input, buf )
1631 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001632
1633 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001634 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001635
Gilles Peskine40b57f42018-10-05 15:06:12 +02001636 /* Check and get padding length in constant time and constant
1637 * memory trace. The first byte must be 0. */
1638 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001641 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001642 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1643 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001644 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001645
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001646 /* Read the whole buffer. Set pad_done to nonzero if we find
1647 * the 0x00 byte and remember the padding length in pad_count. */
1648 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001649 {
Gilles Peskine85a74422018-10-05 14:50:21 +02001650 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001651 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001652 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001653 }
1654 else
1655 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001656 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1657 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001658 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001659
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001660 /* Read the whole buffer. Set pad_done to nonzero if we find
1661 * the 0x00 byte and remember the padding length in pad_count.
1662 * If there's a non-0xff byte in the padding, the padding is bad. */
1663 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001664 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001665 pad_done |= if_int( buf[i], 0, 1 );
1666 pad_count += if_int( pad_done, 0, 1 );
1667 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 }
1670
Gilles Peskine40b57f42018-10-05 15:06:12 +02001671 /* If pad_done is still zero, there's no data, only unfinished padding. */
1672 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001673
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001674 /* There must be at least 8 bytes of padding. */
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001675 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001676
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001677 /* If the padding is valid, set plaintext_size to the number of
1678 * remaining bytes after stripping the padding. If the padding
1679 * is invalid, avoid leaking this fact through the size of the
1680 * output: use the maximum message size that fits in the output
1681 * buffer. Do it without branches to avoid leaking the padding
1682 * validity through timing. RSA keys are small enough that all the
1683 * size_t values involved fit in unsigned int. */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001684 plaintext_size = if_int( bad,
1685 (unsigned) plaintext_max_size,
Gilles Peskine85a74422018-10-05 14:50:21 +02001686 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001687
Gilles Peskine9265ff42018-10-04 19:13:43 +02001688 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001689 * buffer and to 1 otherwise. */
1690 output_too_large = size_greater_than( plaintext_size,
1691 plaintext_max_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001692
Gilles Peskine9265ff42018-10-04 19:13:43 +02001693 /* Set ret without branches to avoid timing attacks. Return:
1694 * - INVALID_PADDING if the padding is bad (bad != 0).
1695 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1696 * plaintext does not fit in the output buffer.
1697 * - 0 if the padding is correct. */
Gilles Peskine48992472018-10-12 19:19:12 +02001698 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1699 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1700 0 ) );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001701
Gilles Peskine9265ff42018-10-04 19:13:43 +02001702 /* If the padding is bad or the plaintext is too large, zero the
1703 * data that we're about to copy to the output buffer.
1704 * We need to copy the same amount of data
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001705 * from the same buffer whether the padding is good or not to
1706 * avoid leaking the padding validity through overall timing or
1707 * through memory or cache access patterns. */
Gilles Peskine40b57f42018-10-05 15:06:12 +02001708 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001709 for( i = 11; i < ilen; i++ )
Gilles Peskine40b57f42018-10-05 15:06:12 +02001710 buf[i] &= ~bad;
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001711
Gilles Peskine9265ff42018-10-04 19:13:43 +02001712 /* If the plaintext is too large, truncate it to the buffer size.
1713 * Copy anyway to avoid revealing the length through timing, because
1714 * revealing the length is as bad as revealing the padding validity
1715 * for a Bleichenbacher attack. */
1716 plaintext_size = if_int( output_too_large,
1717 (unsigned) plaintext_max_size,
1718 (unsigned) plaintext_size );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001719
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001720 /* Move the plaintext to the leftmost position where it can start in
1721 * the working buffer, i.e. make it start plaintext_max_size from
1722 * the end of the buffer. Do this with a memory access trace that
1723 * does not depend on the plaintext size. After this move, the
1724 * starting location of the plaintext is no longer sensitive
1725 * information. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001726 mem_move_to_left( buf + ilen - plaintext_max_size,
1727 plaintext_max_size,
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001728 plaintext_max_size - plaintext_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001729
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001730 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskine9265ff42018-10-04 19:13:43 +02001731 * into the output buffer. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001732 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001733
1734 /* Report the amount of data we copied to the output buffer. In case
1735 * of errors (bad padding or output too large), the value of *olen
1736 * when this function returns is not specified. Making it equivalent
1737 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001738 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001739
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001740cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001741 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001742
1743 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001744}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001746
1747/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001748 * Do an RSA operation, then remove the message padding
1749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001751 int (*f_rng)(void *, unsigned char *, size_t),
1752 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001753 int mode, size_t *olen,
1754 const unsigned char *input,
1755 unsigned char *output,
1756 size_t output_max_len)
1757{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001758 RSA_VALIDATE_RET( ctx != NULL );
1759 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1760 mode == MBEDTLS_RSA_PUBLIC );
1761 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1762 RSA_VALIDATE_RET( input != NULL );
1763 RSA_VALIDATE_RET( olen != NULL );
1764
Paul Bakkerb3869132013-02-28 17:21:01 +01001765 switch( ctx->padding )
1766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#if defined(MBEDTLS_PKCS1_V15)
1768 case MBEDTLS_RSA_PKCS_V15:
1769 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001770 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001771#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_PKCS1_V21)
1774 case MBEDTLS_RSA_PKCS_V21:
1775 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001776 olen, input, output,
1777 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001778#endif
1779
1780 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001782 }
1783}
1784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001786/*
1787 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001790 int (*f_rng)(void *, unsigned char *, size_t),
1791 void *p_rng,
1792 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001794 unsigned int hashlen,
1795 const unsigned char *hash,
1796 unsigned char *sig )
1797{
1798 size_t olen;
1799 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001801 size_t slen, min_slen, hlen, offset = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001802 int ret;
1803 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 const mbedtls_md_info_t *md_info;
1805 mbedtls_md_context_t md_ctx;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001806 RSA_VALIDATE_RET( ctx != NULL );
1807 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1808 mode == MBEDTLS_RSA_PUBLIC );
1809 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1810 hashlen == 0 ) ||
1811 hash != NULL );
1812 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1815 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001816
1817 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001819
1820 olen = ctx->len;
1821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001823 {
Simon Butcher02037452016-03-01 21:19:12 +00001824 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001826 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001830 }
1831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001833 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001837
Jaeden Amero3725bb22018-09-07 19:12:36 +01001838 /* Calculate the largest possible salt length. Normally this is the hash
1839 * length, which is the maximum length the salt can have. If there is not
1840 * enough room, use the maximum salt length that fits. The constraint is
1841 * that the hash length plus the salt length plus 2 bytes must be at most
1842 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1843 * (PKCS#1 v2.2) §9.1.1 step 3. */
1844 min_slen = hlen - 2;
1845 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001847 else if( olen >= hlen + hlen + 2 )
1848 slen = hlen;
1849 else
1850 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001851
1852 memset( sig, 0, olen );
1853
Simon Butcher02037452016-03-01 21:19:12 +00001854 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001855 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001857
Simon Butcher02037452016-03-01 21:19:12 +00001858 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001859 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001860 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001861 *p++ = 0x01;
1862 memcpy( p, salt, slen );
1863 p += slen;
1864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001866 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001867 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001868
Simon Butcher02037452016-03-01 21:19:12 +00001869 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001870 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1871 goto exit;
1872 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1873 goto exit;
1874 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1875 goto exit;
1876 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1877 goto exit;
1878 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1879 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001880
Simon Butcher02037452016-03-01 21:19:12 +00001881 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001882 if( msb % 8 == 0 )
1883 offset = 1;
1884
Simon Butcher02037452016-03-01 21:19:12 +00001885 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001886 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1887 &md_ctx ) ) != 0 )
1888 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001889
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001890 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001891 sig[0] &= 0xFF >> ( olen * 8 - msb );
1892
1893 p += hlen;
1894 *p++ = 0xBC;
1895
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001896 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001897
1898exit:
1899 mbedtls_md_free( &md_ctx );
1900
1901 if( ret != 0 )
1902 return( ret );
1903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 return( ( mode == MBEDTLS_RSA_PUBLIC )
1905 ? mbedtls_rsa_public( ctx, sig, sig )
1906 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001911/*
1912 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1913 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001914
1915/* Construct a PKCS v1.5 encoding of a hashed message
1916 *
1917 * This is used both for signature generation and verification.
1918 *
1919 * Parameters:
1920 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001921 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001922 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001923 * - hash: Buffer containing the hashed message or the raw data.
1924 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001925 * - dst: Buffer to hold the encoded message.
1926 *
1927 * Assumptions:
1928 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1929 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001930 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001931 *
1932 */
1933static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1934 unsigned int hashlen,
1935 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001936 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001937 unsigned char *dst )
1938{
1939 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001940 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001941 unsigned char *p = dst;
1942 const char *oid = NULL;
1943
1944 /* Are we signing hashed or raw data? */
1945 if( md_alg != MBEDTLS_MD_NONE )
1946 {
1947 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1948 if( md_info == NULL )
1949 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1950
1951 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1952 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1953
1954 hashlen = mbedtls_md_get_size( md_info );
1955
1956 /* Double-check that 8 + hashlen + oid_size can be used as a
1957 * 1-byte ASN.1 length encoding and that there's no overflow. */
1958 if( 8 + hashlen + oid_size >= 0x80 ||
1959 10 + hashlen < hashlen ||
1960 10 + hashlen + oid_size < 10 + hashlen )
1961 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1962
1963 /*
1964 * Static bounds check:
1965 * - Need 10 bytes for five tag-length pairs.
1966 * (Insist on 1-byte length encodings to protect against variants of
1967 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1968 * - Need hashlen bytes for hash
1969 * - Need oid_size bytes for hash alg OID.
1970 */
1971 if( nb_pad < 10 + hashlen + oid_size )
1972 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1973 nb_pad -= 10 + hashlen + oid_size;
1974 }
1975 else
1976 {
1977 if( nb_pad < hashlen )
1978 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1979
1980 nb_pad -= hashlen;
1981 }
1982
Hanno Becker2b2f8982017-09-27 17:10:03 +01001983 /* Need space for signature header and padding delimiter (3 bytes),
1984 * and 8 bytes for the minimal padding */
1985 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001986 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1987 nb_pad -= 3;
1988
1989 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001990 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001991
1992 /* Write signature header and padding */
1993 *p++ = 0;
1994 *p++ = MBEDTLS_RSA_SIGN;
1995 memset( p, 0xFF, nb_pad );
1996 p += nb_pad;
1997 *p++ = 0;
1998
1999 /* Are we signing raw data? */
2000 if( md_alg == MBEDTLS_MD_NONE )
2001 {
2002 memcpy( p, hash, hashlen );
2003 return( 0 );
2004 }
2005
2006 /* Signing hashed data, add corresponding ASN.1 structure
2007 *
2008 * DigestInfo ::= SEQUENCE {
2009 * digestAlgorithm DigestAlgorithmIdentifier,
2010 * digest Digest }
2011 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2012 * Digest ::= OCTET STRING
2013 *
2014 * Schematic:
2015 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2016 * TAG-NULL + LEN [ NULL ] ]
2017 * TAG-OCTET + LEN [ HASH ] ]
2018 */
2019 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002020 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002021 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002022 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002023 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002024 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002025 memcpy( p, oid, oid_size );
2026 p += oid_size;
2027 *p++ = MBEDTLS_ASN1_NULL;
2028 *p++ = 0x00;
2029 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002030 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002031 memcpy( p, hash, hashlen );
2032 p += hashlen;
2033
2034 /* Just a sanity-check, should be automatic
2035 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002036 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002037 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002038 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002039 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2040 }
2041
2042 return( 0 );
2043}
2044
Paul Bakkerb3869132013-02-28 17:21:01 +01002045/*
2046 * Do an RSA operation to sign the message digest
2047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002049 int (*f_rng)(void *, unsigned char *, size_t),
2050 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002051 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002053 unsigned int hashlen,
2054 const unsigned char *hash,
2055 unsigned char *sig )
2056{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002057 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002058 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002059
Hanno Beckerddeeed72018-12-13 18:07:00 +00002060 RSA_VALIDATE_RET( ctx != NULL );
2061 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2062 mode == MBEDTLS_RSA_PUBLIC );
2063 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2064 hashlen == 0 ) ||
2065 hash != NULL );
2066 RSA_VALIDATE_RET( sig != NULL );
2067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2069 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002070
Hanno Beckerfdf38032017-09-06 12:35:55 +01002071 /*
2072 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2073 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002074
Hanno Beckerfdf38032017-09-06 12:35:55 +01002075 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2076 ctx->len, sig ) ) != 0 )
2077 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002078
2079 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002080 * Call respective RSA primitive
2081 */
2082
2083 if( mode == MBEDTLS_RSA_PUBLIC )
2084 {
2085 /* Skip verification on a public key operation */
2086 return( mbedtls_rsa_public( ctx, sig, sig ) );
2087 }
2088
2089 /* Private key operation
2090 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002091 * In order to prevent Lenstra's attack, make the signature in a
2092 * temporary buffer and check it before returning it.
2093 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002094
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002095 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002096 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002097 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2098
Hanno Beckerfdf38032017-09-06 12:35:55 +01002099 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002100 if( verif == NULL )
2101 {
2102 mbedtls_free( sig_try );
2103 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2104 }
2105
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002106 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2107 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2108
Hanno Becker171a8f12017-09-06 12:32:16 +01002109 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002110 {
2111 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2112 goto cleanup;
2113 }
2114
2115 memcpy( sig, sig_try, ctx->len );
2116
2117cleanup:
2118 mbedtls_free( sig_try );
2119 mbedtls_free( verif );
2120
2121 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002122}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002124
2125/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 * Do an RSA operation to sign the message digest
2127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002129 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002130 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002133 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002134 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 unsigned char *sig )
2136{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002137 RSA_VALIDATE_RET( ctx != NULL );
2138 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2139 mode == MBEDTLS_RSA_PUBLIC );
2140 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2141 hashlen == 0 ) ||
2142 hash != NULL );
2143 RSA_VALIDATE_RET( sig != NULL );
2144
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 switch( ctx->padding )
2146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147#if defined(MBEDTLS_PKCS1_V15)
2148 case MBEDTLS_RSA_PKCS_V15:
2149 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002150 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002151#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153#if defined(MBEDTLS_PKCS1_V21)
2154 case MBEDTLS_RSA_PKCS_V21:
2155 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002156 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002157#endif
2158
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002162}
2163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002165/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002166 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002169 int (*f_rng)(void *, unsigned char *, size_t),
2170 void *p_rng,
2171 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002173 unsigned int hashlen,
2174 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002176 int expected_salt_len,
2177 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002178{
Paul Bakker23986e52011-04-24 08:57:21 +00002179 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002180 size_t siglen;
2181 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002182 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002184 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002185 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002186 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 const mbedtls_md_info_t *md_info;
2188 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002189 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002190
Hanno Beckerddeeed72018-12-13 18:07:00 +00002191 RSA_VALIDATE_RET( ctx != NULL );
2192 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2193 mode == MBEDTLS_RSA_PUBLIC );
2194 RSA_VALIDATE_RET( sig != NULL );
2195 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2196 hashlen == 0 ) ||
2197 hash != NULL );
2198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2200 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002201
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 siglen = ctx->len;
2203
Paul Bakker27fdf462011-06-09 13:55:13 +00002204 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2208 ? mbedtls_rsa_public( ctx, sig, buf )
2209 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002210
2211 if( ret != 0 )
2212 return( ret );
2213
2214 p = buf;
2215
Paul Bakkerb3869132013-02-28 17:21:01 +01002216 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 {
Simon Butcher02037452016-03-01 21:19:12 +00002221 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002223 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002227 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002230 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002234
Paul Bakkerb3869132013-02-28 17:21:01 +01002235 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002236
Simon Butcher02037452016-03-01 21:19:12 +00002237 /*
2238 * Note: EMSA-PSS verification is over the length of N - 1 bits
2239 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002240 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002241
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002242 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2243 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2244
Simon Butcher02037452016-03-01 21:19:12 +00002245 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002246 if( msb % 8 == 0 )
2247 {
2248 p++;
2249 siglen -= 1;
2250 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002251
Gilles Peskine139108a2017-10-18 19:03:42 +02002252 if( siglen < hlen + 2 )
2253 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2254 hash_start = p + siglen - hlen - 1;
2255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002257 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002258 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002259
Jaeden Amero66954e12018-01-25 16:05:54 +00002260 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2261 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002262 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002263
Paul Bakkerb3869132013-02-28 17:21:01 +01002264 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002265
Gilles Peskine6a54b022017-10-17 19:02:13 +02002266 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002267 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002268
Gilles Peskine91048a32017-10-19 17:46:14 +02002269 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002270 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002271 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2272 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002273 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002274
Gilles Peskine6a54b022017-10-17 19:02:13 +02002275 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002278 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002279 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002280 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2281 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002282 }
2283
Simon Butcher02037452016-03-01 21:19:12 +00002284 /*
2285 * Generate H = Hash( M' )
2286 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002287 ret = mbedtls_md_starts( &md_ctx );
2288 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002289 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002290 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2291 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002292 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002293 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2294 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002295 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002296 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2297 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002298 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002299 ret = mbedtls_md_finish( &md_ctx, result );
2300 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002301 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002302
Jaeden Amero66954e12018-01-25 16:05:54 +00002303 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002304 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002305 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002306 goto exit;
2307 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002308
2309exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002311
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002312 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002313}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002314
2315/*
2316 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002319 int (*f_rng)(void *, unsigned char *, size_t),
2320 void *p_rng,
2321 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002323 unsigned int hashlen,
2324 const unsigned char *hash,
2325 const unsigned char *sig )
2326{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002327 mbedtls_md_type_t mgf1_hash_id;
2328 RSA_VALIDATE_RET( ctx != NULL );
2329 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2330 mode == MBEDTLS_RSA_PUBLIC );
2331 RSA_VALIDATE_RET( sig != NULL );
2332 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2333 hashlen == 0 ) ||
2334 hash != NULL );
2335
2336 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002338 : md_alg;
2339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002341 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002343 sig ) );
2344
2345}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002349/*
2350 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002353 int (*f_rng)(void *, unsigned char *, size_t),
2354 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002355 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002357 unsigned int hashlen,
2358 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002359 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002360{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002361 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002362 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002363 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002364
Hanno Beckerddeeed72018-12-13 18:07:00 +00002365 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 sig_len = ctx->len;
2374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2376 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002377
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002378 /*
2379 * Prepare expected PKCS1 v1.5 encoding of hash.
2380 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002381
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002382 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2383 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2384 {
2385 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2386 goto cleanup;
2387 }
2388
2389 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2390 encoded_expected ) ) != 0 )
2391 goto cleanup;
2392
2393 /*
2394 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2395 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002398 ? mbedtls_rsa_public( ctx, sig, encoded )
2399 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002400 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002401 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002402
Simon Butcher02037452016-03-01 21:19:12 +00002403 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002404 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002405 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002406
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002407 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2408 sig_len ) ) != 0 )
2409 {
2410 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2411 goto cleanup;
2412 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002413
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002414cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002415
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002416 if( encoded != NULL )
2417 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002418 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002419 mbedtls_free( encoded );
2420 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002421
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002422 if( encoded_expected != NULL )
2423 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002424 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002425 mbedtls_free( encoded_expected );
2426 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002427
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002428 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002431
2432/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002433 * Do an RSA operation and check the message digest
2434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002436 int (*f_rng)(void *, unsigned char *, size_t),
2437 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002438 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002440 unsigned int hashlen,
2441 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002442 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002443{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002444 RSA_VALIDATE_RET( ctx != NULL );
2445 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2446 mode == MBEDTLS_RSA_PUBLIC );
2447 RSA_VALIDATE_RET( sig != NULL );
2448 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2449 hashlen == 0 ) ||
2450 hash != NULL );
2451
Paul Bakkerb3869132013-02-28 17:21:01 +01002452 switch( ctx->padding )
2453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#if defined(MBEDTLS_PKCS1_V15)
2455 case MBEDTLS_RSA_PKCS_V15:
2456 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002457 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002458#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460#if defined(MBEDTLS_PKCS1_V21)
2461 case MBEDTLS_RSA_PKCS_V21:
2462 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002463 hashlen, hash, sig );
2464#endif
2465
2466 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002468 }
2469}
2470
2471/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002472 * Copy the components of an RSA key
2473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002475{
2476 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002477 RSA_VALIDATE_RET( dst != NULL );
2478 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002479
2480 dst->ver = src->ver;
2481 dst->len = src->len;
2482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2484 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2487 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2488 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002489
2490#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2492 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2493 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2495 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002496#endif
2497
2498 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2501 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002502
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002503 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002504 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002505
2506cleanup:
2507 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002509
2510 return( ret );
2511}
2512
2513/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 * Free the components of an RSA key
2515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002517{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002518 if( ctx == NULL )
2519 return;
2520
irwir2239a862018-06-12 18:25:09 +03002521 mbedtls_mpi_free( &ctx->Vi );
2522 mbedtls_mpi_free( &ctx->Vf );
2523 mbedtls_mpi_free( &ctx->RN );
2524 mbedtls_mpi_free( &ctx->D );
2525 mbedtls_mpi_free( &ctx->Q );
2526 mbedtls_mpi_free( &ctx->P );
2527 mbedtls_mpi_free( &ctx->E );
2528 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002529
Hanno Becker33c30a02017-08-23 07:00:22 +01002530#if !defined(MBEDTLS_RSA_NO_CRT)
irwir2239a862018-06-12 18:25:09 +03002531 mbedtls_mpi_free( &ctx->RQ );
2532 mbedtls_mpi_free( &ctx->RP );
2533 mbedtls_mpi_free( &ctx->QP );
2534 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002535 mbedtls_mpi_free( &ctx->DP );
2536#endif /* MBEDTLS_RSA_NO_CRT */
2537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538#if defined(MBEDTLS_THREADING_C)
2539 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002540#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002541}
2542
Hanno Beckerab377312017-08-23 16:24:51 +01002543#endif /* !MBEDTLS_RSA_ALT */
2544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002547#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
2549/*
2550 * Example RSA-1024 keypair, for test purposes
2551 */
2552#define KEY_LEN 128
2553
2554#define RSA_N "9292758453063D803DD603D5E777D788" \
2555 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2556 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2557 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2558 "93A89813FBF3C4F8066D2D800F7C38A8" \
2559 "1AE31942917403FF4946B0A83D3D3E05" \
2560 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2561 "5E94BB77B07507233A0BC7BAC8F90F79"
2562
2563#define RSA_E "10001"
2564
2565#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2566 "66CA472BC44D253102F8B4A9D3BFA750" \
2567 "91386C0077937FE33FA3252D28855837" \
2568 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2569 "DF79C5CE07EE72C7F123142198164234" \
2570 "CABB724CF78B8173B9F880FC86322407" \
2571 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2572 "071513A1E85B5DFA031F21ECAE91A34D"
2573
2574#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2575 "2C01CAD19EA484A87EA4377637E75500" \
2576 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2577 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2578
2579#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2580 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2581 "910E4168387E3C30AA1E00C339A79508" \
2582 "8452DD96A9A5EA5D9DCA68DA636032AF"
2583
Paul Bakker5121ce52009-01-03 21:22:43 +00002584#define PT_LEN 24
2585#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2586 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002589static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002590{
gufe443fa7c642020-08-03 17:56:50 +02002591#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002592 size_t i;
2593
Paul Bakker545570e2010-07-18 09:00:25 +00002594 if( rng_state != NULL )
2595 rng_state = NULL;
2596
Paul Bakkera3d195c2011-11-27 21:07:34 +00002597 for( i = 0; i < len; ++i )
2598 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002599#else
2600 if( rng_state != NULL )
2601 rng_state = NULL;
2602
2603 arc4random_buf( output, len );
gufe443fa7c642020-08-03 17:56:50 +02002604#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002605
Paul Bakkera3d195c2011-11-27 21:07:34 +00002606 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002607}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002609
Paul Bakker5121ce52009-01-03 21:22:43 +00002610/*
2611 * Checkup routine
2612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002614{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002615 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002617 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 unsigned char rsa_plaintext[PT_LEN];
2620 unsigned char rsa_decrypted[PT_LEN];
2621 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002623 unsigned char sha1sum[20];
2624#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Hanno Becker3a701162017-08-22 13:52:43 +01002626 mbedtls_mpi K;
2627
2628 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
Hanno Becker3a701162017-08-22 13:52:43 +01002631 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2632 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2633 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2634 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2635 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2636 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2637 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2638 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2639 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2640 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2641
Hanno Becker7f25f852017-10-10 16:56:22 +01002642 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
2644 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2648 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 {
2650 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Hanno Becker5bc87292017-05-03 15:09:31 +01002653 ret = 1;
2654 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656
2657 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
2660 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2661
Hanno Becker98838b02017-10-02 13:16:10 +01002662 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2663 PT_LEN, rsa_plaintext,
2664 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 {
2666 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Hanno Becker5bc87292017-05-03 15:09:31 +01002669 ret = 1;
2670 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 }
2672
2673 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Hanno Becker98838b02017-10-02 13:16:10 +01002676 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2677 &len, rsa_ciphertext, rsa_decrypted,
2678 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 {
2680 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Hanno Becker5bc87292017-05-03 15:09:31 +01002683 ret = 1;
2684 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 }
2686
2687 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2688 {
2689 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Hanno Becker5bc87292017-05-03 15:09:31 +01002692 ret = 1;
2693 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 }
2695
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002696 if( verbose != 0 )
2697 mbedtls_printf( "passed\n" );
2698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002701 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002703 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002704 {
2705 if( verbose != 0 )
2706 mbedtls_printf( "failed\n" );
2707
2708 return( 1 );
2709 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Hanno Becker98838b02017-10-02 13:16:10 +01002711 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2712 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2713 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 {
2715 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Hanno Becker5bc87292017-05-03 15:09:31 +01002718 ret = 1;
2719 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 }
2721
2722 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
Hanno Becker98838b02017-10-02 13:16:10 +01002725 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2726 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2727 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 {
2729 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731
Hanno Becker5bc87292017-05-03 15:09:31 +01002732 ret = 1;
2733 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 }
2735
2736 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002737 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002740 if( verbose != 0 )
2741 mbedtls_printf( "\n" );
2742
Paul Bakker3d8fb632014-04-17 12:42:41 +02002743cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002744 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 mbedtls_rsa_free( &rsa );
2746#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002747 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002749 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002750}
2751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#endif /* MBEDTLS_RSA_C */