blob: c5dbdacf22af318d1bd8a2349f0fa6356326971e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
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 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
Hanno Becker74716312017-10-02 10:00:37 +010046
Paul Bakker5121ce52009-01-03 21:22:43 +000047/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000048 * The following sources were referenced in the design of this implementation
49 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000050 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000051 * [1] A method for obtaining digital signatures and public-key cryptosystems
52 * R Rivest, A Shamir, and L Adleman
53 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
54 *
55 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
56 * Menezes, van Oorschot and Vanstone
57 *
Janos Follathe81102e2017-03-22 13:38:28 +000058 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
59 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
60 * Stefan Mangard
61 * https://arxiv.org/abs/1702.08719v2
62 *
Paul Bakker5121ce52009-01-03 21:22:43 +000063 */
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020067#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020069#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010074#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000076
Rich Evans00ab4702015-02-06 13:43:58 +000077#include <string.h>
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000081#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000082
gufe44206cb392020-08-03 17:56:50 +020083#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000084#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000085#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000088#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010089#else
Rich Evans00ab4702015-02-06 13:43:58 +000090#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020092#define mbedtls_calloc calloc
93#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010094#endif
95
Hanno Beckera565f542017-10-11 11:00:19 +010096#if !defined(MBEDTLS_RSA_ALT)
97
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010098/* Implementation that should never be optimized out by the compiler */
99static void mbedtls_zeroize( void *v, size_t n ) {
100 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
101}
102
Manuel Pégourié-Gonnardb0ba5bc2018-03-13 13:27:14 +0100103#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +0100104/* constant-time buffer comparison */
105static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
106{
107 size_t i;
108 const unsigned char *A = (const unsigned char *) a;
109 const unsigned char *B = (const unsigned char *) b;
110 unsigned char diff = 0;
111
112 for( i = 0; i < n; i++ )
113 diff |= A[i] ^ B[i];
114
115 return( diff );
116}
Manuel Pégourié-Gonnardb0ba5bc2018-03-13 13:27:14 +0100117#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +0100118
Hanno Becker617c1ae2017-08-23 14:11:24 +0100119int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
120 const mbedtls_mpi *N,
121 const mbedtls_mpi *P, const mbedtls_mpi *Q,
122 const mbedtls_mpi *D, const mbedtls_mpi *E )
123{
124 int ret;
125
126 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
127 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
128 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
129 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
130 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
131 {
132 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
133 }
134
135 if( N != NULL )
136 ctx->len = mbedtls_mpi_size( &ctx->N );
137
138 return( 0 );
139}
140
141int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100142 unsigned char const *N, size_t N_len,
143 unsigned char const *P, size_t P_len,
144 unsigned char const *Q, size_t Q_len,
145 unsigned char const *D, size_t D_len,
146 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100147{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000148 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100149
150 if( N != NULL )
151 {
152 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
153 ctx->len = mbedtls_mpi_size( &ctx->N );
154 }
155
156 if( P != NULL )
157 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
158
159 if( Q != NULL )
160 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
161
162 if( D != NULL )
163 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
164
165 if( E != NULL )
166 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
167
168cleanup:
169
170 if( ret != 0 )
171 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
172
173 return( 0 );
174}
175
Hanno Becker705fc682017-10-10 17:57:02 +0100176/*
177 * Checks whether the context fields are set in such a way
178 * that the RSA primitives will be able to execute without error.
179 * It does *not* make guarantees for consistency of the parameters.
180 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100181static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
182 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100183{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100184#if !defined(MBEDTLS_RSA_NO_CRT)
185 /* blinding_needed is only used for NO_CRT to decide whether
186 * P,Q need to be present or not. */
187 ((void) blinding_needed);
188#endif
189
Hanno Becker3a760a12018-01-05 08:14:49 +0000190 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
191 ctx->len > MBEDTLS_MPI_MAX_SIZE )
192 {
Hanno Becker705fc682017-10-10 17:57:02 +0100193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000194 }
Hanno Becker705fc682017-10-10 17:57:02 +0100195
196 /*
197 * 1. Modular exponentiation needs positive, odd moduli.
198 */
199
200 /* Modular exponentiation wrt. N is always used for
201 * RSA public key operations. */
202 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
203 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
204 {
205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
206 }
207
208#if !defined(MBEDTLS_RSA_NO_CRT)
209 /* Modular exponentiation for P and Q is only
210 * used for private key operations and if CRT
211 * is used. */
212 if( is_priv &&
213 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
214 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
215 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
216 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
217 {
218 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
219 }
220#endif /* !MBEDTLS_RSA_NO_CRT */
221
222 /*
223 * 2. Exponents must be positive
224 */
225
226 /* Always need E for public key operations */
227 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
228 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
229
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100230#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100231 /* For private key operations, use D or DP & DQ
232 * as (unblinded) exponents. */
233 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
234 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
235#else
236 if( is_priv &&
237 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
238 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
239 {
240 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
241 }
242#endif /* MBEDTLS_RSA_NO_CRT */
243
244 /* Blinding shouldn't make exponents negative either,
245 * so check that P, Q >= 1 if that hasn't yet been
246 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100247#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100248 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100249 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
250 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
251 {
252 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
253 }
254#endif
255
256 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100257 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100258#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100259 if( is_priv &&
260 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
261 {
262 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
263 }
264#endif
265
266 return( 0 );
267}
268
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100269int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100270{
271 int ret = 0;
272
Hanno Becker617c1ae2017-08-23 14:11:24 +0100273 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
274 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
275 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
276 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
277 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100278
Jack Lloyd100e1472020-01-29 13:13:04 -0500279#if !defined(MBEDTLS_RSA_NO_CRT)
280 const int have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
281 const int have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
282 const int have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
283#endif
284
Hanno Becker617c1ae2017-08-23 14:11:24 +0100285 /*
286 * Check whether provided parameters are enough
287 * to deduce all others. The following incomplete
288 * parameter sets for private keys are supported:
289 *
290 * (1) P, Q missing.
291 * (2) D and potentially N missing.
292 *
293 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100294
Hanno Becker2cca6f32017-09-29 11:46:40 +0100295 const int n_missing = have_P && have_Q && have_D && have_E;
296 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
297 const int d_missing = have_P && have_Q && !have_D && have_E;
298 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
299
300 /* These three alternatives are mutually exclusive */
301 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100302
Hanno Becker617c1ae2017-08-23 14:11:24 +0100303 if( !is_priv && !is_pub )
304 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
305
306 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100307 * Step 1: Deduce N if P, Q are provided.
308 */
309
310 if( !have_N && have_P && have_Q )
311 {
312 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
313 &ctx->Q ) ) != 0 )
314 {
315 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
316 }
317
318 ctx->len = mbedtls_mpi_size( &ctx->N );
319 }
320
321 /*
322 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100323 */
324
325 if( pq_missing )
326 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100327 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100328 &ctx->P, &ctx->Q );
329 if( ret != 0 )
330 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
331
332 }
333 else if( d_missing )
334 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100335 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
336 &ctx->Q,
337 &ctx->E,
338 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100339 {
340 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
341 }
342 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100343
Hanno Becker617c1ae2017-08-23 14:11:24 +0100344 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100345 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100346 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100347 */
348
Hanno Becker23344b52017-08-23 07:43:27 +0100349#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd100e1472020-01-29 13:13:04 -0500350 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100351 {
352 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
353 &ctx->DP, &ctx->DQ, &ctx->QP );
354 if( ret != 0 )
355 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
356 }
Hanno Becker23344b52017-08-23 07:43:27 +0100357#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100358
359 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100360 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100361 */
362
Hanno Beckerebd2c022017-10-12 10:54:53 +0100363 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100364}
365
Hanno Becker617c1ae2017-08-23 14:11:24 +0100366int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
367 unsigned char *N, size_t N_len,
368 unsigned char *P, size_t P_len,
369 unsigned char *Q, size_t Q_len,
370 unsigned char *D, size_t D_len,
371 unsigned char *E, size_t E_len )
372{
373 int ret = 0;
374
375 /* Check if key is private or public */
376 const int is_priv =
377 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
378 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
379 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
380 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
381 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
382
383 if( !is_priv )
384 {
385 /* If we're trying to export private parameters for a public key,
386 * something must be wrong. */
387 if( P != NULL || Q != NULL || D != NULL )
388 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
389
390 }
391
392 if( N != NULL )
393 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
394
395 if( P != NULL )
396 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
397
398 if( Q != NULL )
399 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
400
401 if( D != NULL )
402 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
403
404 if( E != NULL )
405 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100406
407cleanup:
408
409 return( ret );
410}
411
Hanno Becker617c1ae2017-08-23 14:11:24 +0100412int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
413 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
414 mbedtls_mpi *D, mbedtls_mpi *E )
415{
416 int ret;
417
418 /* Check if key is private or public */
419 int is_priv =
420 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
421 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
422 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
423 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
424 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
425
426 if( !is_priv )
427 {
428 /* If we're trying to export private parameters for a public key,
429 * something must be wrong. */
430 if( P != NULL || Q != NULL || D != NULL )
431 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
432
433 }
434
435 /* Export all requested core parameters. */
436
437 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
438 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
439 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
440 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
441 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
442 {
443 return( ret );
444 }
445
446 return( 0 );
447}
448
449/*
450 * Export CRT parameters
451 * This must also be implemented if CRT is not used, for being able to
452 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
453 * can be used in this case.
454 */
455int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
456 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
457{
458 int ret;
459
460 /* Check if key is private or public */
461 int is_priv =
462 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
463 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
464 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
465 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
466 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
467
468 if( !is_priv )
469 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
470
Hanno Beckerdc95c892017-08-23 06:57:02 +0100471#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100472 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100473 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
474 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
475 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
476 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100477 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100478 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100479#else
480 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
481 DP, DQ, QP ) ) != 0 )
482 {
483 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
484 }
485#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100486
487 return( 0 );
488}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100489
Paul Bakker5121ce52009-01-03 21:22:43 +0000490/*
491 * Initialize an RSA context
492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000495 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#if defined(MBEDTLS_THREADING_C)
502 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200503#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000504}
505
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100506/*
507 * Set padding for an existing RSA context
508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100510{
511 ctx->padding = padding;
512 ctx->hash_id = hash_id;
513}
514
Hanno Becker617c1ae2017-08-23 14:11:24 +0100515/*
516 * Get length in bytes of RSA modulus
517 */
518
519size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
520{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100521 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100522}
523
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527/*
528 * Generate an RSA keypair
529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000531 int (*f_rng)(void *, unsigned char *, size_t),
532 void *p_rng,
533 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000534{
535 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100536 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
Paul Bakker21eb2802010-08-16 11:10:02 +0000538 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Janos Follathef441782016-09-21 13:18:12 +0100541 if( nbits % 2 )
542 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
543
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100544 mbedtls_mpi_init( &H );
545 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547 /*
548 * find primes P and Q with Q < P so that:
549 * GCD( E, (P-1)*(Q-1) ) == 1
550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553 do
554 {
Janos Follath10c575b2016-02-23 14:42:48 +0000555 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100556 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
Janos Follathef441782016-09-21 13:18:12 +0100558 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100559 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 continue;
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200565 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 continue;
567
Janos Follathef441782016-09-21 13:18:12 +0100568 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100569 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100570
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100571 /* Temporarily replace P,Q by P-1, Q-1 */
572 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
573 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
574 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100579 /* Restore P,Q */
580 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
581 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
582
583 ctx->len = mbedtls_mpi_size( &ctx->N );
584
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 /*
586 * D = E^-1 mod ((P-1)*(Q-1))
587 * DP = D mod (P - 1)
588 * DQ = D mod (Q - 1)
589 * QP = Q^-1 mod P
590 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100592 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
593
594#if !defined(MBEDTLS_RSA_NO_CRT)
595 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
596 &ctx->DP, &ctx->DQ, &ctx->QP ) );
597#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
Hanno Becker83aad1f2017-08-23 06:45:10 +0100599 /* Double-check */
600 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602cleanup:
603
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100604 mbedtls_mpi_free( &H );
605 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
607 if( ret != 0 )
608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_rsa_free( ctx );
610 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 }
612
Paul Bakker48377d92013-08-30 12:06:24 +0200613 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000614}
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
618/*
619 * Check a public RSA key
620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000622{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100623 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000625
Hanno Becker3a760a12018-01-05 08:14:49 +0000626 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100629 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
Hanno Becker705fc682017-10-10 17:57:02 +0100631 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
632 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100636 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
638 return( 0 );
639}
640
641/*
Hanno Becker705fc682017-10-10 17:57:02 +0100642 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000645{
Hanno Becker705fc682017-10-10 17:57:02 +0100646 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100647 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 {
Hanno Becker98838b02017-10-02 13:16:10 +0100649 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 }
Paul Bakker48377d92013-08-30 12:06:24 +0200651
Hanno Becker98838b02017-10-02 13:16:10 +0100652 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100653 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100655 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000657
Hanno Beckerb269a852017-08-25 08:03:21 +0100658#if !defined(MBEDTLS_RSA_NO_CRT)
659 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
660 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
661 {
662 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
663 }
664#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000665
666 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000667}
668
669/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100670 * Check if contexts holding a public and private key match
671 */
Hanno Becker98838b02017-10-02 13:16:10 +0100672int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
673 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100674{
Hanno Becker98838b02017-10-02 13:16:10 +0100675 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100679 }
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
682 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100685 }
686
687 return( 0 );
688}
689
690/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 * Do an RSA public key operation
692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000694 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 unsigned char *output )
696{
Paul Bakker23986e52011-04-24 08:57:21 +0000697 int ret;
698 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
Hanno Beckerebd2c022017-10-12 10:54:53 +0100701 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100702 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200706#if defined(MBEDTLS_THREADING_C)
707 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
708 return( ret );
709#endif
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200715 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
716 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 }
718
719 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
721 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
723cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200725 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
726 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100727#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
731 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
734 return( 0 );
735}
736
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200737/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200738 * Generate or update blinding values, see section 10 of:
739 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200740 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200741 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200742 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200743static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200744 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
745{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200746 int ret, count = 0;
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200747 mbedtls_mpi R;
748
749 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200750
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200751 if( ctx->Vf.p != NULL )
752 {
753 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
755 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
756 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
757 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200758
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200759 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200760 }
761
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200762 /* Unblinding value: Vf = random number, invertible mod N */
763 do {
764 if( count++ > 10 )
Manuel Pégourié-Gonnardab601d62020-07-16 09:23:30 +0200765 {
766 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
767 goto cleanup;
768 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 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 +0200771
Manuel Pégourié-Gonnardb2b1d8e2020-07-16 09:48:54 +0200772 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200773 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
774 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
775 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
776
Manuel Pégourié-Gonnardb2b1d8e2020-07-16 09:48:54 +0200777 /* At this point, Vi is invertible mod N if and only if both Vf and R
778 * are invertible mod N. If one of them isn't, we don't need to know
779 * which one, we just loop and choose new values for both of them.
780 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200781 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
782 if( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
783 continue;
784 if( ret != 0 )
Manuel Pégourié-Gonnard6ab924d2020-06-26 11:03:19 +0200785 goto cleanup;
786
Manuel Pégourié-Gonnardb2b1d8e2020-07-16 09:48:54 +0200787 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200788 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
789 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
790 } while( 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200791
Manuel Pégourié-Gonnardb2b1d8e2020-07-16 09:48:54 +0200792 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200793 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 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 +0200795
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200796
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200797cleanup:
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200798 mbedtls_mpi_free( &R );
799
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200800 return( ret );
801}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200802
Paul Bakker5121ce52009-01-03 21:22:43 +0000803/*
Janos Follathe81102e2017-03-22 13:38:28 +0000804 * Exponent blinding supposed to prevent side-channel attacks using multiple
805 * traces of measurements to recover the RSA key. The more collisions are there,
806 * the more bits of the key can be recovered. See [3].
807 *
808 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
809 * observations on avarage.
810 *
811 * For example with 28 byte blinding to achieve 2 collisions the adversary has
812 * to make 2^112 observations on avarage.
813 *
814 * (With the currently (as of 2017 April) known best algorithms breaking 2048
815 * bit RSA requires approximately as much time as trying out 2^112 random keys.
816 * Thus in this sense with 28 byte blinding the security is not reduced by
817 * side-channel attacks like the one in [3])
818 *
819 * This countermeasure does not help if the key recovery is possible with a
820 * single trace.
821 */
822#define RSA_EXPONENT_BLINDING 28
823
824/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 * Do an RSA private key operation
826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200828 int (*f_rng)(void *, unsigned char *, size_t),
829 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000830 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000831 unsigned char *output )
832{
Paul Bakker23986e52011-04-24 08:57:21 +0000833 int ret;
834 size_t olen;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000835
836 /* Temporary holding the result */
837 mbedtls_mpi T;
838
839 /* Temporaries holding P-1, Q-1 and the
840 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000841 mbedtls_mpi P1, Q1, R;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000842
843#if !defined(MBEDTLS_RSA_NO_CRT)
844 /* Temporaries holding the results mod p resp. mod q. */
845 mbedtls_mpi TP, TQ;
846
847 /* Temporaries holding the blinded exponents for
848 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000849 mbedtls_mpi DP_blind, DQ_blind;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000850
851 /* Pointers to actual exponents to be used - either the unblinded
852 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000853 mbedtls_mpi *DP = &ctx->DP;
854 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000855#else
856 /* Temporary holding the blinded exponent (if used). */
857 mbedtls_mpi D_blind;
858
859 /* Pointer to actual exponent to be used - either the unblinded
860 * or the blinded one, depending on the presence of a PRNG. */
861 mbedtls_mpi *D = &ctx->D;
862#endif /* MBEDTLS_RSA_NO_CRT */
863
864 /* Temporaries holding the initial input and the double
865 * checked result; should be the same in the end. */
866 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000867
Hanno Beckerebd2c022017-10-12 10:54:53 +0100868 if( rsa_check_context( ctx, 1 /* private key checks */,
869 f_rng != NULL /* blinding y/n */ ) != 0 )
870 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100871 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100872 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100873
Hanno Beckera5fa0792018-03-09 10:42:23 +0000874#if defined(MBEDTLS_THREADING_C)
875 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
876 return( ret );
877#endif
878
879 /* MPI Initialization */
880 mbedtls_mpi_init( &T );
881
882 mbedtls_mpi_init( &P1 );
883 mbedtls_mpi_init( &Q1 );
884 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000885
Janos Follathf9203b42017-03-22 15:13:15 +0000886 if( f_rng != NULL )
887 {
Janos Follathe81102e2017-03-22 13:38:28 +0000888#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000889 mbedtls_mpi_init( &D_blind );
890#else
891 mbedtls_mpi_init( &DP_blind );
892 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000893#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000894 }
Janos Follathe81102e2017-03-22 13:38:28 +0000895
Hanno Beckera5fa0792018-03-09 10:42:23 +0000896#if !defined(MBEDTLS_RSA_NO_CRT)
897 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200898#endif
899
Hanno Beckera5fa0792018-03-09 10:42:23 +0000900 mbedtls_mpi_init( &I );
901 mbedtls_mpi_init( &C );
902
903 /* End of MPI initialization */
904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
906 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000907 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200908 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
909 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 }
911
Hanno Beckera5fa0792018-03-09 10:42:23 +0000912 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
913
Paul Bakkerf451bac2013-08-30 15:37:02 +0200914 if( f_rng != NULL )
915 {
916 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200917 * Blinding
918 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200919 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200920 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
921 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000923
Janos Follathe81102e2017-03-22 13:38:28 +0000924 /*
925 * Exponent blinding
926 */
927 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
928 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
929
Janos Follathf9203b42017-03-22 15:13:15 +0000930#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000931 /*
932 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
933 */
934 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
935 f_rng, p_rng ) );
936 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
937 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
938 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
939
940 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000941#else
942 /*
943 * DP_blind = ( P - 1 ) * R + DP
944 */
945 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
946 f_rng, p_rng ) );
947 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
948 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
949 &ctx->DP ) );
950
951 DP = &DP_blind;
952
953 /*
954 * DQ_blind = ( Q - 1 ) * R + DQ
955 */
956 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
957 f_rng, p_rng ) );
958 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
959 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
960 &ctx->DQ ) );
961
962 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000963#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200964 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000967 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100968#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200969 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000970 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000971 *
Hanno Beckera5fa0792018-03-09 10:42:23 +0000972 * TP = input ^ dP mod P
973 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000975
976 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
977 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
979 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000980 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000981 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000982 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
983 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
984 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000985
986 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000987 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000989 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
990 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200992
Paul Bakkerf451bac2013-08-30 15:37:02 +0200993 if( f_rng != NULL )
994 {
995 /*
996 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200997 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200998 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200999 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001001 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
Hanno Beckera5fa0792018-03-09 10:42:23 +00001003 /* Verify the result to prevent glitching attacks. */
1004 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1005 &ctx->N, &ctx->RN ) );
1006 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
1007 {
1008 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1009 goto cleanup;
1010 }
1011
Paul Bakker5121ce52009-01-03 21:22:43 +00001012 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
1015cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001017 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1018 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001019#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001020
Hanno Beckera5fa0792018-03-09 10:42:23 +00001021 mbedtls_mpi_free( &P1 );
1022 mbedtls_mpi_free( &Q1 );
1023 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001024
1025 if( f_rng != NULL )
1026 {
Janos Follathe81102e2017-03-22 13:38:28 +00001027#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001028 mbedtls_mpi_free( &D_blind );
1029#else
1030 mbedtls_mpi_free( &DP_blind );
1031 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001032#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001033 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
Hanno Beckera5fa0792018-03-09 10:42:23 +00001035 mbedtls_mpi_free( &T );
1036
1037#if !defined(MBEDTLS_RSA_NO_CRT)
1038 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1039#endif
1040
1041 mbedtls_mpi_free( &C );
1042 mbedtls_mpi_free( &I );
1043
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
1047 return( 0 );
1048}
1049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001051/**
1052 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1053 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001054 * \param dst buffer to mask
1055 * \param dlen length of destination buffer
1056 * \param src source of the mask generation
1057 * \param slen length of the source buffer
1058 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001059 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001060static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001064 unsigned char counter[4];
1065 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001066 unsigned int hlen;
1067 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001068 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001071 memset( counter, 0, 4 );
1072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001074
Simon Butcher02037452016-03-01 21:19:12 +00001075 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001076 p = dst;
1077
1078 while( dlen > 0 )
1079 {
1080 use_len = hlen;
1081 if( dlen < hlen )
1082 use_len = dlen;
1083
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001084 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1085 goto exit;
1086 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1087 goto exit;
1088 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1089 goto exit;
1090 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1091 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001092
1093 for( i = 0; i < use_len; ++i )
1094 *p++ ^= mask[i];
1095
1096 counter[3]++;
1097
1098 dlen -= use_len;
1099 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001100
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001101exit:
Gilles Peskine18ac7162017-05-05 19:24:06 +02001102 mbedtls_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001103
1104 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001109/*
1110 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001113 int (*f_rng)(void *, unsigned char *, size_t),
1114 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001115 int mode,
1116 const unsigned char *label, size_t label_len,
1117 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001118 const unsigned char *input,
1119 unsigned char *output )
1120{
1121 size_t olen;
1122 int ret;
1123 unsigned char *p = output;
1124 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 const mbedtls_md_info_t *md_info;
1126 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1129 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001130
1131 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001135 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001137
1138 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001140
Simon Butcher02037452016-03-01 21:19:12 +00001141 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001142 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001144
1145 memset( output, 0, olen );
1146
1147 *p++ = 0;
1148
Simon Butcher02037452016-03-01 21:19:12 +00001149 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001150 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001152
1153 p += hlen;
1154
Simon Butcher02037452016-03-01 21:19:12 +00001155 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001156 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1157 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 p += hlen;
1159 p += olen - 2 * hlen - 2 - ilen;
1160 *p++ = 1;
1161 memcpy( p, input, ilen );
1162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001164 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001165 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001166
Simon Butcher02037452016-03-01 21:19:12 +00001167 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001168 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1169 &md_ctx ) ) != 0 )
1170 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001171
Simon Butcher02037452016-03-01 21:19:12 +00001172 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001173 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1174 &md_ctx ) ) != 0 )
1175 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001176
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001177exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001179
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001180 if( ret != 0 )
1181 return( ret );
1182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( ( mode == MBEDTLS_RSA_PUBLIC )
1184 ? mbedtls_rsa_public( ctx, output, output )
1185 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001186}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001190/*
1191 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001194 int (*f_rng)(void *, unsigned char *, size_t),
1195 void *p_rng,
1196 int mode, size_t ilen,
1197 const unsigned char *input,
1198 unsigned char *output )
1199{
1200 size_t nb_pad, olen;
1201 int ret;
1202 unsigned char *p = output;
1203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001206
Janos Follath1ed9f992016-03-18 11:45:44 +00001207 // We don't check p_rng because it won't be dereferenced here
1208 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001210
1211 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001212
Simon Butcher02037452016-03-01 21:19:12 +00001213 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001214 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001216
1217 nb_pad = olen - 3 - ilen;
1218
1219 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001223
1224 while( nb_pad-- > 0 )
1225 {
1226 int rng_dl = 100;
1227
1228 do {
1229 ret = f_rng( p_rng, p, 1 );
1230 } while( *p == 0 && --rng_dl && ret == 0 );
1231
Simon Butcher02037452016-03-01 21:19:12 +00001232 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001233 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001235
1236 p++;
1237 }
1238 }
1239 else
1240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001242
1243 while( nb_pad-- > 0 )
1244 *p++ = 0xFF;
1245 }
1246
1247 *p++ = 0;
1248 memcpy( p, input, ilen );
1249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 return( ( mode == MBEDTLS_RSA_PUBLIC )
1251 ? mbedtls_rsa_public( ctx, output, output )
1252 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001255
Paul Bakker5121ce52009-01-03 21:22:43 +00001256/*
1257 * Add the message padding, then do an RSA operation
1258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001260 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001261 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001262 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001263 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 unsigned char *output )
1265{
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 switch( ctx->padding )
1267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268#if defined(MBEDTLS_PKCS1_V15)
1269 case MBEDTLS_RSA_PKCS_V15:
1270 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001271 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#if defined(MBEDTLS_PKCS1_V21)
1275 case MBEDTLS_RSA_PKCS_V21:
1276 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001277 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
1280 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001283}
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001286/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001287 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001290 int (*f_rng)(void *, unsigned char *, size_t),
1291 void *p_rng,
1292 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001293 const unsigned char *label, size_t label_len,
1294 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001295 const unsigned char *input,
1296 unsigned char *output,
1297 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001298{
Paul Bakker23986e52011-04-24 08:57:21 +00001299 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001300 size_t ilen, i, pad_len;
1301 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1303 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001304 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 const mbedtls_md_info_t *md_info;
1306 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001307
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001308 /*
1309 * Parameters sanity checks
1310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1312 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313
1314 ilen = ctx->len;
1315
Paul Bakker27fdf462011-06-09 13:55:13 +00001316 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001320 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001322
Janos Follathc17cda12016-02-11 11:08:18 +00001323 hlen = mbedtls_md_get_size( md_info );
1324
1325 // checking for integer underflow
1326 if( 2 * hlen + 2 > ilen )
1327 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1328
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001329 /*
1330 * RSA operation
1331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1333 ? mbedtls_rsa_public( ctx, input, buf )
1334 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
1336 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001337 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001339 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001340 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001343 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1344 {
1345 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001346 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001347 }
1348
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001349 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001350 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1351 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001352 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001353 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1354 &md_ctx ) ) != 0 )
1355 {
1356 mbedtls_md_free( &md_ctx );
1357 goto cleanup;
1358 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001361
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001362 /* Generate lHash */
1363 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1364 goto cleanup;
1365
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001366 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001367 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001368 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001370 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001372 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001373
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001374 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001375
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001376 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001377 for( i = 0; i < hlen; i++ )
1378 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001379
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001380 /* Get zero-padding len, but always read till end of buffer
1381 * (minus one, for the 01 byte) */
1382 pad_len = 0;
1383 pad_done = 0;
1384 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1385 {
1386 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001387 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001388 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001389
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001390 p += pad_len;
1391 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001392
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001393 /*
1394 * The only information "leaked" is whether the padding was correct or not
1395 * (eg, no data is copied if it was not correct). This meets the
1396 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1397 * the different error conditions.
1398 */
1399 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001400 {
1401 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1402 goto cleanup;
1403 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001404
Paul Bakker66d5d072014-06-17 16:39:18 +02001405 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001406 {
1407 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1408 goto cleanup;
1409 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001410
1411 *olen = ilen - (p - buf);
1412 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001413 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001414
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001415cleanup:
1416 mbedtls_zeroize( buf, sizeof( buf ) );
1417 mbedtls_zeroize( lhash, sizeof( lhash ) );
1418
1419 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001424/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1425 *
1426 * \param value The value to analyze.
Gilles Peskineb4739162018-10-04 18:32:29 +02001427 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001428 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001429static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001430{
1431 /* MSVC has a warning about unary minus on unsigned, but this is
1432 * well-defined and precisely what we want to do here */
1433#if defined(_MSC_VER)
1434#pragma warning( push )
1435#pragma warning( disable : 4146 )
1436#endif
1437 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1438#if defined(_MSC_VER)
1439#pragma warning( pop )
1440#endif
1441}
1442
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001443/** Check whether a size is out of bounds, without branches.
1444 *
1445 * This is equivalent to `size > max`, but is likely to be compiled to
1446 * to code using bitwise operation rather than a branch.
1447 *
1448 * \param size Size to check.
1449 * \param max Maximum desired value for \p size.
1450 * \return \c 0 if `size <= max`.
1451 * \return \c 1 if `size > max`.
1452 */
1453static unsigned size_greater_than( size_t size, size_t max )
1454{
1455 /* Return the sign bit (1 for negative) of (max - size). */
1456 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1457}
1458
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001459/** Choose between two integer values, without branches.
1460 *
Gilles Peskineb4739162018-10-04 18:32:29 +02001461 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1462 * to code using bitwise operation rather than a branch.
1463 *
1464 * \param cond Condition to test.
1465 * \param if1 Value to use if \p cond is nonzero.
1466 * \param if0 Value to use if \p cond is zero.
1467 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001468 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001469static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001470{
Gilles Peskineb4739162018-10-04 18:32:29 +02001471 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001472 return( ( mask & if1 ) | (~mask & if0 ) );
1473}
1474
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001475/** Shift some data towards the left inside a buffer without leaking
1476 * the length of the data through side channels.
1477 *
1478 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1479 * ```
1480 * memmove(start, start + offset, total - offset);
1481 * memset(start + offset, 0, total - offset);
1482 * ```
1483 * but it strives to use a memory access pattern (and thus total timing)
1484 * that does not depend on \p offset. This timing independence comes at
1485 * the expense of performance.
1486 *
1487 * \param start Pointer to the start of the buffer.
1488 * \param total Total size of the buffer.
1489 * \param offset Offset from which to copy \p total - \p offset bytes.
1490 */
1491static void mem_move_to_left( void *start,
1492 size_t total,
1493 size_t offset )
1494{
1495 volatile unsigned char *buf = start;
1496 size_t i, n;
1497 if( total == 0 )
1498 return;
1499 for( i = 0; i < total; i++ )
1500 {
1501 unsigned no_op = size_greater_than( total - offset, i );
1502 /* The first `total - offset` passes are a no-op. The last
1503 * `offset` passes shift the data one byte to the left and
1504 * zero out the last byte. */
1505 for( n = 0; n < total - 1; n++ )
Gilles Peskine66a28e92018-10-12 19:15:34 +02001506 {
1507 unsigned char current = buf[n];
1508 unsigned char next = buf[n+1];
1509 buf[n] = if_int( no_op, current, next );
1510 }
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001511 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1512 }
1513}
1514
Paul Bakkerb3869132013-02-28 17:21:01 +01001515/*
1516 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001519 int (*f_rng)(void *, unsigned char *, size_t),
1520 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001521 int mode, size_t *olen,
1522 const unsigned char *input,
1523 unsigned char *output,
Gilles Peskinecd500f32018-10-02 22:43:06 +02001524 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001525{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001526 int ret;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001527 size_t ilen = ctx->len;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001528 size_t i;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001529 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1530 ilen - 11 :
1531 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001533 /* The following variables take sensitive values: their value must
1534 * not leak into the observable behavior of the function other than
1535 * the designated outputs (output, olen, return value). Otherwise
1536 * this would open the execution of the function to
1537 * side-channel-based variants of the Bleichenbacher padding oracle
1538 * attack. Potential side channels include overall timing, memory
1539 * access patterns (especially visible to an adversary who has access
1540 * to a shared memory cache), and branches (especially visible to
1541 * an adversary who has access to a shared code cache or to a shared
1542 * branch predictor). */
1543 size_t pad_count = 0;
1544 unsigned bad = 0;
1545 unsigned char pad_done = 0;
1546 size_t plaintext_size = 0;
1547 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1550 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001551
Paul Bakkerb3869132013-02-28 17:21:01 +01001552 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1556 ? mbedtls_rsa_public( ctx, input, buf )
1557 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001558
1559 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001560 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001561
Gilles Peskine0b330f72018-10-05 15:06:12 +02001562 /* Check and get padding length in constant time and constant
1563 * memory trace. The first byte must be 0. */
1564 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001568 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1569 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001570 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001572 /* Read the whole buffer. Set pad_done to nonzero if we find
1573 * the 0x00 byte and remember the padding length in pad_count. */
1574 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001575 {
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001576 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001577 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001578 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001579 }
1580 else
1581 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001582 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1583 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001584 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001585
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001586 /* Read the whole buffer. Set pad_done to nonzero if we find
1587 * the 0x00 byte and remember the padding length in pad_count.
1588 * If there's a non-0xff byte in the padding, the padding is bad. */
1589 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001590 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001591 pad_done |= if_int( buf[i], 0, 1 );
1592 pad_count += if_int( pad_done, 0, 1 );
1593 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001594 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 }
1596
Gilles Peskine0b330f72018-10-05 15:06:12 +02001597 /* If pad_done is still zero, there's no data, only unfinished padding. */
1598 bad |= if_int( pad_done, 0, 1 );
1599
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001600 /* There must be at least 8 bytes of padding. */
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001601 bad |= size_greater_than( 8, pad_count );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001602
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001603 /* If the padding is valid, set plaintext_size to the number of
1604 * remaining bytes after stripping the padding. If the padding
1605 * is invalid, avoid leaking this fact through the size of the
1606 * output: use the maximum message size that fits in the output
1607 * buffer. Do it without branches to avoid leaking the padding
1608 * validity through timing. RSA keys are small enough that all the
1609 * size_t values involved fit in unsigned int. */
Gilles Peskineb4739162018-10-04 18:32:29 +02001610 plaintext_size = if_int( bad,
1611 (unsigned) plaintext_max_size,
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001612 (unsigned) ( ilen - pad_count - 3 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001613
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001614 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001615 * buffer and to 1 otherwise. */
1616 output_too_large = size_greater_than( plaintext_size,
1617 plaintext_max_size );
Paul Bakker060c5682009-01-12 21:48:39 +00001618
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001619 /* Set ret without branches to avoid timing attacks. Return:
1620 * - INVALID_PADDING if the padding is bad (bad != 0).
1621 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1622 * plaintext does not fit in the output buffer.
1623 * - 0 if the padding is correct. */
Gilles Peskine84a21d52018-10-12 19:19:12 +02001624 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1625 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1626 0 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001627
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001628 /* If the padding is bad or the plaintext is too large, zero the
1629 * data that we're about to copy to the output buffer.
1630 * We need to copy the same amount of data
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001631 * from the same buffer whether the padding is good or not to
1632 * avoid leaking the padding validity through overall timing or
1633 * through memory or cache access patterns. */
Gilles Peskine0b330f72018-10-05 15:06:12 +02001634 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001635 for( i = 11; i < ilen; i++ )
Gilles Peskine0b330f72018-10-05 15:06:12 +02001636 buf[i] &= ~bad;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001637
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001638 /* If the plaintext is too large, truncate it to the buffer size.
1639 * Copy anyway to avoid revealing the length through timing, because
1640 * revealing the length is as bad as revealing the padding validity
1641 * for a Bleichenbacher attack. */
1642 plaintext_size = if_int( output_too_large,
1643 (unsigned) plaintext_max_size,
1644 (unsigned) plaintext_size );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001645
Gilles Peskine087544b2018-10-04 22:45:13 +02001646 /* Move the plaintext to the leftmost position where it can start in
1647 * the working buffer, i.e. make it start plaintext_max_size from
1648 * the end of the buffer. Do this with a memory access trace that
1649 * does not depend on the plaintext size. After this move, the
1650 * starting location of the plaintext is no longer sensitive
1651 * information. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001652 mem_move_to_left( buf + ilen - plaintext_max_size,
1653 plaintext_max_size,
Gilles Peskine087544b2018-10-04 22:45:13 +02001654 plaintext_max_size - plaintext_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001655
Gilles Peskine087544b2018-10-04 22:45:13 +02001656 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001657 * into the output buffer. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001658 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001659
1660 /* Report the amount of data we copied to the output buffer. In case
1661 * of errors (bad padding or output too large), the value of *olen
1662 * when this function returns is not specified. Making it equivalent
1663 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001664 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001665
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001666cleanup:
1667 mbedtls_zeroize( buf, sizeof( buf ) );
1668
1669 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
1673/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001674 * Do an RSA operation, then remove the message padding
1675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001677 int (*f_rng)(void *, unsigned char *, size_t),
1678 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001679 int mode, size_t *olen,
1680 const unsigned char *input,
1681 unsigned char *output,
1682 size_t output_max_len)
1683{
1684 switch( ctx->padding )
1685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#if defined(MBEDTLS_PKCS1_V15)
1687 case MBEDTLS_RSA_PKCS_V15:
1688 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001689 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001690#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_PKCS1_V21)
1693 case MBEDTLS_RSA_PKCS_V21:
1694 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001695 olen, input, output,
1696 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001697#endif
1698
1699 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001701 }
1702}
1703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001705/*
1706 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1707 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001709 int (*f_rng)(void *, unsigned char *, size_t),
1710 void *p_rng,
1711 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001713 unsigned int hashlen,
1714 const unsigned char *hash,
1715 unsigned char *sig )
1716{
1717 size_t olen;
1718 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001720 unsigned int slen, hlen, offset = 0;
1721 int ret;
1722 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 const mbedtls_md_info_t *md_info;
1724 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1727 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001728
1729 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001731
1732 olen = ctx->len;
1733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001735 {
Simon Butcher02037452016-03-01 21:19:12 +00001736 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001738 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001742 }
1743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001745 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001749 slen = hlen;
1750
1751 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001753
1754 memset( sig, 0, olen );
1755
Simon Butcher02037452016-03-01 21:19:12 +00001756 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001757 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001759
Simon Butcher02037452016-03-01 21:19:12 +00001760 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001761 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001762 p += olen - hlen * 2 - 2;
1763 *p++ = 0x01;
1764 memcpy( p, salt, slen );
1765 p += slen;
1766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001768 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001769 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001770
Simon Butcher02037452016-03-01 21:19:12 +00001771 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001772 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1773 goto exit;
1774 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1775 goto exit;
1776 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1777 goto exit;
1778 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1779 goto exit;
1780 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1781 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001782
Simon Butcher02037452016-03-01 21:19:12 +00001783 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001784 if( msb % 8 == 0 )
1785 offset = 1;
1786
Simon Butcher02037452016-03-01 21:19:12 +00001787 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001788 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1789 &md_ctx ) ) != 0 )
1790 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001791
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001792 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001793 sig[0] &= 0xFF >> ( olen * 8 - msb );
1794
1795 p += hlen;
1796 *p++ = 0xBC;
1797
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001798 mbedtls_zeroize( salt, sizeof( salt ) );
1799
1800exit:
1801 mbedtls_md_free( &md_ctx );
1802
1803 if( ret != 0 )
1804 return( ret );
1805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 return( ( mode == MBEDTLS_RSA_PUBLIC )
1807 ? mbedtls_rsa_public( ctx, sig, sig )
1808 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001809}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001813/*
1814 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1815 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001816
1817/* Construct a PKCS v1.5 encoding of a hashed message
1818 *
1819 * This is used both for signature generation and verification.
1820 *
1821 * Parameters:
1822 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001823 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001824 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001825 * - hash: Buffer containing the hashed message or the raw data.
1826 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001827 * - dst: Buffer to hold the encoded message.
1828 *
1829 * Assumptions:
1830 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1831 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001832 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001833 *
1834 */
1835static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1836 unsigned int hashlen,
1837 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001838 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001839 unsigned char *dst )
1840{
1841 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001842 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001843 unsigned char *p = dst;
1844 const char *oid = NULL;
1845
1846 /* Are we signing hashed or raw data? */
1847 if( md_alg != MBEDTLS_MD_NONE )
1848 {
1849 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1850 if( md_info == NULL )
1851 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1852
1853 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1854 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1855
1856 hashlen = mbedtls_md_get_size( md_info );
1857
1858 /* Double-check that 8 + hashlen + oid_size can be used as a
1859 * 1-byte ASN.1 length encoding and that there's no overflow. */
1860 if( 8 + hashlen + oid_size >= 0x80 ||
1861 10 + hashlen < hashlen ||
1862 10 + hashlen + oid_size < 10 + hashlen )
1863 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1864
1865 /*
1866 * Static bounds check:
1867 * - Need 10 bytes for five tag-length pairs.
1868 * (Insist on 1-byte length encodings to protect against variants of
1869 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1870 * - Need hashlen bytes for hash
1871 * - Need oid_size bytes for hash alg OID.
1872 */
1873 if( nb_pad < 10 + hashlen + oid_size )
1874 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1875 nb_pad -= 10 + hashlen + oid_size;
1876 }
1877 else
1878 {
1879 if( nb_pad < hashlen )
1880 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1881
1882 nb_pad -= hashlen;
1883 }
1884
Hanno Becker2b2f8982017-09-27 17:10:03 +01001885 /* Need space for signature header and padding delimiter (3 bytes),
1886 * and 8 bytes for the minimal padding */
1887 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001888 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1889 nb_pad -= 3;
1890
1891 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001892 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001893
1894 /* Write signature header and padding */
1895 *p++ = 0;
1896 *p++ = MBEDTLS_RSA_SIGN;
1897 memset( p, 0xFF, nb_pad );
1898 p += nb_pad;
1899 *p++ = 0;
1900
1901 /* Are we signing raw data? */
1902 if( md_alg == MBEDTLS_MD_NONE )
1903 {
1904 memcpy( p, hash, hashlen );
1905 return( 0 );
1906 }
1907
1908 /* Signing hashed data, add corresponding ASN.1 structure
1909 *
1910 * DigestInfo ::= SEQUENCE {
1911 * digestAlgorithm DigestAlgorithmIdentifier,
1912 * digest Digest }
1913 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1914 * Digest ::= OCTET STRING
1915 *
1916 * Schematic:
1917 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1918 * TAG-NULL + LEN [ NULL ] ]
1919 * TAG-OCTET + LEN [ HASH ] ]
1920 */
1921 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001922 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001923 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001924 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001925 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001926 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001927 memcpy( p, oid, oid_size );
1928 p += oid_size;
1929 *p++ = MBEDTLS_ASN1_NULL;
1930 *p++ = 0x00;
1931 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001932 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001933 memcpy( p, hash, hashlen );
1934 p += hashlen;
1935
1936 /* Just a sanity-check, should be automatic
1937 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001938 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001939 {
Hanno Beckere58d38c2017-09-27 17:09:00 +01001940 mbedtls_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001941 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1942 }
1943
1944 return( 0 );
1945}
1946
Paul Bakkerb3869132013-02-28 17:21:01 +01001947/*
1948 * Do an RSA operation to sign the message digest
1949 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001951 int (*f_rng)(void *, unsigned char *, size_t),
1952 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001953 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001955 unsigned int hashlen,
1956 const unsigned char *hash,
1957 unsigned char *sig )
1958{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001959 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001960 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1963 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001964
Hanno Beckerfdf38032017-09-06 12:35:55 +01001965 /*
1966 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1967 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001968
Hanno Beckerfdf38032017-09-06 12:35:55 +01001969 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1970 ctx->len, sig ) ) != 0 )
1971 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001972
1973 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001974 * Call respective RSA primitive
1975 */
1976
1977 if( mode == MBEDTLS_RSA_PUBLIC )
1978 {
1979 /* Skip verification on a public key operation */
1980 return( mbedtls_rsa_public( ctx, sig, sig ) );
1981 }
1982
1983 /* Private key operation
1984 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001985 * In order to prevent Lenstra's attack, make the signature in a
1986 * temporary buffer and check it before returning it.
1987 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001988
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001989 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001990 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001991 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1992
Hanno Beckerfdf38032017-09-06 12:35:55 +01001993 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001994 if( verif == NULL )
1995 {
1996 mbedtls_free( sig_try );
1997 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1998 }
1999
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002000 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2001 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2002
Hanno Becker171a8f12017-09-06 12:32:16 +01002003 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002004 {
2005 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2006 goto cleanup;
2007 }
2008
2009 memcpy( sig, sig_try, ctx->len );
2010
2011cleanup:
2012 mbedtls_free( sig_try );
2013 mbedtls_free( verif );
2014
2015 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002016}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002018
2019/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 * Do an RSA operation to sign the message digest
2021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002023 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002024 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002027 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002028 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 unsigned char *sig )
2030{
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 switch( ctx->padding )
2032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#if defined(MBEDTLS_PKCS1_V15)
2034 case MBEDTLS_RSA_PKCS_V15:
2035 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002036 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002037#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#if defined(MBEDTLS_PKCS1_V21)
2040 case MBEDTLS_RSA_PKCS_V21:
2041 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002042 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002043#endif
2044
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002048}
2049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002051/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002052 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002055 int (*f_rng)(void *, unsigned char *, size_t),
2056 void *p_rng,
2057 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002059 unsigned int hashlen,
2060 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002062 int expected_salt_len,
2063 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002064{
Paul Bakker23986e52011-04-24 08:57:21 +00002065 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002066 size_t siglen;
2067 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002068 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002070 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002071 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002072 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 const mbedtls_md_info_t *md_info;
2074 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002075 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2078 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002079
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 siglen = ctx->len;
2081
Paul Bakker27fdf462011-06-09 13:55:13 +00002082 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2086 ? mbedtls_rsa_public( ctx, sig, buf )
2087 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002088
2089 if( ret != 0 )
2090 return( ret );
2091
2092 p = buf;
2093
Paul Bakkerb3869132013-02-28 17:21:01 +01002094 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 {
Simon Butcher02037452016-03-01 21:19:12 +00002099 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002101 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002105 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002108 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002112
Paul Bakkerb3869132013-02-28 17:21:01 +01002113 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002114
Simon Butcher02037452016-03-01 21:19:12 +00002115 /*
2116 * Note: EMSA-PSS verification is over the length of N - 1 bits
2117 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002118 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002119
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002120 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2121 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2122
Simon Butcher02037452016-03-01 21:19:12 +00002123 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002124 if( msb % 8 == 0 )
2125 {
2126 p++;
2127 siglen -= 1;
2128 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002129
Gilles Peskine139108a2017-10-18 19:03:42 +02002130 if( siglen < hlen + 2 )
2131 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2132 hash_start = p + siglen - hlen - 1;
2133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002135 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002136 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002137
Jaeden Amero66954e12018-01-25 16:05:54 +00002138 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2139 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002140 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002141
Paul Bakkerb3869132013-02-28 17:21:01 +01002142 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002143
Gilles Peskine6a54b022017-10-17 19:02:13 +02002144 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002145 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002146
Gilles Peskine91048a32017-10-19 17:46:14 +02002147 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002148 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002149 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2150 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002151 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002152
Gilles Peskine6a54b022017-10-17 19:02:13 +02002153 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002156 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002157 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002158 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2159 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002160 }
2161
Simon Butcher02037452016-03-01 21:19:12 +00002162 /*
2163 * Generate H = Hash( M' )
2164 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002165 ret = mbedtls_md_starts( &md_ctx );
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, zeros, 8 );
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, hash, hashlen );
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_update( &md_ctx, p, observed_salt_len );
2175 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002176 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002177 ret = mbedtls_md_finish( &md_ctx, result );
2178 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002179 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002180
Jaeden Amero66954e12018-01-25 16:05:54 +00002181 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002182 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002183 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002184 goto exit;
2185 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002186
2187exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002189
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002190 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002191}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002192
2193/*
2194 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002197 int (*f_rng)(void *, unsigned char *, size_t),
2198 void *p_rng,
2199 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002201 unsigned int hashlen,
2202 const unsigned char *hash,
2203 const unsigned char *sig )
2204{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2206 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002207 : md_alg;
2208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002210 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002212 sig ) );
2213
2214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002218/*
2219 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002222 int (*f_rng)(void *, unsigned char *, size_t),
2223 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002224 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002226 unsigned int hashlen,
2227 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002228 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002229{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002230 int ret = 0;
2231 const size_t sig_len = ctx->len;
2232 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2235 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002236
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002237 /*
2238 * Prepare expected PKCS1 v1.5 encoding of hash.
2239 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002240
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002241 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2242 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2243 {
2244 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2245 goto cleanup;
2246 }
2247
2248 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2249 encoded_expected ) ) != 0 )
2250 goto cleanup;
2251
2252 /*
2253 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2254 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002257 ? mbedtls_rsa_public( ctx, sig, encoded )
2258 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002259 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002260 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002261
Simon Butcher02037452016-03-01 21:19:12 +00002262 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002263 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002264 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002265
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002266 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2267 sig_len ) ) != 0 )
2268 {
2269 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2270 goto cleanup;
2271 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002272
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002273cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002274
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002275 if( encoded != NULL )
2276 {
2277 mbedtls_zeroize( encoded, sig_len );
2278 mbedtls_free( encoded );
2279 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002280
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002281 if( encoded_expected != NULL )
2282 {
2283 mbedtls_zeroize( encoded_expected, sig_len );
2284 mbedtls_free( encoded_expected );
2285 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002286
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002287 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002288}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002290
2291/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002292 * Do an RSA operation and check the message digest
2293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002295 int (*f_rng)(void *, unsigned char *, size_t),
2296 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002297 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002299 unsigned int hashlen,
2300 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002301 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002302{
2303 switch( ctx->padding )
2304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305#if defined(MBEDTLS_PKCS1_V15)
2306 case MBEDTLS_RSA_PKCS_V15:
2307 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002308 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002309#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#if defined(MBEDTLS_PKCS1_V21)
2312 case MBEDTLS_RSA_PKCS_V21:
2313 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002314 hashlen, hash, sig );
2315#endif
2316
2317 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002319 }
2320}
2321
2322/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002323 * Copy the components of an RSA key
2324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002326{
2327 int ret;
2328
2329 dst->ver = src->ver;
2330 dst->len = src->len;
2331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2333 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2336 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2337 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002338
2339#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2341 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2342 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2344 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002345#endif
2346
2347 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2350 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002351
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002352 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002353 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002354
2355cleanup:
2356 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002358
2359 return( ret );
2360}
2361
2362/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 * Free the components of an RSA key
2364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002366{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002368 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2369 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002371
Hanno Becker33c30a02017-08-23 07:00:22 +01002372#if !defined(MBEDTLS_RSA_NO_CRT)
2373 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2374 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2375 mbedtls_mpi_free( &ctx->DP );
2376#endif /* MBEDTLS_RSA_NO_CRT */
2377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378#if defined(MBEDTLS_THREADING_C)
2379 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002380#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002381}
2382
Hanno Beckerab377312017-08-23 16:24:51 +01002383#endif /* !MBEDTLS_RSA_ALT */
2384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002387#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
2389/*
2390 * Example RSA-1024 keypair, for test purposes
2391 */
2392#define KEY_LEN 128
2393
2394#define RSA_N "9292758453063D803DD603D5E777D788" \
2395 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2396 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2397 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2398 "93A89813FBF3C4F8066D2D800F7C38A8" \
2399 "1AE31942917403FF4946B0A83D3D3E05" \
2400 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2401 "5E94BB77B07507233A0BC7BAC8F90F79"
2402
2403#define RSA_E "10001"
2404
2405#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2406 "66CA472BC44D253102F8B4A9D3BFA750" \
2407 "91386C0077937FE33FA3252D28855837" \
2408 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2409 "DF79C5CE07EE72C7F123142198164234" \
2410 "CABB724CF78B8173B9F880FC86322407" \
2411 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2412 "071513A1E85B5DFA031F21ECAE91A34D"
2413
2414#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2415 "2C01CAD19EA484A87EA4377637E75500" \
2416 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2417 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2418
2419#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2420 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2421 "910E4168387E3C30AA1E00C339A79508" \
2422 "8452DD96A9A5EA5D9DCA68DA636032AF"
2423
Paul Bakker5121ce52009-01-03 21:22:43 +00002424#define PT_LEN 24
2425#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2426 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002429static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002430{
gufe44206cb392020-08-03 17:56:50 +02002431#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002432 size_t i;
2433
Paul Bakker545570e2010-07-18 09:00:25 +00002434 if( rng_state != NULL )
2435 rng_state = NULL;
2436
Paul Bakkera3d195c2011-11-27 21:07:34 +00002437 for( i = 0; i < len; ++i )
2438 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002439#else
2440 if( rng_state != NULL )
2441 rng_state = NULL;
2442
2443 arc4random_buf( output, len );
gufe44206cb392020-08-03 17:56:50 +02002444#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002445
Paul Bakkera3d195c2011-11-27 21:07:34 +00002446 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002449
Paul Bakker5121ce52009-01-03 21:22:43 +00002450/*
2451 * Checkup routine
2452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002454{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002455 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002457 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 unsigned char rsa_plaintext[PT_LEN];
2460 unsigned char rsa_decrypted[PT_LEN];
2461 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002463 unsigned char sha1sum[20];
2464#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Hanno Becker3a701162017-08-22 13:52:43 +01002466 mbedtls_mpi K;
2467
2468 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
Hanno Becker3a701162017-08-22 13:52:43 +01002471 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2472 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2473 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2474 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2475 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2476 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2477 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2478 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2479 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2480 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2481
Hanno Becker7f25f852017-10-10 16:56:22 +01002482 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
2484 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2488 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 {
2490 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Hanno Beckera5fa0792018-03-09 10:42:23 +00002493 ret = 1;
2494 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 }
2496
2497 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
2500 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2501
Hanno Becker98838b02017-10-02 13:16:10 +01002502 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2503 PT_LEN, rsa_plaintext,
2504 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 {
2506 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
Hanno Beckera5fa0792018-03-09 10:42:23 +00002509 ret = 1;
2510 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 }
2512
2513 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Hanno Becker98838b02017-10-02 13:16:10 +01002516 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2517 &len, rsa_ciphertext, rsa_decrypted,
2518 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 {
2520 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Hanno Beckera5fa0792018-03-09 10:42:23 +00002523 ret = 1;
2524 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 }
2526
2527 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2528 {
2529 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Hanno Beckera5fa0792018-03-09 10:42:23 +00002532 ret = 1;
2533 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 }
2535
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002536 if( verbose != 0 )
2537 mbedtls_printf( "passed\n" );
2538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002541 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002543 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002544 {
2545 if( verbose != 0 )
2546 mbedtls_printf( "failed\n" );
2547
2548 return( 1 );
2549 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
Hanno Becker98838b02017-10-02 13:16:10 +01002551 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2552 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2553 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 {
2555 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
Hanno Beckera5fa0792018-03-09 10:42:23 +00002558 ret = 1;
2559 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 }
2561
2562 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002564
Hanno Becker98838b02017-10-02 13:16:10 +01002565 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2566 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2567 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 {
2569 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Hanno Beckera5fa0792018-03-09 10:42:23 +00002572 ret = 1;
2573 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 }
2575
2576 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002577 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002580 if( verbose != 0 )
2581 mbedtls_printf( "\n" );
2582
Paul Bakker3d8fb632014-04-17 12:42:41 +02002583cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002584 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 mbedtls_rsa_free( &rsa );
2586#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002587 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002589 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002590}
2591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594#endif /* MBEDTLS_RSA_C */