blob: d69456b0476a5c969b796bc6f8a8d6537f901159 [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úti4e9f7122020-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úti4e9f7122020-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"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000078
Rich Evans00ab4702015-02-06 13:43:58 +000079#include <string.h>
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000082#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000083#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000086#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000087#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000090#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010091#else
Rich Evans00ab4702015-02-06 13:43:58 +000092#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020094#define mbedtls_calloc calloc
95#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010096#endif
97
Hanno Beckera565f542017-10-11 11:00:19 +010098#if !defined(MBEDTLS_RSA_ALT)
99
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100100/* Implementation that should never be optimized out by the compiler */
101static void mbedtls_zeroize( void *v, size_t n ) {
102 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
103}
104
Manuel Pégourié-Gonnardb0ba5bc2018-03-13 13:27:14 +0100105#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +0100106/* constant-time buffer comparison */
107static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
108{
109 size_t i;
110 const unsigned char *A = (const unsigned char *) a;
111 const unsigned char *B = (const unsigned char *) b;
112 unsigned char diff = 0;
113
114 for( i = 0; i < n; i++ )
115 diff |= A[i] ^ B[i];
116
117 return( diff );
118}
Manuel Pégourié-Gonnardb0ba5bc2018-03-13 13:27:14 +0100119#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +0100120
Hanno Becker617c1ae2017-08-23 14:11:24 +0100121int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
122 const mbedtls_mpi *N,
123 const mbedtls_mpi *P, const mbedtls_mpi *Q,
124 const mbedtls_mpi *D, const mbedtls_mpi *E )
125{
126 int ret;
127
128 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
129 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
130 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
131 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
132 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
133 {
134 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
135 }
136
137 if( N != NULL )
138 ctx->len = mbedtls_mpi_size( &ctx->N );
139
140 return( 0 );
141}
142
143int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100144 unsigned char const *N, size_t N_len,
145 unsigned char const *P, size_t P_len,
146 unsigned char const *Q, size_t Q_len,
147 unsigned char const *D, size_t D_len,
148 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100149{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000150 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100151
152 if( N != NULL )
153 {
154 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
155 ctx->len = mbedtls_mpi_size( &ctx->N );
156 }
157
158 if( P != NULL )
159 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
160
161 if( Q != NULL )
162 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
163
164 if( D != NULL )
165 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
166
167 if( E != NULL )
168 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
169
170cleanup:
171
172 if( ret != 0 )
173 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
174
175 return( 0 );
176}
177
Hanno Becker705fc682017-10-10 17:57:02 +0100178/*
179 * Checks whether the context fields are set in such a way
180 * that the RSA primitives will be able to execute without error.
181 * It does *not* make guarantees for consistency of the parameters.
182 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100183static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
184 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100185{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100186#if !defined(MBEDTLS_RSA_NO_CRT)
187 /* blinding_needed is only used for NO_CRT to decide whether
188 * P,Q need to be present or not. */
189 ((void) blinding_needed);
190#endif
191
Hanno Becker3a760a12018-01-05 08:14:49 +0000192 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
193 ctx->len > MBEDTLS_MPI_MAX_SIZE )
194 {
Hanno Becker705fc682017-10-10 17:57:02 +0100195 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000196 }
Hanno Becker705fc682017-10-10 17:57:02 +0100197
198 /*
199 * 1. Modular exponentiation needs positive, odd moduli.
200 */
201
202 /* Modular exponentiation wrt. N is always used for
203 * RSA public key operations. */
204 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
205 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
206 {
207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
208 }
209
210#if !defined(MBEDTLS_RSA_NO_CRT)
211 /* Modular exponentiation for P and Q is only
212 * used for private key operations and if CRT
213 * is used. */
214 if( is_priv &&
215 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
216 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
217 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
218 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
219 {
220 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
221 }
222#endif /* !MBEDTLS_RSA_NO_CRT */
223
224 /*
225 * 2. Exponents must be positive
226 */
227
228 /* Always need E for public key operations */
229 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
230 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
231
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100232#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100233 /* For private key operations, use D or DP & DQ
234 * as (unblinded) exponents. */
235 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
236 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
237#else
238 if( is_priv &&
239 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
240 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
241 {
242 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
243 }
244#endif /* MBEDTLS_RSA_NO_CRT */
245
246 /* Blinding shouldn't make exponents negative either,
247 * so check that P, Q >= 1 if that hasn't yet been
248 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100249#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100250 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100251 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
252 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
253 {
254 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
255 }
256#endif
257
258 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100259 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100260#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100261 if( is_priv &&
262 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
263 {
264 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
265 }
266#endif
267
268 return( 0 );
269}
270
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100271int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100272{
273 int ret = 0;
274
Hanno Becker617c1ae2017-08-23 14:11:24 +0100275 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
276 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
277 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
278 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
279 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100280
Jack Lloyd100e1472020-01-29 13:13:04 -0500281#if !defined(MBEDTLS_RSA_NO_CRT)
282 const int have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
283 const int have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
284 const int have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
285#endif
286
Hanno Becker617c1ae2017-08-23 14:11:24 +0100287 /*
288 * Check whether provided parameters are enough
289 * to deduce all others. The following incomplete
290 * parameter sets for private keys are supported:
291 *
292 * (1) P, Q missing.
293 * (2) D and potentially N missing.
294 *
295 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100296
Hanno Becker2cca6f32017-09-29 11:46:40 +0100297 const int n_missing = have_P && have_Q && have_D && have_E;
298 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
299 const int d_missing = have_P && have_Q && !have_D && have_E;
300 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
301
302 /* These three alternatives are mutually exclusive */
303 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100304
Hanno Becker617c1ae2017-08-23 14:11:24 +0100305 if( !is_priv && !is_pub )
306 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
307
308 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100309 * Step 1: Deduce N if P, Q are provided.
310 */
311
312 if( !have_N && have_P && have_Q )
313 {
314 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
315 &ctx->Q ) ) != 0 )
316 {
317 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
318 }
319
320 ctx->len = mbedtls_mpi_size( &ctx->N );
321 }
322
323 /*
324 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100325 */
326
327 if( pq_missing )
328 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100329 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100330 &ctx->P, &ctx->Q );
331 if( ret != 0 )
332 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
333
334 }
335 else if( d_missing )
336 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100337 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
338 &ctx->Q,
339 &ctx->E,
340 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100341 {
342 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
343 }
344 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100345
Hanno Becker617c1ae2017-08-23 14:11:24 +0100346 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100347 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100348 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100349 */
350
Hanno Becker23344b52017-08-23 07:43:27 +0100351#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd100e1472020-01-29 13:13:04 -0500352 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100353 {
354 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
355 &ctx->DP, &ctx->DQ, &ctx->QP );
356 if( ret != 0 )
357 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
358 }
Hanno Becker23344b52017-08-23 07:43:27 +0100359#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100360
361 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100362 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100363 */
364
Hanno Beckerebd2c022017-10-12 10:54:53 +0100365 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100366}
367
Hanno Becker617c1ae2017-08-23 14:11:24 +0100368int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
369 unsigned char *N, size_t N_len,
370 unsigned char *P, size_t P_len,
371 unsigned char *Q, size_t Q_len,
372 unsigned char *D, size_t D_len,
373 unsigned char *E, size_t E_len )
374{
375 int ret = 0;
376
377 /* Check if key is private or public */
378 const int is_priv =
379 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
380 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
381 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
382 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
383 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
384
385 if( !is_priv )
386 {
387 /* If we're trying to export private parameters for a public key,
388 * something must be wrong. */
389 if( P != NULL || Q != NULL || D != NULL )
390 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
391
392 }
393
394 if( N != NULL )
395 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
396
397 if( P != NULL )
398 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
399
400 if( Q != NULL )
401 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
402
403 if( D != NULL )
404 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
405
406 if( E != NULL )
407 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100408
409cleanup:
410
411 return( ret );
412}
413
Hanno Becker617c1ae2017-08-23 14:11:24 +0100414int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
415 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
416 mbedtls_mpi *D, mbedtls_mpi *E )
417{
418 int ret;
419
420 /* Check if key is private or public */
421 int is_priv =
422 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
423 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
424 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
425 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
426 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
427
428 if( !is_priv )
429 {
430 /* If we're trying to export private parameters for a public key,
431 * something must be wrong. */
432 if( P != NULL || Q != NULL || D != NULL )
433 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
434
435 }
436
437 /* Export all requested core parameters. */
438
439 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
440 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
441 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
442 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
443 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
444 {
445 return( ret );
446 }
447
448 return( 0 );
449}
450
451/*
452 * Export CRT parameters
453 * This must also be implemented if CRT is not used, for being able to
454 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
455 * can be used in this case.
456 */
457int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
458 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
459{
460 int ret;
461
462 /* Check if key is private or public */
463 int is_priv =
464 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
465 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
466 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
467 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
468 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
469
470 if( !is_priv )
471 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
472
Hanno Beckerdc95c892017-08-23 06:57:02 +0100473#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100474 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100475 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
476 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
477 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
478 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100479 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100480 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100481#else
482 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
483 DP, DQ, QP ) ) != 0 )
484 {
485 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
486 }
487#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100488
489 return( 0 );
490}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100491
Paul Bakker5121ce52009-01-03 21:22:43 +0000492/*
493 * Initialize an RSA context
494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000497 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000498{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#if defined(MBEDTLS_THREADING_C)
504 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200505#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000506}
507
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100508/*
509 * Set padding for an existing RSA context
510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100512{
513 ctx->padding = padding;
514 ctx->hash_id = hash_id;
515}
516
Hanno Becker617c1ae2017-08-23 14:11:24 +0100517/*
518 * Get length in bytes of RSA modulus
519 */
520
521size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
522{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100523 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100524}
525
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529/*
530 * Generate an RSA keypair
531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000533 int (*f_rng)(void *, unsigned char *, size_t),
534 void *p_rng,
535 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000536{
537 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100538 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker21eb2802010-08-16 11:10:02 +0000540 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
Janos Follathef441782016-09-21 13:18:12 +0100543 if( nbits % 2 )
544 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
545
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100546 mbedtls_mpi_init( &H );
547 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 /*
550 * find primes P and Q with Q < P so that:
551 * GCD( E, (P-1)*(Q-1) ) == 1
552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555 do
556 {
Janos Follath10c575b2016-02-23 14:42:48 +0000557 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100558 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Janos Follathef441782016-09-21 13:18:12 +0100560 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100561 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 continue;
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200567 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 continue;
569
Janos Follathef441782016-09-21 13:18:12 +0100570 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100571 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100572
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100573 /* Temporarily replace P,Q by P-1, Q-1 */
574 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
575 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
576 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100581 /* Restore P,Q */
582 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
583 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
584
585 ctx->len = mbedtls_mpi_size( &ctx->N );
586
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 /*
588 * D = E^-1 mod ((P-1)*(Q-1))
589 * DP = D mod (P - 1)
590 * DQ = D mod (Q - 1)
591 * QP = Q^-1 mod P
592 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100594 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
595
596#if !defined(MBEDTLS_RSA_NO_CRT)
597 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
598 &ctx->DP, &ctx->DQ, &ctx->QP ) );
599#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Hanno Becker83aad1f2017-08-23 06:45:10 +0100601 /* Double-check */
602 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
604cleanup:
605
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100606 mbedtls_mpi_free( &H );
607 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
609 if( ret != 0 )
610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_rsa_free( ctx );
612 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 }
614
Paul Bakker48377d92013-08-30 12:06:24 +0200615 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616}
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620/*
621 * Check a public RSA key
622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000624{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100625 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000627
Hanno Becker3a760a12018-01-05 08:14:49 +0000628 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100631 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000632
Hanno Becker705fc682017-10-10 17:57:02 +0100633 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
634 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100638 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640 return( 0 );
641}
642
643/*
Hanno Becker705fc682017-10-10 17:57:02 +0100644 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000647{
Hanno Becker705fc682017-10-10 17:57:02 +0100648 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100649 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 {
Hanno Becker98838b02017-10-02 13:16:10 +0100651 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 }
Paul Bakker48377d92013-08-30 12:06:24 +0200653
Hanno Becker98838b02017-10-02 13:16:10 +0100654 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100655 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100657 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000659
Hanno Beckerb269a852017-08-25 08:03:21 +0100660#if !defined(MBEDTLS_RSA_NO_CRT)
661 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
662 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
663 {
664 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
665 }
666#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000667
668 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000669}
670
671/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100672 * Check if contexts holding a public and private key match
673 */
Hanno Becker98838b02017-10-02 13:16:10 +0100674int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
675 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100676{
Hanno Becker98838b02017-10-02 13:16:10 +0100677 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100681 }
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
684 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100687 }
688
689 return( 0 );
690}
691
692/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 * Do an RSA public key operation
694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000696 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 unsigned char *output )
698{
Paul Bakker23986e52011-04-24 08:57:21 +0000699 int ret;
700 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
Hanno Beckerebd2c022017-10-12 10:54:53 +0100703 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100704 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200708#if defined(MBEDTLS_THREADING_C)
709 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
710 return( ret );
711#endif
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000716 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200717 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
718 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 }
720
721 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
723 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200727 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
728 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100729#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
733 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000735
736 return( 0 );
737}
738
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200739/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200740 * Generate or update blinding values, see section 10 of:
741 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200742 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200743 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200744 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200745static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200746 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
747{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200748 int ret, count = 0;
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200749 mbedtls_mpi R;
750
751 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200752
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200753 if( ctx->Vf.p != NULL )
754 {
755 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
757 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
758 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
759 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200760
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200761 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200762 }
763
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200764 /* Unblinding value: Vf = random number, invertible mod N */
765 do {
766 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
Manuel Pégourié-Gonnard6ab924d2020-06-26 11:03:19 +0200770
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200771 /* Compute the Vf^1 as R * (R Vf)^-1 to avoid leaks from inv_mod.
772 * There's a negligible but non-zero probability that R is not
773 * invertible mod N, in that case we'd just loop one more time,
774 * just as if Vf itself wasn't invertible - no need to distinguish. */
775 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
776 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
777 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
778
779 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
780 if( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
781 continue;
782 if( ret != 0 )
Manuel Pégourié-Gonnard6ab924d2020-06-26 11:03:19 +0200783 goto cleanup;
784
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200785 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
786 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
787 } while( 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200788
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200789 /* Blinding value: Vi = Vf^(-e) mod N
790 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 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 +0200792
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200793
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200794cleanup:
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200795 mbedtls_mpi_free( &R );
796
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200797 return( ret );
798}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200799
Paul Bakker5121ce52009-01-03 21:22:43 +0000800/*
Janos Follathe81102e2017-03-22 13:38:28 +0000801 * Exponent blinding supposed to prevent side-channel attacks using multiple
802 * traces of measurements to recover the RSA key. The more collisions are there,
803 * the more bits of the key can be recovered. See [3].
804 *
805 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
806 * observations on avarage.
807 *
808 * For example with 28 byte blinding to achieve 2 collisions the adversary has
809 * to make 2^112 observations on avarage.
810 *
811 * (With the currently (as of 2017 April) known best algorithms breaking 2048
812 * bit RSA requires approximately as much time as trying out 2^112 random keys.
813 * Thus in this sense with 28 byte blinding the security is not reduced by
814 * side-channel attacks like the one in [3])
815 *
816 * This countermeasure does not help if the key recovery is possible with a
817 * single trace.
818 */
819#define RSA_EXPONENT_BLINDING 28
820
821/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 * Do an RSA private key operation
823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200825 int (*f_rng)(void *, unsigned char *, size_t),
826 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000827 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000828 unsigned char *output )
829{
Paul Bakker23986e52011-04-24 08:57:21 +0000830 int ret;
831 size_t olen;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000832
833 /* Temporary holding the result */
834 mbedtls_mpi T;
835
836 /* Temporaries holding P-1, Q-1 and the
837 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000838 mbedtls_mpi P1, Q1, R;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000839
840#if !defined(MBEDTLS_RSA_NO_CRT)
841 /* Temporaries holding the results mod p resp. mod q. */
842 mbedtls_mpi TP, TQ;
843
844 /* Temporaries holding the blinded exponents for
845 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000846 mbedtls_mpi DP_blind, DQ_blind;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000847
848 /* Pointers to actual exponents to be used - either the unblinded
849 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000850 mbedtls_mpi *DP = &ctx->DP;
851 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000852#else
853 /* Temporary holding the blinded exponent (if used). */
854 mbedtls_mpi D_blind;
855
856 /* Pointer to actual exponent to be used - either the unblinded
857 * or the blinded one, depending on the presence of a PRNG. */
858 mbedtls_mpi *D = &ctx->D;
859#endif /* MBEDTLS_RSA_NO_CRT */
860
861 /* Temporaries holding the initial input and the double
862 * checked result; should be the same in the end. */
863 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
Hanno Beckerebd2c022017-10-12 10:54:53 +0100865 if( rsa_check_context( ctx, 1 /* private key checks */,
866 f_rng != NULL /* blinding y/n */ ) != 0 )
867 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100868 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100869 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100870
Hanno Beckera5fa0792018-03-09 10:42:23 +0000871#if defined(MBEDTLS_THREADING_C)
872 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
873 return( ret );
874#endif
875
876 /* MPI Initialization */
877 mbedtls_mpi_init( &T );
878
879 mbedtls_mpi_init( &P1 );
880 mbedtls_mpi_init( &Q1 );
881 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000882
Janos Follathf9203b42017-03-22 15:13:15 +0000883 if( f_rng != NULL )
884 {
Janos Follathe81102e2017-03-22 13:38:28 +0000885#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000886 mbedtls_mpi_init( &D_blind );
887#else
888 mbedtls_mpi_init( &DP_blind );
889 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000890#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000891 }
Janos Follathe81102e2017-03-22 13:38:28 +0000892
Hanno Beckera5fa0792018-03-09 10:42:23 +0000893#if !defined(MBEDTLS_RSA_NO_CRT)
894 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200895#endif
896
Hanno Beckera5fa0792018-03-09 10:42:23 +0000897 mbedtls_mpi_init( &I );
898 mbedtls_mpi_init( &C );
899
900 /* End of MPI initialization */
901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
903 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000904 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200905 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
906 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000907 }
908
Hanno Beckera5fa0792018-03-09 10:42:23 +0000909 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
910
Paul Bakkerf451bac2013-08-30 15:37:02 +0200911 if( f_rng != NULL )
912 {
913 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200914 * Blinding
915 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200916 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200917 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
918 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000920
Janos Follathe81102e2017-03-22 13:38:28 +0000921 /*
922 * Exponent blinding
923 */
924 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
925 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
926
Janos Follathf9203b42017-03-22 15:13:15 +0000927#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000928 /*
929 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
930 */
931 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
932 f_rng, p_rng ) );
933 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
934 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
935 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
936
937 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000938#else
939 /*
940 * DP_blind = ( P - 1 ) * R + DP
941 */
942 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
943 f_rng, p_rng ) );
944 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
945 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
946 &ctx->DP ) );
947
948 DP = &DP_blind;
949
950 /*
951 * DQ_blind = ( Q - 1 ) * R + DQ
952 */
953 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
954 f_rng, p_rng ) );
955 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
956 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
957 &ctx->DQ ) );
958
959 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000960#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200961 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000964 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100965#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200966 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000967 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 *
Hanno Beckera5fa0792018-03-09 10:42:23 +0000969 * TP = input ^ dP mod P
970 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000971 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000972
973 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
974 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
976 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000977 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000979 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
980 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
981 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
983 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000984 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000985 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000986 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
987 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200989
Paul Bakkerf451bac2013-08-30 15:37:02 +0200990 if( f_rng != NULL )
991 {
992 /*
993 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200994 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200995 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200996 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200998 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000999
Hanno Beckera5fa0792018-03-09 10:42:23 +00001000 /* Verify the result to prevent glitching attacks. */
1001 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1002 &ctx->N, &ctx->RN ) );
1003 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
1004 {
1005 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1006 goto cleanup;
1007 }
1008
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001011
1012cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001014 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1015 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001016#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001017
Hanno Beckera5fa0792018-03-09 10:42:23 +00001018 mbedtls_mpi_free( &P1 );
1019 mbedtls_mpi_free( &Q1 );
1020 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001021
1022 if( f_rng != NULL )
1023 {
Janos Follathe81102e2017-03-22 13:38:28 +00001024#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001025 mbedtls_mpi_free( &D_blind );
1026#else
1027 mbedtls_mpi_free( &DP_blind );
1028 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001029#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001030 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
Hanno Beckera5fa0792018-03-09 10:42:23 +00001032 mbedtls_mpi_free( &T );
1033
1034#if !defined(MBEDTLS_RSA_NO_CRT)
1035 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1036#endif
1037
1038 mbedtls_mpi_free( &C );
1039 mbedtls_mpi_free( &I );
1040
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
1044 return( 0 );
1045}
1046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001048/**
1049 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1050 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001051 * \param dst buffer to mask
1052 * \param dlen length of destination buffer
1053 * \param src source of the mask generation
1054 * \param slen length of the source buffer
1055 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001056 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001057static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001061 unsigned char counter[4];
1062 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001063 unsigned int hlen;
1064 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001065 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001068 memset( counter, 0, 4 );
1069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001071
Simon Butcher02037452016-03-01 21:19:12 +00001072 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001073 p = dst;
1074
1075 while( dlen > 0 )
1076 {
1077 use_len = hlen;
1078 if( dlen < hlen )
1079 use_len = dlen;
1080
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001081 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1082 goto exit;
1083 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1084 goto exit;
1085 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1086 goto exit;
1087 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1088 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001089
1090 for( i = 0; i < use_len; ++i )
1091 *p++ ^= mask[i];
1092
1093 counter[3]++;
1094
1095 dlen -= use_len;
1096 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001097
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001098exit:
Gilles Peskine18ac7162017-05-05 19:24:06 +02001099 mbedtls_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001100
1101 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001106/*
1107 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001110 int (*f_rng)(void *, unsigned char *, size_t),
1111 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001112 int mode,
1113 const unsigned char *label, size_t label_len,
1114 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001115 const unsigned char *input,
1116 unsigned char *output )
1117{
1118 size_t olen;
1119 int ret;
1120 unsigned char *p = output;
1121 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 const mbedtls_md_info_t *md_info;
1123 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1126 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001127
1128 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001132 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001134
1135 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001137
Simon Butcher02037452016-03-01 21:19:12 +00001138 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001139 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001141
1142 memset( output, 0, olen );
1143
1144 *p++ = 0;
1145
Simon Butcher02037452016-03-01 21:19:12 +00001146 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001147 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001149
1150 p += hlen;
1151
Simon Butcher02037452016-03-01 21:19:12 +00001152 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001153 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1154 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001155 p += hlen;
1156 p += olen - 2 * hlen - 2 - ilen;
1157 *p++ = 1;
1158 memcpy( p, input, ilen );
1159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001161 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001162 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001163
Simon Butcher02037452016-03-01 21:19:12 +00001164 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001165 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1166 &md_ctx ) ) != 0 )
1167 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001168
Simon Butcher02037452016-03-01 21:19:12 +00001169 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001170 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1171 &md_ctx ) ) != 0 )
1172 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001173
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001174exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001176
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001177 if( ret != 0 )
1178 return( ret );
1179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 return( ( mode == MBEDTLS_RSA_PUBLIC )
1181 ? mbedtls_rsa_public( ctx, output, output )
1182 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001187/*
1188 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001191 int (*f_rng)(void *, unsigned char *, size_t),
1192 void *p_rng,
1193 int mode, size_t ilen,
1194 const unsigned char *input,
1195 unsigned char *output )
1196{
1197 size_t nb_pad, olen;
1198 int ret;
1199 unsigned char *p = output;
1200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1202 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001203
Janos Follath1ed9f992016-03-18 11:45:44 +00001204 // We don't check p_rng because it won't be dereferenced here
1205 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001207
1208 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001209
Simon Butcher02037452016-03-01 21:19:12 +00001210 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001211 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001213
1214 nb_pad = olen - 3 - ilen;
1215
1216 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001220
1221 while( nb_pad-- > 0 )
1222 {
1223 int rng_dl = 100;
1224
1225 do {
1226 ret = f_rng( p_rng, p, 1 );
1227 } while( *p == 0 && --rng_dl && ret == 0 );
1228
Simon Butcher02037452016-03-01 21:19:12 +00001229 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001230 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001232
1233 p++;
1234 }
1235 }
1236 else
1237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001239
1240 while( nb_pad-- > 0 )
1241 *p++ = 0xFF;
1242 }
1243
1244 *p++ = 0;
1245 memcpy( p, input, ilen );
1246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 return( ( mode == MBEDTLS_RSA_PUBLIC )
1248 ? mbedtls_rsa_public( ctx, output, output )
1249 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001252
Paul Bakker5121ce52009-01-03 21:22:43 +00001253/*
1254 * Add the message padding, then do an RSA operation
1255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001257 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001258 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001259 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001260 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 unsigned char *output )
1262{
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 switch( ctx->padding )
1264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_PKCS1_V15)
1266 case MBEDTLS_RSA_PKCS_V15:
1267 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001268 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001269#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#if defined(MBEDTLS_PKCS1_V21)
1272 case MBEDTLS_RSA_PKCS_V21:
1273 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001274 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001276
1277 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001280}
1281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001283/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001284 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001287 int (*f_rng)(void *, unsigned char *, size_t),
1288 void *p_rng,
1289 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001290 const unsigned char *label, size_t label_len,
1291 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001292 const unsigned char *input,
1293 unsigned char *output,
1294 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001295{
Paul Bakker23986e52011-04-24 08:57:21 +00001296 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001297 size_t ilen, i, pad_len;
1298 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1300 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001301 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 const mbedtls_md_info_t *md_info;
1303 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001304
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001305 /*
1306 * Parameters sanity checks
1307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1309 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
1311 ilen = ctx->len;
1312
Paul Bakker27fdf462011-06-09 13:55:13 +00001313 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001317 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001319
Janos Follathc17cda12016-02-11 11:08:18 +00001320 hlen = mbedtls_md_get_size( md_info );
1321
1322 // checking for integer underflow
1323 if( 2 * hlen + 2 > ilen )
1324 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1325
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001326 /*
1327 * RSA operation
1328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1330 ? mbedtls_rsa_public( ctx, input, buf )
1331 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
1333 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001334 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001336 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001337 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001340 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1341 {
1342 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001343 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001344 }
1345
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001346 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001347 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1348 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001349 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001350 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1351 &md_ctx ) ) != 0 )
1352 {
1353 mbedtls_md_free( &md_ctx );
1354 goto cleanup;
1355 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001358
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001359 /* Generate lHash */
1360 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1361 goto cleanup;
1362
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001363 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001364 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001365 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001367 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001368
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001369 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001370
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001371 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001372
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001373 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001374 for( i = 0; i < hlen; i++ )
1375 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001376
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001377 /* Get zero-padding len, but always read till end of buffer
1378 * (minus one, for the 01 byte) */
1379 pad_len = 0;
1380 pad_done = 0;
1381 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1382 {
1383 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001384 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001385 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001386
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001387 p += pad_len;
1388 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001389
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001390 /*
1391 * The only information "leaked" is whether the padding was correct or not
1392 * (eg, no data is copied if it was not correct). This meets the
1393 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1394 * the different error conditions.
1395 */
1396 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001397 {
1398 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1399 goto cleanup;
1400 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001401
Paul Bakker66d5d072014-06-17 16:39:18 +02001402 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001403 {
1404 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1405 goto cleanup;
1406 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001407
1408 *olen = ilen - (p - buf);
1409 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001410 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001411
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001412cleanup:
1413 mbedtls_zeroize( buf, sizeof( buf ) );
1414 mbedtls_zeroize( lhash, sizeof( lhash ) );
1415
1416 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001417}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001421/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1422 *
1423 * \param value The value to analyze.
Gilles Peskineb4739162018-10-04 18:32:29 +02001424 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001425 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001426static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001427{
1428 /* MSVC has a warning about unary minus on unsigned, but this is
1429 * well-defined and precisely what we want to do here */
1430#if defined(_MSC_VER)
1431#pragma warning( push )
1432#pragma warning( disable : 4146 )
1433#endif
1434 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1435#if defined(_MSC_VER)
1436#pragma warning( pop )
1437#endif
1438}
1439
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001440/** Check whether a size is out of bounds, without branches.
1441 *
1442 * This is equivalent to `size > max`, but is likely to be compiled to
1443 * to code using bitwise operation rather than a branch.
1444 *
1445 * \param size Size to check.
1446 * \param max Maximum desired value for \p size.
1447 * \return \c 0 if `size <= max`.
1448 * \return \c 1 if `size > max`.
1449 */
1450static unsigned size_greater_than( size_t size, size_t max )
1451{
1452 /* Return the sign bit (1 for negative) of (max - size). */
1453 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1454}
1455
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001456/** Choose between two integer values, without branches.
1457 *
Gilles Peskineb4739162018-10-04 18:32:29 +02001458 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1459 * to code using bitwise operation rather than a branch.
1460 *
1461 * \param cond Condition to test.
1462 * \param if1 Value to use if \p cond is nonzero.
1463 * \param if0 Value to use if \p cond is zero.
1464 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001465 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001466static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001467{
Gilles Peskineb4739162018-10-04 18:32:29 +02001468 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001469 return( ( mask & if1 ) | (~mask & if0 ) );
1470}
1471
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001472/** Shift some data towards the left inside a buffer without leaking
1473 * the length of the data through side channels.
1474 *
1475 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1476 * ```
1477 * memmove(start, start + offset, total - offset);
1478 * memset(start + offset, 0, total - offset);
1479 * ```
1480 * but it strives to use a memory access pattern (and thus total timing)
1481 * that does not depend on \p offset. This timing independence comes at
1482 * the expense of performance.
1483 *
1484 * \param start Pointer to the start of the buffer.
1485 * \param total Total size of the buffer.
1486 * \param offset Offset from which to copy \p total - \p offset bytes.
1487 */
1488static void mem_move_to_left( void *start,
1489 size_t total,
1490 size_t offset )
1491{
1492 volatile unsigned char *buf = start;
1493 size_t i, n;
1494 if( total == 0 )
1495 return;
1496 for( i = 0; i < total; i++ )
1497 {
1498 unsigned no_op = size_greater_than( total - offset, i );
1499 /* The first `total - offset` passes are a no-op. The last
1500 * `offset` passes shift the data one byte to the left and
1501 * zero out the last byte. */
1502 for( n = 0; n < total - 1; n++ )
Gilles Peskine66a28e92018-10-12 19:15:34 +02001503 {
1504 unsigned char current = buf[n];
1505 unsigned char next = buf[n+1];
1506 buf[n] = if_int( no_op, current, next );
1507 }
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001508 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1509 }
1510}
1511
Paul Bakkerb3869132013-02-28 17:21:01 +01001512/*
1513 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001516 int (*f_rng)(void *, unsigned char *, size_t),
1517 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001518 int mode, size_t *olen,
1519 const unsigned char *input,
1520 unsigned char *output,
Gilles Peskinecd500f32018-10-02 22:43:06 +02001521 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001522{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001523 int ret;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001524 size_t ilen = ctx->len;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001525 size_t i;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001526 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1527 ilen - 11 :
1528 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001530 /* The following variables take sensitive values: their value must
1531 * not leak into the observable behavior of the function other than
1532 * the designated outputs (output, olen, return value). Otherwise
1533 * this would open the execution of the function to
1534 * side-channel-based variants of the Bleichenbacher padding oracle
1535 * attack. Potential side channels include overall timing, memory
1536 * access patterns (especially visible to an adversary who has access
1537 * to a shared memory cache), and branches (especially visible to
1538 * an adversary who has access to a shared code cache or to a shared
1539 * branch predictor). */
1540 size_t pad_count = 0;
1541 unsigned bad = 0;
1542 unsigned char pad_done = 0;
1543 size_t plaintext_size = 0;
1544 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1547 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001548
Paul Bakkerb3869132013-02-28 17:21:01 +01001549 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1553 ? mbedtls_rsa_public( ctx, input, buf )
1554 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001555
1556 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001557 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001558
Gilles Peskine0b330f72018-10-05 15:06:12 +02001559 /* Check and get padding length in constant time and constant
1560 * memory trace. The first byte must be 0. */
1561 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001565 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1566 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001567 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001569 /* Read the whole buffer. Set pad_done to nonzero if we find
1570 * the 0x00 byte and remember the padding length in pad_count. */
1571 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001572 {
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001573 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001574 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001575 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001576 }
1577 else
1578 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001579 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1580 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001581 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001582
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001583 /* Read the whole buffer. Set pad_done to nonzero if we find
1584 * the 0x00 byte and remember the padding length in pad_count.
1585 * If there's a non-0xff byte in the padding, the padding is bad. */
1586 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001587 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001588 pad_done |= if_int( buf[i], 0, 1 );
1589 pad_count += if_int( pad_done, 0, 1 );
1590 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 }
1593
Gilles Peskine0b330f72018-10-05 15:06:12 +02001594 /* If pad_done is still zero, there's no data, only unfinished padding. */
1595 bad |= if_int( pad_done, 0, 1 );
1596
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001597 /* There must be at least 8 bytes of padding. */
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001598 bad |= size_greater_than( 8, pad_count );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001599
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001600 /* If the padding is valid, set plaintext_size to the number of
1601 * remaining bytes after stripping the padding. If the padding
1602 * is invalid, avoid leaking this fact through the size of the
1603 * output: use the maximum message size that fits in the output
1604 * buffer. Do it without branches to avoid leaking the padding
1605 * validity through timing. RSA keys are small enough that all the
1606 * size_t values involved fit in unsigned int. */
Gilles Peskineb4739162018-10-04 18:32:29 +02001607 plaintext_size = if_int( bad,
1608 (unsigned) plaintext_max_size,
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001609 (unsigned) ( ilen - pad_count - 3 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001610
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001611 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001612 * buffer and to 1 otherwise. */
1613 output_too_large = size_greater_than( plaintext_size,
1614 plaintext_max_size );
Paul Bakker060c5682009-01-12 21:48:39 +00001615
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001616 /* Set ret without branches to avoid timing attacks. Return:
1617 * - INVALID_PADDING if the padding is bad (bad != 0).
1618 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1619 * plaintext does not fit in the output buffer.
1620 * - 0 if the padding is correct. */
Gilles Peskine84a21d52018-10-12 19:19:12 +02001621 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1622 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1623 0 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001624
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001625 /* If the padding is bad or the plaintext is too large, zero the
1626 * data that we're about to copy to the output buffer.
1627 * We need to copy the same amount of data
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001628 * from the same buffer whether the padding is good or not to
1629 * avoid leaking the padding validity through overall timing or
1630 * through memory or cache access patterns. */
Gilles Peskine0b330f72018-10-05 15:06:12 +02001631 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001632 for( i = 11; i < ilen; i++ )
Gilles Peskine0b330f72018-10-05 15:06:12 +02001633 buf[i] &= ~bad;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001634
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001635 /* If the plaintext is too large, truncate it to the buffer size.
1636 * Copy anyway to avoid revealing the length through timing, because
1637 * revealing the length is as bad as revealing the padding validity
1638 * for a Bleichenbacher attack. */
1639 plaintext_size = if_int( output_too_large,
1640 (unsigned) plaintext_max_size,
1641 (unsigned) plaintext_size );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001642
Gilles Peskine087544b2018-10-04 22:45:13 +02001643 /* Move the plaintext to the leftmost position where it can start in
1644 * the working buffer, i.e. make it start plaintext_max_size from
1645 * the end of the buffer. Do this with a memory access trace that
1646 * does not depend on the plaintext size. After this move, the
1647 * starting location of the plaintext is no longer sensitive
1648 * information. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001649 mem_move_to_left( buf + ilen - plaintext_max_size,
1650 plaintext_max_size,
Gilles Peskine087544b2018-10-04 22:45:13 +02001651 plaintext_max_size - plaintext_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001652
Gilles Peskine087544b2018-10-04 22:45:13 +02001653 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001654 * into the output buffer. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001655 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001656
1657 /* Report the amount of data we copied to the output buffer. In case
1658 * of errors (bad padding or output too large), the value of *olen
1659 * when this function returns is not specified. Making it equivalent
1660 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001661 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001663cleanup:
1664 mbedtls_zeroize( buf, sizeof( buf ) );
1665
1666 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
1670/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001671 * Do an RSA operation, then remove the message padding
1672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001674 int (*f_rng)(void *, unsigned char *, size_t),
1675 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001676 int mode, size_t *olen,
1677 const unsigned char *input,
1678 unsigned char *output,
1679 size_t output_max_len)
1680{
1681 switch( ctx->padding )
1682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#if defined(MBEDTLS_PKCS1_V15)
1684 case MBEDTLS_RSA_PKCS_V15:
1685 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001686 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001687#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#if defined(MBEDTLS_PKCS1_V21)
1690 case MBEDTLS_RSA_PKCS_V21:
1691 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001692 olen, input, output,
1693 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001694#endif
1695
1696 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001698 }
1699}
1700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001702/*
1703 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001706 int (*f_rng)(void *, unsigned char *, size_t),
1707 void *p_rng,
1708 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001710 unsigned int hashlen,
1711 const unsigned char *hash,
1712 unsigned char *sig )
1713{
1714 size_t olen;
1715 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001717 unsigned int slen, hlen, offset = 0;
1718 int ret;
1719 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 const mbedtls_md_info_t *md_info;
1721 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1724 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001725
1726 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001728
1729 olen = ctx->len;
1730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001732 {
Simon Butcher02037452016-03-01 21:19:12 +00001733 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001735 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001739 }
1740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001742 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001746 slen = hlen;
1747
1748 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001750
1751 memset( sig, 0, olen );
1752
Simon Butcher02037452016-03-01 21:19:12 +00001753 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001754 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001756
Simon Butcher02037452016-03-01 21:19:12 +00001757 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001758 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001759 p += olen - hlen * 2 - 2;
1760 *p++ = 0x01;
1761 memcpy( p, salt, slen );
1762 p += slen;
1763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001765 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001766 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001767
Simon Butcher02037452016-03-01 21:19:12 +00001768 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001769 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1770 goto exit;
1771 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1772 goto exit;
1773 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1774 goto exit;
1775 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1776 goto exit;
1777 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1778 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001779
Simon Butcher02037452016-03-01 21:19:12 +00001780 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001781 if( msb % 8 == 0 )
1782 offset = 1;
1783
Simon Butcher02037452016-03-01 21:19:12 +00001784 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001785 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1786 &md_ctx ) ) != 0 )
1787 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001788
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001789 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001790 sig[0] &= 0xFF >> ( olen * 8 - msb );
1791
1792 p += hlen;
1793 *p++ = 0xBC;
1794
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001795 mbedtls_zeroize( salt, sizeof( salt ) );
1796
1797exit:
1798 mbedtls_md_free( &md_ctx );
1799
1800 if( ret != 0 )
1801 return( ret );
1802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 return( ( mode == MBEDTLS_RSA_PUBLIC )
1804 ? mbedtls_rsa_public( ctx, sig, sig )
1805 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001806}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001810/*
1811 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1812 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001813
1814/* Construct a PKCS v1.5 encoding of a hashed message
1815 *
1816 * This is used both for signature generation and verification.
1817 *
1818 * Parameters:
1819 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001820 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001821 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001822 * - hash: Buffer containing the hashed message or the raw data.
1823 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001824 * - dst: Buffer to hold the encoded message.
1825 *
1826 * Assumptions:
1827 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1828 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001829 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001830 *
1831 */
1832static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1833 unsigned int hashlen,
1834 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001835 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001836 unsigned char *dst )
1837{
1838 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001839 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001840 unsigned char *p = dst;
1841 const char *oid = NULL;
1842
1843 /* Are we signing hashed or raw data? */
1844 if( md_alg != MBEDTLS_MD_NONE )
1845 {
1846 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1847 if( md_info == NULL )
1848 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1849
1850 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1851 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1852
1853 hashlen = mbedtls_md_get_size( md_info );
1854
1855 /* Double-check that 8 + hashlen + oid_size can be used as a
1856 * 1-byte ASN.1 length encoding and that there's no overflow. */
1857 if( 8 + hashlen + oid_size >= 0x80 ||
1858 10 + hashlen < hashlen ||
1859 10 + hashlen + oid_size < 10 + hashlen )
1860 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1861
1862 /*
1863 * Static bounds check:
1864 * - Need 10 bytes for five tag-length pairs.
1865 * (Insist on 1-byte length encodings to protect against variants of
1866 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1867 * - Need hashlen bytes for hash
1868 * - Need oid_size bytes for hash alg OID.
1869 */
1870 if( nb_pad < 10 + hashlen + oid_size )
1871 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1872 nb_pad -= 10 + hashlen + oid_size;
1873 }
1874 else
1875 {
1876 if( nb_pad < hashlen )
1877 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1878
1879 nb_pad -= hashlen;
1880 }
1881
Hanno Becker2b2f8982017-09-27 17:10:03 +01001882 /* Need space for signature header and padding delimiter (3 bytes),
1883 * and 8 bytes for the minimal padding */
1884 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001885 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1886 nb_pad -= 3;
1887
1888 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001889 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001890
1891 /* Write signature header and padding */
1892 *p++ = 0;
1893 *p++ = MBEDTLS_RSA_SIGN;
1894 memset( p, 0xFF, nb_pad );
1895 p += nb_pad;
1896 *p++ = 0;
1897
1898 /* Are we signing raw data? */
1899 if( md_alg == MBEDTLS_MD_NONE )
1900 {
1901 memcpy( p, hash, hashlen );
1902 return( 0 );
1903 }
1904
1905 /* Signing hashed data, add corresponding ASN.1 structure
1906 *
1907 * DigestInfo ::= SEQUENCE {
1908 * digestAlgorithm DigestAlgorithmIdentifier,
1909 * digest Digest }
1910 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1911 * Digest ::= OCTET STRING
1912 *
1913 * Schematic:
1914 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1915 * TAG-NULL + LEN [ NULL ] ]
1916 * TAG-OCTET + LEN [ HASH ] ]
1917 */
1918 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001919 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001920 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001921 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001922 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001923 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001924 memcpy( p, oid, oid_size );
1925 p += oid_size;
1926 *p++ = MBEDTLS_ASN1_NULL;
1927 *p++ = 0x00;
1928 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001929 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001930 memcpy( p, hash, hashlen );
1931 p += hashlen;
1932
1933 /* Just a sanity-check, should be automatic
1934 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001935 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001936 {
Hanno Beckere58d38c2017-09-27 17:09:00 +01001937 mbedtls_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001938 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1939 }
1940
1941 return( 0 );
1942}
1943
Paul Bakkerb3869132013-02-28 17:21:01 +01001944/*
1945 * Do an RSA operation to sign the message digest
1946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001948 int (*f_rng)(void *, unsigned char *, size_t),
1949 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001950 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001952 unsigned int hashlen,
1953 const unsigned char *hash,
1954 unsigned char *sig )
1955{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001956 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001957 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1960 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001961
Hanno Beckerfdf38032017-09-06 12:35:55 +01001962 /*
1963 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1964 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001965
Hanno Beckerfdf38032017-09-06 12:35:55 +01001966 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1967 ctx->len, sig ) ) != 0 )
1968 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001969
1970 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001971 * Call respective RSA primitive
1972 */
1973
1974 if( mode == MBEDTLS_RSA_PUBLIC )
1975 {
1976 /* Skip verification on a public key operation */
1977 return( mbedtls_rsa_public( ctx, sig, sig ) );
1978 }
1979
1980 /* Private key operation
1981 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001982 * In order to prevent Lenstra's attack, make the signature in a
1983 * temporary buffer and check it before returning it.
1984 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001985
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001986 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001987 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001988 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1989
Hanno Beckerfdf38032017-09-06 12:35:55 +01001990 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001991 if( verif == NULL )
1992 {
1993 mbedtls_free( sig_try );
1994 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1995 }
1996
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001997 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1998 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1999
Hanno Becker171a8f12017-09-06 12:32:16 +01002000 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002001 {
2002 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2003 goto cleanup;
2004 }
2005
2006 memcpy( sig, sig_try, ctx->len );
2007
2008cleanup:
2009 mbedtls_free( sig_try );
2010 mbedtls_free( verif );
2011
2012 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002013}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002015
2016/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 * Do an RSA operation to sign the message digest
2018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002020 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002021 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002024 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002025 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 unsigned char *sig )
2027{
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 switch( ctx->padding )
2029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030#if defined(MBEDTLS_PKCS1_V15)
2031 case MBEDTLS_RSA_PKCS_V15:
2032 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002033 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#if defined(MBEDTLS_PKCS1_V21)
2037 case MBEDTLS_RSA_PKCS_V21:
2038 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002039 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002040#endif
2041
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002045}
2046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002048/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002049 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002052 int (*f_rng)(void *, unsigned char *, size_t),
2053 void *p_rng,
2054 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002056 unsigned int hashlen,
2057 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002059 int expected_salt_len,
2060 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002061{
Paul Bakker23986e52011-04-24 08:57:21 +00002062 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002063 size_t siglen;
2064 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002065 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002067 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002068 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002069 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 const mbedtls_md_info_t *md_info;
2071 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002072 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2075 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002076
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 siglen = ctx->len;
2078
Paul Bakker27fdf462011-06-09 13:55:13 +00002079 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2083 ? mbedtls_rsa_public( ctx, sig, buf )
2084 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002085
2086 if( ret != 0 )
2087 return( ret );
2088
2089 p = buf;
2090
Paul Bakkerb3869132013-02-28 17:21:01 +01002091 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 {
Simon Butcher02037452016-03-01 21:19:12 +00002096 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002098 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002102 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002105 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002109
Paul Bakkerb3869132013-02-28 17:21:01 +01002110 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002111
Simon Butcher02037452016-03-01 21:19:12 +00002112 /*
2113 * Note: EMSA-PSS verification is over the length of N - 1 bits
2114 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002115 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002116
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002117 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2118 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2119
Simon Butcher02037452016-03-01 21:19:12 +00002120 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002121 if( msb % 8 == 0 )
2122 {
2123 p++;
2124 siglen -= 1;
2125 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002126
Gilles Peskine139108a2017-10-18 19:03:42 +02002127 if( siglen < hlen + 2 )
2128 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2129 hash_start = p + siglen - hlen - 1;
2130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002132 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002133 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002134
Jaeden Amero66954e12018-01-25 16:05:54 +00002135 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2136 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002137 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002138
Paul Bakkerb3869132013-02-28 17:21:01 +01002139 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002140
Gilles Peskine6a54b022017-10-17 19:02:13 +02002141 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002142 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002143
Gilles Peskine91048a32017-10-19 17:46:14 +02002144 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002145 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002146 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2147 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002148 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002149
Gilles Peskine6a54b022017-10-17 19:02:13 +02002150 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002153 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002154 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002155 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2156 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002157 }
2158
Simon Butcher02037452016-03-01 21:19:12 +00002159 /*
2160 * Generate H = Hash( M' )
2161 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002162 ret = mbedtls_md_starts( &md_ctx );
2163 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002164 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002165 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2166 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002167 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002168 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2169 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002170 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002171 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2172 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002173 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002174 ret = mbedtls_md_finish( &md_ctx, result );
2175 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002176 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002177
Jaeden Amero66954e12018-01-25 16:05:54 +00002178 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002179 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002180 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002181 goto exit;
2182 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002183
2184exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002186
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002187 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002188}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002189
2190/*
2191 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002194 int (*f_rng)(void *, unsigned char *, size_t),
2195 void *p_rng,
2196 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002198 unsigned int hashlen,
2199 const unsigned char *hash,
2200 const unsigned char *sig )
2201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2203 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002204 : md_alg;
2205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002207 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002209 sig ) );
2210
2211}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002215/*
2216 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002219 int (*f_rng)(void *, unsigned char *, size_t),
2220 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002221 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002223 unsigned int hashlen,
2224 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002225 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002226{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002227 int ret = 0;
2228 const size_t sig_len = ctx->len;
2229 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2232 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002233
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002234 /*
2235 * Prepare expected PKCS1 v1.5 encoding of hash.
2236 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002237
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002238 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2239 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2240 {
2241 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2242 goto cleanup;
2243 }
2244
2245 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2246 encoded_expected ) ) != 0 )
2247 goto cleanup;
2248
2249 /*
2250 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2251 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002254 ? mbedtls_rsa_public( ctx, sig, encoded )
2255 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002256 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002257 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002258
Simon Butcher02037452016-03-01 21:19:12 +00002259 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002260 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002261 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002262
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002263 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2264 sig_len ) ) != 0 )
2265 {
2266 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2267 goto cleanup;
2268 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002269
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002270cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002271
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002272 if( encoded != NULL )
2273 {
2274 mbedtls_zeroize( encoded, sig_len );
2275 mbedtls_free( encoded );
2276 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002277
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002278 if( encoded_expected != NULL )
2279 {
2280 mbedtls_zeroize( encoded_expected, sig_len );
2281 mbedtls_free( encoded_expected );
2282 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002283
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002284 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002287
2288/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002289 * Do an RSA operation and check the message digest
2290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002292 int (*f_rng)(void *, unsigned char *, size_t),
2293 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002294 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002296 unsigned int hashlen,
2297 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002298 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002299{
2300 switch( ctx->padding )
2301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302#if defined(MBEDTLS_PKCS1_V15)
2303 case MBEDTLS_RSA_PKCS_V15:
2304 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002305 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002306#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308#if defined(MBEDTLS_PKCS1_V21)
2309 case MBEDTLS_RSA_PKCS_V21:
2310 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002311 hashlen, hash, sig );
2312#endif
2313
2314 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002316 }
2317}
2318
2319/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002320 * Copy the components of an RSA key
2321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002323{
2324 int ret;
2325
2326 dst->ver = src->ver;
2327 dst->len = src->len;
2328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2330 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2333 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2334 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002335
2336#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2338 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2339 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2341 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002342#endif
2343
2344 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2347 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002348
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002349 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002350 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002351
2352cleanup:
2353 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002355
2356 return( ret );
2357}
2358
2359/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002360 * Free the components of an RSA key
2361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002363{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002365 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2366 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002368
Hanno Becker33c30a02017-08-23 07:00:22 +01002369#if !defined(MBEDTLS_RSA_NO_CRT)
2370 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2371 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2372 mbedtls_mpi_free( &ctx->DP );
2373#endif /* MBEDTLS_RSA_NO_CRT */
2374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375#if defined(MBEDTLS_THREADING_C)
2376 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002377#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002378}
2379
Hanno Beckerab377312017-08-23 16:24:51 +01002380#endif /* !MBEDTLS_RSA_ALT */
2381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002383
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002384#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
2386/*
2387 * Example RSA-1024 keypair, for test purposes
2388 */
2389#define KEY_LEN 128
2390
2391#define RSA_N "9292758453063D803DD603D5E777D788" \
2392 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2393 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2394 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2395 "93A89813FBF3C4F8066D2D800F7C38A8" \
2396 "1AE31942917403FF4946B0A83D3D3E05" \
2397 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2398 "5E94BB77B07507233A0BC7BAC8F90F79"
2399
2400#define RSA_E "10001"
2401
2402#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2403 "66CA472BC44D253102F8B4A9D3BFA750" \
2404 "91386C0077937FE33FA3252D28855837" \
2405 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2406 "DF79C5CE07EE72C7F123142198164234" \
2407 "CABB724CF78B8173B9F880FC86322407" \
2408 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2409 "071513A1E85B5DFA031F21ECAE91A34D"
2410
2411#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2412 "2C01CAD19EA484A87EA4377637E75500" \
2413 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2414 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2415
2416#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2417 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2418 "910E4168387E3C30AA1E00C339A79508" \
2419 "8452DD96A9A5EA5D9DCA68DA636032AF"
2420
Paul Bakker5121ce52009-01-03 21:22:43 +00002421#define PT_LEN 24
2422#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2423 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002426static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002427{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002428#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002429 size_t i;
2430
Paul Bakker545570e2010-07-18 09:00:25 +00002431 if( rng_state != NULL )
2432 rng_state = NULL;
2433
Paul Bakkera3d195c2011-11-27 21:07:34 +00002434 for( i = 0; i < len; ++i )
2435 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002436#else
2437 if( rng_state != NULL )
2438 rng_state = NULL;
2439
2440 arc4random_buf( output, len );
2441#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002442
Paul Bakkera3d195c2011-11-27 21:07:34 +00002443 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002446
Paul Bakker5121ce52009-01-03 21:22:43 +00002447/*
2448 * Checkup routine
2449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002451{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002452 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002454 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 unsigned char rsa_plaintext[PT_LEN];
2457 unsigned char rsa_decrypted[PT_LEN];
2458 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002460 unsigned char sha1sum[20];
2461#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
Hanno Becker3a701162017-08-22 13:52:43 +01002463 mbedtls_mpi K;
2464
2465 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002467
Hanno Becker3a701162017-08-22 13:52:43 +01002468 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2469 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2470 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2471 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2472 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2473 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2474 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2475 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2476 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2477 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2478
Hanno Becker7f25f852017-10-10 16:56:22 +01002479 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
2481 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2485 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 {
2487 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
Hanno Beckera5fa0792018-03-09 10:42:23 +00002490 ret = 1;
2491 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 }
2493
2494 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
2497 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2498
Hanno Becker98838b02017-10-02 13:16:10 +01002499 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2500 PT_LEN, rsa_plaintext,
2501 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 {
2503 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
Hanno Beckera5fa0792018-03-09 10:42:23 +00002506 ret = 1;
2507 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 }
2509
2510 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Hanno Becker98838b02017-10-02 13:16:10 +01002513 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2514 &len, rsa_ciphertext, rsa_decrypted,
2515 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 {
2517 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Hanno Beckera5fa0792018-03-09 10:42:23 +00002520 ret = 1;
2521 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 }
2523
2524 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2525 {
2526 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
Hanno Beckera5fa0792018-03-09 10:42:23 +00002529 ret = 1;
2530 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 }
2532
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002533 if( verbose != 0 )
2534 mbedtls_printf( "passed\n" );
2535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002537 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002538 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002540 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002541 {
2542 if( verbose != 0 )
2543 mbedtls_printf( "failed\n" );
2544
2545 return( 1 );
2546 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Hanno Becker98838b02017-10-02 13:16:10 +01002548 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2549 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2550 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 {
2552 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Hanno Beckera5fa0792018-03-09 10:42:23 +00002555 ret = 1;
2556 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 }
2558
2559 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
Hanno Becker98838b02017-10-02 13:16:10 +01002562 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2563 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2564 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 {
2566 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
Hanno Beckera5fa0792018-03-09 10:42:23 +00002569 ret = 1;
2570 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 }
2572
2573 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002574 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002576
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002577 if( verbose != 0 )
2578 mbedtls_printf( "\n" );
2579
Paul Bakker3d8fb632014-04-17 12:42:41 +02002580cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002581 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 mbedtls_rsa_free( &rsa );
2583#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002584 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002586 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002587}
2588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591#endif /* MBEDTLS_RSA_C */