blob: 41643d25318687e71a267282b32181420ef89a1b [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é-Gonnard49e94e32020-06-26 11:19:12 +0200811 mbedtls_mpi R;
812
813 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200814
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200815 if( ctx->Vf.p != NULL )
816 {
817 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
819 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
820 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
821 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200822
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200823 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200824 }
825
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200826 /* Unblinding value: Vf = random number, invertible mod N */
827 do {
828 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 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 +0200832
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200833 /* Compute the Vf^1 as R * (R Vf)^-1 to avoid leaks from inv_mod.
834 * There's a negligible but non-zero probability that R is not
835 * invertible mod N, in that case we'd just loop one more time,
836 * just as if Vf itself wasn't invertible - no need to distinguish. */
837 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
838 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
839 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
840
841 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
842 if( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
843 continue;
844 if( ret != 0 )
Manuel Pégourié-Gonnard86ad5be2020-06-26 11:03:19 +0200845 goto cleanup;
846
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200847 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
848 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
849 } while( 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200850
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200851 /* Blinding value: Vi = Vf^(-e) mod N
852 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 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 +0200854
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200855
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200856cleanup:
Manuel Pégourié-Gonnard49e94e32020-06-26 11:19:12 +0200857 mbedtls_mpi_free( &R );
858
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200859 return( ret );
860}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200861
Paul Bakker5121ce52009-01-03 21:22:43 +0000862/*
Janos Follathe81102e2017-03-22 13:38:28 +0000863 * Exponent blinding supposed to prevent side-channel attacks using multiple
864 * traces of measurements to recover the RSA key. The more collisions are there,
865 * the more bits of the key can be recovered. See [3].
866 *
867 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
868 * observations on avarage.
869 *
870 * For example with 28 byte blinding to achieve 2 collisions the adversary has
871 * to make 2^112 observations on avarage.
872 *
873 * (With the currently (as of 2017 April) known best algorithms breaking 2048
874 * bit RSA requires approximately as much time as trying out 2^112 random keys.
875 * Thus in this sense with 28 byte blinding the security is not reduced by
876 * side-channel attacks like the one in [3])
877 *
878 * This countermeasure does not help if the key recovery is possible with a
879 * single trace.
880 */
881#define RSA_EXPONENT_BLINDING 28
882
883/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000884 * Do an RSA private key operation
885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200887 int (*f_rng)(void *, unsigned char *, size_t),
888 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000889 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000890 unsigned char *output )
891{
Paul Bakker23986e52011-04-24 08:57:21 +0000892 int ret;
893 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100894
895 /* Temporary holding the result */
896 mbedtls_mpi T;
897
898 /* Temporaries holding P-1, Q-1 and the
899 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000900 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100901
902#if !defined(MBEDTLS_RSA_NO_CRT)
903 /* Temporaries holding the results mod p resp. mod q. */
904 mbedtls_mpi TP, TQ;
905
906 /* Temporaries holding the blinded exponents for
907 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000908 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100909
910 /* Pointers to actual exponents to be used - either the unblinded
911 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000912 mbedtls_mpi *DP = &ctx->DP;
913 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100914#else
915 /* Temporary holding the blinded exponent (if used). */
916 mbedtls_mpi D_blind;
917
918 /* Pointer to actual exponent to be used - either the unblinded
919 * or the blinded one, depending on the presence of a PRNG. */
920 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100921#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100922
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100923 /* Temporaries holding the initial input and the double
924 * checked result; should be the same in the end. */
925 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Hanno Beckerddeeed72018-12-13 18:07:00 +0000927 RSA_VALIDATE_RET( ctx != NULL );
928 RSA_VALIDATE_RET( input != NULL );
929 RSA_VALIDATE_RET( output != NULL );
930
Hanno Beckerebd2c022017-10-12 10:54:53 +0100931 if( rsa_check_context( ctx, 1 /* private key checks */,
932 f_rng != NULL /* blinding y/n */ ) != 0 )
933 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100934 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100935 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100936
Hanno Becker06811ce2017-05-03 15:10:34 +0100937#if defined(MBEDTLS_THREADING_C)
938 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
939 return( ret );
940#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000941
Hanno Becker06811ce2017-05-03 15:10:34 +0100942 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100943 mbedtls_mpi_init( &T );
944
945 mbedtls_mpi_init( &P1 );
946 mbedtls_mpi_init( &Q1 );
947 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000948
Janos Follathf9203b42017-03-22 15:13:15 +0000949 if( f_rng != NULL )
950 {
Janos Follathe81102e2017-03-22 13:38:28 +0000951#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000952 mbedtls_mpi_init( &D_blind );
953#else
954 mbedtls_mpi_init( &DP_blind );
955 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000956#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000957 }
Janos Follathe81102e2017-03-22 13:38:28 +0000958
Hanno Becker06811ce2017-05-03 15:10:34 +0100959#if !defined(MBEDTLS_RSA_NO_CRT)
960 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200961#endif
962
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100963 mbedtls_mpi_init( &I );
964 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100965
966 /* End of MPI initialization */
967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
969 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200971 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
972 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 }
974
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100975 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100976
Paul Bakkerf451bac2013-08-30 15:37:02 +0200977 if( f_rng != NULL )
978 {
979 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200980 * Blinding
981 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200982 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200983 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
984 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000986
Janos Follathe81102e2017-03-22 13:38:28 +0000987 /*
988 * Exponent blinding
989 */
990 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
991 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
992
Janos Follathf9203b42017-03-22 15:13:15 +0000993#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000994 /*
995 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
996 */
997 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
998 f_rng, p_rng ) );
999 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1000 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1001 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1002
1003 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001004#else
1005 /*
1006 * DP_blind = ( P - 1 ) * R + DP
1007 */
1008 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1009 f_rng, p_rng ) );
1010 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1011 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1012 &ctx->DP ) );
1013
1014 DP = &DP_blind;
1015
1016 /*
1017 * DQ_blind = ( Q - 1 ) * R + DQ
1018 */
1019 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1020 f_rng, p_rng ) );
1021 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1022 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1023 &ctx->DQ ) );
1024
1025 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001026#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001027 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001030 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001031#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001032 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001033 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001035 * TP = input ^ dP mod P
1036 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001038
1039 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1040 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
1042 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001043 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001045 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1046 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1047 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001048
1049 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001050 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001052 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1053 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001055
Paul Bakkerf451bac2013-08-30 15:37:02 +02001056 if( f_rng != NULL )
1057 {
1058 /*
1059 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001060 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001061 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001062 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001064 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
Hanno Becker2dec5e82017-10-03 07:49:52 +01001066 /* Verify the result to prevent glitching attacks. */
1067 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1068 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001069 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001070 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001071 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1072 goto cleanup;
1073 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001074
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
1078cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001080 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1081 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001082#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001083
Hanno Becker06811ce2017-05-03 15:10:34 +01001084 mbedtls_mpi_free( &P1 );
1085 mbedtls_mpi_free( &Q1 );
1086 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001087
1088 if( f_rng != NULL )
1089 {
Janos Follathe81102e2017-03-22 13:38:28 +00001090#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001091 mbedtls_mpi_free( &D_blind );
1092#else
1093 mbedtls_mpi_free( &DP_blind );
1094 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001095#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001096 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Hanno Becker06811ce2017-05-03 15:10:34 +01001098 mbedtls_mpi_free( &T );
1099
1100#if !defined(MBEDTLS_RSA_NO_CRT)
1101 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1102#endif
1103
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001104 mbedtls_mpi_free( &C );
1105 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001106
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
1110 return( 0 );
1111}
1112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114/**
1115 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1116 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001117 * \param dst buffer to mask
1118 * \param dlen length of destination buffer
1119 * \param src source of the mask generation
1120 * \param slen length of the source buffer
1121 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001122 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001123static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001127 unsigned char counter[4];
1128 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001129 unsigned int hlen;
1130 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001131 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001134 memset( counter, 0, 4 );
1135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001137
Simon Butcher02037452016-03-01 21:19:12 +00001138 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001139 p = dst;
1140
1141 while( dlen > 0 )
1142 {
1143 use_len = hlen;
1144 if( dlen < hlen )
1145 use_len = dlen;
1146
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001147 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1148 goto exit;
1149 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1150 goto exit;
1151 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1152 goto exit;
1153 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1154 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001155
1156 for( i = 0; i < use_len; ++i )
1157 *p++ ^= mask[i];
1158
1159 counter[3]++;
1160
1161 dlen -= use_len;
1162 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001163
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001164exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001165 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001166
1167 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001172/*
1173 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001176 int (*f_rng)(void *, unsigned char *, size_t),
1177 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001178 int mode,
1179 const unsigned char *label, size_t label_len,
1180 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001181 const unsigned char *input,
1182 unsigned char *output )
1183{
1184 size_t olen;
1185 int ret;
1186 unsigned char *p = output;
1187 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 const mbedtls_md_info_t *md_info;
1189 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001190
Hanno Beckerddeeed72018-12-13 18:07:00 +00001191 RSA_VALIDATE_RET( ctx != NULL );
1192 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1193 mode == MBEDTLS_RSA_PUBLIC );
1194 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001195 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001196 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1199 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001200
1201 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001205 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001207
1208 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001210
Simon Butcher02037452016-03-01 21:19:12 +00001211 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001212 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001214
1215 memset( output, 0, olen );
1216
1217 *p++ = 0;
1218
Simon Butcher02037452016-03-01 21:19:12 +00001219 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001220 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001222
1223 p += hlen;
1224
Simon Butcher02037452016-03-01 21:19:12 +00001225 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001226 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1227 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001228 p += hlen;
1229 p += olen - 2 * hlen - 2 - ilen;
1230 *p++ = 1;
1231 memcpy( p, input, ilen );
1232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001234 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001235 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001236
Simon Butcher02037452016-03-01 21:19:12 +00001237 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001238 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1239 &md_ctx ) ) != 0 )
1240 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001241
Simon Butcher02037452016-03-01 21:19:12 +00001242 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001243 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1244 &md_ctx ) ) != 0 )
1245 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001246
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001247exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001249
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001250 if( ret != 0 )
1251 return( ret );
1252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 return( ( mode == MBEDTLS_RSA_PUBLIC )
1254 ? mbedtls_rsa_public( ctx, output, output )
1255 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001260/*
1261 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001264 int (*f_rng)(void *, unsigned char *, size_t),
1265 void *p_rng,
1266 int mode, size_t ilen,
1267 const unsigned char *input,
1268 unsigned char *output )
1269{
1270 size_t nb_pad, olen;
1271 int ret;
1272 unsigned char *p = output;
1273
Hanno Beckerddeeed72018-12-13 18:07:00 +00001274 RSA_VALIDATE_RET( ctx != NULL );
1275 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1276 mode == MBEDTLS_RSA_PUBLIC );
1277 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001278 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1281 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001282
Paul Bakkerb3869132013-02-28 17:21:01 +01001283 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001284
Simon Butcher02037452016-03-01 21:19:12 +00001285 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001286 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001288
1289 nb_pad = olen - 3 - ilen;
1290
1291 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001293 {
Hanno Beckerb86e6842018-12-18 14:46:04 +00001294 if( f_rng == NULL )
1295 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001298
1299 while( nb_pad-- > 0 )
1300 {
1301 int rng_dl = 100;
1302
1303 do {
1304 ret = f_rng( p_rng, p, 1 );
1305 } while( *p == 0 && --rng_dl && ret == 0 );
1306
Simon Butcher02037452016-03-01 21:19:12 +00001307 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001308 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001310
1311 p++;
1312 }
1313 }
1314 else
1315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001317
1318 while( nb_pad-- > 0 )
1319 *p++ = 0xFF;
1320 }
1321
1322 *p++ = 0;
1323 memcpy( p, input, ilen );
1324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 return( ( mode == MBEDTLS_RSA_PUBLIC )
1326 ? mbedtls_rsa_public( ctx, output, output )
1327 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001328}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001330
Paul Bakker5121ce52009-01-03 21:22:43 +00001331/*
1332 * Add the message padding, then do an RSA operation
1333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001335 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001336 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001337 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001338 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 unsigned char *output )
1340{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001341 RSA_VALIDATE_RET( ctx != NULL );
1342 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1343 mode == MBEDTLS_RSA_PUBLIC );
1344 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001345 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001346
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 switch( ctx->padding )
1348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_PKCS1_V15)
1350 case MBEDTLS_RSA_PKCS_V15:
1351 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001352 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001353#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#if defined(MBEDTLS_PKCS1_V21)
1356 case MBEDTLS_RSA_PKCS_V21:
1357 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001358 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001359#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001360
1361 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001364}
1365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001367/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001368 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001371 int (*f_rng)(void *, unsigned char *, size_t),
1372 void *p_rng,
1373 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001374 const unsigned char *label, size_t label_len,
1375 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001376 const unsigned char *input,
1377 unsigned char *output,
1378 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001379{
Paul Bakker23986e52011-04-24 08:57:21 +00001380 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001381 size_t ilen, i, pad_len;
1382 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1384 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001385 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 const mbedtls_md_info_t *md_info;
1387 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001388
Hanno Beckerddeeed72018-12-13 18:07:00 +00001389 RSA_VALIDATE_RET( ctx != NULL );
1390 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1391 mode == MBEDTLS_RSA_PUBLIC );
1392 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1393 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1394 RSA_VALIDATE_RET( input != NULL );
1395 RSA_VALIDATE_RET( olen != NULL );
1396
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001397 /*
1398 * Parameters sanity checks
1399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1401 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
1403 ilen = ctx->len;
1404
Paul Bakker27fdf462011-06-09 13:55:13 +00001405 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001409 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001411
Janos Follathc17cda12016-02-11 11:08:18 +00001412 hlen = mbedtls_md_get_size( md_info );
1413
1414 // checking for integer underflow
1415 if( 2 * hlen + 2 > ilen )
1416 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1417
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001418 /*
1419 * RSA operation
1420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1422 ? mbedtls_rsa_public( ctx, input, buf )
1423 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
1425 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001426 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001427
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001428 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001429 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001432 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1433 {
1434 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001435 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001436 }
1437
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001438 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001439 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1440 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001441 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001442 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1443 &md_ctx ) ) != 0 )
1444 {
1445 mbedtls_md_free( &md_ctx );
1446 goto cleanup;
1447 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001450
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001451 /* Generate lHash */
1452 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1453 goto cleanup;
1454
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001455 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001456 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001457 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001459 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001460
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001461 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001462
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001463 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001464
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001465 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001466 for( i = 0; i < hlen; i++ )
1467 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001468
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001469 /* Get zero-padding len, but always read till end of buffer
1470 * (minus one, for the 01 byte) */
1471 pad_len = 0;
1472 pad_done = 0;
1473 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1474 {
1475 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001476 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001477 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001478
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001479 p += pad_len;
1480 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001481
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001482 /*
1483 * The only information "leaked" is whether the padding was correct or not
1484 * (eg, no data is copied if it was not correct). This meets the
1485 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1486 * the different error conditions.
1487 */
1488 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001489 {
1490 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1491 goto cleanup;
1492 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001493
Paul Bakker66d5d072014-06-17 16:39:18 +02001494 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001495 {
1496 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1497 goto cleanup;
1498 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001499
1500 *olen = ilen - (p - buf);
1501 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001502 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001503
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001504cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001505 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1506 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001507
1508 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001513/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1514 *
1515 * \param value The value to analyze.
Gilles Peskine331d80e2018-10-04 18:32:29 +02001516 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001517 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001518static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001519{
1520 /* MSVC has a warning about unary minus on unsigned, but this is
1521 * well-defined and precisely what we want to do here */
1522#if defined(_MSC_VER)
1523#pragma warning( push )
1524#pragma warning( disable : 4146 )
1525#endif
1526 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1527#if defined(_MSC_VER)
1528#pragma warning( pop )
1529#endif
1530}
1531
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001532/** Check whether a size is out of bounds, without branches.
1533 *
1534 * This is equivalent to `size > max`, but is likely to be compiled to
1535 * to code using bitwise operation rather than a branch.
1536 *
1537 * \param size Size to check.
1538 * \param max Maximum desired value for \p size.
1539 * \return \c 0 if `size <= max`.
1540 * \return \c 1 if `size > max`.
1541 */
1542static unsigned size_greater_than( size_t size, size_t max )
1543{
1544 /* Return the sign bit (1 for negative) of (max - size). */
1545 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1546}
1547
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001548/** Choose between two integer values, without branches.
1549 *
Gilles Peskine331d80e2018-10-04 18:32:29 +02001550 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1551 * to code using bitwise operation rather than a branch.
1552 *
1553 * \param cond Condition to test.
1554 * \param if1 Value to use if \p cond is nonzero.
1555 * \param if0 Value to use if \p cond is zero.
1556 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001557 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001558static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001559{
Gilles Peskine331d80e2018-10-04 18:32:29 +02001560 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001561 return( ( mask & if1 ) | (~mask & if0 ) );
1562}
1563
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001564/** Shift some data towards the left inside a buffer without leaking
1565 * the length of the data through side channels.
1566 *
1567 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1568 * ```
1569 * memmove(start, start + offset, total - offset);
1570 * memset(start + offset, 0, total - offset);
1571 * ```
1572 * but it strives to use a memory access pattern (and thus total timing)
1573 * that does not depend on \p offset. This timing independence comes at
1574 * the expense of performance.
1575 *
1576 * \param start Pointer to the start of the buffer.
1577 * \param total Total size of the buffer.
1578 * \param offset Offset from which to copy \p total - \p offset bytes.
1579 */
1580static void mem_move_to_left( void *start,
1581 size_t total,
1582 size_t offset )
1583{
1584 volatile unsigned char *buf = start;
1585 size_t i, n;
1586 if( total == 0 )
1587 return;
1588 for( i = 0; i < total; i++ )
1589 {
1590 unsigned no_op = size_greater_than( total - offset, i );
1591 /* The first `total - offset` passes are a no-op. The last
1592 * `offset` passes shift the data one byte to the left and
1593 * zero out the last byte. */
1594 for( n = 0; n < total - 1; n++ )
Gilles Peskine9b430702018-10-12 19:15:34 +02001595 {
1596 unsigned char current = buf[n];
1597 unsigned char next = buf[n+1];
1598 buf[n] = if_int( no_op, current, next );
1599 }
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001600 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1601 }
1602}
1603
Paul Bakkerb3869132013-02-28 17:21:01 +01001604/*
1605 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001608 int (*f_rng)(void *, unsigned char *, size_t),
1609 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001610 int mode, size_t *olen,
1611 const unsigned char *input,
1612 unsigned char *output,
Gilles Peskine5908dd42018-10-02 22:43:06 +02001613 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001614{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001615 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001616 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine85a74422018-10-05 14:50:21 +02001618 /* The following variables take sensitive values: their value must
1619 * not leak into the observable behavior of the function other than
1620 * the designated outputs (output, olen, return value). Otherwise
1621 * this would open the execution of the function to
1622 * side-channel-based variants of the Bleichenbacher padding oracle
1623 * attack. Potential side channels include overall timing, memory
1624 * access patterns (especially visible to an adversary who has access
1625 * to a shared memory cache), and branches (especially visible to
1626 * an adversary who has access to a shared code cache or to a shared
1627 * branch predictor). */
1628 size_t pad_count = 0;
1629 unsigned bad = 0;
1630 unsigned char pad_done = 0;
1631 size_t plaintext_size = 0;
1632 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001633
Hanno Beckerddeeed72018-12-13 18:07:00 +00001634 RSA_VALIDATE_RET( ctx != NULL );
1635 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1636 mode == MBEDTLS_RSA_PUBLIC );
1637 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1638 RSA_VALIDATE_RET( input != NULL );
1639 RSA_VALIDATE_RET( olen != NULL );
1640
1641 ilen = ctx->len;
1642 plaintext_max_size = ( output_max_len > ilen - 11 ?
1643 ilen - 11 :
1644 output_max_len );
1645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1647 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001648
Paul Bakkerb3869132013-02-28 17:21:01 +01001649 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1653 ? mbedtls_rsa_public( ctx, input, buf )
1654 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001655
1656 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001657 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001658
Gilles Peskine40b57f42018-10-05 15:06:12 +02001659 /* Check and get padding length in constant time and constant
1660 * memory trace. The first byte must be 0. */
1661 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001664 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001665 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1666 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001667 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001668
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001669 /* Read the whole buffer. Set pad_done to nonzero if we find
1670 * the 0x00 byte and remember the padding length in pad_count. */
1671 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001672 {
Gilles Peskine85a74422018-10-05 14:50:21 +02001673 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001674 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001675 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001676 }
1677 else
1678 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001679 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1680 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001681 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001682
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001683 /* Read the whole buffer. Set pad_done to nonzero if we find
1684 * the 0x00 byte and remember the padding length in pad_count.
1685 * If there's a non-0xff byte in the padding, the padding is bad. */
1686 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001687 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001688 pad_done |= if_int( buf[i], 0, 1 );
1689 pad_count += if_int( pad_done, 0, 1 );
1690 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001691 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001692 }
1693
Gilles Peskine40b57f42018-10-05 15:06:12 +02001694 /* If pad_done is still zero, there's no data, only unfinished padding. */
1695 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001696
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001697 /* There must be at least 8 bytes of padding. */
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001698 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001699
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001700 /* If the padding is valid, set plaintext_size to the number of
1701 * remaining bytes after stripping the padding. If the padding
1702 * is invalid, avoid leaking this fact through the size of the
1703 * output: use the maximum message size that fits in the output
1704 * buffer. Do it without branches to avoid leaking the padding
1705 * validity through timing. RSA keys are small enough that all the
1706 * size_t values involved fit in unsigned int. */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001707 plaintext_size = if_int( bad,
1708 (unsigned) plaintext_max_size,
Gilles Peskine85a74422018-10-05 14:50:21 +02001709 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001710
Gilles Peskine9265ff42018-10-04 19:13:43 +02001711 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001712 * buffer and to 1 otherwise. */
1713 output_too_large = size_greater_than( plaintext_size,
1714 plaintext_max_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Gilles Peskine9265ff42018-10-04 19:13:43 +02001716 /* Set ret without branches to avoid timing attacks. Return:
1717 * - INVALID_PADDING if the padding is bad (bad != 0).
1718 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1719 * plaintext does not fit in the output buffer.
1720 * - 0 if the padding is correct. */
Gilles Peskine48992472018-10-12 19:19:12 +02001721 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1722 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1723 0 ) );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001724
Gilles Peskine9265ff42018-10-04 19:13:43 +02001725 /* If the padding is bad or the plaintext is too large, zero the
1726 * data that we're about to copy to the output buffer.
1727 * We need to copy the same amount of data
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001728 * from the same buffer whether the padding is good or not to
1729 * avoid leaking the padding validity through overall timing or
1730 * through memory or cache access patterns. */
Gilles Peskine40b57f42018-10-05 15:06:12 +02001731 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001732 for( i = 11; i < ilen; i++ )
Gilles Peskine40b57f42018-10-05 15:06:12 +02001733 buf[i] &= ~bad;
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001734
Gilles Peskine9265ff42018-10-04 19:13:43 +02001735 /* If the plaintext is too large, truncate it to the buffer size.
1736 * Copy anyway to avoid revealing the length through timing, because
1737 * revealing the length is as bad as revealing the padding validity
1738 * for a Bleichenbacher attack. */
1739 plaintext_size = if_int( output_too_large,
1740 (unsigned) plaintext_max_size,
1741 (unsigned) plaintext_size );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001742
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001743 /* Move the plaintext to the leftmost position where it can start in
1744 * the working buffer, i.e. make it start plaintext_max_size from
1745 * the end of the buffer. Do this with a memory access trace that
1746 * does not depend on the plaintext size. After this move, the
1747 * starting location of the plaintext is no longer sensitive
1748 * information. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001749 mem_move_to_left( buf + ilen - plaintext_max_size,
1750 plaintext_max_size,
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001751 plaintext_max_size - plaintext_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001752
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001753 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskine9265ff42018-10-04 19:13:43 +02001754 * into the output buffer. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001755 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001756
1757 /* Report the amount of data we copied to the output buffer. In case
1758 * of errors (bad padding or output too large), the value of *olen
1759 * when this function returns is not specified. Making it equivalent
1760 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001761 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001763cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001764 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001765
1766 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001767}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001769
1770/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001771 * Do an RSA operation, then remove the message padding
1772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001774 int (*f_rng)(void *, unsigned char *, size_t),
1775 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001776 int mode, size_t *olen,
1777 const unsigned char *input,
1778 unsigned char *output,
1779 size_t output_max_len)
1780{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001781 RSA_VALIDATE_RET( ctx != NULL );
1782 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1783 mode == MBEDTLS_RSA_PUBLIC );
1784 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1785 RSA_VALIDATE_RET( input != NULL );
1786 RSA_VALIDATE_RET( olen != NULL );
1787
Paul Bakkerb3869132013-02-28 17:21:01 +01001788 switch( ctx->padding )
1789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790#if defined(MBEDTLS_PKCS1_V15)
1791 case MBEDTLS_RSA_PKCS_V15:
1792 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001793 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001794#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#if defined(MBEDTLS_PKCS1_V21)
1797 case MBEDTLS_RSA_PKCS_V21:
1798 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001799 olen, input, output,
1800 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001801#endif
1802
1803 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001805 }
1806}
1807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001809/*
1810 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001813 int (*f_rng)(void *, unsigned char *, size_t),
1814 void *p_rng,
1815 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001817 unsigned int hashlen,
1818 const unsigned char *hash,
1819 unsigned char *sig )
1820{
1821 size_t olen;
1822 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001824 size_t slen, min_slen, hlen, offset = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001825 int ret;
1826 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 const mbedtls_md_info_t *md_info;
1828 mbedtls_md_context_t md_ctx;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001829 RSA_VALIDATE_RET( ctx != NULL );
1830 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1831 mode == MBEDTLS_RSA_PUBLIC );
1832 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1833 hashlen == 0 ) ||
1834 hash != NULL );
1835 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1838 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001839
1840 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001842
1843 olen = ctx->len;
1844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001846 {
Simon Butcher02037452016-03-01 21:19:12 +00001847 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001849 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001853 }
1854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001856 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001860
Jaeden Amero3725bb22018-09-07 19:12:36 +01001861 /* Calculate the largest possible salt length. Normally this is the hash
1862 * length, which is the maximum length the salt can have. If there is not
1863 * enough room, use the maximum salt length that fits. The constraint is
1864 * that the hash length plus the salt length plus 2 bytes must be at most
1865 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1866 * (PKCS#1 v2.2) §9.1.1 step 3. */
1867 min_slen = hlen - 2;
1868 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001870 else if( olen >= hlen + hlen + 2 )
1871 slen = hlen;
1872 else
1873 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001874
1875 memset( sig, 0, olen );
1876
Simon Butcher02037452016-03-01 21:19:12 +00001877 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001878 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001880
Simon Butcher02037452016-03-01 21:19:12 +00001881 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001882 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001883 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001884 *p++ = 0x01;
1885 memcpy( p, salt, slen );
1886 p += slen;
1887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001889 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001890 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001891
Simon Butcher02037452016-03-01 21:19:12 +00001892 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001893 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1894 goto exit;
1895 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1896 goto exit;
1897 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1898 goto exit;
1899 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1900 goto exit;
1901 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1902 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001903
Simon Butcher02037452016-03-01 21:19:12 +00001904 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001905 if( msb % 8 == 0 )
1906 offset = 1;
1907
Simon Butcher02037452016-03-01 21:19:12 +00001908 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001909 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1910 &md_ctx ) ) != 0 )
1911 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001912
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001913 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001914 sig[0] &= 0xFF >> ( olen * 8 - msb );
1915
1916 p += hlen;
1917 *p++ = 0xBC;
1918
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001919 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001920
1921exit:
1922 mbedtls_md_free( &md_ctx );
1923
1924 if( ret != 0 )
1925 return( ret );
1926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 return( ( mode == MBEDTLS_RSA_PUBLIC )
1928 ? mbedtls_rsa_public( ctx, sig, sig )
1929 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001930}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001934/*
1935 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1936 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001937
1938/* Construct a PKCS v1.5 encoding of a hashed message
1939 *
1940 * This is used both for signature generation and verification.
1941 *
1942 * Parameters:
1943 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001944 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001945 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001946 * - hash: Buffer containing the hashed message or the raw data.
1947 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001948 * - dst: Buffer to hold the encoded message.
1949 *
1950 * Assumptions:
1951 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1952 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001953 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001954 *
1955 */
1956static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1957 unsigned int hashlen,
1958 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001959 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001960 unsigned char *dst )
1961{
1962 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001963 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001964 unsigned char *p = dst;
1965 const char *oid = NULL;
1966
1967 /* Are we signing hashed or raw data? */
1968 if( md_alg != MBEDTLS_MD_NONE )
1969 {
1970 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1971 if( md_info == NULL )
1972 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1973
1974 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1975 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1976
1977 hashlen = mbedtls_md_get_size( md_info );
1978
1979 /* Double-check that 8 + hashlen + oid_size can be used as a
1980 * 1-byte ASN.1 length encoding and that there's no overflow. */
1981 if( 8 + hashlen + oid_size >= 0x80 ||
1982 10 + hashlen < hashlen ||
1983 10 + hashlen + oid_size < 10 + hashlen )
1984 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1985
1986 /*
1987 * Static bounds check:
1988 * - Need 10 bytes for five tag-length pairs.
1989 * (Insist on 1-byte length encodings to protect against variants of
1990 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1991 * - Need hashlen bytes for hash
1992 * - Need oid_size bytes for hash alg OID.
1993 */
1994 if( nb_pad < 10 + hashlen + oid_size )
1995 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1996 nb_pad -= 10 + hashlen + oid_size;
1997 }
1998 else
1999 {
2000 if( nb_pad < hashlen )
2001 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2002
2003 nb_pad -= hashlen;
2004 }
2005
Hanno Becker2b2f8982017-09-27 17:10:03 +01002006 /* Need space for signature header and padding delimiter (3 bytes),
2007 * and 8 bytes for the minimal padding */
2008 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002009 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2010 nb_pad -= 3;
2011
2012 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002013 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002014
2015 /* Write signature header and padding */
2016 *p++ = 0;
2017 *p++ = MBEDTLS_RSA_SIGN;
2018 memset( p, 0xFF, nb_pad );
2019 p += nb_pad;
2020 *p++ = 0;
2021
2022 /* Are we signing raw data? */
2023 if( md_alg == MBEDTLS_MD_NONE )
2024 {
2025 memcpy( p, hash, hashlen );
2026 return( 0 );
2027 }
2028
2029 /* Signing hashed data, add corresponding ASN.1 structure
2030 *
2031 * DigestInfo ::= SEQUENCE {
2032 * digestAlgorithm DigestAlgorithmIdentifier,
2033 * digest Digest }
2034 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2035 * Digest ::= OCTET STRING
2036 *
2037 * Schematic:
2038 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2039 * TAG-NULL + LEN [ NULL ] ]
2040 * TAG-OCTET + LEN [ HASH ] ]
2041 */
2042 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002043 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002044 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002045 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002046 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002047 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002048 memcpy( p, oid, oid_size );
2049 p += oid_size;
2050 *p++ = MBEDTLS_ASN1_NULL;
2051 *p++ = 0x00;
2052 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002053 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002054 memcpy( p, hash, hashlen );
2055 p += hashlen;
2056
2057 /* Just a sanity-check, should be automatic
2058 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002059 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002060 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002061 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002062 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2063 }
2064
2065 return( 0 );
2066}
2067
Paul Bakkerb3869132013-02-28 17:21:01 +01002068/*
2069 * Do an RSA operation to sign the message digest
2070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002072 int (*f_rng)(void *, unsigned char *, size_t),
2073 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002074 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002076 unsigned int hashlen,
2077 const unsigned char *hash,
2078 unsigned char *sig )
2079{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002080 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002081 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002082
Hanno Beckerddeeed72018-12-13 18:07:00 +00002083 RSA_VALIDATE_RET( ctx != NULL );
2084 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2085 mode == MBEDTLS_RSA_PUBLIC );
2086 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2087 hashlen == 0 ) ||
2088 hash != NULL );
2089 RSA_VALIDATE_RET( sig != NULL );
2090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2092 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002093
Hanno Beckerfdf38032017-09-06 12:35:55 +01002094 /*
2095 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2096 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002097
Hanno Beckerfdf38032017-09-06 12:35:55 +01002098 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2099 ctx->len, sig ) ) != 0 )
2100 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002101
2102 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002103 * Call respective RSA primitive
2104 */
2105
2106 if( mode == MBEDTLS_RSA_PUBLIC )
2107 {
2108 /* Skip verification on a public key operation */
2109 return( mbedtls_rsa_public( ctx, sig, sig ) );
2110 }
2111
2112 /* Private key operation
2113 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002114 * In order to prevent Lenstra's attack, make the signature in a
2115 * temporary buffer and check it before returning it.
2116 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002117
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002118 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002119 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002120 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2121
Hanno Beckerfdf38032017-09-06 12:35:55 +01002122 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002123 if( verif == NULL )
2124 {
2125 mbedtls_free( sig_try );
2126 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2127 }
2128
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002129 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2130 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2131
Hanno Becker171a8f12017-09-06 12:32:16 +01002132 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002133 {
2134 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2135 goto cleanup;
2136 }
2137
2138 memcpy( sig, sig_try, ctx->len );
2139
2140cleanup:
2141 mbedtls_free( sig_try );
2142 mbedtls_free( verif );
2143
2144 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002147
2148/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 * Do an RSA operation to sign the message digest
2150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002152 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002153 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002156 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002157 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 unsigned char *sig )
2159{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002160 RSA_VALIDATE_RET( ctx != NULL );
2161 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2162 mode == MBEDTLS_RSA_PUBLIC );
2163 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2164 hashlen == 0 ) ||
2165 hash != NULL );
2166 RSA_VALIDATE_RET( sig != NULL );
2167
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 switch( ctx->padding )
2169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170#if defined(MBEDTLS_PKCS1_V15)
2171 case MBEDTLS_RSA_PKCS_V15:
2172 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002173 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002174#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#if defined(MBEDTLS_PKCS1_V21)
2177 case MBEDTLS_RSA_PKCS_V21:
2178 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002179 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002180#endif
2181
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002185}
2186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002188/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002189 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002192 int (*f_rng)(void *, unsigned char *, size_t),
2193 void *p_rng,
2194 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002196 unsigned int hashlen,
2197 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002199 int expected_salt_len,
2200 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002201{
Paul Bakker23986e52011-04-24 08:57:21 +00002202 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002203 size_t siglen;
2204 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002205 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002207 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002208 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002209 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 const mbedtls_md_info_t *md_info;
2211 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002212 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002213
Hanno Beckerddeeed72018-12-13 18:07:00 +00002214 RSA_VALIDATE_RET( ctx != NULL );
2215 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2216 mode == MBEDTLS_RSA_PUBLIC );
2217 RSA_VALIDATE_RET( sig != NULL );
2218 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2219 hashlen == 0 ) ||
2220 hash != NULL );
2221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2223 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002224
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 siglen = ctx->len;
2226
Paul Bakker27fdf462011-06-09 13:55:13 +00002227 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2231 ? mbedtls_rsa_public( ctx, sig, buf )
2232 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002233
2234 if( ret != 0 )
2235 return( ret );
2236
2237 p = buf;
2238
Paul Bakkerb3869132013-02-28 17:21:01 +01002239 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 {
Simon Butcher02037452016-03-01 21:19:12 +00002244 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002246 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002250 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002253 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002257
Paul Bakkerb3869132013-02-28 17:21:01 +01002258 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002259
Simon Butcher02037452016-03-01 21:19:12 +00002260 /*
2261 * Note: EMSA-PSS verification is over the length of N - 1 bits
2262 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002263 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002264
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002265 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2266 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2267
Simon Butcher02037452016-03-01 21:19:12 +00002268 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002269 if( msb % 8 == 0 )
2270 {
2271 p++;
2272 siglen -= 1;
2273 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002274
Gilles Peskine139108a2017-10-18 19:03:42 +02002275 if( siglen < hlen + 2 )
2276 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2277 hash_start = p + siglen - hlen - 1;
2278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002280 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002281 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002282
Jaeden Amero66954e12018-01-25 16:05:54 +00002283 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2284 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002285 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002286
Paul Bakkerb3869132013-02-28 17:21:01 +01002287 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002288
Gilles Peskine6a54b022017-10-17 19:02:13 +02002289 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002290 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002291
Gilles Peskine91048a32017-10-19 17:46:14 +02002292 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002293 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002294 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2295 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002296 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002297
Gilles Peskine6a54b022017-10-17 19:02:13 +02002298 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002301 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002302 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002303 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2304 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002305 }
2306
Simon Butcher02037452016-03-01 21:19:12 +00002307 /*
2308 * Generate H = Hash( M' )
2309 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002310 ret = mbedtls_md_starts( &md_ctx );
2311 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002312 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002313 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2314 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002315 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002316 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2317 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002318 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002319 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2320 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002321 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002322 ret = mbedtls_md_finish( &md_ctx, result );
2323 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002324 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002325
Jaeden Amero66954e12018-01-25 16:05:54 +00002326 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002327 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002328 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002329 goto exit;
2330 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002331
2332exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002334
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002335 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002336}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002337
2338/*
2339 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002342 int (*f_rng)(void *, unsigned char *, size_t),
2343 void *p_rng,
2344 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002346 unsigned int hashlen,
2347 const unsigned char *hash,
2348 const unsigned char *sig )
2349{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002350 mbedtls_md_type_t mgf1_hash_id;
2351 RSA_VALIDATE_RET( ctx != NULL );
2352 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2353 mode == MBEDTLS_RSA_PUBLIC );
2354 RSA_VALIDATE_RET( sig != NULL );
2355 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2356 hashlen == 0 ) ||
2357 hash != NULL );
2358
2359 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002361 : md_alg;
2362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002364 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002366 sig ) );
2367
2368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002372/*
2373 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002376 int (*f_rng)(void *, unsigned char *, size_t),
2377 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002378 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002380 unsigned int hashlen,
2381 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002382 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002383{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002384 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002385 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002386 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002387
Hanno Beckerddeeed72018-12-13 18:07:00 +00002388 RSA_VALIDATE_RET( ctx != NULL );
2389 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2390 mode == MBEDTLS_RSA_PUBLIC );
2391 RSA_VALIDATE_RET( sig != NULL );
2392 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2393 hashlen == 0 ) ||
2394 hash != NULL );
2395
2396 sig_len = ctx->len;
2397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2399 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002400
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002401 /*
2402 * Prepare expected PKCS1 v1.5 encoding of hash.
2403 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002404
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002405 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2406 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2407 {
2408 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2409 goto cleanup;
2410 }
2411
2412 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2413 encoded_expected ) ) != 0 )
2414 goto cleanup;
2415
2416 /*
2417 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2418 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002421 ? mbedtls_rsa_public( ctx, sig, encoded )
2422 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002423 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002424 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002425
Simon Butcher02037452016-03-01 21:19:12 +00002426 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002427 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002428 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002429
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002430 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2431 sig_len ) ) != 0 )
2432 {
2433 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2434 goto cleanup;
2435 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002436
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002437cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002438
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002439 if( encoded != NULL )
2440 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002441 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002442 mbedtls_free( encoded );
2443 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002444
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002445 if( encoded_expected != NULL )
2446 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002447 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002448 mbedtls_free( encoded_expected );
2449 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002450
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002451 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002452}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
2455/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002456 * Do an RSA operation and check the message digest
2457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002459 int (*f_rng)(void *, unsigned char *, size_t),
2460 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002461 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002463 unsigned int hashlen,
2464 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002465 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002466{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002467 RSA_VALIDATE_RET( ctx != NULL );
2468 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2469 mode == MBEDTLS_RSA_PUBLIC );
2470 RSA_VALIDATE_RET( sig != NULL );
2471 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2472 hashlen == 0 ) ||
2473 hash != NULL );
2474
Paul Bakkerb3869132013-02-28 17:21:01 +01002475 switch( ctx->padding )
2476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477#if defined(MBEDTLS_PKCS1_V15)
2478 case MBEDTLS_RSA_PKCS_V15:
2479 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002480 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002481#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#if defined(MBEDTLS_PKCS1_V21)
2484 case MBEDTLS_RSA_PKCS_V21:
2485 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002486 hashlen, hash, sig );
2487#endif
2488
2489 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002491 }
2492}
2493
2494/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002495 * Copy the components of an RSA key
2496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002498{
2499 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002500 RSA_VALIDATE_RET( dst != NULL );
2501 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002502
2503 dst->ver = src->ver;
2504 dst->len = src->len;
2505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2507 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2510 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2511 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002512
2513#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2515 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2516 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2518 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002519#endif
2520
2521 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2524 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002525
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002526 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002527 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002528
2529cleanup:
2530 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002532
2533 return( ret );
2534}
2535
2536/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002537 * Free the components of an RSA key
2538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002540{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002541 if( ctx == NULL )
2542 return;
2543
irwir2239a862018-06-12 18:25:09 +03002544 mbedtls_mpi_free( &ctx->Vi );
2545 mbedtls_mpi_free( &ctx->Vf );
2546 mbedtls_mpi_free( &ctx->RN );
2547 mbedtls_mpi_free( &ctx->D );
2548 mbedtls_mpi_free( &ctx->Q );
2549 mbedtls_mpi_free( &ctx->P );
2550 mbedtls_mpi_free( &ctx->E );
2551 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002552
Hanno Becker33c30a02017-08-23 07:00:22 +01002553#if !defined(MBEDTLS_RSA_NO_CRT)
irwir2239a862018-06-12 18:25:09 +03002554 mbedtls_mpi_free( &ctx->RQ );
2555 mbedtls_mpi_free( &ctx->RP );
2556 mbedtls_mpi_free( &ctx->QP );
2557 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002558 mbedtls_mpi_free( &ctx->DP );
2559#endif /* MBEDTLS_RSA_NO_CRT */
2560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561#if defined(MBEDTLS_THREADING_C)
2562 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002563#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002564}
2565
Hanno Beckerab377312017-08-23 16:24:51 +01002566#endif /* !MBEDTLS_RSA_ALT */
2567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002570#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
2572/*
2573 * Example RSA-1024 keypair, for test purposes
2574 */
2575#define KEY_LEN 128
2576
2577#define RSA_N "9292758453063D803DD603D5E777D788" \
2578 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2579 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2580 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2581 "93A89813FBF3C4F8066D2D800F7C38A8" \
2582 "1AE31942917403FF4946B0A83D3D3E05" \
2583 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2584 "5E94BB77B07507233A0BC7BAC8F90F79"
2585
2586#define RSA_E "10001"
2587
2588#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2589 "66CA472BC44D253102F8B4A9D3BFA750" \
2590 "91386C0077937FE33FA3252D28855837" \
2591 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2592 "DF79C5CE07EE72C7F123142198164234" \
2593 "CABB724CF78B8173B9F880FC86322407" \
2594 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2595 "071513A1E85B5DFA031F21ECAE91A34D"
2596
2597#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2598 "2C01CAD19EA484A87EA4377637E75500" \
2599 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2600 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2601
2602#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2603 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2604 "910E4168387E3C30AA1E00C339A79508" \
2605 "8452DD96A9A5EA5D9DCA68DA636032AF"
2606
Paul Bakker5121ce52009-01-03 21:22:43 +00002607#define PT_LEN 24
2608#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2609 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002612static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002613{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002614#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002615 size_t i;
2616
Paul Bakker545570e2010-07-18 09:00:25 +00002617 if( rng_state != NULL )
2618 rng_state = NULL;
2619
Paul Bakkera3d195c2011-11-27 21:07:34 +00002620 for( i = 0; i < len; ++i )
2621 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002622#else
2623 if( rng_state != NULL )
2624 rng_state = NULL;
2625
2626 arc4random_buf( output, len );
2627#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002628
Paul Bakkera3d195c2011-11-27 21:07:34 +00002629 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002630}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002632
Paul Bakker5121ce52009-01-03 21:22:43 +00002633/*
2634 * Checkup routine
2635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002637{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002638 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002640 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 unsigned char rsa_plaintext[PT_LEN];
2643 unsigned char rsa_decrypted[PT_LEN];
2644 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002646 unsigned char sha1sum[20];
2647#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Hanno Becker3a701162017-08-22 13:52:43 +01002649 mbedtls_mpi K;
2650
2651 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
Hanno Becker3a701162017-08-22 13:52:43 +01002654 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2655 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2656 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2657 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2658 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2659 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2660 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2661 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2662 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2663 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2664
Hanno Becker7f25f852017-10-10 16:56:22 +01002665 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
2667 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2671 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 {
2673 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Hanno Becker5bc87292017-05-03 15:09:31 +01002676 ret = 1;
2677 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 }
2679
2680 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
2683 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2684
Hanno Becker98838b02017-10-02 13:16:10 +01002685 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2686 PT_LEN, rsa_plaintext,
2687 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 {
2689 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Hanno Becker5bc87292017-05-03 15:09:31 +01002692 ret = 1;
2693 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 }
2695
2696 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Hanno Becker98838b02017-10-02 13:16:10 +01002699 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2700 &len, rsa_ciphertext, rsa_decrypted,
2701 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 {
2703 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Hanno Becker5bc87292017-05-03 15:09:31 +01002706 ret = 1;
2707 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 }
2709
2710 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2711 {
2712 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
Hanno Becker5bc87292017-05-03 15:09:31 +01002715 ret = 1;
2716 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 }
2718
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002719 if( verbose != 0 )
2720 mbedtls_printf( "passed\n" );
2721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002724 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002726 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002727 {
2728 if( verbose != 0 )
2729 mbedtls_printf( "failed\n" );
2730
2731 return( 1 );
2732 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
Hanno Becker98838b02017-10-02 13:16:10 +01002734 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2735 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2736 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 {
2738 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Hanno Becker5bc87292017-05-03 15:09:31 +01002741 ret = 1;
2742 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 }
2744
2745 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002747
Hanno Becker98838b02017-10-02 13:16:10 +01002748 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2749 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2750 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 {
2752 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002754
Hanno Becker5bc87292017-05-03 15:09:31 +01002755 ret = 1;
2756 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 }
2758
2759 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002760 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002763 if( verbose != 0 )
2764 mbedtls_printf( "\n" );
2765
Paul Bakker3d8fb632014-04-17 12:42:41 +02002766cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002767 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 mbedtls_rsa_free( &rsa );
2769#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002770 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002772 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002773}
2774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#endif /* MBEDTLS_RSA_C */