blob: 7e853b152cd1e6aa4fe5b684016ecc001e84948e [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 );
Peter Kolbuse6345642020-09-24 11:11:50 -0500782 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnard6ab924d2020-06-26 11:03:19 +0200783 goto cleanup;
784
Peter Kolbuse6345642020-09-24 11:11:50 -0500785 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
786
787 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
788 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 ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200790
Manuel Pégourié-Gonnardb2b1d8e2020-07-16 09:48:54 +0200791 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200792 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 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 +0200794
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200795
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200796cleanup:
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200797 mbedtls_mpi_free( &R );
798
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200799 return( ret );
800}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200801
Paul Bakker5121ce52009-01-03 21:22:43 +0000802/*
Janos Follathe81102e2017-03-22 13:38:28 +0000803 * Exponent blinding supposed to prevent side-channel attacks using multiple
804 * traces of measurements to recover the RSA key. The more collisions are there,
805 * the more bits of the key can be recovered. See [3].
806 *
807 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
808 * observations on avarage.
809 *
810 * For example with 28 byte blinding to achieve 2 collisions the adversary has
811 * to make 2^112 observations on avarage.
812 *
813 * (With the currently (as of 2017 April) known best algorithms breaking 2048
814 * bit RSA requires approximately as much time as trying out 2^112 random keys.
815 * Thus in this sense with 28 byte blinding the security is not reduced by
816 * side-channel attacks like the one in [3])
817 *
818 * This countermeasure does not help if the key recovery is possible with a
819 * single trace.
820 */
821#define RSA_EXPONENT_BLINDING 28
822
823/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000824 * Do an RSA private key operation
825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200827 int (*f_rng)(void *, unsigned char *, size_t),
828 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000829 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000830 unsigned char *output )
831{
Paul Bakker23986e52011-04-24 08:57:21 +0000832 int ret;
833 size_t olen;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000834
835 /* Temporary holding the result */
836 mbedtls_mpi T;
837
838 /* Temporaries holding P-1, Q-1 and the
839 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000840 mbedtls_mpi P1, Q1, R;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000841
842#if !defined(MBEDTLS_RSA_NO_CRT)
843 /* Temporaries holding the results mod p resp. mod q. */
844 mbedtls_mpi TP, TQ;
845
846 /* Temporaries holding the blinded exponents for
847 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000848 mbedtls_mpi DP_blind, DQ_blind;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000849
850 /* Pointers to actual exponents to be used - either the unblinded
851 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000852 mbedtls_mpi *DP = &ctx->DP;
853 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000854#else
855 /* Temporary holding the blinded exponent (if used). */
856 mbedtls_mpi D_blind;
857
858 /* Pointer to actual exponent to be used - either the unblinded
859 * or the blinded one, depending on the presence of a PRNG. */
860 mbedtls_mpi *D = &ctx->D;
861#endif /* MBEDTLS_RSA_NO_CRT */
862
863 /* Temporaries holding the initial input and the double
864 * checked result; should be the same in the end. */
865 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000866
Hanno Beckerebd2c022017-10-12 10:54:53 +0100867 if( rsa_check_context( ctx, 1 /* private key checks */,
868 f_rng != NULL /* blinding y/n */ ) != 0 )
869 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100870 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100871 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100872
Hanno Beckera5fa0792018-03-09 10:42:23 +0000873#if defined(MBEDTLS_THREADING_C)
874 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
875 return( ret );
876#endif
877
878 /* MPI Initialization */
879 mbedtls_mpi_init( &T );
880
881 mbedtls_mpi_init( &P1 );
882 mbedtls_mpi_init( &Q1 );
883 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000884
Janos Follathf9203b42017-03-22 15:13:15 +0000885 if( f_rng != NULL )
886 {
Janos Follathe81102e2017-03-22 13:38:28 +0000887#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000888 mbedtls_mpi_init( &D_blind );
889#else
890 mbedtls_mpi_init( &DP_blind );
891 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000892#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000893 }
Janos Follathe81102e2017-03-22 13:38:28 +0000894
Hanno Beckera5fa0792018-03-09 10:42:23 +0000895#if !defined(MBEDTLS_RSA_NO_CRT)
896 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200897#endif
898
Hanno Beckera5fa0792018-03-09 10:42:23 +0000899 mbedtls_mpi_init( &I );
900 mbedtls_mpi_init( &C );
901
902 /* End of MPI initialization */
903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
905 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000906 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200907 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
908 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 }
910
Hanno Beckera5fa0792018-03-09 10:42:23 +0000911 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
912
Paul Bakkerf451bac2013-08-30 15:37:02 +0200913 if( f_rng != NULL )
914 {
915 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200916 * Blinding
917 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200918 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200919 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
920 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000922
Janos Follathe81102e2017-03-22 13:38:28 +0000923 /*
924 * Exponent blinding
925 */
926 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
927 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
928
Janos Follathf9203b42017-03-22 15:13:15 +0000929#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000930 /*
931 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
932 */
933 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
934 f_rng, p_rng ) );
935 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
936 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
937 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
938
939 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000940#else
941 /*
942 * DP_blind = ( P - 1 ) * R + DP
943 */
944 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
945 f_rng, p_rng ) );
946 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
947 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
948 &ctx->DP ) );
949
950 DP = &DP_blind;
951
952 /*
953 * DQ_blind = ( Q - 1 ) * R + DQ
954 */
955 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
956 f_rng, p_rng ) );
957 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
958 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
959 &ctx->DQ ) );
960
961 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000962#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200963 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000966 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100967#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200968 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000969 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 *
Hanno Beckera5fa0792018-03-09 10:42:23 +0000971 * TP = input ^ dP mod P
972 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000974
975 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
976 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000977
978 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000979 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000981 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
982 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
983 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
985 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000986 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000987 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000988 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
989 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200991
Paul Bakkerf451bac2013-08-30 15:37:02 +0200992 if( f_rng != NULL )
993 {
994 /*
995 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200996 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200997 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200998 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001000 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Hanno Beckera5fa0792018-03-09 10:42:23 +00001002 /* Verify the result to prevent glitching attacks. */
1003 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1004 &ctx->N, &ctx->RN ) );
1005 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
1006 {
1007 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1008 goto cleanup;
1009 }
1010
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
1014cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001016 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1017 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001018#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001019
Hanno Beckera5fa0792018-03-09 10:42:23 +00001020 mbedtls_mpi_free( &P1 );
1021 mbedtls_mpi_free( &Q1 );
1022 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001023
1024 if( f_rng != NULL )
1025 {
Janos Follathe81102e2017-03-22 13:38:28 +00001026#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001027 mbedtls_mpi_free( &D_blind );
1028#else
1029 mbedtls_mpi_free( &DP_blind );
1030 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001031#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001032 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001033
Hanno Beckera5fa0792018-03-09 10:42:23 +00001034 mbedtls_mpi_free( &T );
1035
1036#if !defined(MBEDTLS_RSA_NO_CRT)
1037 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1038#endif
1039
1040 mbedtls_mpi_free( &C );
1041 mbedtls_mpi_free( &I );
1042
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
1046 return( 0 );
1047}
1048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001050/**
1051 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1052 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001053 * \param dst buffer to mask
1054 * \param dlen length of destination buffer
1055 * \param src source of the mask generation
1056 * \param slen length of the source buffer
1057 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001058 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001059static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001063 unsigned char counter[4];
1064 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001065 unsigned int hlen;
1066 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001067 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001070 memset( counter, 0, 4 );
1071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001073
Simon Butcher02037452016-03-01 21:19:12 +00001074 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001075 p = dst;
1076
1077 while( dlen > 0 )
1078 {
1079 use_len = hlen;
1080 if( dlen < hlen )
1081 use_len = dlen;
1082
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001083 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1084 goto exit;
1085 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1086 goto exit;
1087 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1088 goto exit;
1089 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1090 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001091
1092 for( i = 0; i < use_len; ++i )
1093 *p++ ^= mask[i];
1094
1095 counter[3]++;
1096
1097 dlen -= use_len;
1098 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001099
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001100exit:
Gilles Peskine18ac7162017-05-05 19:24:06 +02001101 mbedtls_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001102
1103 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001108/*
1109 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001112 int (*f_rng)(void *, unsigned char *, size_t),
1113 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001114 int mode,
1115 const unsigned char *label, size_t label_len,
1116 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001117 const unsigned char *input,
1118 unsigned char *output )
1119{
1120 size_t olen;
1121 int ret;
1122 unsigned char *p = output;
1123 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 const mbedtls_md_info_t *md_info;
1125 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1128 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001129
1130 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001134 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001136
1137 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001139
Simon Butcher02037452016-03-01 21:19:12 +00001140 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001141 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001143
1144 memset( output, 0, olen );
1145
1146 *p++ = 0;
1147
Simon Butcher02037452016-03-01 21:19:12 +00001148 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001149 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001151
1152 p += hlen;
1153
Simon Butcher02037452016-03-01 21:19:12 +00001154 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001155 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1156 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001157 p += hlen;
1158 p += olen - 2 * hlen - 2 - ilen;
1159 *p++ = 1;
1160 memcpy( p, input, ilen );
1161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001163 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001164 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001165
Simon Butcher02037452016-03-01 21:19:12 +00001166 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001167 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1168 &md_ctx ) ) != 0 )
1169 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001170
Simon Butcher02037452016-03-01 21:19:12 +00001171 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001172 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1173 &md_ctx ) ) != 0 )
1174 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001175
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001176exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001178
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001179 if( ret != 0 )
1180 return( ret );
1181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 return( ( mode == MBEDTLS_RSA_PUBLIC )
1183 ? mbedtls_rsa_public( ctx, output, output )
1184 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001185}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001189/*
1190 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001193 int (*f_rng)(void *, unsigned char *, size_t),
1194 void *p_rng,
1195 int mode, size_t ilen,
1196 const unsigned char *input,
1197 unsigned char *output )
1198{
1199 size_t nb_pad, olen;
1200 int ret;
1201 unsigned char *p = output;
1202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1204 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001205
Janos Follath1ed9f992016-03-18 11:45:44 +00001206 // We don't check p_rng because it won't be dereferenced here
1207 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001209
1210 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001211
Simon Butcher02037452016-03-01 21:19:12 +00001212 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001213 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001215
1216 nb_pad = olen - 3 - ilen;
1217
1218 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001222
1223 while( nb_pad-- > 0 )
1224 {
1225 int rng_dl = 100;
1226
1227 do {
1228 ret = f_rng( p_rng, p, 1 );
1229 } while( *p == 0 && --rng_dl && ret == 0 );
1230
Simon Butcher02037452016-03-01 21:19:12 +00001231 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001232 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001234
1235 p++;
1236 }
1237 }
1238 else
1239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001241
1242 while( nb_pad-- > 0 )
1243 *p++ = 0xFF;
1244 }
1245
1246 *p++ = 0;
1247 memcpy( p, input, ilen );
1248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 return( ( mode == MBEDTLS_RSA_PUBLIC )
1250 ? mbedtls_rsa_public( ctx, output, output )
1251 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001254
Paul Bakker5121ce52009-01-03 21:22:43 +00001255/*
1256 * Add the message padding, then do an RSA operation
1257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001259 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001260 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001261 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001262 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 unsigned char *output )
1264{
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 switch( ctx->padding )
1266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#if defined(MBEDTLS_PKCS1_V15)
1268 case MBEDTLS_RSA_PKCS_V15:
1269 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001270 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001271#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#if defined(MBEDTLS_PKCS1_V21)
1274 case MBEDTLS_RSA_PKCS_V21:
1275 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001276 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001277#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
1279 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001282}
1283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001285/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001286 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001289 int (*f_rng)(void *, unsigned char *, size_t),
1290 void *p_rng,
1291 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001292 const unsigned char *label, size_t label_len,
1293 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001294 const unsigned char *input,
1295 unsigned char *output,
1296 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001297{
Paul Bakker23986e52011-04-24 08:57:21 +00001298 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001299 size_t ilen, i, pad_len;
1300 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1302 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001303 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 const mbedtls_md_info_t *md_info;
1305 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001306
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001307 /*
1308 * Parameters sanity checks
1309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1311 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001312
1313 ilen = ctx->len;
1314
Paul Bakker27fdf462011-06-09 13:55:13 +00001315 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001319 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001321
Janos Follathc17cda12016-02-11 11:08:18 +00001322 hlen = mbedtls_md_get_size( md_info );
1323
1324 // checking for integer underflow
1325 if( 2 * hlen + 2 > ilen )
1326 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1327
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001328 /*
1329 * RSA operation
1330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1332 ? mbedtls_rsa_public( ctx, input, buf )
1333 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
1335 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001336 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001338 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001339 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001342 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1343 {
1344 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001345 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001346 }
1347
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001348 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001349 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1350 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001351 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001352 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1353 &md_ctx ) ) != 0 )
1354 {
1355 mbedtls_md_free( &md_ctx );
1356 goto cleanup;
1357 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001360
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001361 /* Generate lHash */
1362 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1363 goto cleanup;
1364
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001365 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001366 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001367 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001369 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001371 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001372
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001373 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001374
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001375 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001376 for( i = 0; i < hlen; i++ )
1377 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001378
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001379 /* Get zero-padding len, but always read till end of buffer
1380 * (minus one, for the 01 byte) */
1381 pad_len = 0;
1382 pad_done = 0;
1383 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1384 {
1385 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001386 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001387 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001388
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001389 p += pad_len;
1390 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001391
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001392 /*
1393 * The only information "leaked" is whether the padding was correct or not
1394 * (eg, no data is copied if it was not correct). This meets the
1395 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1396 * the different error conditions.
1397 */
1398 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001399 {
1400 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1401 goto cleanup;
1402 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001403
Paul Bakker66d5d072014-06-17 16:39:18 +02001404 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001405 {
1406 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1407 goto cleanup;
1408 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001409
1410 *olen = ilen - (p - buf);
1411 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001412 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001413
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001414cleanup:
1415 mbedtls_zeroize( buf, sizeof( buf ) );
1416 mbedtls_zeroize( lhash, sizeof( lhash ) );
1417
1418 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001419}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001423/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1424 *
1425 * \param value The value to analyze.
Gilles Peskineb4739162018-10-04 18:32:29 +02001426 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001427 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001428static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001429{
1430 /* MSVC has a warning about unary minus on unsigned, but this is
1431 * well-defined and precisely what we want to do here */
1432#if defined(_MSC_VER)
1433#pragma warning( push )
1434#pragma warning( disable : 4146 )
1435#endif
1436 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1437#if defined(_MSC_VER)
1438#pragma warning( pop )
1439#endif
1440}
1441
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001442/** Check whether a size is out of bounds, without branches.
1443 *
1444 * This is equivalent to `size > max`, but is likely to be compiled to
1445 * to code using bitwise operation rather than a branch.
1446 *
1447 * \param size Size to check.
1448 * \param max Maximum desired value for \p size.
1449 * \return \c 0 if `size <= max`.
1450 * \return \c 1 if `size > max`.
1451 */
1452static unsigned size_greater_than( size_t size, size_t max )
1453{
1454 /* Return the sign bit (1 for negative) of (max - size). */
1455 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1456}
1457
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001458/** Choose between two integer values, without branches.
1459 *
Gilles Peskineb4739162018-10-04 18:32:29 +02001460 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1461 * to code using bitwise operation rather than a branch.
1462 *
1463 * \param cond Condition to test.
1464 * \param if1 Value to use if \p cond is nonzero.
1465 * \param if0 Value to use if \p cond is zero.
1466 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001467 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001468static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001469{
Gilles Peskineb4739162018-10-04 18:32:29 +02001470 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001471 return( ( mask & if1 ) | (~mask & if0 ) );
1472}
1473
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001474/** Shift some data towards the left inside a buffer without leaking
1475 * the length of the data through side channels.
1476 *
1477 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1478 * ```
1479 * memmove(start, start + offset, total - offset);
1480 * memset(start + offset, 0, total - offset);
1481 * ```
1482 * but it strives to use a memory access pattern (and thus total timing)
1483 * that does not depend on \p offset. This timing independence comes at
1484 * the expense of performance.
1485 *
1486 * \param start Pointer to the start of the buffer.
1487 * \param total Total size of the buffer.
1488 * \param offset Offset from which to copy \p total - \p offset bytes.
1489 */
1490static void mem_move_to_left( void *start,
1491 size_t total,
1492 size_t offset )
1493{
1494 volatile unsigned char *buf = start;
1495 size_t i, n;
1496 if( total == 0 )
1497 return;
1498 for( i = 0; i < total; i++ )
1499 {
1500 unsigned no_op = size_greater_than( total - offset, i );
1501 /* The first `total - offset` passes are a no-op. The last
1502 * `offset` passes shift the data one byte to the left and
1503 * zero out the last byte. */
1504 for( n = 0; n < total - 1; n++ )
Gilles Peskine66a28e92018-10-12 19:15:34 +02001505 {
1506 unsigned char current = buf[n];
1507 unsigned char next = buf[n+1];
1508 buf[n] = if_int( no_op, current, next );
1509 }
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001510 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1511 }
1512}
1513
Paul Bakkerb3869132013-02-28 17:21:01 +01001514/*
1515 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001518 int (*f_rng)(void *, unsigned char *, size_t),
1519 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001520 int mode, size_t *olen,
1521 const unsigned char *input,
1522 unsigned char *output,
Gilles Peskinecd500f32018-10-02 22:43:06 +02001523 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001524{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001525 int ret;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001526 size_t ilen = ctx->len;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001527 size_t i;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001528 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1529 ilen - 11 :
1530 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001532 /* The following variables take sensitive values: their value must
1533 * not leak into the observable behavior of the function other than
1534 * the designated outputs (output, olen, return value). Otherwise
1535 * this would open the execution of the function to
1536 * side-channel-based variants of the Bleichenbacher padding oracle
1537 * attack. Potential side channels include overall timing, memory
1538 * access patterns (especially visible to an adversary who has access
1539 * to a shared memory cache), and branches (especially visible to
1540 * an adversary who has access to a shared code cache or to a shared
1541 * branch predictor). */
1542 size_t pad_count = 0;
1543 unsigned bad = 0;
1544 unsigned char pad_done = 0;
1545 size_t plaintext_size = 0;
1546 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1549 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001550
Paul Bakkerb3869132013-02-28 17:21:01 +01001551 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1555 ? mbedtls_rsa_public( ctx, input, buf )
1556 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001557
1558 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001559 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001560
Gilles Peskine0b330f72018-10-05 15:06:12 +02001561 /* Check and get padding length in constant time and constant
1562 * memory trace. The first byte must be 0. */
1563 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001567 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1568 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001569 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001570
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001571 /* Read the whole buffer. Set pad_done to nonzero if we find
1572 * the 0x00 byte and remember the padding length in pad_count. */
1573 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001574 {
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001575 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001576 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001577 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001578 }
1579 else
1580 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001581 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1582 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001583 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001584
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001585 /* Read the whole buffer. Set pad_done to nonzero if we find
1586 * the 0x00 byte and remember the padding length in pad_count.
1587 * If there's a non-0xff byte in the padding, the padding is bad. */
1588 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001589 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001590 pad_done |= if_int( buf[i], 0, 1 );
1591 pad_count += if_int( pad_done, 0, 1 );
1592 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001594 }
1595
Gilles Peskine0b330f72018-10-05 15:06:12 +02001596 /* If pad_done is still zero, there's no data, only unfinished padding. */
1597 bad |= if_int( pad_done, 0, 1 );
1598
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001599 /* There must be at least 8 bytes of padding. */
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001600 bad |= size_greater_than( 8, pad_count );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001601
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001602 /* If the padding is valid, set plaintext_size to the number of
1603 * remaining bytes after stripping the padding. If the padding
1604 * is invalid, avoid leaking this fact through the size of the
1605 * output: use the maximum message size that fits in the output
1606 * buffer. Do it without branches to avoid leaking the padding
1607 * validity through timing. RSA keys are small enough that all the
1608 * size_t values involved fit in unsigned int. */
Gilles Peskineb4739162018-10-04 18:32:29 +02001609 plaintext_size = if_int( bad,
1610 (unsigned) plaintext_max_size,
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001611 (unsigned) ( ilen - pad_count - 3 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001612
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001613 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001614 * buffer and to 1 otherwise. */
1615 output_too_large = size_greater_than( plaintext_size,
1616 plaintext_max_size );
Paul Bakker060c5682009-01-12 21:48:39 +00001617
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001618 /* Set ret without branches to avoid timing attacks. Return:
1619 * - INVALID_PADDING if the padding is bad (bad != 0).
1620 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1621 * plaintext does not fit in the output buffer.
1622 * - 0 if the padding is correct. */
Gilles Peskine84a21d52018-10-12 19:19:12 +02001623 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1624 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1625 0 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001626
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001627 /* If the padding is bad or the plaintext is too large, zero the
1628 * data that we're about to copy to the output buffer.
1629 * We need to copy the same amount of data
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001630 * from the same buffer whether the padding is good or not to
1631 * avoid leaking the padding validity through overall timing or
1632 * through memory or cache access patterns. */
Gilles Peskine0b330f72018-10-05 15:06:12 +02001633 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001634 for( i = 11; i < ilen; i++ )
Gilles Peskine0b330f72018-10-05 15:06:12 +02001635 buf[i] &= ~bad;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001636
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001637 /* If the plaintext is too large, truncate it to the buffer size.
1638 * Copy anyway to avoid revealing the length through timing, because
1639 * revealing the length is as bad as revealing the padding validity
1640 * for a Bleichenbacher attack. */
1641 plaintext_size = if_int( output_too_large,
1642 (unsigned) plaintext_max_size,
1643 (unsigned) plaintext_size );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001644
Gilles Peskine087544b2018-10-04 22:45:13 +02001645 /* Move the plaintext to the leftmost position where it can start in
1646 * the working buffer, i.e. make it start plaintext_max_size from
1647 * the end of the buffer. Do this with a memory access trace that
1648 * does not depend on the plaintext size. After this move, the
1649 * starting location of the plaintext is no longer sensitive
1650 * information. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001651 mem_move_to_left( buf + ilen - plaintext_max_size,
1652 plaintext_max_size,
Gilles Peskine087544b2018-10-04 22:45:13 +02001653 plaintext_max_size - plaintext_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001654
Gilles Peskine087544b2018-10-04 22:45:13 +02001655 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001656 * into the output buffer. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001657 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001658
1659 /* Report the amount of data we copied to the output buffer. In case
1660 * of errors (bad padding or output too large), the value of *olen
1661 * when this function returns is not specified. Making it equivalent
1662 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001663 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001665cleanup:
1666 mbedtls_zeroize( buf, sizeof( buf ) );
1667
1668 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
1672/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001673 * Do an RSA operation, then remove the message padding
1674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001676 int (*f_rng)(void *, unsigned char *, size_t),
1677 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001678 int mode, size_t *olen,
1679 const unsigned char *input,
1680 unsigned char *output,
1681 size_t output_max_len)
1682{
1683 switch( ctx->padding )
1684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#if defined(MBEDTLS_PKCS1_V15)
1686 case MBEDTLS_RSA_PKCS_V15:
1687 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001688 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001689#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#if defined(MBEDTLS_PKCS1_V21)
1692 case MBEDTLS_RSA_PKCS_V21:
1693 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001694 olen, input, output,
1695 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001696#endif
1697
1698 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001700 }
1701}
1702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001704/*
1705 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001708 int (*f_rng)(void *, unsigned char *, size_t),
1709 void *p_rng,
1710 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001712 unsigned int hashlen,
1713 const unsigned char *hash,
1714 unsigned char *sig )
1715{
1716 size_t olen;
1717 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001719 unsigned int slen, hlen, offset = 0;
1720 int ret;
1721 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 const mbedtls_md_info_t *md_info;
1723 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1726 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001727
1728 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001730
1731 olen = ctx->len;
1732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001734 {
Simon Butcher02037452016-03-01 21:19:12 +00001735 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001737 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001741 }
1742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001744 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001748 slen = hlen;
1749
1750 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001752
1753 memset( sig, 0, olen );
1754
Simon Butcher02037452016-03-01 21:19:12 +00001755 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001756 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001758
Simon Butcher02037452016-03-01 21:19:12 +00001759 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001760 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001761 p += olen - hlen * 2 - 2;
1762 *p++ = 0x01;
1763 memcpy( p, salt, slen );
1764 p += slen;
1765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001767 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001768 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001769
Simon Butcher02037452016-03-01 21:19:12 +00001770 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001771 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1772 goto exit;
1773 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1774 goto exit;
1775 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1776 goto exit;
1777 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1778 goto exit;
1779 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1780 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001781
Simon Butcher02037452016-03-01 21:19:12 +00001782 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001783 if( msb % 8 == 0 )
1784 offset = 1;
1785
Simon Butcher02037452016-03-01 21:19:12 +00001786 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001787 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1788 &md_ctx ) ) != 0 )
1789 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001790
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001791 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001792 sig[0] &= 0xFF >> ( olen * 8 - msb );
1793
1794 p += hlen;
1795 *p++ = 0xBC;
1796
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001797 mbedtls_zeroize( salt, sizeof( salt ) );
1798
1799exit:
1800 mbedtls_md_free( &md_ctx );
1801
1802 if( ret != 0 )
1803 return( ret );
1804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 return( ( mode == MBEDTLS_RSA_PUBLIC )
1806 ? mbedtls_rsa_public( ctx, sig, sig )
1807 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001812/*
1813 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1814 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001815
1816/* Construct a PKCS v1.5 encoding of a hashed message
1817 *
1818 * This is used both for signature generation and verification.
1819 *
1820 * Parameters:
1821 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001822 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001823 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001824 * - hash: Buffer containing the hashed message or the raw data.
1825 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001826 * - dst: Buffer to hold the encoded message.
1827 *
1828 * Assumptions:
1829 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1830 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001831 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001832 *
1833 */
1834static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1835 unsigned int hashlen,
1836 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001837 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001838 unsigned char *dst )
1839{
1840 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001841 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001842 unsigned char *p = dst;
1843 const char *oid = NULL;
1844
1845 /* Are we signing hashed or raw data? */
1846 if( md_alg != MBEDTLS_MD_NONE )
1847 {
1848 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1849 if( md_info == NULL )
1850 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1851
1852 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1853 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1854
1855 hashlen = mbedtls_md_get_size( md_info );
1856
1857 /* Double-check that 8 + hashlen + oid_size can be used as a
1858 * 1-byte ASN.1 length encoding and that there's no overflow. */
1859 if( 8 + hashlen + oid_size >= 0x80 ||
1860 10 + hashlen < hashlen ||
1861 10 + hashlen + oid_size < 10 + hashlen )
1862 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1863
1864 /*
1865 * Static bounds check:
1866 * - Need 10 bytes for five tag-length pairs.
1867 * (Insist on 1-byte length encodings to protect against variants of
1868 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1869 * - Need hashlen bytes for hash
1870 * - Need oid_size bytes for hash alg OID.
1871 */
1872 if( nb_pad < 10 + hashlen + oid_size )
1873 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1874 nb_pad -= 10 + hashlen + oid_size;
1875 }
1876 else
1877 {
1878 if( nb_pad < hashlen )
1879 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1880
1881 nb_pad -= hashlen;
1882 }
1883
Hanno Becker2b2f8982017-09-27 17:10:03 +01001884 /* Need space for signature header and padding delimiter (3 bytes),
1885 * and 8 bytes for the minimal padding */
1886 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001887 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1888 nb_pad -= 3;
1889
1890 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001891 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001892
1893 /* Write signature header and padding */
1894 *p++ = 0;
1895 *p++ = MBEDTLS_RSA_SIGN;
1896 memset( p, 0xFF, nb_pad );
1897 p += nb_pad;
1898 *p++ = 0;
1899
1900 /* Are we signing raw data? */
1901 if( md_alg == MBEDTLS_MD_NONE )
1902 {
1903 memcpy( p, hash, hashlen );
1904 return( 0 );
1905 }
1906
1907 /* Signing hashed data, add corresponding ASN.1 structure
1908 *
1909 * DigestInfo ::= SEQUENCE {
1910 * digestAlgorithm DigestAlgorithmIdentifier,
1911 * digest Digest }
1912 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1913 * Digest ::= OCTET STRING
1914 *
1915 * Schematic:
1916 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1917 * TAG-NULL + LEN [ NULL ] ]
1918 * TAG-OCTET + LEN [ HASH ] ]
1919 */
1920 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001921 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001922 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001923 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001924 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001925 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001926 memcpy( p, oid, oid_size );
1927 p += oid_size;
1928 *p++ = MBEDTLS_ASN1_NULL;
1929 *p++ = 0x00;
1930 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001931 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001932 memcpy( p, hash, hashlen );
1933 p += hashlen;
1934
1935 /* Just a sanity-check, should be automatic
1936 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001937 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001938 {
Hanno Beckere58d38c2017-09-27 17:09:00 +01001939 mbedtls_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001940 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1941 }
1942
1943 return( 0 );
1944}
1945
Paul Bakkerb3869132013-02-28 17:21:01 +01001946/*
1947 * Do an RSA operation to sign the message digest
1948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001950 int (*f_rng)(void *, unsigned char *, size_t),
1951 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001952 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001954 unsigned int hashlen,
1955 const unsigned char *hash,
1956 unsigned char *sig )
1957{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001958 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001959 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1962 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001963
Hanno Beckerfdf38032017-09-06 12:35:55 +01001964 /*
1965 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1966 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001967
Hanno Beckerfdf38032017-09-06 12:35:55 +01001968 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1969 ctx->len, sig ) ) != 0 )
1970 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001971
1972 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001973 * Call respective RSA primitive
1974 */
1975
1976 if( mode == MBEDTLS_RSA_PUBLIC )
1977 {
1978 /* Skip verification on a public key operation */
1979 return( mbedtls_rsa_public( ctx, sig, sig ) );
1980 }
1981
1982 /* Private key operation
1983 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001984 * In order to prevent Lenstra's attack, make the signature in a
1985 * temporary buffer and check it before returning it.
1986 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001987
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001988 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001989 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001990 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1991
Hanno Beckerfdf38032017-09-06 12:35:55 +01001992 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001993 if( verif == NULL )
1994 {
1995 mbedtls_free( sig_try );
1996 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1997 }
1998
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001999 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2000 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2001
Hanno Becker171a8f12017-09-06 12:32:16 +01002002 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002003 {
2004 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2005 goto cleanup;
2006 }
2007
2008 memcpy( sig, sig_try, ctx->len );
2009
2010cleanup:
2011 mbedtls_free( sig_try );
2012 mbedtls_free( verif );
2013
2014 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002015}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002017
2018/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 * Do an RSA operation to sign the message digest
2020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002022 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002023 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002026 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002027 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 unsigned char *sig )
2029{
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 switch( ctx->padding )
2031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#if defined(MBEDTLS_PKCS1_V15)
2033 case MBEDTLS_RSA_PKCS_V15:
2034 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002035 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002036#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038#if defined(MBEDTLS_PKCS1_V21)
2039 case MBEDTLS_RSA_PKCS_V21:
2040 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002041 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002042#endif
2043
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002047}
2048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002050/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002051 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002054 int (*f_rng)(void *, unsigned char *, size_t),
2055 void *p_rng,
2056 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002058 unsigned int hashlen,
2059 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002061 int expected_salt_len,
2062 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002063{
Paul Bakker23986e52011-04-24 08:57:21 +00002064 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002065 size_t siglen;
2066 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002067 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002069 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002070 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002071 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 const mbedtls_md_info_t *md_info;
2073 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002074 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2077 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002078
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 siglen = ctx->len;
2080
Paul Bakker27fdf462011-06-09 13:55:13 +00002081 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2085 ? mbedtls_rsa_public( ctx, sig, buf )
2086 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
2088 if( ret != 0 )
2089 return( ret );
2090
2091 p = buf;
2092
Paul Bakkerb3869132013-02-28 17:21:01 +01002093 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 {
Simon Butcher02037452016-03-01 21:19:12 +00002098 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002100 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002104 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002107 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002111
Paul Bakkerb3869132013-02-28 17:21:01 +01002112 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002113
Simon Butcher02037452016-03-01 21:19:12 +00002114 /*
2115 * Note: EMSA-PSS verification is over the length of N - 1 bits
2116 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002117 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002118
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002119 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2120 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2121
Simon Butcher02037452016-03-01 21:19:12 +00002122 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002123 if( msb % 8 == 0 )
2124 {
2125 p++;
2126 siglen -= 1;
2127 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002128
Gilles Peskine139108a2017-10-18 19:03:42 +02002129 if( siglen < hlen + 2 )
2130 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2131 hash_start = p + siglen - hlen - 1;
2132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002134 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002135 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002136
Jaeden Amero66954e12018-01-25 16:05:54 +00002137 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2138 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002139 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002140
Paul Bakkerb3869132013-02-28 17:21:01 +01002141 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002142
Gilles Peskine6a54b022017-10-17 19:02:13 +02002143 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002144 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002145
Gilles Peskine91048a32017-10-19 17:46:14 +02002146 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002147 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002148 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2149 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002150 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002151
Gilles Peskine6a54b022017-10-17 19:02:13 +02002152 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002155 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002156 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002157 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2158 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002159 }
2160
Simon Butcher02037452016-03-01 21:19:12 +00002161 /*
2162 * Generate H = Hash( M' )
2163 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002164 ret = mbedtls_md_starts( &md_ctx );
2165 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002166 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002167 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2168 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002169 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002170 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2171 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002172 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002173 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2174 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002175 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002176 ret = mbedtls_md_finish( &md_ctx, result );
2177 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002178 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002179
Jaeden Amero66954e12018-01-25 16:05:54 +00002180 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002181 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002182 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002183 goto exit;
2184 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002185
2186exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002188
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002189 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002190}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002191
2192/*
2193 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002196 int (*f_rng)(void *, unsigned char *, size_t),
2197 void *p_rng,
2198 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002200 unsigned int hashlen,
2201 const unsigned char *hash,
2202 const unsigned char *sig )
2203{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2205 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002206 : md_alg;
2207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002209 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002211 sig ) );
2212
2213}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002217/*
2218 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002221 int (*f_rng)(void *, unsigned char *, size_t),
2222 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002223 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002225 unsigned int hashlen,
2226 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002227 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002228{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002229 int ret = 0;
2230 const size_t sig_len = ctx->len;
2231 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2234 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002235
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002236 /*
2237 * Prepare expected PKCS1 v1.5 encoding of hash.
2238 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002239
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002240 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2241 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2242 {
2243 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2244 goto cleanup;
2245 }
2246
2247 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2248 encoded_expected ) ) != 0 )
2249 goto cleanup;
2250
2251 /*
2252 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2253 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002256 ? mbedtls_rsa_public( ctx, sig, encoded )
2257 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002258 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002259 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002260
Simon Butcher02037452016-03-01 21:19:12 +00002261 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002262 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002263 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002264
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002265 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2266 sig_len ) ) != 0 )
2267 {
2268 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2269 goto cleanup;
2270 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002271
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002272cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002273
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002274 if( encoded != NULL )
2275 {
2276 mbedtls_zeroize( encoded, sig_len );
2277 mbedtls_free( encoded );
2278 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002279
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002280 if( encoded_expected != NULL )
2281 {
2282 mbedtls_zeroize( encoded_expected, sig_len );
2283 mbedtls_free( encoded_expected );
2284 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002285
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002286 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002287}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002289
2290/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002291 * Do an RSA operation and check the message digest
2292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002294 int (*f_rng)(void *, unsigned char *, size_t),
2295 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002296 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002298 unsigned int hashlen,
2299 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002300 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002301{
2302 switch( ctx->padding )
2303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304#if defined(MBEDTLS_PKCS1_V15)
2305 case MBEDTLS_RSA_PKCS_V15:
2306 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002307 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002308#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#if defined(MBEDTLS_PKCS1_V21)
2311 case MBEDTLS_RSA_PKCS_V21:
2312 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002313 hashlen, hash, sig );
2314#endif
2315
2316 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002318 }
2319}
2320
2321/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002322 * Copy the components of an RSA key
2323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002325{
2326 int ret;
2327
2328 dst->ver = src->ver;
2329 dst->len = src->len;
2330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2332 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2335 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2336 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002337
2338#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2340 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2341 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2343 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002344#endif
2345
2346 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2349 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002350
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002351 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002352 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002353
2354cleanup:
2355 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002357
2358 return( ret );
2359}
2360
2361/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002362 * Free the components of an RSA key
2363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002367 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2368 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002370
Hanno Becker33c30a02017-08-23 07:00:22 +01002371#if !defined(MBEDTLS_RSA_NO_CRT)
2372 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2373 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2374 mbedtls_mpi_free( &ctx->DP );
2375#endif /* MBEDTLS_RSA_NO_CRT */
2376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377#if defined(MBEDTLS_THREADING_C)
2378 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002379#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002380}
2381
Hanno Beckerab377312017-08-23 16:24:51 +01002382#endif /* !MBEDTLS_RSA_ALT */
2383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002386#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
2388/*
2389 * Example RSA-1024 keypair, for test purposes
2390 */
2391#define KEY_LEN 128
2392
2393#define RSA_N "9292758453063D803DD603D5E777D788" \
2394 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2395 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2396 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2397 "93A89813FBF3C4F8066D2D800F7C38A8" \
2398 "1AE31942917403FF4946B0A83D3D3E05" \
2399 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2400 "5E94BB77B07507233A0BC7BAC8F90F79"
2401
2402#define RSA_E "10001"
2403
2404#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2405 "66CA472BC44D253102F8B4A9D3BFA750" \
2406 "91386C0077937FE33FA3252D28855837" \
2407 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2408 "DF79C5CE07EE72C7F123142198164234" \
2409 "CABB724CF78B8173B9F880FC86322407" \
2410 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2411 "071513A1E85B5DFA031F21ECAE91A34D"
2412
2413#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2414 "2C01CAD19EA484A87EA4377637E75500" \
2415 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2416 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2417
2418#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2419 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2420 "910E4168387E3C30AA1E00C339A79508" \
2421 "8452DD96A9A5EA5D9DCA68DA636032AF"
2422
Paul Bakker5121ce52009-01-03 21:22:43 +00002423#define PT_LEN 24
2424#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2425 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002428static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002429{
gufe44206cb392020-08-03 17:56:50 +02002430#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002431 size_t i;
2432
Paul Bakker545570e2010-07-18 09:00:25 +00002433 if( rng_state != NULL )
2434 rng_state = NULL;
2435
Paul Bakkera3d195c2011-11-27 21:07:34 +00002436 for( i = 0; i < len; ++i )
2437 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002438#else
2439 if( rng_state != NULL )
2440 rng_state = NULL;
2441
2442 arc4random_buf( output, len );
gufe44206cb392020-08-03 17:56:50 +02002443#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002444
Paul Bakkera3d195c2011-11-27 21:07:34 +00002445 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002448
Paul Bakker5121ce52009-01-03 21:22:43 +00002449/*
2450 * Checkup routine
2451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002453{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002454 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002456 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 unsigned char rsa_plaintext[PT_LEN];
2459 unsigned char rsa_decrypted[PT_LEN];
2460 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002462 unsigned char sha1sum[20];
2463#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
Hanno Becker3a701162017-08-22 13:52:43 +01002465 mbedtls_mpi K;
2466
2467 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
Hanno Becker3a701162017-08-22 13:52:43 +01002470 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2471 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2472 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2473 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2474 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2475 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2476 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2477 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2478 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2479 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2480
Hanno Becker7f25f852017-10-10 16:56:22 +01002481 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
2483 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2487 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 {
2489 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Hanno Beckera5fa0792018-03-09 10:42:23 +00002492 ret = 1;
2493 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 }
2495
2496 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498
2499 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2500
Hanno Becker98838b02017-10-02 13:16:10 +01002501 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2502 PT_LEN, rsa_plaintext,
2503 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 {
2505 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Hanno Beckera5fa0792018-03-09 10:42:23 +00002508 ret = 1;
2509 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 }
2511
2512 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002514
Hanno Becker98838b02017-10-02 13:16:10 +01002515 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2516 &len, rsa_ciphertext, rsa_decrypted,
2517 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 {
2519 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Hanno Beckera5fa0792018-03-09 10:42:23 +00002522 ret = 1;
2523 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 }
2525
2526 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2527 {
2528 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Hanno Beckera5fa0792018-03-09 10:42:23 +00002531 ret = 1;
2532 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 }
2534
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002535 if( verbose != 0 )
2536 mbedtls_printf( "passed\n" );
2537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002540 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002542 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002543 {
2544 if( verbose != 0 )
2545 mbedtls_printf( "failed\n" );
2546
2547 return( 1 );
2548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Hanno Becker98838b02017-10-02 13:16:10 +01002550 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2551 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2552 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 {
2554 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Hanno Beckera5fa0792018-03-09 10:42:23 +00002557 ret = 1;
2558 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 }
2560
2561 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Hanno Becker98838b02017-10-02 13:16:10 +01002564 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2565 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2566 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 {
2568 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Hanno Beckera5fa0792018-03-09 10:42:23 +00002571 ret = 1;
2572 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 }
2574
2575 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002576 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002579 if( verbose != 0 )
2580 mbedtls_printf( "\n" );
2581
Paul Bakker3d8fb632014-04-17 12:42:41 +02002582cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002583 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 mbedtls_rsa_free( &rsa );
2585#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002586 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002588 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589}
2590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#endif /* MBEDTLS_RSA_C */