blob: 7c1a73ff0cfdc0d5fd108caf3b8dd572854b258f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
Hanno Becker74716312017-10-02 10:00:37 +010048
Paul Bakker5121ce52009-01-03 21:22:43 +000049/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000050 * The following sources were referenced in the design of this implementation
51 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000052 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000053 * [1] A method for obtaining digital signatures and public-key cryptosystems
54 * R Rivest, A Shamir, and L Adleman
55 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
56 *
57 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
58 * Menezes, van Oorschot and Vanstone
59 *
Janos Follathe81102e2017-03-22 13:38:28 +000060 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
61 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
62 * Stefan Mangard
63 * https://arxiv.org/abs/1702.08719v2
64 *
Paul Bakker5121ce52009-01-03 21:22:43 +000065 */
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020069#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010076#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050078#include "mbedtls/platform_util.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000079
Rich Evans00ab4702015-02-06 13:43:58 +000080#include <string.h>
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000083#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000084#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000087#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000088#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000091#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010092#else
Rich Evans00ab4702015-02-06 13:43:58 +000093#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020095#define mbedtls_calloc calloc
96#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010097#endif
98
Hanno Beckera565f542017-10-11 11:00:19 +010099#if !defined(MBEDTLS_RSA_ALT)
100
Hanno Beckerddeeed72018-12-13 18:07:00 +0000101/* Parameter validation macros */
102#define RSA_VALIDATE_RET( cond ) \
103 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
104#define RSA_VALIDATE( cond ) \
105 MBEDTLS_INTERNAL_VALIDATE( cond )
106
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100107#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +0100108/* constant-time buffer comparison */
109static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
110{
111 size_t i;
112 const unsigned char *A = (const unsigned char *) a;
113 const unsigned char *B = (const unsigned char *) b;
114 unsigned char diff = 0;
115
116 for( i = 0; i < n; i++ )
117 diff |= A[i] ^ B[i];
118
119 return( diff );
120}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100121#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +0100122
Hanno Becker617c1ae2017-08-23 14:11:24 +0100123int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
124 const mbedtls_mpi *N,
125 const mbedtls_mpi *P, const mbedtls_mpi *Q,
126 const mbedtls_mpi *D, const mbedtls_mpi *E )
127{
128 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000129 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100130
131 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
132 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
133 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
134 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
135 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
136 {
137 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
138 }
139
140 if( N != NULL )
141 ctx->len = mbedtls_mpi_size( &ctx->N );
142
143 return( 0 );
144}
145
146int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100147 unsigned char const *N, size_t N_len,
148 unsigned char const *P, size_t P_len,
149 unsigned char const *Q, size_t Q_len,
150 unsigned char const *D, size_t D_len,
151 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100152{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000153 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000154 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100155
156 if( N != NULL )
157 {
158 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
159 ctx->len = mbedtls_mpi_size( &ctx->N );
160 }
161
162 if( P != NULL )
163 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
164
165 if( Q != NULL )
166 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
167
168 if( D != NULL )
169 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
170
171 if( E != NULL )
172 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
173
174cleanup:
175
176 if( ret != 0 )
177 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
178
179 return( 0 );
180}
181
Hanno Becker705fc682017-10-10 17:57:02 +0100182/*
183 * Checks whether the context fields are set in such a way
184 * that the RSA primitives will be able to execute without error.
185 * It does *not* make guarantees for consistency of the parameters.
186 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100187static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
188 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100189{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100190#if !defined(MBEDTLS_RSA_NO_CRT)
191 /* blinding_needed is only used for NO_CRT to decide whether
192 * P,Q need to be present or not. */
193 ((void) blinding_needed);
194#endif
195
Hanno Becker3a760a12018-01-05 08:14:49 +0000196 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
197 ctx->len > MBEDTLS_MPI_MAX_SIZE )
198 {
Hanno Becker705fc682017-10-10 17:57:02 +0100199 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000200 }
Hanno Becker705fc682017-10-10 17:57:02 +0100201
202 /*
203 * 1. Modular exponentiation needs positive, odd moduli.
204 */
205
206 /* Modular exponentiation wrt. N is always used for
207 * RSA public key operations. */
208 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
209 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
210 {
211 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
212 }
213
214#if !defined(MBEDTLS_RSA_NO_CRT)
215 /* Modular exponentiation for P and Q is only
216 * used for private key operations and if CRT
217 * is used. */
218 if( is_priv &&
219 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
220 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
221 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
222 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
223 {
224 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
225 }
226#endif /* !MBEDTLS_RSA_NO_CRT */
227
228 /*
229 * 2. Exponents must be positive
230 */
231
232 /* Always need E for public key operations */
233 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
234 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
235
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100236#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100237 /* For private key operations, use D or DP & DQ
238 * as (unblinded) exponents. */
239 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
240 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
241#else
242 if( is_priv &&
243 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
244 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
245 {
246 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
247 }
248#endif /* MBEDTLS_RSA_NO_CRT */
249
250 /* Blinding shouldn't make exponents negative either,
251 * so check that P, Q >= 1 if that hasn't yet been
252 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100253#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100254 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100255 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
256 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
257 {
258 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
259 }
260#endif
261
262 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100263 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100264#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100265 if( is_priv &&
266 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
267 {
268 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
269 }
270#endif
271
272 return( 0 );
273}
274
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100275int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100276{
277 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000278 int have_N, have_P, have_Q, have_D, have_E;
Jack Lloydb10fd062020-01-29 13:09:55 -0500279#if !defined(MBEDTLS_RSA_NO_CRT)
280 int have_DP, have_DQ, have_QP;
281#endif
Hanno Beckerddeeed72018-12-13 18:07:00 +0000282 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100283
Hanno Beckerddeeed72018-12-13 18:07:00 +0000284 RSA_VALIDATE_RET( ctx != NULL );
285
286 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
287 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
288 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
289 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
290 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100291
Jack Lloydb10fd062020-01-29 13:09:55 -0500292#if !defined(MBEDTLS_RSA_NO_CRT)
293 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
294 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
295 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
296#endif
297
Hanno Becker617c1ae2017-08-23 14:11:24 +0100298 /*
299 * Check whether provided parameters are enough
300 * to deduce all others. The following incomplete
301 * parameter sets for private keys are supported:
302 *
303 * (1) P, Q missing.
304 * (2) D and potentially N missing.
305 *
306 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100307
Hanno Beckerddeeed72018-12-13 18:07:00 +0000308 n_missing = have_P && have_Q && have_D && have_E;
309 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
310 d_missing = have_P && have_Q && !have_D && have_E;
311 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100312
313 /* These three alternatives are mutually exclusive */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000314 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100315
Hanno Becker617c1ae2017-08-23 14:11:24 +0100316 if( !is_priv && !is_pub )
317 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
318
319 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100320 * Step 1: Deduce N if P, Q are provided.
321 */
322
323 if( !have_N && have_P && have_Q )
324 {
325 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
326 &ctx->Q ) ) != 0 )
327 {
328 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
329 }
330
331 ctx->len = mbedtls_mpi_size( &ctx->N );
332 }
333
334 /*
335 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100336 */
337
338 if( pq_missing )
339 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100340 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100341 &ctx->P, &ctx->Q );
342 if( ret != 0 )
343 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
344
345 }
346 else if( d_missing )
347 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100348 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
349 &ctx->Q,
350 &ctx->E,
351 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100352 {
353 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
354 }
355 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100356
Hanno Becker617c1ae2017-08-23 14:11:24 +0100357 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100358 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100359 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100360 */
361
Hanno Becker23344b52017-08-23 07:43:27 +0100362#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloydb10fd062020-01-29 13:09:55 -0500363 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100364 {
365 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
366 &ctx->DP, &ctx->DQ, &ctx->QP );
367 if( ret != 0 )
368 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
369 }
Hanno Becker23344b52017-08-23 07:43:27 +0100370#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100371
372 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100373 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100374 */
375
Hanno Beckerebd2c022017-10-12 10:54:53 +0100376 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100377}
378
Hanno Becker617c1ae2017-08-23 14:11:24 +0100379int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
380 unsigned char *N, size_t N_len,
381 unsigned char *P, size_t P_len,
382 unsigned char *Q, size_t Q_len,
383 unsigned char *D, size_t D_len,
384 unsigned char *E, size_t E_len )
385{
386 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000387 int is_priv;
388 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100389
390 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000391 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100392 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
393 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
394 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
395 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
396 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
397
398 if( !is_priv )
399 {
400 /* If we're trying to export private parameters for a public key,
401 * something must be wrong. */
402 if( P != NULL || Q != NULL || D != NULL )
403 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
404
405 }
406
407 if( N != NULL )
408 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
409
410 if( P != NULL )
411 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
412
413 if( Q != NULL )
414 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
415
416 if( D != NULL )
417 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
418
419 if( E != NULL )
420 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100421
422cleanup:
423
424 return( ret );
425}
426
Hanno Becker617c1ae2017-08-23 14:11:24 +0100427int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
428 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
429 mbedtls_mpi *D, mbedtls_mpi *E )
430{
431 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000432 int is_priv;
433 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100434
435 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000436 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100437 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
438 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
439 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
440 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
441 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
442
443 if( !is_priv )
444 {
445 /* If we're trying to export private parameters for a public key,
446 * something must be wrong. */
447 if( P != NULL || Q != NULL || D != NULL )
448 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
449
450 }
451
452 /* Export all requested core parameters. */
453
454 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
455 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
456 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
457 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
458 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
459 {
460 return( ret );
461 }
462
463 return( 0 );
464}
465
466/*
467 * Export CRT parameters
468 * This must also be implemented if CRT is not used, for being able to
469 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
470 * can be used in this case.
471 */
472int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
473 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
474{
475 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000476 int is_priv;
477 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100478
479 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000480 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100481 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
482 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
483 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
484 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
485 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
486
487 if( !is_priv )
488 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
489
Hanno Beckerdc95c892017-08-23 06:57:02 +0100490#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100491 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100492 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
493 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
494 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
495 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100496 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100497 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100498#else
499 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
500 DP, DQ, QP ) ) != 0 )
501 {
502 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
503 }
504#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100505
506 return( 0 );
507}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100508
Paul Bakker5121ce52009-01-03 21:22:43 +0000509/*
510 * Initialize an RSA context
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000514 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000515{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000516 RSA_VALIDATE( ctx != NULL );
517 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
518 padding == MBEDTLS_RSA_PKCS_V21 );
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_THREADING_C)
525 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200526#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000527}
528
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100529/*
530 * Set padding for an existing RSA context
531 */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000532void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
533 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100534{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000535 RSA_VALIDATE( ctx != NULL );
536 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
537 padding == MBEDTLS_RSA_PKCS_V21 );
538
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100539 ctx->padding = padding;
540 ctx->hash_id = hash_id;
541}
542
Hanno Becker617c1ae2017-08-23 14:11:24 +0100543/*
544 * Get length in bytes of RSA modulus
545 */
546
547size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
548{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100549 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100550}
551
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555/*
556 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800557 *
558 * This generation method follows the RSA key pair generation procedure of
559 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000562 int (*f_rng)(void *, unsigned char *, size_t),
563 void *p_rng,
564 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000565{
566 int ret;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800567 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100568 int prime_quality = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000569 RSA_VALIDATE_RET( ctx != NULL );
570 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Hanno Beckerddeeed72018-12-13 18:07:00 +0000572 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
Janos Follathef441782016-09-21 13:18:12 +0100573 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
574
Janos Follathb8fc1b02018-09-03 15:37:01 +0100575 /*
576 * If the modulus is 1024 bit long or shorter, then the security strength of
577 * the RSA algorithm is less than or equal to 80 bits and therefore an error
578 * rate of 2^-80 is sufficient.
579 */
580 if( nbits > 1024 )
581 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
582
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100583 mbedtls_mpi_init( &H );
584 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800585 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587 /*
588 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800589 * 1. |P-Q| > 2^( nbits / 2 - 100 )
590 * 2. GCD( E, (P-1)*(Q-1) ) == 1
591 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595 do
596 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100597 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
598 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
Janos Follathb8fc1b02018-09-03 15:37:01 +0100600 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
601 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800603 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
604 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
605 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 continue;
607
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800608 /* not required by any standards, but some users rely on the fact that P > Q */
609 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100610 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100611
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100612 /* Temporarily replace P,Q by P-1, Q-1 */
613 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
614 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
615 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800616
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800617 /* 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 +0200618 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800619 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
620 continue;
621
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800622 /* 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 -0800623 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
624 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
625 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
626
627 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
628 continue;
629
630 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800632 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100634 /* Restore P,Q */
635 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
636 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
637
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800638 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
639
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100640 ctx->len = mbedtls_mpi_size( &ctx->N );
641
Jethro Beekman97f95c92018-02-13 15:50:36 -0800642#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 * DP = D mod (P - 1)
645 * DQ = D mod (Q - 1)
646 * QP = Q^-1 mod P
647 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100648 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
649 &ctx->DP, &ctx->DQ, &ctx->QP ) );
650#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Hanno Becker83aad1f2017-08-23 06:45:10 +0100652 /* Double-check */
653 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
655cleanup:
656
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100657 mbedtls_mpi_free( &H );
658 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800659 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
661 if( ret != 0 )
662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_rsa_free( ctx );
664 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 }
666
Paul Bakker48377d92013-08-30 12:06:24 +0200667 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668}
669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672/*
673 * Check a public RSA key
674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000676{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000677 RSA_VALIDATE_RET( ctx != NULL );
678
Hanno Beckerebd2c022017-10-12 10:54:53 +0100679 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000681
Hanno Becker3a760a12018-01-05 08:14:49 +0000682 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100685 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000686
Hanno Becker705fc682017-10-10 17:57:02 +0100687 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
688 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100692 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
694 return( 0 );
695}
696
697/*
Hanno Becker705fc682017-10-10 17:57:02 +0100698 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000701{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000702 RSA_VALIDATE_RET( ctx != NULL );
703
Hanno Becker705fc682017-10-10 17:57:02 +0100704 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100705 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 {
Hanno Becker98838b02017-10-02 13:16:10 +0100707 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 }
Paul Bakker48377d92013-08-30 12:06:24 +0200709
Hanno Becker98838b02017-10-02 13:16:10 +0100710 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100711 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100713 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000715
Hanno Beckerb269a852017-08-25 08:03:21 +0100716#if !defined(MBEDTLS_RSA_NO_CRT)
717 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
718 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
719 {
720 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
721 }
722#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000723
724 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000725}
726
727/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100728 * Check if contexts holding a public and private key match
729 */
Hanno Becker98838b02017-10-02 13:16:10 +0100730int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
731 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100732{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000733 RSA_VALIDATE_RET( pub != NULL );
734 RSA_VALIDATE_RET( prv != NULL );
735
Hanno Becker98838b02017-10-02 13:16:10 +0100736 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100740 }
741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
743 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100746 }
747
748 return( 0 );
749}
750
751/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000752 * Do an RSA public key operation
753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000755 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 unsigned char *output )
757{
Paul Bakker23986e52011-04-24 08:57:21 +0000758 int ret;
759 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_mpi T;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000761 RSA_VALIDATE_RET( ctx != NULL );
762 RSA_VALIDATE_RET( input != NULL );
763 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000764
Hanno Beckerebd2c022017-10-12 10:54:53 +0100765 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100766 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200770#if defined(MBEDTLS_THREADING_C)
771 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
772 return( ret );
773#endif
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000778 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200779 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
780 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000781 }
782
783 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
785 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
787cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200789 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
790 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100791#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
795 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
798 return( 0 );
799}
800
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200801/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200802 * Generate or update blinding values, see section 10 of:
803 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200804 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200805 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200806 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200807static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200808 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
809{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200810 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200811
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200812 if( ctx->Vf.p != NULL )
813 {
814 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
816 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
817 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
818 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200819
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200820 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200821 }
822
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200823 /* Unblinding value: Vf = random number, invertible mod N */
824 do {
825 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
Manuel Pégourié-Gonnard86ad5be2020-06-26 11:03:19 +0200829
830 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N );
831 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
832 goto cleanup;
833
834 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200835
836 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 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 +0200838
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200839
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200840cleanup:
841 return( ret );
842}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200843
Paul Bakker5121ce52009-01-03 21:22:43 +0000844/*
Janos Follathe81102e2017-03-22 13:38:28 +0000845 * Exponent blinding supposed to prevent side-channel attacks using multiple
846 * traces of measurements to recover the RSA key. The more collisions are there,
847 * the more bits of the key can be recovered. See [3].
848 *
849 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
850 * observations on avarage.
851 *
852 * For example with 28 byte blinding to achieve 2 collisions the adversary has
853 * to make 2^112 observations on avarage.
854 *
855 * (With the currently (as of 2017 April) known best algorithms breaking 2048
856 * bit RSA requires approximately as much time as trying out 2^112 random keys.
857 * Thus in this sense with 28 byte blinding the security is not reduced by
858 * side-channel attacks like the one in [3])
859 *
860 * This countermeasure does not help if the key recovery is possible with a
861 * single trace.
862 */
863#define RSA_EXPONENT_BLINDING 28
864
865/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000866 * Do an RSA private key operation
867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200869 int (*f_rng)(void *, unsigned char *, size_t),
870 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000871 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 unsigned char *output )
873{
Paul Bakker23986e52011-04-24 08:57:21 +0000874 int ret;
875 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100876
877 /* Temporary holding the result */
878 mbedtls_mpi T;
879
880 /* Temporaries holding P-1, Q-1 and the
881 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000882 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100883
884#if !defined(MBEDTLS_RSA_NO_CRT)
885 /* Temporaries holding the results mod p resp. mod q. */
886 mbedtls_mpi TP, TQ;
887
888 /* Temporaries holding the blinded exponents for
889 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000890 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100891
892 /* Pointers to actual exponents to be used - either the unblinded
893 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000894 mbedtls_mpi *DP = &ctx->DP;
895 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100896#else
897 /* Temporary holding the blinded exponent (if used). */
898 mbedtls_mpi D_blind;
899
900 /* Pointer to actual exponent to be used - either the unblinded
901 * or the blinded one, depending on the presence of a PRNG. */
902 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100903#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100904
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100905 /* Temporaries holding the initial input and the double
906 * checked result; should be the same in the end. */
907 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Hanno Beckerddeeed72018-12-13 18:07:00 +0000909 RSA_VALIDATE_RET( ctx != NULL );
910 RSA_VALIDATE_RET( input != NULL );
911 RSA_VALIDATE_RET( output != NULL );
912
Hanno Beckerebd2c022017-10-12 10:54:53 +0100913 if( rsa_check_context( ctx, 1 /* private key checks */,
914 f_rng != NULL /* blinding y/n */ ) != 0 )
915 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100916 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100917 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100918
Hanno Becker06811ce2017-05-03 15:10:34 +0100919#if defined(MBEDTLS_THREADING_C)
920 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
921 return( ret );
922#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000923
Hanno Becker06811ce2017-05-03 15:10:34 +0100924 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100925 mbedtls_mpi_init( &T );
926
927 mbedtls_mpi_init( &P1 );
928 mbedtls_mpi_init( &Q1 );
929 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000930
Janos Follathf9203b42017-03-22 15:13:15 +0000931 if( f_rng != NULL )
932 {
Janos Follathe81102e2017-03-22 13:38:28 +0000933#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000934 mbedtls_mpi_init( &D_blind );
935#else
936 mbedtls_mpi_init( &DP_blind );
937 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000938#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000939 }
Janos Follathe81102e2017-03-22 13:38:28 +0000940
Hanno Becker06811ce2017-05-03 15:10:34 +0100941#if !defined(MBEDTLS_RSA_NO_CRT)
942 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200943#endif
944
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100945 mbedtls_mpi_init( &I );
946 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100947
948 /* End of MPI initialization */
949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
951 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200953 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
954 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 }
956
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100957 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100958
Paul Bakkerf451bac2013-08-30 15:37:02 +0200959 if( f_rng != NULL )
960 {
961 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200962 * Blinding
963 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200964 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200965 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
966 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000968
Janos Follathe81102e2017-03-22 13:38:28 +0000969 /*
970 * Exponent blinding
971 */
972 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
973 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
974
Janos Follathf9203b42017-03-22 15:13:15 +0000975#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000976 /*
977 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
978 */
979 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
980 f_rng, p_rng ) );
981 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
982 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
983 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
984
985 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000986#else
987 /*
988 * DP_blind = ( P - 1 ) * R + DP
989 */
990 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
991 f_rng, p_rng ) );
992 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
993 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
994 &ctx->DP ) );
995
996 DP = &DP_blind;
997
998 /*
999 * DQ_blind = ( Q - 1 ) * R + DQ
1000 */
1001 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1002 f_rng, p_rng ) );
1003 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1004 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1005 &ctx->DQ ) );
1006
1007 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001008#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001009 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001012 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001013#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001014 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001015 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001016 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001017 * TP = input ^ dP mod P
1018 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001020
1021 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1022 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001023
1024 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001025 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001027 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1028 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1029 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
1031 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001032 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001034 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1035 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001037
Paul Bakkerf451bac2013-08-30 15:37:02 +02001038 if( f_rng != NULL )
1039 {
1040 /*
1041 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001042 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001043 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001044 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
Hanno Becker2dec5e82017-10-03 07:49:52 +01001048 /* Verify the result to prevent glitching attacks. */
1049 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1050 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001051 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001052 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001053 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1054 goto cleanup;
1055 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001056
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
1060cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001062 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1063 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001064#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001065
Hanno Becker06811ce2017-05-03 15:10:34 +01001066 mbedtls_mpi_free( &P1 );
1067 mbedtls_mpi_free( &Q1 );
1068 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001069
1070 if( f_rng != NULL )
1071 {
Janos Follathe81102e2017-03-22 13:38:28 +00001072#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001073 mbedtls_mpi_free( &D_blind );
1074#else
1075 mbedtls_mpi_free( &DP_blind );
1076 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001077#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001078 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Hanno Becker06811ce2017-05-03 15:10:34 +01001080 mbedtls_mpi_free( &T );
1081
1082#if !defined(MBEDTLS_RSA_NO_CRT)
1083 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1084#endif
1085
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001086 mbedtls_mpi_free( &C );
1087 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001088
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
1092 return( 0 );
1093}
1094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001096/**
1097 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1098 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001099 * \param dst buffer to mask
1100 * \param dlen length of destination buffer
1101 * \param src source of the mask generation
1102 * \param slen length of the source buffer
1103 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001105static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001109 unsigned char counter[4];
1110 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001111 unsigned int hlen;
1112 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001113 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001116 memset( counter, 0, 4 );
1117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001119
Simon Butcher02037452016-03-01 21:19:12 +00001120 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001121 p = dst;
1122
1123 while( dlen > 0 )
1124 {
1125 use_len = hlen;
1126 if( dlen < hlen )
1127 use_len = dlen;
1128
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001129 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1130 goto exit;
1131 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1132 goto exit;
1133 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1134 goto exit;
1135 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1136 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001137
1138 for( i = 0; i < use_len; ++i )
1139 *p++ ^= mask[i];
1140
1141 counter[3]++;
1142
1143 dlen -= use_len;
1144 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001145
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001146exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001147 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001148
1149 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001150}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001154/*
1155 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 int (*f_rng)(void *, unsigned char *, size_t),
1159 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001160 int mode,
1161 const unsigned char *label, size_t label_len,
1162 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001163 const unsigned char *input,
1164 unsigned char *output )
1165{
1166 size_t olen;
1167 int ret;
1168 unsigned char *p = output;
1169 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 const mbedtls_md_info_t *md_info;
1171 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001172
Hanno Beckerddeeed72018-12-13 18:07:00 +00001173 RSA_VALIDATE_RET( ctx != NULL );
1174 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1175 mode == MBEDTLS_RSA_PUBLIC );
1176 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001177 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001178 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1181 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001182
1183 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001187 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001189
1190 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001192
Simon Butcher02037452016-03-01 21:19:12 +00001193 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001194 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001196
1197 memset( output, 0, olen );
1198
1199 *p++ = 0;
1200
Simon Butcher02037452016-03-01 21:19:12 +00001201 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001202 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001204
1205 p += hlen;
1206
Simon Butcher02037452016-03-01 21:19:12 +00001207 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001208 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1209 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001210 p += hlen;
1211 p += olen - 2 * hlen - 2 - ilen;
1212 *p++ = 1;
1213 memcpy( p, input, ilen );
1214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001216 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001217 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001218
Simon Butcher02037452016-03-01 21:19:12 +00001219 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001220 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1221 &md_ctx ) ) != 0 )
1222 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001223
Simon Butcher02037452016-03-01 21:19:12 +00001224 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001225 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1226 &md_ctx ) ) != 0 )
1227 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001228
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001229exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001231
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001232 if( ret != 0 )
1233 return( ret );
1234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 return( ( mode == MBEDTLS_RSA_PUBLIC )
1236 ? mbedtls_rsa_public( ctx, output, output )
1237 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001242/*
1243 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001246 int (*f_rng)(void *, unsigned char *, size_t),
1247 void *p_rng,
1248 int mode, size_t ilen,
1249 const unsigned char *input,
1250 unsigned char *output )
1251{
1252 size_t nb_pad, olen;
1253 int ret;
1254 unsigned char *p = output;
1255
Hanno Beckerddeeed72018-12-13 18:07:00 +00001256 RSA_VALIDATE_RET( ctx != NULL );
1257 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1258 mode == MBEDTLS_RSA_PUBLIC );
1259 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001260 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1263 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001264
Paul Bakkerb3869132013-02-28 17:21:01 +01001265 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001266
Simon Butcher02037452016-03-01 21:19:12 +00001267 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001268 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001270
1271 nb_pad = olen - 3 - ilen;
1272
1273 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001275 {
Hanno Beckerb86e6842018-12-18 14:46:04 +00001276 if( f_rng == NULL )
1277 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001280
1281 while( nb_pad-- > 0 )
1282 {
1283 int rng_dl = 100;
1284
1285 do {
1286 ret = f_rng( p_rng, p, 1 );
1287 } while( *p == 0 && --rng_dl && ret == 0 );
1288
Simon Butcher02037452016-03-01 21:19:12 +00001289 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001290 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001292
1293 p++;
1294 }
1295 }
1296 else
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001299
1300 while( nb_pad-- > 0 )
1301 *p++ = 0xFF;
1302 }
1303
1304 *p++ = 0;
1305 memcpy( p, input, ilen );
1306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 return( ( mode == MBEDTLS_RSA_PUBLIC )
1308 ? mbedtls_rsa_public( ctx, output, output )
1309 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001310}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001312
Paul Bakker5121ce52009-01-03 21:22:43 +00001313/*
1314 * Add the message padding, then do an RSA operation
1315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001317 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001318 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001319 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001320 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 unsigned char *output )
1322{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001323 RSA_VALIDATE_RET( ctx != NULL );
1324 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1325 mode == MBEDTLS_RSA_PUBLIC );
1326 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001327 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001328
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 switch( ctx->padding )
1330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_PKCS1_V15)
1332 case MBEDTLS_RSA_PKCS_V15:
1333 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001334 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001335#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#if defined(MBEDTLS_PKCS1_V21)
1338 case MBEDTLS_RSA_PKCS_V21:
1339 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001340 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001341#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
1343 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001346}
1347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001349/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001350 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001353 int (*f_rng)(void *, unsigned char *, size_t),
1354 void *p_rng,
1355 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001356 const unsigned char *label, size_t label_len,
1357 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001358 const unsigned char *input,
1359 unsigned char *output,
1360 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001361{
Paul Bakker23986e52011-04-24 08:57:21 +00001362 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001363 size_t ilen, i, pad_len;
1364 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1366 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001367 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 const mbedtls_md_info_t *md_info;
1369 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001370
Hanno Beckerddeeed72018-12-13 18:07:00 +00001371 RSA_VALIDATE_RET( ctx != NULL );
1372 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1373 mode == MBEDTLS_RSA_PUBLIC );
1374 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1375 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1376 RSA_VALIDATE_RET( input != NULL );
1377 RSA_VALIDATE_RET( olen != NULL );
1378
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001379 /*
1380 * Parameters sanity checks
1381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1383 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
1385 ilen = ctx->len;
1386
Paul Bakker27fdf462011-06-09 13:55:13 +00001387 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001391 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001393
Janos Follathc17cda12016-02-11 11:08:18 +00001394 hlen = mbedtls_md_get_size( md_info );
1395
1396 // checking for integer underflow
1397 if( 2 * hlen + 2 > ilen )
1398 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1399
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001400 /*
1401 * RSA operation
1402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1404 ? mbedtls_rsa_public( ctx, input, buf )
1405 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
1407 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001408 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001410 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001411 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001414 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1415 {
1416 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001417 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001418 }
1419
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001420 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001421 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1422 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001423 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001424 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1425 &md_ctx ) ) != 0 )
1426 {
1427 mbedtls_md_free( &md_ctx );
1428 goto cleanup;
1429 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001432
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001433 /* Generate lHash */
1434 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1435 goto cleanup;
1436
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001437 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001438 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001439 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001441 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001443 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001444
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001445 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001446
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001447 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001448 for( i = 0; i < hlen; i++ )
1449 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001450
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001451 /* Get zero-padding len, but always read till end of buffer
1452 * (minus one, for the 01 byte) */
1453 pad_len = 0;
1454 pad_done = 0;
1455 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1456 {
1457 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001458 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001459 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001460
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001461 p += pad_len;
1462 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001463
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001464 /*
1465 * The only information "leaked" is whether the padding was correct or not
1466 * (eg, no data is copied if it was not correct). This meets the
1467 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1468 * the different error conditions.
1469 */
1470 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001471 {
1472 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1473 goto cleanup;
1474 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001475
Paul Bakker66d5d072014-06-17 16:39:18 +02001476 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001477 {
1478 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1479 goto cleanup;
1480 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001481
1482 *olen = ilen - (p - buf);
1483 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001484 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001485
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001486cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001487 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1488 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001489
1490 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001495/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1496 *
1497 * \param value The value to analyze.
Gilles Peskine331d80e2018-10-04 18:32:29 +02001498 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001499 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001500static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001501{
1502 /* MSVC has a warning about unary minus on unsigned, but this is
1503 * well-defined and precisely what we want to do here */
1504#if defined(_MSC_VER)
1505#pragma warning( push )
1506#pragma warning( disable : 4146 )
1507#endif
1508 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1509#if defined(_MSC_VER)
1510#pragma warning( pop )
1511#endif
1512}
1513
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001514/** Check whether a size is out of bounds, without branches.
1515 *
1516 * This is equivalent to `size > max`, but is likely to be compiled to
1517 * to code using bitwise operation rather than a branch.
1518 *
1519 * \param size Size to check.
1520 * \param max Maximum desired value for \p size.
1521 * \return \c 0 if `size <= max`.
1522 * \return \c 1 if `size > max`.
1523 */
1524static unsigned size_greater_than( size_t size, size_t max )
1525{
1526 /* Return the sign bit (1 for negative) of (max - size). */
1527 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1528}
1529
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001530/** Choose between two integer values, without branches.
1531 *
Gilles Peskine331d80e2018-10-04 18:32:29 +02001532 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1533 * to code using bitwise operation rather than a branch.
1534 *
1535 * \param cond Condition to test.
1536 * \param if1 Value to use if \p cond is nonzero.
1537 * \param if0 Value to use if \p cond is zero.
1538 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001539 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001540static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001541{
Gilles Peskine331d80e2018-10-04 18:32:29 +02001542 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001543 return( ( mask & if1 ) | (~mask & if0 ) );
1544}
1545
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001546/** Shift some data towards the left inside a buffer without leaking
1547 * the length of the data through side channels.
1548 *
1549 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1550 * ```
1551 * memmove(start, start + offset, total - offset);
1552 * memset(start + offset, 0, total - offset);
1553 * ```
1554 * but it strives to use a memory access pattern (and thus total timing)
1555 * that does not depend on \p offset. This timing independence comes at
1556 * the expense of performance.
1557 *
1558 * \param start Pointer to the start of the buffer.
1559 * \param total Total size of the buffer.
1560 * \param offset Offset from which to copy \p total - \p offset bytes.
1561 */
1562static void mem_move_to_left( void *start,
1563 size_t total,
1564 size_t offset )
1565{
1566 volatile unsigned char *buf = start;
1567 size_t i, n;
1568 if( total == 0 )
1569 return;
1570 for( i = 0; i < total; i++ )
1571 {
1572 unsigned no_op = size_greater_than( total - offset, i );
1573 /* The first `total - offset` passes are a no-op. The last
1574 * `offset` passes shift the data one byte to the left and
1575 * zero out the last byte. */
1576 for( n = 0; n < total - 1; n++ )
Gilles Peskine9b430702018-10-12 19:15:34 +02001577 {
1578 unsigned char current = buf[n];
1579 unsigned char next = buf[n+1];
1580 buf[n] = if_int( no_op, current, next );
1581 }
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001582 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1583 }
1584}
1585
Paul Bakkerb3869132013-02-28 17:21:01 +01001586/*
1587 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001590 int (*f_rng)(void *, unsigned char *, size_t),
1591 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001592 int mode, size_t *olen,
1593 const unsigned char *input,
1594 unsigned char *output,
Gilles Peskine5908dd42018-10-02 22:43:06 +02001595 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001596{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001597 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001598 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine85a74422018-10-05 14:50:21 +02001600 /* The following variables take sensitive values: their value must
1601 * not leak into the observable behavior of the function other than
1602 * the designated outputs (output, olen, return value). Otherwise
1603 * this would open the execution of the function to
1604 * side-channel-based variants of the Bleichenbacher padding oracle
1605 * attack. Potential side channels include overall timing, memory
1606 * access patterns (especially visible to an adversary who has access
1607 * to a shared memory cache), and branches (especially visible to
1608 * an adversary who has access to a shared code cache or to a shared
1609 * branch predictor). */
1610 size_t pad_count = 0;
1611 unsigned bad = 0;
1612 unsigned char pad_done = 0;
1613 size_t plaintext_size = 0;
1614 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001615
Hanno Beckerddeeed72018-12-13 18:07:00 +00001616 RSA_VALIDATE_RET( ctx != NULL );
1617 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1618 mode == MBEDTLS_RSA_PUBLIC );
1619 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1620 RSA_VALIDATE_RET( input != NULL );
1621 RSA_VALIDATE_RET( olen != NULL );
1622
1623 ilen = ctx->len;
1624 plaintext_max_size = ( output_max_len > ilen - 11 ?
1625 ilen - 11 :
1626 output_max_len );
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1629 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001630
Paul Bakkerb3869132013-02-28 17:21:01 +01001631 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1635 ? mbedtls_rsa_public( ctx, input, buf )
1636 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001637
1638 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001639 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001640
Gilles Peskine40b57f42018-10-05 15:06:12 +02001641 /* Check and get padding length in constant time and constant
1642 * memory trace. The first byte must be 0. */
1643 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001647 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1648 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001649 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001651 /* Read the whole buffer. Set pad_done to nonzero if we find
1652 * the 0x00 byte and remember the padding length in pad_count. */
1653 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001654 {
Gilles Peskine85a74422018-10-05 14:50:21 +02001655 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001656 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001657 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001658 }
1659 else
1660 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001661 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1662 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001663 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001664
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001665 /* Read the whole buffer. Set pad_done to nonzero if we find
1666 * the 0x00 byte and remember the padding length in pad_count.
1667 * If there's a non-0xff byte in the padding, the padding is bad. */
1668 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001669 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001670 pad_done |= if_int( buf[i], 0, 1 );
1671 pad_count += if_int( pad_done, 0, 1 );
1672 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001673 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001674 }
1675
Gilles Peskine40b57f42018-10-05 15:06:12 +02001676 /* If pad_done is still zero, there's no data, only unfinished padding. */
1677 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001678
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001679 /* There must be at least 8 bytes of padding. */
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001680 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001681
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001682 /* If the padding is valid, set plaintext_size to the number of
1683 * remaining bytes after stripping the padding. If the padding
1684 * is invalid, avoid leaking this fact through the size of the
1685 * output: use the maximum message size that fits in the output
1686 * buffer. Do it without branches to avoid leaking the padding
1687 * validity through timing. RSA keys are small enough that all the
1688 * size_t values involved fit in unsigned int. */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001689 plaintext_size = if_int( bad,
1690 (unsigned) plaintext_max_size,
Gilles Peskine85a74422018-10-05 14:50:21 +02001691 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001692
Gilles Peskine9265ff42018-10-04 19:13:43 +02001693 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001694 * buffer and to 1 otherwise. */
1695 output_too_large = size_greater_than( plaintext_size,
1696 plaintext_max_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Gilles Peskine9265ff42018-10-04 19:13:43 +02001698 /* Set ret without branches to avoid timing attacks. Return:
1699 * - INVALID_PADDING if the padding is bad (bad != 0).
1700 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1701 * plaintext does not fit in the output buffer.
1702 * - 0 if the padding is correct. */
Gilles Peskine48992472018-10-12 19:19:12 +02001703 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1704 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1705 0 ) );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001706
Gilles Peskine9265ff42018-10-04 19:13:43 +02001707 /* If the padding is bad or the plaintext is too large, zero the
1708 * data that we're about to copy to the output buffer.
1709 * We need to copy the same amount of data
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001710 * from the same buffer whether the padding is good or not to
1711 * avoid leaking the padding validity through overall timing or
1712 * through memory or cache access patterns. */
Gilles Peskine40b57f42018-10-05 15:06:12 +02001713 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001714 for( i = 11; i < ilen; i++ )
Gilles Peskine40b57f42018-10-05 15:06:12 +02001715 buf[i] &= ~bad;
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001716
Gilles Peskine9265ff42018-10-04 19:13:43 +02001717 /* If the plaintext is too large, truncate it to the buffer size.
1718 * Copy anyway to avoid revealing the length through timing, because
1719 * revealing the length is as bad as revealing the padding validity
1720 * for a Bleichenbacher attack. */
1721 plaintext_size = if_int( output_too_large,
1722 (unsigned) plaintext_max_size,
1723 (unsigned) plaintext_size );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001724
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001725 /* Move the plaintext to the leftmost position where it can start in
1726 * the working buffer, i.e. make it start plaintext_max_size from
1727 * the end of the buffer. Do this with a memory access trace that
1728 * does not depend on the plaintext size. After this move, the
1729 * starting location of the plaintext is no longer sensitive
1730 * information. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001731 mem_move_to_left( buf + ilen - plaintext_max_size,
1732 plaintext_max_size,
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001733 plaintext_max_size - plaintext_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001734
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001735 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskine9265ff42018-10-04 19:13:43 +02001736 * into the output buffer. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001737 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001738
1739 /* Report the amount of data we copied to the output buffer. In case
1740 * of errors (bad padding or output too large), the value of *olen
1741 * when this function returns is not specified. Making it equivalent
1742 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001743 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001745cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001746 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001747
1748 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001749}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
1752/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001753 * Do an RSA operation, then remove the message padding
1754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001756 int (*f_rng)(void *, unsigned char *, size_t),
1757 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001758 int mode, size_t *olen,
1759 const unsigned char *input,
1760 unsigned char *output,
1761 size_t output_max_len)
1762{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001763 RSA_VALIDATE_RET( ctx != NULL );
1764 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1765 mode == MBEDTLS_RSA_PUBLIC );
1766 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1767 RSA_VALIDATE_RET( input != NULL );
1768 RSA_VALIDATE_RET( olen != NULL );
1769
Paul Bakkerb3869132013-02-28 17:21:01 +01001770 switch( ctx->padding )
1771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772#if defined(MBEDTLS_PKCS1_V15)
1773 case MBEDTLS_RSA_PKCS_V15:
1774 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001775 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001776#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778#if defined(MBEDTLS_PKCS1_V21)
1779 case MBEDTLS_RSA_PKCS_V21:
1780 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001781 olen, input, output,
1782 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001783#endif
1784
1785 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001787 }
1788}
1789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001791/*
1792 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001795 int (*f_rng)(void *, unsigned char *, size_t),
1796 void *p_rng,
1797 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001799 unsigned int hashlen,
1800 const unsigned char *hash,
1801 unsigned char *sig )
1802{
1803 size_t olen;
1804 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001806 size_t slen, min_slen, hlen, offset = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001807 int ret;
1808 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 const mbedtls_md_info_t *md_info;
1810 mbedtls_md_context_t md_ctx;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001811 RSA_VALIDATE_RET( ctx != NULL );
1812 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1813 mode == MBEDTLS_RSA_PUBLIC );
1814 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1815 hashlen == 0 ) ||
1816 hash != NULL );
1817 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1820 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001821
1822 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001824
1825 olen = ctx->len;
1826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001828 {
Simon Butcher02037452016-03-01 21:19:12 +00001829 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001831 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001835 }
1836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001838 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001842
Jaeden Amero3725bb22018-09-07 19:12:36 +01001843 /* Calculate the largest possible salt length. Normally this is the hash
1844 * length, which is the maximum length the salt can have. If there is not
1845 * enough room, use the maximum salt length that fits. The constraint is
1846 * that the hash length plus the salt length plus 2 bytes must be at most
1847 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1848 * (PKCS#1 v2.2) §9.1.1 step 3. */
1849 min_slen = hlen - 2;
1850 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001852 else if( olen >= hlen + hlen + 2 )
1853 slen = hlen;
1854 else
1855 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001856
1857 memset( sig, 0, olen );
1858
Simon Butcher02037452016-03-01 21:19:12 +00001859 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001860 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001862
Simon Butcher02037452016-03-01 21:19:12 +00001863 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001864 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001865 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001866 *p++ = 0x01;
1867 memcpy( p, salt, slen );
1868 p += slen;
1869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001871 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001872 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001873
Simon Butcher02037452016-03-01 21:19:12 +00001874 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001875 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1876 goto exit;
1877 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1878 goto exit;
1879 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1880 goto exit;
1881 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1882 goto exit;
1883 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1884 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001885
Simon Butcher02037452016-03-01 21:19:12 +00001886 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001887 if( msb % 8 == 0 )
1888 offset = 1;
1889
Simon Butcher02037452016-03-01 21:19:12 +00001890 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001891 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1892 &md_ctx ) ) != 0 )
1893 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001894
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001895 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001896 sig[0] &= 0xFF >> ( olen * 8 - msb );
1897
1898 p += hlen;
1899 *p++ = 0xBC;
1900
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001901 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001902
1903exit:
1904 mbedtls_md_free( &md_ctx );
1905
1906 if( ret != 0 )
1907 return( ret );
1908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 return( ( mode == MBEDTLS_RSA_PUBLIC )
1910 ? mbedtls_rsa_public( ctx, sig, sig )
1911 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001912}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001916/*
1917 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1918 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001919
1920/* Construct a PKCS v1.5 encoding of a hashed message
1921 *
1922 * This is used both for signature generation and verification.
1923 *
1924 * Parameters:
1925 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001926 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001927 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001928 * - hash: Buffer containing the hashed message or the raw data.
1929 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001930 * - dst: Buffer to hold the encoded message.
1931 *
1932 * Assumptions:
1933 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1934 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001935 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001936 *
1937 */
1938static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1939 unsigned int hashlen,
1940 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001941 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001942 unsigned char *dst )
1943{
1944 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001945 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001946 unsigned char *p = dst;
1947 const char *oid = NULL;
1948
1949 /* Are we signing hashed or raw data? */
1950 if( md_alg != MBEDTLS_MD_NONE )
1951 {
1952 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1953 if( md_info == NULL )
1954 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1955
1956 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1957 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1958
1959 hashlen = mbedtls_md_get_size( md_info );
1960
1961 /* Double-check that 8 + hashlen + oid_size can be used as a
1962 * 1-byte ASN.1 length encoding and that there's no overflow. */
1963 if( 8 + hashlen + oid_size >= 0x80 ||
1964 10 + hashlen < hashlen ||
1965 10 + hashlen + oid_size < 10 + hashlen )
1966 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1967
1968 /*
1969 * Static bounds check:
1970 * - Need 10 bytes for five tag-length pairs.
1971 * (Insist on 1-byte length encodings to protect against variants of
1972 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1973 * - Need hashlen bytes for hash
1974 * - Need oid_size bytes for hash alg OID.
1975 */
1976 if( nb_pad < 10 + hashlen + oid_size )
1977 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1978 nb_pad -= 10 + hashlen + oid_size;
1979 }
1980 else
1981 {
1982 if( nb_pad < hashlen )
1983 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1984
1985 nb_pad -= hashlen;
1986 }
1987
Hanno Becker2b2f8982017-09-27 17:10:03 +01001988 /* Need space for signature header and padding delimiter (3 bytes),
1989 * and 8 bytes for the minimal padding */
1990 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001991 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1992 nb_pad -= 3;
1993
1994 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001995 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001996
1997 /* Write signature header and padding */
1998 *p++ = 0;
1999 *p++ = MBEDTLS_RSA_SIGN;
2000 memset( p, 0xFF, nb_pad );
2001 p += nb_pad;
2002 *p++ = 0;
2003
2004 /* Are we signing raw data? */
2005 if( md_alg == MBEDTLS_MD_NONE )
2006 {
2007 memcpy( p, hash, hashlen );
2008 return( 0 );
2009 }
2010
2011 /* Signing hashed data, add corresponding ASN.1 structure
2012 *
2013 * DigestInfo ::= SEQUENCE {
2014 * digestAlgorithm DigestAlgorithmIdentifier,
2015 * digest Digest }
2016 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2017 * Digest ::= OCTET STRING
2018 *
2019 * Schematic:
2020 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2021 * TAG-NULL + LEN [ NULL ] ]
2022 * TAG-OCTET + LEN [ HASH ] ]
2023 */
2024 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002025 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002026 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002027 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002028 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002029 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002030 memcpy( p, oid, oid_size );
2031 p += oid_size;
2032 *p++ = MBEDTLS_ASN1_NULL;
2033 *p++ = 0x00;
2034 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002035 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002036 memcpy( p, hash, hashlen );
2037 p += hashlen;
2038
2039 /* Just a sanity-check, should be automatic
2040 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002041 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002042 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002043 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002044 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2045 }
2046
2047 return( 0 );
2048}
2049
Paul Bakkerb3869132013-02-28 17:21:01 +01002050/*
2051 * Do an RSA operation to sign the message digest
2052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002054 int (*f_rng)(void *, unsigned char *, size_t),
2055 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002056 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002058 unsigned int hashlen,
2059 const unsigned char *hash,
2060 unsigned char *sig )
2061{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002062 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002063 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002064
Hanno Beckerddeeed72018-12-13 18:07:00 +00002065 RSA_VALIDATE_RET( ctx != NULL );
2066 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2067 mode == MBEDTLS_RSA_PUBLIC );
2068 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2069 hashlen == 0 ) ||
2070 hash != NULL );
2071 RSA_VALIDATE_RET( sig != NULL );
2072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2074 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002075
Hanno Beckerfdf38032017-09-06 12:35:55 +01002076 /*
2077 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2078 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002079
Hanno Beckerfdf38032017-09-06 12:35:55 +01002080 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2081 ctx->len, sig ) ) != 0 )
2082 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002083
2084 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002085 * Call respective RSA primitive
2086 */
2087
2088 if( mode == MBEDTLS_RSA_PUBLIC )
2089 {
2090 /* Skip verification on a public key operation */
2091 return( mbedtls_rsa_public( ctx, sig, sig ) );
2092 }
2093
2094 /* Private key operation
2095 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002096 * In order to prevent Lenstra's attack, make the signature in a
2097 * temporary buffer and check it before returning it.
2098 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002099
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002100 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002101 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002102 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2103
Hanno Beckerfdf38032017-09-06 12:35:55 +01002104 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002105 if( verif == NULL )
2106 {
2107 mbedtls_free( sig_try );
2108 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2109 }
2110
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002111 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2112 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2113
Hanno Becker171a8f12017-09-06 12:32:16 +01002114 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002115 {
2116 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2117 goto cleanup;
2118 }
2119
2120 memcpy( sig, sig_try, ctx->len );
2121
2122cleanup:
2123 mbedtls_free( sig_try );
2124 mbedtls_free( verif );
2125
2126 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002127}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002129
2130/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 * Do an RSA operation to sign the message digest
2132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002134 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002135 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002138 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002139 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 unsigned char *sig )
2141{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002142 RSA_VALIDATE_RET( ctx != NULL );
2143 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2144 mode == MBEDTLS_RSA_PUBLIC );
2145 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2146 hashlen == 0 ) ||
2147 hash != NULL );
2148 RSA_VALIDATE_RET( sig != NULL );
2149
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 switch( ctx->padding )
2151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#if defined(MBEDTLS_PKCS1_V15)
2153 case MBEDTLS_RSA_PKCS_V15:
2154 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002155 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002156#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158#if defined(MBEDTLS_PKCS1_V21)
2159 case MBEDTLS_RSA_PKCS_V21:
2160 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002161 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002162#endif
2163
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002167}
2168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002170/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002171 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002174 int (*f_rng)(void *, unsigned char *, size_t),
2175 void *p_rng,
2176 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002178 unsigned int hashlen,
2179 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002181 int expected_salt_len,
2182 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002183{
Paul Bakker23986e52011-04-24 08:57:21 +00002184 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002185 size_t siglen;
2186 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002187 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002189 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002190 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002191 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 const mbedtls_md_info_t *md_info;
2193 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002194 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002195
Hanno Beckerddeeed72018-12-13 18:07:00 +00002196 RSA_VALIDATE_RET( ctx != NULL );
2197 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2198 mode == MBEDTLS_RSA_PUBLIC );
2199 RSA_VALIDATE_RET( sig != NULL );
2200 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2201 hashlen == 0 ) ||
2202 hash != NULL );
2203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002206
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 siglen = ctx->len;
2208
Paul Bakker27fdf462011-06-09 13:55:13 +00002209 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2213 ? mbedtls_rsa_public( ctx, sig, buf )
2214 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215
2216 if( ret != 0 )
2217 return( ret );
2218
2219 p = buf;
2220
Paul Bakkerb3869132013-02-28 17:21:01 +01002221 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 {
Simon Butcher02037452016-03-01 21:19:12 +00002226 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002228 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002232 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002235 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002239
Paul Bakkerb3869132013-02-28 17:21:01 +01002240 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002241
Simon Butcher02037452016-03-01 21:19:12 +00002242 /*
2243 * Note: EMSA-PSS verification is over the length of N - 1 bits
2244 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002245 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002246
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002247 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2248 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2249
Simon Butcher02037452016-03-01 21:19:12 +00002250 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002251 if( msb % 8 == 0 )
2252 {
2253 p++;
2254 siglen -= 1;
2255 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002256
Gilles Peskine139108a2017-10-18 19:03:42 +02002257 if( siglen < hlen + 2 )
2258 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2259 hash_start = p + siglen - hlen - 1;
2260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002262 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002263 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002264
Jaeden Amero66954e12018-01-25 16:05:54 +00002265 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2266 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002267 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002268
Paul Bakkerb3869132013-02-28 17:21:01 +01002269 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002270
Gilles Peskine6a54b022017-10-17 19:02:13 +02002271 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002272 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002273
Gilles Peskine91048a32017-10-19 17:46:14 +02002274 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002275 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002276 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2277 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002278 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002279
Gilles Peskine6a54b022017-10-17 19:02:13 +02002280 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002283 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002284 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002285 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2286 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002287 }
2288
Simon Butcher02037452016-03-01 21:19:12 +00002289 /*
2290 * Generate H = Hash( M' )
2291 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002292 ret = mbedtls_md_starts( &md_ctx );
2293 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002294 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002295 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2296 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002297 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002298 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2299 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002300 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002301 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2302 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002303 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002304 ret = mbedtls_md_finish( &md_ctx, result );
2305 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002306 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002307
Jaeden Amero66954e12018-01-25 16:05:54 +00002308 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002309 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002310 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002311 goto exit;
2312 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002313
2314exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002316
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002317 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002318}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002319
2320/*
2321 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002324 int (*f_rng)(void *, unsigned char *, size_t),
2325 void *p_rng,
2326 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002328 unsigned int hashlen,
2329 const unsigned char *hash,
2330 const unsigned char *sig )
2331{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002332 mbedtls_md_type_t mgf1_hash_id;
2333 RSA_VALIDATE_RET( ctx != NULL );
2334 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2335 mode == MBEDTLS_RSA_PUBLIC );
2336 RSA_VALIDATE_RET( sig != NULL );
2337 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2338 hashlen == 0 ) ||
2339 hash != NULL );
2340
2341 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002343 : md_alg;
2344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002346 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002348 sig ) );
2349
2350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002354/*
2355 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002358 int (*f_rng)(void *, unsigned char *, size_t),
2359 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002360 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002362 unsigned int hashlen,
2363 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002364 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002365{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002366 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002367 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002368 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002369
Hanno Beckerddeeed72018-12-13 18:07:00 +00002370 RSA_VALIDATE_RET( ctx != NULL );
2371 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2372 mode == MBEDTLS_RSA_PUBLIC );
2373 RSA_VALIDATE_RET( sig != NULL );
2374 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2375 hashlen == 0 ) ||
2376 hash != NULL );
2377
2378 sig_len = ctx->len;
2379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2381 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002382
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002383 /*
2384 * Prepare expected PKCS1 v1.5 encoding of hash.
2385 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002386
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002387 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2388 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2389 {
2390 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2391 goto cleanup;
2392 }
2393
2394 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2395 encoded_expected ) ) != 0 )
2396 goto cleanup;
2397
2398 /*
2399 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2400 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002403 ? mbedtls_rsa_public( ctx, sig, encoded )
2404 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002405 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002406 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002407
Simon Butcher02037452016-03-01 21:19:12 +00002408 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002409 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002410 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002411
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002412 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2413 sig_len ) ) != 0 )
2414 {
2415 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2416 goto cleanup;
2417 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002418
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002419cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002420
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002421 if( encoded != NULL )
2422 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002423 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002424 mbedtls_free( encoded );
2425 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002426
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002427 if( encoded_expected != NULL )
2428 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002429 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002430 mbedtls_free( encoded_expected );
2431 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002432
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002433 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
2437/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002438 * Do an RSA operation and check the message digest
2439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002441 int (*f_rng)(void *, unsigned char *, size_t),
2442 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002443 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002445 unsigned int hashlen,
2446 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002447 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002448{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002449 RSA_VALIDATE_RET( ctx != NULL );
2450 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2451 mode == MBEDTLS_RSA_PUBLIC );
2452 RSA_VALIDATE_RET( sig != NULL );
2453 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2454 hashlen == 0 ) ||
2455 hash != NULL );
2456
Paul Bakkerb3869132013-02-28 17:21:01 +01002457 switch( ctx->padding )
2458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459#if defined(MBEDTLS_PKCS1_V15)
2460 case MBEDTLS_RSA_PKCS_V15:
2461 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002462 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002463#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465#if defined(MBEDTLS_PKCS1_V21)
2466 case MBEDTLS_RSA_PKCS_V21:
2467 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002468 hashlen, hash, sig );
2469#endif
2470
2471 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002473 }
2474}
2475
2476/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002477 * Copy the components of an RSA key
2478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002480{
2481 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002482 RSA_VALIDATE_RET( dst != NULL );
2483 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002484
2485 dst->ver = src->ver;
2486 dst->len = src->len;
2487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2489 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2492 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2493 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002494
2495#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2497 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2498 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2500 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002501#endif
2502
2503 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2506 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002507
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002508 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002509 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002510
2511cleanup:
2512 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002514
2515 return( ret );
2516}
2517
2518/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 * Free the components of an RSA key
2520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002522{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002523 if( ctx == NULL )
2524 return;
2525
irwir2239a862018-06-12 18:25:09 +03002526 mbedtls_mpi_free( &ctx->Vi );
2527 mbedtls_mpi_free( &ctx->Vf );
2528 mbedtls_mpi_free( &ctx->RN );
2529 mbedtls_mpi_free( &ctx->D );
2530 mbedtls_mpi_free( &ctx->Q );
2531 mbedtls_mpi_free( &ctx->P );
2532 mbedtls_mpi_free( &ctx->E );
2533 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002534
Hanno Becker33c30a02017-08-23 07:00:22 +01002535#if !defined(MBEDTLS_RSA_NO_CRT)
irwir2239a862018-06-12 18:25:09 +03002536 mbedtls_mpi_free( &ctx->RQ );
2537 mbedtls_mpi_free( &ctx->RP );
2538 mbedtls_mpi_free( &ctx->QP );
2539 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002540 mbedtls_mpi_free( &ctx->DP );
2541#endif /* MBEDTLS_RSA_NO_CRT */
2542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543#if defined(MBEDTLS_THREADING_C)
2544 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002545#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002546}
2547
Hanno Beckerab377312017-08-23 16:24:51 +01002548#endif /* !MBEDTLS_RSA_ALT */
2549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002551
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002552#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
2554/*
2555 * Example RSA-1024 keypair, for test purposes
2556 */
2557#define KEY_LEN 128
2558
2559#define RSA_N "9292758453063D803DD603D5E777D788" \
2560 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2561 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2562 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2563 "93A89813FBF3C4F8066D2D800F7C38A8" \
2564 "1AE31942917403FF4946B0A83D3D3E05" \
2565 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2566 "5E94BB77B07507233A0BC7BAC8F90F79"
2567
2568#define RSA_E "10001"
2569
2570#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2571 "66CA472BC44D253102F8B4A9D3BFA750" \
2572 "91386C0077937FE33FA3252D28855837" \
2573 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2574 "DF79C5CE07EE72C7F123142198164234" \
2575 "CABB724CF78B8173B9F880FC86322407" \
2576 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2577 "071513A1E85B5DFA031F21ECAE91A34D"
2578
2579#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2580 "2C01CAD19EA484A87EA4377637E75500" \
2581 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2582 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2583
2584#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2585 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2586 "910E4168387E3C30AA1E00C339A79508" \
2587 "8452DD96A9A5EA5D9DCA68DA636032AF"
2588
Paul Bakker5121ce52009-01-03 21:22:43 +00002589#define PT_LEN 24
2590#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2591 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002594static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002595{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002596#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002597 size_t i;
2598
Paul Bakker545570e2010-07-18 09:00:25 +00002599 if( rng_state != NULL )
2600 rng_state = NULL;
2601
Paul Bakkera3d195c2011-11-27 21:07:34 +00002602 for( i = 0; i < len; ++i )
2603 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002604#else
2605 if( rng_state != NULL )
2606 rng_state = NULL;
2607
2608 arc4random_buf( output, len );
2609#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002610
Paul Bakkera3d195c2011-11-27 21:07:34 +00002611 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002612}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002614
Paul Bakker5121ce52009-01-03 21:22:43 +00002615/*
2616 * Checkup routine
2617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002619{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002620 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002622 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 unsigned char rsa_plaintext[PT_LEN];
2625 unsigned char rsa_decrypted[PT_LEN];
2626 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002628 unsigned char sha1sum[20];
2629#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
Hanno Becker3a701162017-08-22 13:52:43 +01002631 mbedtls_mpi K;
2632
2633 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Hanno Becker3a701162017-08-22 13:52:43 +01002636 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2637 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2638 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2639 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2640 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2641 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2642 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2643 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2644 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2645 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2646
Hanno Becker7f25f852017-10-10 16:56:22 +01002647 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
2649 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2653 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 {
2655 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
Hanno Becker5bc87292017-05-03 15:09:31 +01002658 ret = 1;
2659 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 }
2661
2662 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
2665 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2666
Hanno Becker98838b02017-10-02 13:16:10 +01002667 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2668 PT_LEN, rsa_plaintext,
2669 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 {
2671 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Hanno Becker5bc87292017-05-03 15:09:31 +01002674 ret = 1;
2675 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 }
2677
2678 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Hanno Becker98838b02017-10-02 13:16:10 +01002681 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2682 &len, rsa_ciphertext, rsa_decrypted,
2683 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 {
2685 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Hanno Becker5bc87292017-05-03 15:09:31 +01002688 ret = 1;
2689 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 }
2691
2692 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2693 {
2694 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Hanno Becker5bc87292017-05-03 15:09:31 +01002697 ret = 1;
2698 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
2700
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002701 if( verbose != 0 )
2702 mbedtls_printf( "passed\n" );
2703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002706 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002708 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002709 {
2710 if( verbose != 0 )
2711 mbedtls_printf( "failed\n" );
2712
2713 return( 1 );
2714 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Hanno Becker98838b02017-10-02 13:16:10 +01002716 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2717 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2718 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 {
2720 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Hanno Becker5bc87292017-05-03 15:09:31 +01002723 ret = 1;
2724 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 }
2726
2727 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Hanno Becker98838b02017-10-02 13:16:10 +01002730 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2731 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2732 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 {
2734 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Hanno Becker5bc87292017-05-03 15:09:31 +01002737 ret = 1;
2738 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 }
2740
2741 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002742 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002745 if( verbose != 0 )
2746 mbedtls_printf( "\n" );
2747
Paul Bakker3d8fb632014-04-17 12:42:41 +02002748cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002749 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 mbedtls_rsa_free( &rsa );
2751#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002752 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002754 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002755}
2756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759#endif /* MBEDTLS_RSA_C */