blob: af1cef6515bf810788c248a1ab9f2b4afafad2d3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
Hanno Becker74716312017-10-02 10:00:37 +010048
Paul Bakker5121ce52009-01-03 21:22:43 +000049/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000050 * The following sources were referenced in the design of this implementation
51 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000052 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000053 * [1] A method for obtaining digital signatures and public-key cryptosystems
54 * R Rivest, A Shamir, and L Adleman
55 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
56 *
57 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
58 * Menezes, van Oorschot and Vanstone
59 *
Janos Follathe81102e2017-03-22 13:38:28 +000060 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
61 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
62 * Stefan Mangard
63 * https://arxiv.org/abs/1702.08719v2
64 *
Paul Bakker5121ce52009-01-03 21:22:43 +000065 */
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020069#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010076#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050078#include "mbedtls/platform_util.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000079
Rich Evans00ab4702015-02-06 13:43:58 +000080#include <string.h>
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000083#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000084#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000087#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000088#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000091#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010092#else
Rich Evans00ab4702015-02-06 13:43:58 +000093#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020095#define mbedtls_calloc calloc
96#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010097#endif
98
Hanno Beckera565f542017-10-11 11:00:19 +010099#if !defined(MBEDTLS_RSA_ALT)
100
Hanno Beckerddeeed72018-12-13 18:07:00 +0000101/* Parameter validation macros */
102#define RSA_VALIDATE_RET( cond ) \
103 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
104#define RSA_VALIDATE( cond ) \
105 MBEDTLS_INTERNAL_VALIDATE( cond )
106
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100107#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +0100108/* constant-time buffer comparison */
109static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
110{
111 size_t i;
112 const unsigned char *A = (const unsigned char *) a;
113 const unsigned char *B = (const unsigned char *) b;
114 unsigned char diff = 0;
115
116 for( i = 0; i < n; i++ )
117 diff |= A[i] ^ B[i];
118
119 return( diff );
120}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100121#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +0100122
Hanno Becker617c1ae2017-08-23 14:11:24 +0100123int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
124 const mbedtls_mpi *N,
125 const mbedtls_mpi *P, const mbedtls_mpi *Q,
126 const mbedtls_mpi *D, const mbedtls_mpi *E )
127{
128 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000129 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100130
131 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
132 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
133 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
134 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
135 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
136 {
137 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
138 }
139
140 if( N != NULL )
141 ctx->len = mbedtls_mpi_size( &ctx->N );
142
143 return( 0 );
144}
145
146int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100147 unsigned char const *N, size_t N_len,
148 unsigned char const *P, size_t P_len,
149 unsigned char const *Q, size_t Q_len,
150 unsigned char const *D, size_t D_len,
151 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100152{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000153 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000154 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100155
156 if( N != NULL )
157 {
158 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
159 ctx->len = mbedtls_mpi_size( &ctx->N );
160 }
161
162 if( P != NULL )
163 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
164
165 if( Q != NULL )
166 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
167
168 if( D != NULL )
169 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
170
171 if( E != NULL )
172 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
173
174cleanup:
175
176 if( ret != 0 )
177 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
178
179 return( 0 );
180}
181
Hanno Becker705fc682017-10-10 17:57:02 +0100182/*
183 * Checks whether the context fields are set in such a way
184 * that the RSA primitives will be able to execute without error.
185 * It does *not* make guarantees for consistency of the parameters.
186 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100187static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
188 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100189{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100190#if !defined(MBEDTLS_RSA_NO_CRT)
191 /* blinding_needed is only used for NO_CRT to decide whether
192 * P,Q need to be present or not. */
193 ((void) blinding_needed);
194#endif
195
Hanno Becker3a760a12018-01-05 08:14:49 +0000196 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
197 ctx->len > MBEDTLS_MPI_MAX_SIZE )
198 {
Hanno Becker705fc682017-10-10 17:57:02 +0100199 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000200 }
Hanno Becker705fc682017-10-10 17:57:02 +0100201
202 /*
203 * 1. Modular exponentiation needs positive, odd moduli.
204 */
205
206 /* Modular exponentiation wrt. N is always used for
207 * RSA public key operations. */
208 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
209 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
210 {
211 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
212 }
213
214#if !defined(MBEDTLS_RSA_NO_CRT)
215 /* Modular exponentiation for P and Q is only
216 * used for private key operations and if CRT
217 * is used. */
218 if( is_priv &&
219 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
220 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
221 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
222 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
223 {
224 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
225 }
226#endif /* !MBEDTLS_RSA_NO_CRT */
227
228 /*
229 * 2. Exponents must be positive
230 */
231
232 /* Always need E for public key operations */
233 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
234 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
235
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100236#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100237 /* For private key operations, use D or DP & DQ
238 * as (unblinded) exponents. */
239 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
240 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
241#else
242 if( is_priv &&
243 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
244 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
245 {
246 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
247 }
248#endif /* MBEDTLS_RSA_NO_CRT */
249
250 /* Blinding shouldn't make exponents negative either,
251 * so check that P, Q >= 1 if that hasn't yet been
252 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100253#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100254 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100255 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
256 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
257 {
258 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
259 }
260#endif
261
262 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100263 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100264#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100265 if( is_priv &&
266 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
267 {
268 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
269 }
270#endif
271
272 return( 0 );
273}
274
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100275int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100276{
277 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000278 int have_N, have_P, have_Q, have_D, have_E;
Jack Lloydb10fd062020-01-29 13:09:55 -0500279#if !defined(MBEDTLS_RSA_NO_CRT)
280 int have_DP, have_DQ, have_QP;
281#endif
Hanno Beckerddeeed72018-12-13 18:07:00 +0000282 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100283
Hanno Beckerddeeed72018-12-13 18:07:00 +0000284 RSA_VALIDATE_RET( ctx != NULL );
285
286 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
287 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
288 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
289 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
290 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100291
Jack Lloydb10fd062020-01-29 13:09:55 -0500292#if !defined(MBEDTLS_RSA_NO_CRT)
293 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
294 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
295 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
296#endif
297
Hanno Becker617c1ae2017-08-23 14:11:24 +0100298 /*
299 * Check whether provided parameters are enough
300 * to deduce all others. The following incomplete
301 * parameter sets for private keys are supported:
302 *
303 * (1) P, Q missing.
304 * (2) D and potentially N missing.
305 *
306 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100307
Hanno Beckerddeeed72018-12-13 18:07:00 +0000308 n_missing = have_P && have_Q && have_D && have_E;
309 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
310 d_missing = have_P && have_Q && !have_D && have_E;
311 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100312
313 /* These three alternatives are mutually exclusive */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000314 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100315
Hanno Becker617c1ae2017-08-23 14:11:24 +0100316 if( !is_priv && !is_pub )
317 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
318
319 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100320 * Step 1: Deduce N if P, Q are provided.
321 */
322
323 if( !have_N && have_P && have_Q )
324 {
325 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
326 &ctx->Q ) ) != 0 )
327 {
328 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
329 }
330
331 ctx->len = mbedtls_mpi_size( &ctx->N );
332 }
333
334 /*
335 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100336 */
337
338 if( pq_missing )
339 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100340 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100341 &ctx->P, &ctx->Q );
342 if( ret != 0 )
343 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
344
345 }
346 else if( d_missing )
347 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100348 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
349 &ctx->Q,
350 &ctx->E,
351 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100352 {
353 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
354 }
355 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100356
Hanno Becker617c1ae2017-08-23 14:11:24 +0100357 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100358 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100359 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100360 */
361
Hanno Becker23344b52017-08-23 07:43:27 +0100362#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloydb10fd062020-01-29 13:09:55 -0500363 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100364 {
365 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
366 &ctx->DP, &ctx->DQ, &ctx->QP );
367 if( ret != 0 )
368 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
369 }
Hanno Becker23344b52017-08-23 07:43:27 +0100370#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100371
372 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100373 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100374 */
375
Hanno Beckerebd2c022017-10-12 10:54:53 +0100376 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100377}
378
Hanno Becker617c1ae2017-08-23 14:11:24 +0100379int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
380 unsigned char *N, size_t N_len,
381 unsigned char *P, size_t P_len,
382 unsigned char *Q, size_t Q_len,
383 unsigned char *D, size_t D_len,
384 unsigned char *E, size_t E_len )
385{
386 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000387 int is_priv;
388 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100389
390 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000391 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100392 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
393 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
394 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
395 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
396 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
397
398 if( !is_priv )
399 {
400 /* If we're trying to export private parameters for a public key,
401 * something must be wrong. */
402 if( P != NULL || Q != NULL || D != NULL )
403 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
404
405 }
406
407 if( N != NULL )
408 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
409
410 if( P != NULL )
411 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
412
413 if( Q != NULL )
414 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
415
416 if( D != NULL )
417 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
418
419 if( E != NULL )
420 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100421
422cleanup:
423
424 return( ret );
425}
426
Hanno Becker617c1ae2017-08-23 14:11:24 +0100427int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
428 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
429 mbedtls_mpi *D, mbedtls_mpi *E )
430{
431 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000432 int is_priv;
433 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100434
435 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000436 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100437 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
438 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
439 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
440 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
441 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
442
443 if( !is_priv )
444 {
445 /* If we're trying to export private parameters for a public key,
446 * something must be wrong. */
447 if( P != NULL || Q != NULL || D != NULL )
448 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
449
450 }
451
452 /* Export all requested core parameters. */
453
454 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
455 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
456 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
457 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
458 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
459 {
460 return( ret );
461 }
462
463 return( 0 );
464}
465
466/*
467 * Export CRT parameters
468 * This must also be implemented if CRT is not used, for being able to
469 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
470 * can be used in this case.
471 */
472int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
473 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
474{
475 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000476 int is_priv;
477 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100478
479 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000480 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100481 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
482 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
483 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
484 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
485 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
486
487 if( !is_priv )
488 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
489
Hanno Beckerdc95c892017-08-23 06:57:02 +0100490#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100491 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100492 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
493 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
494 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
495 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100496 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100497 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100498#else
499 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
500 DP, DQ, QP ) ) != 0 )
501 {
502 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
503 }
504#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100505
506 return( 0 );
507}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100508
Paul Bakker5121ce52009-01-03 21:22:43 +0000509/*
510 * Initialize an RSA context
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000514 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000515{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000516 RSA_VALIDATE( ctx != NULL );
517 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
518 padding == MBEDTLS_RSA_PKCS_V21 );
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_THREADING_C)
525 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200526#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000527}
528
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100529/*
530 * Set padding for an existing RSA context
531 */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000532void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
533 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100534{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000535 RSA_VALIDATE( ctx != NULL );
536 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
537 padding == MBEDTLS_RSA_PKCS_V21 );
538
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100539 ctx->padding = padding;
540 ctx->hash_id = hash_id;
541}
542
Hanno Becker617c1ae2017-08-23 14:11:24 +0100543/*
544 * Get length in bytes of RSA modulus
545 */
546
547size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
548{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100549 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100550}
551
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555/*
556 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800557 *
558 * This generation method follows the RSA key pair generation procedure of
559 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000562 int (*f_rng)(void *, unsigned char *, size_t),
563 void *p_rng,
564 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000565{
566 int ret;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800567 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100568 int prime_quality = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000569 RSA_VALIDATE_RET( ctx != NULL );
570 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Hanno Beckerddeeed72018-12-13 18:07:00 +0000572 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
Janos Follathef441782016-09-21 13:18:12 +0100573 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
574
Janos Follathb8fc1b02018-09-03 15:37:01 +0100575 /*
576 * If the modulus is 1024 bit long or shorter, then the security strength of
577 * the RSA algorithm is less than or equal to 80 bits and therefore an error
578 * rate of 2^-80 is sufficient.
579 */
580 if( nbits > 1024 )
581 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
582
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100583 mbedtls_mpi_init( &H );
584 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800585 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587 /*
588 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800589 * 1. |P-Q| > 2^( nbits / 2 - 100 )
590 * 2. GCD( E, (P-1)*(Q-1) ) == 1
591 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595 do
596 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100597 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
598 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
Janos Follathb8fc1b02018-09-03 15:37:01 +0100600 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
601 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800603 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
604 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
605 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 continue;
607
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800608 /* not required by any standards, but some users rely on the fact that P > Q */
609 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100610 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100611
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100612 /* Temporarily replace P,Q by P-1, Q-1 */
613 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
614 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
615 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800616
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800617 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800619 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
620 continue;
621
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800622 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Jethro Beekman97f95c92018-02-13 15:50:36 -0800623 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
624 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
625 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
626
627 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
628 continue;
629
630 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800632 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100634 /* Restore P,Q */
635 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
636 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
637
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800638 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
639
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100640 ctx->len = mbedtls_mpi_size( &ctx->N );
641
Jethro Beekman97f95c92018-02-13 15:50:36 -0800642#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 * DP = D mod (P - 1)
645 * DQ = D mod (Q - 1)
646 * QP = Q^-1 mod P
647 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100648 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
649 &ctx->DP, &ctx->DQ, &ctx->QP ) );
650#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Hanno Becker83aad1f2017-08-23 06:45:10 +0100652 /* Double-check */
653 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
655cleanup:
656
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100657 mbedtls_mpi_free( &H );
658 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800659 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
661 if( ret != 0 )
662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_rsa_free( ctx );
664 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 }
666
Paul Bakker48377d92013-08-30 12:06:24 +0200667 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668}
669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672/*
673 * Check a public RSA key
674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000676{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000677 RSA_VALIDATE_RET( ctx != NULL );
678
Hanno Beckerebd2c022017-10-12 10:54:53 +0100679 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000681
Hanno Becker3a760a12018-01-05 08:14:49 +0000682 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100685 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000686
Hanno Becker705fc682017-10-10 17:57:02 +0100687 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
688 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100692 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
694 return( 0 );
695}
696
697/*
Hanno Becker705fc682017-10-10 17:57:02 +0100698 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000701{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000702 RSA_VALIDATE_RET( ctx != NULL );
703
Hanno Becker705fc682017-10-10 17:57:02 +0100704 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100705 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 {
Hanno Becker98838b02017-10-02 13:16:10 +0100707 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 }
Paul Bakker48377d92013-08-30 12:06:24 +0200709
Hanno Becker98838b02017-10-02 13:16:10 +0100710 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100711 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100713 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000715
Hanno Beckerb269a852017-08-25 08:03:21 +0100716#if !defined(MBEDTLS_RSA_NO_CRT)
717 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
718 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
719 {
720 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
721 }
722#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000723
724 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000725}
726
727/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100728 * Check if contexts holding a public and private key match
729 */
Hanno Becker98838b02017-10-02 13:16:10 +0100730int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
731 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100732{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000733 RSA_VALIDATE_RET( pub != NULL );
734 RSA_VALIDATE_RET( prv != NULL );
735
Hanno Becker98838b02017-10-02 13:16:10 +0100736 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100740 }
741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
743 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100746 }
747
748 return( 0 );
749}
750
751/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000752 * Do an RSA public key operation
753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000755 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 unsigned char *output )
757{
Paul Bakker23986e52011-04-24 08:57:21 +0000758 int ret;
759 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_mpi T;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000761 RSA_VALIDATE_RET( ctx != NULL );
762 RSA_VALIDATE_RET( input != NULL );
763 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000764
Hanno Beckerebd2c022017-10-12 10:54:53 +0100765 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100766 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200770#if defined(MBEDTLS_THREADING_C)
771 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
772 return( ret );
773#endif
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000778 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200779 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
780 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000781 }
782
783 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
785 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
787cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200789 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
790 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100791#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
795 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
798 return( 0 );
799}
800
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200801/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200802 * Generate or update blinding values, see section 10 of:
803 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200804 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200805 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200806 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200807static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200808 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
809{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200810 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200811
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200812 if( ctx->Vf.p != NULL )
813 {
814 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
816 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
817 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
818 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200819
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200820 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200821 }
822
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200823 /* Unblinding value: Vf = random number, invertible mod N */
824 do {
825 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
829 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
830 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200831
832 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
834 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 +0200835
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200836
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200837cleanup:
838 return( ret );
839}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200840
Paul Bakker5121ce52009-01-03 21:22:43 +0000841/*
Janos Follathe81102e2017-03-22 13:38:28 +0000842 * Exponent blinding supposed to prevent side-channel attacks using multiple
843 * traces of measurements to recover the RSA key. The more collisions are there,
844 * the more bits of the key can be recovered. See [3].
845 *
846 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
847 * observations on avarage.
848 *
849 * For example with 28 byte blinding to achieve 2 collisions the adversary has
850 * to make 2^112 observations on avarage.
851 *
852 * (With the currently (as of 2017 April) known best algorithms breaking 2048
853 * bit RSA requires approximately as much time as trying out 2^112 random keys.
854 * Thus in this sense with 28 byte blinding the security is not reduced by
855 * side-channel attacks like the one in [3])
856 *
857 * This countermeasure does not help if the key recovery is possible with a
858 * single trace.
859 */
860#define RSA_EXPONENT_BLINDING 28
861
862/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 * Do an RSA private key operation
864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200866 int (*f_rng)(void *, unsigned char *, size_t),
867 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000868 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 unsigned char *output )
870{
Paul Bakker23986e52011-04-24 08:57:21 +0000871 int ret;
872 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100873
874 /* Temporary holding the result */
875 mbedtls_mpi T;
876
877 /* Temporaries holding P-1, Q-1 and the
878 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000879 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100880
881#if !defined(MBEDTLS_RSA_NO_CRT)
882 /* Temporaries holding the results mod p resp. mod q. */
883 mbedtls_mpi TP, TQ;
884
885 /* Temporaries holding the blinded exponents for
886 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000887 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100888
889 /* Pointers to actual exponents to be used - either the unblinded
890 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000891 mbedtls_mpi *DP = &ctx->DP;
892 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100893#else
894 /* Temporary holding the blinded exponent (if used). */
895 mbedtls_mpi D_blind;
896
897 /* Pointer to actual exponent to be used - either the unblinded
898 * or the blinded one, depending on the presence of a PRNG. */
899 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100900#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100901
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100902 /* Temporaries holding the initial input and the double
903 * checked result; should be the same in the end. */
904 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Hanno Beckerddeeed72018-12-13 18:07:00 +0000906 RSA_VALIDATE_RET( ctx != NULL );
907 RSA_VALIDATE_RET( input != NULL );
908 RSA_VALIDATE_RET( output != NULL );
909
Hanno Beckerebd2c022017-10-12 10:54:53 +0100910 if( rsa_check_context( ctx, 1 /* private key checks */,
911 f_rng != NULL /* blinding y/n */ ) != 0 )
912 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100913 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100914 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100915
Hanno Becker06811ce2017-05-03 15:10:34 +0100916#if defined(MBEDTLS_THREADING_C)
917 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
918 return( ret );
919#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000920
Hanno Becker06811ce2017-05-03 15:10:34 +0100921 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100922 mbedtls_mpi_init( &T );
923
924 mbedtls_mpi_init( &P1 );
925 mbedtls_mpi_init( &Q1 );
926 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000927
Janos Follathf9203b42017-03-22 15:13:15 +0000928 if( f_rng != NULL )
929 {
Janos Follathe81102e2017-03-22 13:38:28 +0000930#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000931 mbedtls_mpi_init( &D_blind );
932#else
933 mbedtls_mpi_init( &DP_blind );
934 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000935#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000936 }
Janos Follathe81102e2017-03-22 13:38:28 +0000937
Hanno Becker06811ce2017-05-03 15:10:34 +0100938#if !defined(MBEDTLS_RSA_NO_CRT)
939 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200940#endif
941
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100942 mbedtls_mpi_init( &I );
943 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100944
945 /* End of MPI initialization */
946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
948 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000949 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200950 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
951 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 }
953
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100954 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100955
Paul Bakkerf451bac2013-08-30 15:37:02 +0200956 if( f_rng != NULL )
957 {
958 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200959 * Blinding
960 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200961 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200962 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
963 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000965
Janos Follathe81102e2017-03-22 13:38:28 +0000966 /*
967 * Exponent blinding
968 */
969 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
970 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
971
Janos Follathf9203b42017-03-22 15:13:15 +0000972#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000973 /*
974 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
975 */
976 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
977 f_rng, p_rng ) );
978 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
979 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
980 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
981
982 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000983#else
984 /*
985 * DP_blind = ( P - 1 ) * R + DP
986 */
987 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
988 f_rng, p_rng ) );
989 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
990 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
991 &ctx->DP ) );
992
993 DP = &DP_blind;
994
995 /*
996 * DQ_blind = ( Q - 1 ) * R + DQ
997 */
998 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
999 f_rng, p_rng ) );
1000 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1001 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1002 &ctx->DQ ) );
1003
1004 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001005#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001006 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001009 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001010#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001011 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001012 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001014 * TP = input ^ dP mod P
1015 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001016 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001017
1018 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1019 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
1021 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001022 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001024 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1025 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1026 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
1028 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001029 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001031 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1032 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001034
Paul Bakkerf451bac2013-08-30 15:37:02 +02001035 if( f_rng != NULL )
1036 {
1037 /*
1038 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001039 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001040 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001041 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001043 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
Hanno Becker2dec5e82017-10-03 07:49:52 +01001045 /* Verify the result to prevent glitching attacks. */
1046 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1047 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001048 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001049 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001050 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1051 goto cleanup;
1052 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001053
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
1057cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001059 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1060 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001061#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001062
Hanno Becker06811ce2017-05-03 15:10:34 +01001063 mbedtls_mpi_free( &P1 );
1064 mbedtls_mpi_free( &Q1 );
1065 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001066
1067 if( f_rng != NULL )
1068 {
Janos Follathe81102e2017-03-22 13:38:28 +00001069#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001070 mbedtls_mpi_free( &D_blind );
1071#else
1072 mbedtls_mpi_free( &DP_blind );
1073 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001074#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001075 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Hanno Becker06811ce2017-05-03 15:10:34 +01001077 mbedtls_mpi_free( &T );
1078
1079#if !defined(MBEDTLS_RSA_NO_CRT)
1080 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1081#endif
1082
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001083 mbedtls_mpi_free( &C );
1084 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001085
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
1089 return( 0 );
1090}
1091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001093/**
1094 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1095 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001096 * \param dst buffer to mask
1097 * \param dlen length of destination buffer
1098 * \param src source of the mask generation
1099 * \param slen length of the source buffer
1100 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001101 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001102static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001106 unsigned char counter[4];
1107 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001108 unsigned int hlen;
1109 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001110 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001113 memset( counter, 0, 4 );
1114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001116
Simon Butcher02037452016-03-01 21:19:12 +00001117 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001118 p = dst;
1119
1120 while( dlen > 0 )
1121 {
1122 use_len = hlen;
1123 if( dlen < hlen )
1124 use_len = dlen;
1125
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001126 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1127 goto exit;
1128 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1129 goto exit;
1130 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1131 goto exit;
1132 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1133 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001134
1135 for( i = 0; i < use_len; ++i )
1136 *p++ ^= mask[i];
1137
1138 counter[3]++;
1139
1140 dlen -= use_len;
1141 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001142
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001143exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001144 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001145
1146 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001151/*
1152 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001155 int (*f_rng)(void *, unsigned char *, size_t),
1156 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001157 int mode,
1158 const unsigned char *label, size_t label_len,
1159 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001160 const unsigned char *input,
1161 unsigned char *output )
1162{
1163 size_t olen;
1164 int ret;
1165 unsigned char *p = output;
1166 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 const mbedtls_md_info_t *md_info;
1168 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001169
Hanno Beckerddeeed72018-12-13 18:07:00 +00001170 RSA_VALIDATE_RET( ctx != NULL );
1171 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1172 mode == MBEDTLS_RSA_PUBLIC );
1173 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001174 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001175 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1178 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001179
1180 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001184 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001186
1187 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001189
Simon Butcher02037452016-03-01 21:19:12 +00001190 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001191 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001193
1194 memset( output, 0, olen );
1195
1196 *p++ = 0;
1197
Simon Butcher02037452016-03-01 21:19:12 +00001198 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001199 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001201
1202 p += hlen;
1203
Simon Butcher02037452016-03-01 21:19:12 +00001204 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001205 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1206 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001207 p += hlen;
1208 p += olen - 2 * hlen - 2 - ilen;
1209 *p++ = 1;
1210 memcpy( p, input, ilen );
1211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001213 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001214 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001215
Simon Butcher02037452016-03-01 21:19:12 +00001216 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001217 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1218 &md_ctx ) ) != 0 )
1219 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001220
Simon Butcher02037452016-03-01 21:19:12 +00001221 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001222 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1223 &md_ctx ) ) != 0 )
1224 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001225
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001226exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001228
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001229 if( ret != 0 )
1230 return( ret );
1231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 return( ( mode == MBEDTLS_RSA_PUBLIC )
1233 ? mbedtls_rsa_public( ctx, output, output )
1234 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001239/*
1240 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001243 int (*f_rng)(void *, unsigned char *, size_t),
1244 void *p_rng,
1245 int mode, size_t ilen,
1246 const unsigned char *input,
1247 unsigned char *output )
1248{
1249 size_t nb_pad, olen;
1250 int ret;
1251 unsigned char *p = output;
1252
Hanno Beckerddeeed72018-12-13 18:07:00 +00001253 RSA_VALIDATE_RET( ctx != NULL );
1254 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1255 mode == MBEDTLS_RSA_PUBLIC );
1256 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001257 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1260 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001261
Paul Bakkerb3869132013-02-28 17:21:01 +01001262 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001263
Simon Butcher02037452016-03-01 21:19:12 +00001264 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001265 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001267
1268 nb_pad = olen - 3 - ilen;
1269
1270 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001272 {
Hanno Beckerb86e6842018-12-18 14:46:04 +00001273 if( f_rng == NULL )
1274 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001277
1278 while( nb_pad-- > 0 )
1279 {
1280 int rng_dl = 100;
1281
1282 do {
1283 ret = f_rng( p_rng, p, 1 );
1284 } while( *p == 0 && --rng_dl && ret == 0 );
1285
Simon Butcher02037452016-03-01 21:19:12 +00001286 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001287 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001289
1290 p++;
1291 }
1292 }
1293 else
1294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001296
1297 while( nb_pad-- > 0 )
1298 *p++ = 0xFF;
1299 }
1300
1301 *p++ = 0;
1302 memcpy( p, input, ilen );
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 return( ( mode == MBEDTLS_RSA_PUBLIC )
1305 ? mbedtls_rsa_public( ctx, output, output )
1306 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001309
Paul Bakker5121ce52009-01-03 21:22:43 +00001310/*
1311 * Add the message padding, then do an RSA operation
1312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001314 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001315 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001316 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001317 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 unsigned char *output )
1319{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001320 RSA_VALIDATE_RET( ctx != NULL );
1321 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1322 mode == MBEDTLS_RSA_PUBLIC );
1323 RSA_VALIDATE_RET( output != NULL );
Hanno Becker2f660d02018-12-18 17:04:59 +00001324 RSA_VALIDATE_RET( input != NULL );
Hanno Beckerddeeed72018-12-13 18:07:00 +00001325
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 switch( ctx->padding )
1327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_PKCS1_V15)
1329 case MBEDTLS_RSA_PKCS_V15:
1330 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001331 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001332#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#if defined(MBEDTLS_PKCS1_V21)
1335 case MBEDTLS_RSA_PKCS_V21:
1336 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001337 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001338#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
1340 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001343}
1344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001346/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001347 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001350 int (*f_rng)(void *, unsigned char *, size_t),
1351 void *p_rng,
1352 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001353 const unsigned char *label, size_t label_len,
1354 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001355 const unsigned char *input,
1356 unsigned char *output,
1357 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001358{
Paul Bakker23986e52011-04-24 08:57:21 +00001359 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001360 size_t ilen, i, pad_len;
1361 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1363 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001364 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 const mbedtls_md_info_t *md_info;
1366 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001367
Hanno Beckerddeeed72018-12-13 18:07:00 +00001368 RSA_VALIDATE_RET( ctx != NULL );
1369 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1370 mode == MBEDTLS_RSA_PUBLIC );
1371 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1372 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1373 RSA_VALIDATE_RET( input != NULL );
1374 RSA_VALIDATE_RET( olen != NULL );
1375
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001376 /*
1377 * Parameters sanity checks
1378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1380 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
1382 ilen = ctx->len;
1383
Paul Bakker27fdf462011-06-09 13:55:13 +00001384 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001388 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001390
Janos Follathc17cda12016-02-11 11:08:18 +00001391 hlen = mbedtls_md_get_size( md_info );
1392
1393 // checking for integer underflow
1394 if( 2 * hlen + 2 > ilen )
1395 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1396
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001397 /*
1398 * RSA operation
1399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1401 ? mbedtls_rsa_public( ctx, input, buf )
1402 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
1404 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001405 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001407 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001408 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001411 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1412 {
1413 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001414 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001415 }
1416
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001417 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001418 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1419 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001420 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001421 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1422 &md_ctx ) ) != 0 )
1423 {
1424 mbedtls_md_free( &md_ctx );
1425 goto cleanup;
1426 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001429
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001430 /* Generate lHash */
1431 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1432 goto cleanup;
1433
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001434 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001435 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001436 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001438 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001439
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001440 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001441
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001442 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001443
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001444 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001445 for( i = 0; i < hlen; i++ )
1446 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001447
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001448 /* Get zero-padding len, but always read till end of buffer
1449 * (minus one, for the 01 byte) */
1450 pad_len = 0;
1451 pad_done = 0;
1452 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1453 {
1454 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001455 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001456 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001457
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001458 p += pad_len;
1459 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001460
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001461 /*
1462 * The only information "leaked" is whether the padding was correct or not
1463 * (eg, no data is copied if it was not correct). This meets the
1464 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1465 * the different error conditions.
1466 */
1467 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001468 {
1469 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1470 goto cleanup;
1471 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001472
Paul Bakker66d5d072014-06-17 16:39:18 +02001473 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001474 {
1475 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1476 goto cleanup;
1477 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001478
1479 *olen = ilen - (p - buf);
1480 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001481 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001482
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001483cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001484 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1485 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001486
1487 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001492/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1493 *
1494 * \param value The value to analyze.
Gilles Peskine331d80e2018-10-04 18:32:29 +02001495 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001496 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001497static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001498{
1499 /* MSVC has a warning about unary minus on unsigned, but this is
1500 * well-defined and precisely what we want to do here */
1501#if defined(_MSC_VER)
1502#pragma warning( push )
1503#pragma warning( disable : 4146 )
1504#endif
1505 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1506#if defined(_MSC_VER)
1507#pragma warning( pop )
1508#endif
1509}
1510
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001511/** Check whether a size is out of bounds, without branches.
1512 *
1513 * This is equivalent to `size > max`, but is likely to be compiled to
1514 * to code using bitwise operation rather than a branch.
1515 *
1516 * \param size Size to check.
1517 * \param max Maximum desired value for \p size.
1518 * \return \c 0 if `size <= max`.
1519 * \return \c 1 if `size > max`.
1520 */
1521static unsigned size_greater_than( size_t size, size_t max )
1522{
1523 /* Return the sign bit (1 for negative) of (max - size). */
1524 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1525}
1526
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001527/** Choose between two integer values, without branches.
1528 *
Gilles Peskine331d80e2018-10-04 18:32:29 +02001529 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1530 * to code using bitwise operation rather than a branch.
1531 *
1532 * \param cond Condition to test.
1533 * \param if1 Value to use if \p cond is nonzero.
1534 * \param if0 Value to use if \p cond is zero.
1535 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001536 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001537static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001538{
Gilles Peskine331d80e2018-10-04 18:32:29 +02001539 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001540 return( ( mask & if1 ) | (~mask & if0 ) );
1541}
1542
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001543/** Shift some data towards the left inside a buffer without leaking
1544 * the length of the data through side channels.
1545 *
1546 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1547 * ```
1548 * memmove(start, start + offset, total - offset);
1549 * memset(start + offset, 0, total - offset);
1550 * ```
1551 * but it strives to use a memory access pattern (and thus total timing)
1552 * that does not depend on \p offset. This timing independence comes at
1553 * the expense of performance.
1554 *
1555 * \param start Pointer to the start of the buffer.
1556 * \param total Total size of the buffer.
1557 * \param offset Offset from which to copy \p total - \p offset bytes.
1558 */
1559static void mem_move_to_left( void *start,
1560 size_t total,
1561 size_t offset )
1562{
1563 volatile unsigned char *buf = start;
1564 size_t i, n;
1565 if( total == 0 )
1566 return;
1567 for( i = 0; i < total; i++ )
1568 {
1569 unsigned no_op = size_greater_than( total - offset, i );
1570 /* The first `total - offset` passes are a no-op. The last
1571 * `offset` passes shift the data one byte to the left and
1572 * zero out the last byte. */
1573 for( n = 0; n < total - 1; n++ )
Gilles Peskine9b430702018-10-12 19:15:34 +02001574 {
1575 unsigned char current = buf[n];
1576 unsigned char next = buf[n+1];
1577 buf[n] = if_int( no_op, current, next );
1578 }
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001579 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1580 }
1581}
1582
Paul Bakkerb3869132013-02-28 17:21:01 +01001583/*
1584 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001587 int (*f_rng)(void *, unsigned char *, size_t),
1588 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001589 int mode, size_t *olen,
1590 const unsigned char *input,
1591 unsigned char *output,
Gilles Peskine5908dd42018-10-02 22:43:06 +02001592 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001593{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001594 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001595 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine85a74422018-10-05 14:50:21 +02001597 /* The following variables take sensitive values: their value must
1598 * not leak into the observable behavior of the function other than
1599 * the designated outputs (output, olen, return value). Otherwise
1600 * this would open the execution of the function to
1601 * side-channel-based variants of the Bleichenbacher padding oracle
1602 * attack. Potential side channels include overall timing, memory
1603 * access patterns (especially visible to an adversary who has access
1604 * to a shared memory cache), and branches (especially visible to
1605 * an adversary who has access to a shared code cache or to a shared
1606 * branch predictor). */
1607 size_t pad_count = 0;
1608 unsigned bad = 0;
1609 unsigned char pad_done = 0;
1610 size_t plaintext_size = 0;
1611 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001612
Hanno Beckerddeeed72018-12-13 18:07:00 +00001613 RSA_VALIDATE_RET( ctx != NULL );
1614 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1615 mode == MBEDTLS_RSA_PUBLIC );
1616 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1617 RSA_VALIDATE_RET( input != NULL );
1618 RSA_VALIDATE_RET( olen != NULL );
1619
1620 ilen = ctx->len;
1621 plaintext_max_size = ( output_max_len > ilen - 11 ?
1622 ilen - 11 :
1623 output_max_len );
1624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1626 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001627
Paul Bakkerb3869132013-02-28 17:21:01 +01001628 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1632 ? mbedtls_rsa_public( ctx, input, buf )
1633 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001634
1635 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001636 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001637
Gilles Peskine40b57f42018-10-05 15:06:12 +02001638 /* Check and get padding length in constant time and constant
1639 * memory trace. The first byte must be 0. */
1640 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001643 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001644 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1645 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001646 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001648 /* Read the whole buffer. Set pad_done to nonzero if we find
1649 * the 0x00 byte and remember the padding length in pad_count. */
1650 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001651 {
Gilles Peskine85a74422018-10-05 14:50:21 +02001652 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001653 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001654 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001655 }
1656 else
1657 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001658 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1659 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001660 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001661
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001662 /* Read the whole buffer. Set pad_done to nonzero if we find
1663 * the 0x00 byte and remember the padding length in pad_count.
1664 * If there's a non-0xff byte in the padding, the padding is bad. */
1665 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001666 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001667 pad_done |= if_int( buf[i], 0, 1 );
1668 pad_count += if_int( pad_done, 0, 1 );
1669 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 }
1672
Gilles Peskine40b57f42018-10-05 15:06:12 +02001673 /* If pad_done is still zero, there's no data, only unfinished padding. */
1674 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001675
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001676 /* There must be at least 8 bytes of padding. */
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001677 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001678
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001679 /* If the padding is valid, set plaintext_size to the number of
1680 * remaining bytes after stripping the padding. If the padding
1681 * is invalid, avoid leaking this fact through the size of the
1682 * output: use the maximum message size that fits in the output
1683 * buffer. Do it without branches to avoid leaking the padding
1684 * validity through timing. RSA keys are small enough that all the
1685 * size_t values involved fit in unsigned int. */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001686 plaintext_size = if_int( bad,
1687 (unsigned) plaintext_max_size,
Gilles Peskine85a74422018-10-05 14:50:21 +02001688 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001689
Gilles Peskine9265ff42018-10-04 19:13:43 +02001690 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001691 * buffer and to 1 otherwise. */
1692 output_too_large = size_greater_than( plaintext_size,
1693 plaintext_max_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
Gilles Peskine9265ff42018-10-04 19:13:43 +02001695 /* Set ret without branches to avoid timing attacks. Return:
1696 * - INVALID_PADDING if the padding is bad (bad != 0).
1697 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1698 * plaintext does not fit in the output buffer.
1699 * - 0 if the padding is correct. */
Gilles Peskine48992472018-10-12 19:19:12 +02001700 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1701 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1702 0 ) );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001703
Gilles Peskine9265ff42018-10-04 19:13:43 +02001704 /* If the padding is bad or the plaintext is too large, zero the
1705 * data that we're about to copy to the output buffer.
1706 * We need to copy the same amount of data
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001707 * from the same buffer whether the padding is good or not to
1708 * avoid leaking the padding validity through overall timing or
1709 * through memory or cache access patterns. */
Gilles Peskine40b57f42018-10-05 15:06:12 +02001710 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001711 for( i = 11; i < ilen; i++ )
Gilles Peskine40b57f42018-10-05 15:06:12 +02001712 buf[i] &= ~bad;
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001713
Gilles Peskine9265ff42018-10-04 19:13:43 +02001714 /* If the plaintext is too large, truncate it to the buffer size.
1715 * Copy anyway to avoid revealing the length through timing, because
1716 * revealing the length is as bad as revealing the padding validity
1717 * for a Bleichenbacher attack. */
1718 plaintext_size = if_int( output_too_large,
1719 (unsigned) plaintext_max_size,
1720 (unsigned) plaintext_size );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001721
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001722 /* Move the plaintext to the leftmost position where it can start in
1723 * the working buffer, i.e. make it start plaintext_max_size from
1724 * the end of the buffer. Do this with a memory access trace that
1725 * does not depend on the plaintext size. After this move, the
1726 * starting location of the plaintext is no longer sensitive
1727 * information. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001728 mem_move_to_left( buf + ilen - plaintext_max_size,
1729 plaintext_max_size,
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001730 plaintext_max_size - plaintext_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001731
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001732 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskine9265ff42018-10-04 19:13:43 +02001733 * into the output buffer. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001734 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001735
1736 /* Report the amount of data we copied to the output buffer. In case
1737 * of errors (bad padding or output too large), the value of *olen
1738 * when this function returns is not specified. Making it equivalent
1739 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001740 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001742cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001743 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001744
1745 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001746}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
1749/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001750 * Do an RSA operation, then remove the message padding
1751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001753 int (*f_rng)(void *, unsigned char *, size_t),
1754 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001755 int mode, size_t *olen,
1756 const unsigned char *input,
1757 unsigned char *output,
1758 size_t output_max_len)
1759{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001760 RSA_VALIDATE_RET( ctx != NULL );
1761 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1762 mode == MBEDTLS_RSA_PUBLIC );
1763 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1764 RSA_VALIDATE_RET( input != NULL );
1765 RSA_VALIDATE_RET( olen != NULL );
1766
Paul Bakkerb3869132013-02-28 17:21:01 +01001767 switch( ctx->padding )
1768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769#if defined(MBEDTLS_PKCS1_V15)
1770 case MBEDTLS_RSA_PKCS_V15:
1771 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001772 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001773#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775#if defined(MBEDTLS_PKCS1_V21)
1776 case MBEDTLS_RSA_PKCS_V21:
1777 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001778 olen, input, output,
1779 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001780#endif
1781
1782 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001784 }
1785}
1786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001788/*
1789 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001792 int (*f_rng)(void *, unsigned char *, size_t),
1793 void *p_rng,
1794 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001796 unsigned int hashlen,
1797 const unsigned char *hash,
1798 unsigned char *sig )
1799{
1800 size_t olen;
1801 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001803 size_t slen, min_slen, hlen, offset = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001804 int ret;
1805 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 const mbedtls_md_info_t *md_info;
1807 mbedtls_md_context_t md_ctx;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001808 RSA_VALIDATE_RET( ctx != NULL );
1809 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1810 mode == MBEDTLS_RSA_PUBLIC );
1811 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1812 hashlen == 0 ) ||
1813 hash != NULL );
1814 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1817 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001818
1819 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001821
1822 olen = ctx->len;
1823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001825 {
Simon Butcher02037452016-03-01 21:19:12 +00001826 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001828 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001832 }
1833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001835 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001839
Jaeden Amero3725bb22018-09-07 19:12:36 +01001840 /* Calculate the largest possible salt length. Normally this is the hash
1841 * length, which is the maximum length the salt can have. If there is not
1842 * enough room, use the maximum salt length that fits. The constraint is
1843 * that the hash length plus the salt length plus 2 bytes must be at most
1844 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1845 * (PKCS#1 v2.2) §9.1.1 step 3. */
1846 min_slen = hlen - 2;
1847 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001849 else if( olen >= hlen + hlen + 2 )
1850 slen = hlen;
1851 else
1852 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001853
1854 memset( sig, 0, olen );
1855
Simon Butcher02037452016-03-01 21:19:12 +00001856 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001857 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001859
Simon Butcher02037452016-03-01 21:19:12 +00001860 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001861 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001862 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001863 *p++ = 0x01;
1864 memcpy( p, salt, slen );
1865 p += slen;
1866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001868 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001869 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001870
Simon Butcher02037452016-03-01 21:19:12 +00001871 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001872 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1873 goto exit;
1874 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1875 goto exit;
1876 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1877 goto exit;
1878 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1879 goto exit;
1880 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1881 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001882
Simon Butcher02037452016-03-01 21:19:12 +00001883 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001884 if( msb % 8 == 0 )
1885 offset = 1;
1886
Simon Butcher02037452016-03-01 21:19:12 +00001887 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001888 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1889 &md_ctx ) ) != 0 )
1890 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001891
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001892 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001893 sig[0] &= 0xFF >> ( olen * 8 - msb );
1894
1895 p += hlen;
1896 *p++ = 0xBC;
1897
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001898 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001899
1900exit:
1901 mbedtls_md_free( &md_ctx );
1902
1903 if( ret != 0 )
1904 return( ret );
1905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 return( ( mode == MBEDTLS_RSA_PUBLIC )
1907 ? mbedtls_rsa_public( ctx, sig, sig )
1908 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001909}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001913/*
1914 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1915 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001916
1917/* Construct a PKCS v1.5 encoding of a hashed message
1918 *
1919 * This is used both for signature generation and verification.
1920 *
1921 * Parameters:
1922 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001923 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001924 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001925 * - hash: Buffer containing the hashed message or the raw data.
1926 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001927 * - dst: Buffer to hold the encoded message.
1928 *
1929 * Assumptions:
1930 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1931 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001932 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001933 *
1934 */
1935static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1936 unsigned int hashlen,
1937 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001938 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001939 unsigned char *dst )
1940{
1941 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001942 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001943 unsigned char *p = dst;
1944 const char *oid = NULL;
1945
1946 /* Are we signing hashed or raw data? */
1947 if( md_alg != MBEDTLS_MD_NONE )
1948 {
1949 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1950 if( md_info == NULL )
1951 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1952
1953 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1954 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1955
1956 hashlen = mbedtls_md_get_size( md_info );
1957
1958 /* Double-check that 8 + hashlen + oid_size can be used as a
1959 * 1-byte ASN.1 length encoding and that there's no overflow. */
1960 if( 8 + hashlen + oid_size >= 0x80 ||
1961 10 + hashlen < hashlen ||
1962 10 + hashlen + oid_size < 10 + hashlen )
1963 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1964
1965 /*
1966 * Static bounds check:
1967 * - Need 10 bytes for five tag-length pairs.
1968 * (Insist on 1-byte length encodings to protect against variants of
1969 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1970 * - Need hashlen bytes for hash
1971 * - Need oid_size bytes for hash alg OID.
1972 */
1973 if( nb_pad < 10 + hashlen + oid_size )
1974 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1975 nb_pad -= 10 + hashlen + oid_size;
1976 }
1977 else
1978 {
1979 if( nb_pad < hashlen )
1980 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1981
1982 nb_pad -= hashlen;
1983 }
1984
Hanno Becker2b2f8982017-09-27 17:10:03 +01001985 /* Need space for signature header and padding delimiter (3 bytes),
1986 * and 8 bytes for the minimal padding */
1987 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001988 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1989 nb_pad -= 3;
1990
1991 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001992 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001993
1994 /* Write signature header and padding */
1995 *p++ = 0;
1996 *p++ = MBEDTLS_RSA_SIGN;
1997 memset( p, 0xFF, nb_pad );
1998 p += nb_pad;
1999 *p++ = 0;
2000
2001 /* Are we signing raw data? */
2002 if( md_alg == MBEDTLS_MD_NONE )
2003 {
2004 memcpy( p, hash, hashlen );
2005 return( 0 );
2006 }
2007
2008 /* Signing hashed data, add corresponding ASN.1 structure
2009 *
2010 * DigestInfo ::= SEQUENCE {
2011 * digestAlgorithm DigestAlgorithmIdentifier,
2012 * digest Digest }
2013 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2014 * Digest ::= OCTET STRING
2015 *
2016 * Schematic:
2017 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2018 * TAG-NULL + LEN [ NULL ] ]
2019 * TAG-OCTET + LEN [ HASH ] ]
2020 */
2021 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002022 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002023 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002024 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002025 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002026 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002027 memcpy( p, oid, oid_size );
2028 p += oid_size;
2029 *p++ = MBEDTLS_ASN1_NULL;
2030 *p++ = 0x00;
2031 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002032 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002033 memcpy( p, hash, hashlen );
2034 p += hashlen;
2035
2036 /* Just a sanity-check, should be automatic
2037 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002038 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002039 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002040 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002041 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2042 }
2043
2044 return( 0 );
2045}
2046
Paul Bakkerb3869132013-02-28 17:21:01 +01002047/*
2048 * Do an RSA operation to sign the message digest
2049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002051 int (*f_rng)(void *, unsigned char *, size_t),
2052 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002053 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002055 unsigned int hashlen,
2056 const unsigned char *hash,
2057 unsigned char *sig )
2058{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002059 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002060 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002061
Hanno Beckerddeeed72018-12-13 18:07:00 +00002062 RSA_VALIDATE_RET( ctx != NULL );
2063 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2064 mode == MBEDTLS_RSA_PUBLIC );
2065 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2066 hashlen == 0 ) ||
2067 hash != NULL );
2068 RSA_VALIDATE_RET( sig != NULL );
2069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2071 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002072
Hanno Beckerfdf38032017-09-06 12:35:55 +01002073 /*
2074 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2075 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002076
Hanno Beckerfdf38032017-09-06 12:35:55 +01002077 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2078 ctx->len, sig ) ) != 0 )
2079 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002080
2081 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002082 * Call respective RSA primitive
2083 */
2084
2085 if( mode == MBEDTLS_RSA_PUBLIC )
2086 {
2087 /* Skip verification on a public key operation */
2088 return( mbedtls_rsa_public( ctx, sig, sig ) );
2089 }
2090
2091 /* Private key operation
2092 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002093 * In order to prevent Lenstra's attack, make the signature in a
2094 * temporary buffer and check it before returning it.
2095 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002096
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002097 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002098 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002099 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2100
Hanno Beckerfdf38032017-09-06 12:35:55 +01002101 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002102 if( verif == NULL )
2103 {
2104 mbedtls_free( sig_try );
2105 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2106 }
2107
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002108 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2109 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2110
Hanno Becker171a8f12017-09-06 12:32:16 +01002111 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002112 {
2113 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2114 goto cleanup;
2115 }
2116
2117 memcpy( sig, sig_try, ctx->len );
2118
2119cleanup:
2120 mbedtls_free( sig_try );
2121 mbedtls_free( verif );
2122
2123 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002126
2127/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 * Do an RSA operation to sign the message digest
2129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002131 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002132 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002135 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002136 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 unsigned char *sig )
2138{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002139 RSA_VALIDATE_RET( ctx != NULL );
2140 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2141 mode == MBEDTLS_RSA_PUBLIC );
2142 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2143 hashlen == 0 ) ||
2144 hash != NULL );
2145 RSA_VALIDATE_RET( sig != NULL );
2146
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 switch( ctx->padding )
2148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149#if defined(MBEDTLS_PKCS1_V15)
2150 case MBEDTLS_RSA_PKCS_V15:
2151 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002152 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002153#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#if defined(MBEDTLS_PKCS1_V21)
2156 case MBEDTLS_RSA_PKCS_V21:
2157 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002158 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002159#endif
2160
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002164}
2165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002167/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002168 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002171 int (*f_rng)(void *, unsigned char *, size_t),
2172 void *p_rng,
2173 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002175 unsigned int hashlen,
2176 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002178 int expected_salt_len,
2179 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002180{
Paul Bakker23986e52011-04-24 08:57:21 +00002181 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002182 size_t siglen;
2183 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002184 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002186 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002187 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002188 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 const mbedtls_md_info_t *md_info;
2190 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002191 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002192
Hanno Beckerddeeed72018-12-13 18:07:00 +00002193 RSA_VALIDATE_RET( ctx != NULL );
2194 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2195 mode == MBEDTLS_RSA_PUBLIC );
2196 RSA_VALIDATE_RET( sig != NULL );
2197 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2198 hashlen == 0 ) ||
2199 hash != NULL );
2200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2202 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002203
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 siglen = ctx->len;
2205
Paul Bakker27fdf462011-06-09 13:55:13 +00002206 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2210 ? mbedtls_rsa_public( ctx, sig, buf )
2211 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002212
2213 if( ret != 0 )
2214 return( ret );
2215
2216 p = buf;
2217
Paul Bakkerb3869132013-02-28 17:21:01 +01002218 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 {
Simon Butcher02037452016-03-01 21:19:12 +00002223 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002225 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002229 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002232 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002236
Paul Bakkerb3869132013-02-28 17:21:01 +01002237 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002238
Simon Butcher02037452016-03-01 21:19:12 +00002239 /*
2240 * Note: EMSA-PSS verification is over the length of N - 1 bits
2241 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002242 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002243
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002244 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2245 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2246
Simon Butcher02037452016-03-01 21:19:12 +00002247 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002248 if( msb % 8 == 0 )
2249 {
2250 p++;
2251 siglen -= 1;
2252 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002253
Gilles Peskine139108a2017-10-18 19:03:42 +02002254 if( siglen < hlen + 2 )
2255 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2256 hash_start = p + siglen - hlen - 1;
2257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002259 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002260 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002261
Jaeden Amero66954e12018-01-25 16:05:54 +00002262 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2263 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002264 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002265
Paul Bakkerb3869132013-02-28 17:21:01 +01002266 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002267
Gilles Peskine6a54b022017-10-17 19:02:13 +02002268 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002269 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002270
Gilles Peskine91048a32017-10-19 17:46:14 +02002271 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002272 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002273 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2274 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002275 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002276
Gilles Peskine6a54b022017-10-17 19:02:13 +02002277 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002280 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002281 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002282 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2283 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002284 }
2285
Simon Butcher02037452016-03-01 21:19:12 +00002286 /*
2287 * Generate H = Hash( M' )
2288 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002289 ret = mbedtls_md_starts( &md_ctx );
2290 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002291 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002292 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2293 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002294 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002295 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2296 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002297 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002298 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2299 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002300 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002301 ret = mbedtls_md_finish( &md_ctx, result );
2302 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002303 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002304
Jaeden Amero66954e12018-01-25 16:05:54 +00002305 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002306 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002307 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002308 goto exit;
2309 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002310
2311exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002313
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002314 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002315}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002316
2317/*
2318 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002321 int (*f_rng)(void *, unsigned char *, size_t),
2322 void *p_rng,
2323 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002325 unsigned int hashlen,
2326 const unsigned char *hash,
2327 const unsigned char *sig )
2328{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002329 mbedtls_md_type_t mgf1_hash_id;
2330 RSA_VALIDATE_RET( ctx != NULL );
2331 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2332 mode == MBEDTLS_RSA_PUBLIC );
2333 RSA_VALIDATE_RET( sig != NULL );
2334 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2335 hashlen == 0 ) ||
2336 hash != NULL );
2337
2338 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002340 : md_alg;
2341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002343 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002345 sig ) );
2346
2347}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002351/*
2352 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002355 int (*f_rng)(void *, unsigned char *, size_t),
2356 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002357 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002359 unsigned int hashlen,
2360 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002361 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002362{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002363 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002364 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002365 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002366
Hanno Beckerddeeed72018-12-13 18:07:00 +00002367 RSA_VALIDATE_RET( ctx != NULL );
2368 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2369 mode == MBEDTLS_RSA_PUBLIC );
2370 RSA_VALIDATE_RET( sig != NULL );
2371 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2372 hashlen == 0 ) ||
2373 hash != NULL );
2374
2375 sig_len = ctx->len;
2376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2378 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002379
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002380 /*
2381 * Prepare expected PKCS1 v1.5 encoding of hash.
2382 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002383
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002384 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2385 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2386 {
2387 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2388 goto cleanup;
2389 }
2390
2391 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2392 encoded_expected ) ) != 0 )
2393 goto cleanup;
2394
2395 /*
2396 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2397 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002400 ? mbedtls_rsa_public( ctx, sig, encoded )
2401 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002402 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002403 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002404
Simon Butcher02037452016-03-01 21:19:12 +00002405 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002406 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002407 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002408
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002409 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2410 sig_len ) ) != 0 )
2411 {
2412 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2413 goto cleanup;
2414 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002415
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002416cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002417
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002418 if( encoded != NULL )
2419 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002420 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002421 mbedtls_free( encoded );
2422 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002423
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002424 if( encoded_expected != NULL )
2425 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002426 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002427 mbedtls_free( encoded_expected );
2428 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002429
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002430 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002431}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
2434/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002435 * Do an RSA operation and check the message digest
2436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002438 int (*f_rng)(void *, unsigned char *, size_t),
2439 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002440 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002442 unsigned int hashlen,
2443 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002444 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002445{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002446 RSA_VALIDATE_RET( ctx != NULL );
2447 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2448 mode == MBEDTLS_RSA_PUBLIC );
2449 RSA_VALIDATE_RET( sig != NULL );
2450 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2451 hashlen == 0 ) ||
2452 hash != NULL );
2453
Paul Bakkerb3869132013-02-28 17:21:01 +01002454 switch( ctx->padding )
2455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#if defined(MBEDTLS_PKCS1_V15)
2457 case MBEDTLS_RSA_PKCS_V15:
2458 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002459 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002460#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462#if defined(MBEDTLS_PKCS1_V21)
2463 case MBEDTLS_RSA_PKCS_V21:
2464 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002465 hashlen, hash, sig );
2466#endif
2467
2468 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002470 }
2471}
2472
2473/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002474 * Copy the components of an RSA key
2475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002477{
2478 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002479 RSA_VALIDATE_RET( dst != NULL );
2480 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002481
2482 dst->ver = src->ver;
2483 dst->len = src->len;
2484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2486 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2489 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2490 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002491
2492#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2494 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2495 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2497 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002498#endif
2499
2500 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2503 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002504
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002505 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002506 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002507
2508cleanup:
2509 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002511
2512 return( ret );
2513}
2514
2515/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 * Free the components of an RSA key
2517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002519{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002520 if( ctx == NULL )
2521 return;
2522
irwir2239a862018-06-12 18:25:09 +03002523 mbedtls_mpi_free( &ctx->Vi );
2524 mbedtls_mpi_free( &ctx->Vf );
2525 mbedtls_mpi_free( &ctx->RN );
2526 mbedtls_mpi_free( &ctx->D );
2527 mbedtls_mpi_free( &ctx->Q );
2528 mbedtls_mpi_free( &ctx->P );
2529 mbedtls_mpi_free( &ctx->E );
2530 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002531
Hanno Becker33c30a02017-08-23 07:00:22 +01002532#if !defined(MBEDTLS_RSA_NO_CRT)
irwir2239a862018-06-12 18:25:09 +03002533 mbedtls_mpi_free( &ctx->RQ );
2534 mbedtls_mpi_free( &ctx->RP );
2535 mbedtls_mpi_free( &ctx->QP );
2536 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002537 mbedtls_mpi_free( &ctx->DP );
2538#endif /* MBEDTLS_RSA_NO_CRT */
2539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540#if defined(MBEDTLS_THREADING_C)
2541 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002542#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002543}
2544
Hanno Beckerab377312017-08-23 16:24:51 +01002545#endif /* !MBEDTLS_RSA_ALT */
2546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002549#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
2551/*
2552 * Example RSA-1024 keypair, for test purposes
2553 */
2554#define KEY_LEN 128
2555
2556#define RSA_N "9292758453063D803DD603D5E777D788" \
2557 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2558 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2559 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2560 "93A89813FBF3C4F8066D2D800F7C38A8" \
2561 "1AE31942917403FF4946B0A83D3D3E05" \
2562 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2563 "5E94BB77B07507233A0BC7BAC8F90F79"
2564
2565#define RSA_E "10001"
2566
2567#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2568 "66CA472BC44D253102F8B4A9D3BFA750" \
2569 "91386C0077937FE33FA3252D28855837" \
2570 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2571 "DF79C5CE07EE72C7F123142198164234" \
2572 "CABB724CF78B8173B9F880FC86322407" \
2573 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2574 "071513A1E85B5DFA031F21ECAE91A34D"
2575
2576#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2577 "2C01CAD19EA484A87EA4377637E75500" \
2578 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2579 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2580
2581#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2582 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2583 "910E4168387E3C30AA1E00C339A79508" \
2584 "8452DD96A9A5EA5D9DCA68DA636032AF"
2585
Paul Bakker5121ce52009-01-03 21:22:43 +00002586#define PT_LEN 24
2587#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2588 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002591static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002592{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002593#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002594 size_t i;
2595
Paul Bakker545570e2010-07-18 09:00:25 +00002596 if( rng_state != NULL )
2597 rng_state = NULL;
2598
Paul Bakkera3d195c2011-11-27 21:07:34 +00002599 for( i = 0; i < len; ++i )
2600 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002601#else
2602 if( rng_state != NULL )
2603 rng_state = NULL;
2604
2605 arc4random_buf( output, len );
2606#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002607
Paul Bakkera3d195c2011-11-27 21:07:34 +00002608 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002609}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002611
Paul Bakker5121ce52009-01-03 21:22:43 +00002612/*
2613 * Checkup routine
2614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002616{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002617 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002619 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 unsigned char rsa_plaintext[PT_LEN];
2622 unsigned char rsa_decrypted[PT_LEN];
2623 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002625 unsigned char sha1sum[20];
2626#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
Hanno Becker3a701162017-08-22 13:52:43 +01002628 mbedtls_mpi K;
2629
2630 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Hanno Becker3a701162017-08-22 13:52:43 +01002633 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2634 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2635 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2636 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2637 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2638 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2639 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2640 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2641 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2642 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2643
Hanno Becker7f25f852017-10-10 16:56:22 +01002644 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
2646 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2650 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 {
2652 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Hanno Becker5bc87292017-05-03 15:09:31 +01002655 ret = 1;
2656 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 }
2658
2659 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
2662 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2663
Hanno Becker98838b02017-10-02 13:16:10 +01002664 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2665 PT_LEN, rsa_plaintext,
2666 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 {
2668 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Hanno Becker5bc87292017-05-03 15:09:31 +01002671 ret = 1;
2672 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002673 }
2674
2675 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002677
Hanno Becker98838b02017-10-02 13:16:10 +01002678 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2679 &len, rsa_ciphertext, rsa_decrypted,
2680 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 {
2682 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Hanno Becker5bc87292017-05-03 15:09:31 +01002685 ret = 1;
2686 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 }
2688
2689 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2690 {
2691 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Hanno Becker5bc87292017-05-03 15:09:31 +01002694 ret = 1;
2695 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 }
2697
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002698 if( verbose != 0 )
2699 mbedtls_printf( "passed\n" );
2700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002703 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002705 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002706 {
2707 if( verbose != 0 )
2708 mbedtls_printf( "failed\n" );
2709
2710 return( 1 );
2711 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Hanno Becker98838b02017-10-02 13:16:10 +01002713 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2714 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2715 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 {
2717 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
Hanno Becker5bc87292017-05-03 15:09:31 +01002720 ret = 1;
2721 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 }
2723
2724 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726
Hanno Becker98838b02017-10-02 13:16:10 +01002727 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2728 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2729 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 {
2731 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
Hanno Becker5bc87292017-05-03 15:09:31 +01002734 ret = 1;
2735 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 }
2737
2738 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002739 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002742 if( verbose != 0 )
2743 mbedtls_printf( "\n" );
2744
Paul Bakker3d8fb632014-04-17 12:42:41 +02002745cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002746 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 mbedtls_rsa_free( &rsa );
2748#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002749 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002751 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752}
2753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756#endif /* MBEDTLS_RSA_C */