blob: 94ace5e2daf6ec658d9de308f2c3b7a4eb3aacb7 [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)
Gilles Peskined7e82ad2021-02-01 17:57:41 +0100502 /* Set ctx->ver to nonzero to indicate that the mutex has been
503 * initialized and will need to be freed. */
504 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200506#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000507}
508
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100509/*
510 * Set padding for an existing RSA context
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100513{
514 ctx->padding = padding;
515 ctx->hash_id = hash_id;
516}
517
Hanno Becker617c1ae2017-08-23 14:11:24 +0100518/*
519 * Get length in bytes of RSA modulus
520 */
521
522size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
523{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100524 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100525}
526
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
530/*
531 * Generate an RSA keypair
532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000534 int (*f_rng)(void *, unsigned char *, size_t),
535 void *p_rng,
536 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000537{
538 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100539 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100541 mbedtls_mpi_init( &H );
542 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Gilles Peskine22dc2e72021-02-02 21:06:10 +0100544 if( f_rng == NULL || nbits < 128 || exponent < 3 || nbits % 2 != 0 )
545 {
546 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
547 goto cleanup;
548 }
549
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 /*
551 * find primes P and Q with Q < P so that:
552 * GCD( E, (P-1)*(Q-1) ) == 1
553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556 do
557 {
Janos Follath10c575b2016-02-23 14:42:48 +0000558 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100559 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
Janos Follathef441782016-09-21 13:18:12 +0100561 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100562 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 continue;
566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200568 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 continue;
570
Janos Follathef441782016-09-21 13:18:12 +0100571 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100572 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100573
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100574 /* Temporarily replace P,Q by P-1, Q-1 */
575 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
576 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
577 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100582 /* Restore P,Q */
583 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
584 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
585
586 ctx->len = mbedtls_mpi_size( &ctx->N );
587
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 /*
589 * D = E^-1 mod ((P-1)*(Q-1))
590 * DP = D mod (P - 1)
591 * DQ = D mod (Q - 1)
592 * QP = Q^-1 mod P
593 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100595 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
596
597#if !defined(MBEDTLS_RSA_NO_CRT)
598 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
599 &ctx->DP, &ctx->DQ, &ctx->QP ) );
600#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Hanno Becker83aad1f2017-08-23 06:45:10 +0100602 /* Double-check */
603 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
605cleanup:
606
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100607 mbedtls_mpi_free( &H );
608 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
610 if( ret != 0 )
611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_rsa_free( ctx );
Gilles Peskine22dc2e72021-02-02 21:06:10 +0100613 if( ( -ret & ~0x7f ) == 0 )
614 ret = MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret;
615 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 }
617
Paul Bakker48377d92013-08-30 12:06:24 +0200618 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619}
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
623/*
624 * Check a public RSA key
625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000627{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100628 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000630
Hanno Becker3a760a12018-01-05 08:14:49 +0000631 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100634 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
Hanno Becker705fc682017-10-10 17:57:02 +0100636 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
637 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100641 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
643 return( 0 );
644}
645
646/*
Hanno Becker705fc682017-10-10 17:57:02 +0100647 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000650{
Hanno Becker705fc682017-10-10 17:57:02 +0100651 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100652 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 {
Hanno Becker98838b02017-10-02 13:16:10 +0100654 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 }
Paul Bakker48377d92013-08-30 12:06:24 +0200656
Hanno Becker98838b02017-10-02 13:16:10 +0100657 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100658 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100660 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000662
Hanno Beckerb269a852017-08-25 08:03:21 +0100663#if !defined(MBEDTLS_RSA_NO_CRT)
664 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
665 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
666 {
667 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
668 }
669#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000670
671 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672}
673
674/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100675 * Check if contexts holding a public and private key match
676 */
Hanno Becker98838b02017-10-02 13:16:10 +0100677int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
678 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100679{
Hanno Becker98838b02017-10-02 13:16:10 +0100680 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100684 }
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
687 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100690 }
691
692 return( 0 );
693}
694
695/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 * Do an RSA public key operation
697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000699 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 unsigned char *output )
701{
Paul Bakker23986e52011-04-24 08:57:21 +0000702 int ret;
703 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
Hanno Beckerebd2c022017-10-12 10:54:53 +0100706 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100707 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200711#if defined(MBEDTLS_THREADING_C)
712 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
713 return( ret );
714#endif
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200720 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
721 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 }
723
724 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
726 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000727
728cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200730 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
731 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100732#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000735
736 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000738
739 return( 0 );
740}
741
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200742/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200743 * Generate or update blinding values, see section 10 of:
744 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200745 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200746 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200747 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200748static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200749 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
750{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200751 int ret, count = 0;
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200752 mbedtls_mpi R;
753
754 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200755
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200756 if( ctx->Vf.p != NULL )
757 {
758 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
760 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
761 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
762 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200763
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200764 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200765 }
766
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200767 /* Unblinding value: Vf = random number, invertible mod N */
768 do {
769 if( count++ > 10 )
Manuel Pégourié-Gonnardab601d62020-07-16 09:23:30 +0200770 {
771 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
772 goto cleanup;
773 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 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 +0200776
Manuel Pégourié-Gonnardb2b1d8e2020-07-16 09:48:54 +0200777 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200778 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
779 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
780 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
781
Manuel Pégourié-Gonnardb2b1d8e2020-07-16 09:48:54 +0200782 /* At this point, Vi is invertible mod N if and only if both Vf and R
783 * are invertible mod N. If one of them isn't, we don't need to know
784 * which one, we just loop and choose new values for both of them.
785 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200786 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbuse6345642020-09-24 11:11:50 -0500787 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnard6ab924d2020-06-26 11:03:19 +0200788 goto cleanup;
789
Peter Kolbuse6345642020-09-24 11:11:50 -0500790 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
791
792 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
793 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
794 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200795
Manuel Pégourié-Gonnardb2b1d8e2020-07-16 09:48:54 +0200796 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200797 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 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 +0200799
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200800
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200801cleanup:
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200802 mbedtls_mpi_free( &R );
803
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200804 return( ret );
805}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200806
Paul Bakker5121ce52009-01-03 21:22:43 +0000807/*
Janos Follathe81102e2017-03-22 13:38:28 +0000808 * Exponent blinding supposed to prevent side-channel attacks using multiple
809 * traces of measurements to recover the RSA key. The more collisions are there,
810 * the more bits of the key can be recovered. See [3].
811 *
812 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
813 * observations on avarage.
814 *
815 * For example with 28 byte blinding to achieve 2 collisions the adversary has
816 * to make 2^112 observations on avarage.
817 *
818 * (With the currently (as of 2017 April) known best algorithms breaking 2048
819 * bit RSA requires approximately as much time as trying out 2^112 random keys.
820 * Thus in this sense with 28 byte blinding the security is not reduced by
821 * side-channel attacks like the one in [3])
822 *
823 * This countermeasure does not help if the key recovery is possible with a
824 * single trace.
825 */
826#define RSA_EXPONENT_BLINDING 28
827
828/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 * Do an RSA private key operation
830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200832 int (*f_rng)(void *, unsigned char *, size_t),
833 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000834 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 unsigned char *output )
836{
Paul Bakker23986e52011-04-24 08:57:21 +0000837 int ret;
838 size_t olen;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000839
840 /* Temporary holding the result */
841 mbedtls_mpi T;
842
843 /* Temporaries holding P-1, Q-1 and the
844 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000845 mbedtls_mpi P1, Q1, R;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000846
847#if !defined(MBEDTLS_RSA_NO_CRT)
848 /* Temporaries holding the results mod p resp. mod q. */
849 mbedtls_mpi TP, TQ;
850
851 /* Temporaries holding the blinded exponents for
852 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000853 mbedtls_mpi DP_blind, DQ_blind;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000854
855 /* Pointers to actual exponents to be used - either the unblinded
856 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000857 mbedtls_mpi *DP = &ctx->DP;
858 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000859#else
860 /* Temporary holding the blinded exponent (if used). */
861 mbedtls_mpi D_blind;
862
863 /* Pointer to actual exponent to be used - either the unblinded
864 * or the blinded one, depending on the presence of a PRNG. */
865 mbedtls_mpi *D = &ctx->D;
866#endif /* MBEDTLS_RSA_NO_CRT */
867
868 /* Temporaries holding the initial input and the double
869 * checked result; should be the same in the end. */
870 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
Hanno Beckerebd2c022017-10-12 10:54:53 +0100872 if( rsa_check_context( ctx, 1 /* private key checks */,
873 f_rng != NULL /* blinding y/n */ ) != 0 )
874 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100875 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100876 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100877
Hanno Beckera5fa0792018-03-09 10:42:23 +0000878#if defined(MBEDTLS_THREADING_C)
879 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
880 return( ret );
881#endif
882
883 /* MPI Initialization */
884 mbedtls_mpi_init( &T );
885
886 mbedtls_mpi_init( &P1 );
887 mbedtls_mpi_init( &Q1 );
888 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000889
Janos Follathf9203b42017-03-22 15:13:15 +0000890 if( f_rng != NULL )
891 {
Janos Follathe81102e2017-03-22 13:38:28 +0000892#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000893 mbedtls_mpi_init( &D_blind );
894#else
895 mbedtls_mpi_init( &DP_blind );
896 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000897#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000898 }
Janos Follathe81102e2017-03-22 13:38:28 +0000899
Hanno Beckera5fa0792018-03-09 10:42:23 +0000900#if !defined(MBEDTLS_RSA_NO_CRT)
901 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200902#endif
903
Hanno Beckera5fa0792018-03-09 10:42:23 +0000904 mbedtls_mpi_init( &I );
905 mbedtls_mpi_init( &C );
906
907 /* End of MPI initialization */
908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
910 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000911 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200912 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
913 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 }
915
Hanno Beckera5fa0792018-03-09 10:42:23 +0000916 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
917
Paul Bakkerf451bac2013-08-30 15:37:02 +0200918 if( f_rng != NULL )
919 {
920 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200921 * Blinding
922 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200923 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200924 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
925 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000927
Janos Follathe81102e2017-03-22 13:38:28 +0000928 /*
929 * Exponent blinding
930 */
931 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
932 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
933
Janos Follathf9203b42017-03-22 15:13:15 +0000934#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000935 /*
936 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
937 */
938 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
939 f_rng, p_rng ) );
940 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
941 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
942 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
943
944 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000945#else
946 /*
947 * DP_blind = ( P - 1 ) * R + DP
948 */
949 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
950 f_rng, p_rng ) );
951 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
952 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
953 &ctx->DP ) );
954
955 DP = &DP_blind;
956
957 /*
958 * DQ_blind = ( Q - 1 ) * R + DQ
959 */
960 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
961 f_rng, p_rng ) );
962 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
963 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
964 &ctx->DQ ) );
965
966 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000967#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200968 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000971 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100972#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200973 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000974 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 *
Hanno Beckera5fa0792018-03-09 10:42:23 +0000976 * TP = input ^ dP mod P
977 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000979
980 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
981 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
983 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000984 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000985 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000986 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
987 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
988 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
990 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000991 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000993 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
994 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200996
Paul Bakkerf451bac2013-08-30 15:37:02 +0200997 if( f_rng != NULL )
998 {
999 /*
1000 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001001 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001002 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001003 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
Hanno Beckera5fa0792018-03-09 10:42:23 +00001007 /* Verify the result to prevent glitching attacks. */
1008 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1009 &ctx->N, &ctx->RN ) );
1010 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
1011 {
1012 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1013 goto cleanup;
1014 }
1015
Paul Bakker5121ce52009-01-03 21:22:43 +00001016 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
1019cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001021 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1022 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001023#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001024
Hanno Beckera5fa0792018-03-09 10:42:23 +00001025 mbedtls_mpi_free( &P1 );
1026 mbedtls_mpi_free( &Q1 );
1027 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001028
1029 if( f_rng != NULL )
1030 {
Janos Follathe81102e2017-03-22 13:38:28 +00001031#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001032 mbedtls_mpi_free( &D_blind );
1033#else
1034 mbedtls_mpi_free( &DP_blind );
1035 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001036#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001037 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001038
Hanno Beckera5fa0792018-03-09 10:42:23 +00001039 mbedtls_mpi_free( &T );
1040
1041#if !defined(MBEDTLS_RSA_NO_CRT)
1042 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1043#endif
1044
1045 mbedtls_mpi_free( &C );
1046 mbedtls_mpi_free( &I );
1047
Gilles Peskinefc2c7962020-11-25 00:10:31 +01001048 if( ret != 0 && ret >= -0x007f )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
Gilles Peskinefc2c7962020-11-25 00:10:31 +01001051 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001052}
1053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001055/**
1056 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1057 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001058 * \param dst buffer to mask
1059 * \param dlen length of destination buffer
1060 * \param src source of the mask generation
1061 * \param slen length of the source buffer
1062 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001063 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001064static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001068 unsigned char counter[4];
1069 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001070 unsigned int hlen;
1071 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001072 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001075 memset( counter, 0, 4 );
1076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001078
Simon Butcher02037452016-03-01 21:19:12 +00001079 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001080 p = dst;
1081
1082 while( dlen > 0 )
1083 {
1084 use_len = hlen;
1085 if( dlen < hlen )
1086 use_len = dlen;
1087
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001088 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1089 goto exit;
1090 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1091 goto exit;
1092 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1093 goto exit;
1094 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1095 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001096
1097 for( i = 0; i < use_len; ++i )
1098 *p++ ^= mask[i];
1099
1100 counter[3]++;
1101
1102 dlen -= use_len;
1103 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001104
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001105exit:
Gilles Peskine18ac7162017-05-05 19:24:06 +02001106 mbedtls_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001107
1108 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001113/*
1114 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001117 int (*f_rng)(void *, unsigned char *, size_t),
1118 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001119 int mode,
1120 const unsigned char *label, size_t label_len,
1121 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001122 const unsigned char *input,
1123 unsigned char *output )
1124{
1125 size_t olen;
1126 int ret;
1127 unsigned char *p = output;
1128 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 const mbedtls_md_info_t *md_info;
1130 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1133 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001134
1135 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001139 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001141
1142 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001144
Simon Butcher02037452016-03-01 21:19:12 +00001145 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001146 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001148
1149 memset( output, 0, olen );
1150
1151 *p++ = 0;
1152
Simon Butcher02037452016-03-01 21:19:12 +00001153 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001154 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001156
1157 p += hlen;
1158
Simon Butcher02037452016-03-01 21:19:12 +00001159 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001160 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1161 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001162 p += hlen;
1163 p += olen - 2 * hlen - 2 - ilen;
1164 *p++ = 1;
1165 memcpy( p, input, ilen );
1166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001168 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001169 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001170
Simon Butcher02037452016-03-01 21:19:12 +00001171 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001172 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1173 &md_ctx ) ) != 0 )
1174 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001175
Simon Butcher02037452016-03-01 21:19:12 +00001176 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001177 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1178 &md_ctx ) ) != 0 )
1179 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001180
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001181exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001183
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001184 if( ret != 0 )
1185 return( ret );
1186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 return( ( mode == MBEDTLS_RSA_PUBLIC )
1188 ? mbedtls_rsa_public( ctx, output, output )
1189 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001194/*
1195 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001198 int (*f_rng)(void *, unsigned char *, size_t),
1199 void *p_rng,
1200 int mode, size_t ilen,
1201 const unsigned char *input,
1202 unsigned char *output )
1203{
1204 size_t nb_pad, olen;
1205 int ret;
1206 unsigned char *p = output;
1207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001210
Janos Follath1ed9f992016-03-18 11:45:44 +00001211 // We don't check p_rng because it won't be dereferenced here
1212 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001214
1215 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001216
Simon Butcher02037452016-03-01 21:19:12 +00001217 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001218 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001220
1221 nb_pad = olen - 3 - ilen;
1222
1223 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001227
1228 while( nb_pad-- > 0 )
1229 {
1230 int rng_dl = 100;
1231
1232 do {
1233 ret = f_rng( p_rng, p, 1 );
1234 } while( *p == 0 && --rng_dl && ret == 0 );
1235
Simon Butcher02037452016-03-01 21:19:12 +00001236 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001237 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001239
1240 p++;
1241 }
1242 }
1243 else
1244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001246
1247 while( nb_pad-- > 0 )
1248 *p++ = 0xFF;
1249 }
1250
1251 *p++ = 0;
1252 memcpy( p, input, ilen );
1253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 return( ( mode == MBEDTLS_RSA_PUBLIC )
1255 ? mbedtls_rsa_public( ctx, output, output )
1256 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001257}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001259
Paul Bakker5121ce52009-01-03 21:22:43 +00001260/*
1261 * Add the message padding, then do an RSA operation
1262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001264 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001265 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001266 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001267 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 unsigned char *output )
1269{
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 switch( ctx->padding )
1271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#if defined(MBEDTLS_PKCS1_V15)
1273 case MBEDTLS_RSA_PKCS_V15:
1274 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001275 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001276#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#if defined(MBEDTLS_PKCS1_V21)
1279 case MBEDTLS_RSA_PKCS_V21:
1280 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001281 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001283
1284 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001287}
1288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001290/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001291 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001294 int (*f_rng)(void *, unsigned char *, size_t),
1295 void *p_rng,
1296 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001297 const unsigned char *label, size_t label_len,
1298 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001299 const unsigned char *input,
1300 unsigned char *output,
1301 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001302{
Paul Bakker23986e52011-04-24 08:57:21 +00001303 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001304 size_t ilen, i, pad_len;
1305 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1307 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001308 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 const mbedtls_md_info_t *md_info;
1310 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001311
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001312 /*
1313 * Parameters sanity checks
1314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1316 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
1318 ilen = ctx->len;
1319
Paul Bakker27fdf462011-06-09 13:55:13 +00001320 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001324 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001326
Janos Follathc17cda12016-02-11 11:08:18 +00001327 hlen = mbedtls_md_get_size( md_info );
1328
1329 // checking for integer underflow
1330 if( 2 * hlen + 2 > ilen )
1331 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1332
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001333 /*
1334 * RSA operation
1335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1337 ? mbedtls_rsa_public( ctx, input, buf )
1338 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
1340 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001341 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001343 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001344 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001347 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1348 {
1349 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001350 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001351 }
1352
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001353 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001354 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1355 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001356 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001357 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1358 &md_ctx ) ) != 0 )
1359 {
1360 mbedtls_md_free( &md_ctx );
1361 goto cleanup;
1362 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001365
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001366 /* Generate lHash */
1367 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1368 goto cleanup;
1369
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001370 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001371 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001372 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001374 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001375
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001376 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001377
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001378 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001379
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001380 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001381 for( i = 0; i < hlen; i++ )
1382 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001383
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001384 /* Get zero-padding len, but always read till end of buffer
1385 * (minus one, for the 01 byte) */
1386 pad_len = 0;
1387 pad_done = 0;
1388 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1389 {
1390 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001391 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001392 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001393
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001394 p += pad_len;
1395 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001396
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001397 /*
1398 * The only information "leaked" is whether the padding was correct or not
1399 * (eg, no data is copied if it was not correct). This meets the
1400 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1401 * the different error conditions.
1402 */
1403 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001404 {
1405 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1406 goto cleanup;
1407 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001408
Paul Bakker66d5d072014-06-17 16:39:18 +02001409 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001410 {
1411 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1412 goto cleanup;
1413 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001414
1415 *olen = ilen - (p - buf);
1416 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001417 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001418
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001419cleanup:
1420 mbedtls_zeroize( buf, sizeof( buf ) );
1421 mbedtls_zeroize( lhash, sizeof( lhash ) );
1422
1423 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001424}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001428/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1429 *
1430 * \param value The value to analyze.
Gilles Peskineb4739162018-10-04 18:32:29 +02001431 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001432 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001433static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001434{
1435 /* MSVC has a warning about unary minus on unsigned, but this is
1436 * well-defined and precisely what we want to do here */
1437#if defined(_MSC_VER)
1438#pragma warning( push )
1439#pragma warning( disable : 4146 )
1440#endif
1441 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1442#if defined(_MSC_VER)
1443#pragma warning( pop )
1444#endif
1445}
1446
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001447/** Check whether a size is out of bounds, without branches.
1448 *
1449 * This is equivalent to `size > max`, but is likely to be compiled to
1450 * to code using bitwise operation rather than a branch.
1451 *
1452 * \param size Size to check.
1453 * \param max Maximum desired value for \p size.
1454 * \return \c 0 if `size <= max`.
1455 * \return \c 1 if `size > max`.
1456 */
1457static unsigned size_greater_than( size_t size, size_t max )
1458{
1459 /* Return the sign bit (1 for negative) of (max - size). */
1460 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1461}
1462
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001463/** Choose between two integer values, without branches.
1464 *
Gilles Peskineb4739162018-10-04 18:32:29 +02001465 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1466 * to code using bitwise operation rather than a branch.
1467 *
1468 * \param cond Condition to test.
1469 * \param if1 Value to use if \p cond is nonzero.
1470 * \param if0 Value to use if \p cond is zero.
1471 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001472 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001473static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001474{
Gilles Peskineb4739162018-10-04 18:32:29 +02001475 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001476 return( ( mask & if1 ) | (~mask & if0 ) );
1477}
1478
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001479/** Shift some data towards the left inside a buffer without leaking
1480 * the length of the data through side channels.
1481 *
1482 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1483 * ```
1484 * memmove(start, start + offset, total - offset);
1485 * memset(start + offset, 0, total - offset);
1486 * ```
1487 * but it strives to use a memory access pattern (and thus total timing)
1488 * that does not depend on \p offset. This timing independence comes at
1489 * the expense of performance.
1490 *
1491 * \param start Pointer to the start of the buffer.
1492 * \param total Total size of the buffer.
1493 * \param offset Offset from which to copy \p total - \p offset bytes.
1494 */
1495static void mem_move_to_left( void *start,
1496 size_t total,
1497 size_t offset )
1498{
1499 volatile unsigned char *buf = start;
1500 size_t i, n;
1501 if( total == 0 )
1502 return;
1503 for( i = 0; i < total; i++ )
1504 {
1505 unsigned no_op = size_greater_than( total - offset, i );
1506 /* The first `total - offset` passes are a no-op. The last
1507 * `offset` passes shift the data one byte to the left and
1508 * zero out the last byte. */
1509 for( n = 0; n < total - 1; n++ )
Gilles Peskine66a28e92018-10-12 19:15:34 +02001510 {
1511 unsigned char current = buf[n];
1512 unsigned char next = buf[n+1];
1513 buf[n] = if_int( no_op, current, next );
1514 }
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001515 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1516 }
1517}
1518
Paul Bakkerb3869132013-02-28 17:21:01 +01001519/*
1520 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001523 int (*f_rng)(void *, unsigned char *, size_t),
1524 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001525 int mode, size_t *olen,
1526 const unsigned char *input,
1527 unsigned char *output,
Gilles Peskinecd500f32018-10-02 22:43:06 +02001528 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001529{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001530 int ret;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001531 size_t ilen = ctx->len;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001532 size_t i;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001533 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1534 ilen - 11 :
1535 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001537 /* The following variables take sensitive values: their value must
1538 * not leak into the observable behavior of the function other than
1539 * the designated outputs (output, olen, return value). Otherwise
1540 * this would open the execution of the function to
1541 * side-channel-based variants of the Bleichenbacher padding oracle
1542 * attack. Potential side channels include overall timing, memory
1543 * access patterns (especially visible to an adversary who has access
1544 * to a shared memory cache), and branches (especially visible to
1545 * an adversary who has access to a shared code cache or to a shared
1546 * branch predictor). */
1547 size_t pad_count = 0;
1548 unsigned bad = 0;
1549 unsigned char pad_done = 0;
1550 size_t plaintext_size = 0;
1551 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1554 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001555
Paul Bakkerb3869132013-02-28 17:21:01 +01001556 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1560 ? mbedtls_rsa_public( ctx, input, buf )
1561 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001562
1563 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001564 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001565
Gilles Peskine0b330f72018-10-05 15:06:12 +02001566 /* Check and get padding length in constant time and constant
1567 * memory trace. The first byte must be 0. */
1568 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001571 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001572 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1573 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001574 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001575
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001576 /* Read the whole buffer. Set pad_done to nonzero if we find
1577 * the 0x00 byte and remember the padding length in pad_count. */
1578 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001579 {
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001580 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001581 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001582 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001583 }
1584 else
1585 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001586 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1587 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001588 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001589
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001590 /* Read the whole buffer. Set pad_done to nonzero if we find
1591 * the 0x00 byte and remember the padding length in pad_count.
1592 * If there's a non-0xff byte in the padding, the padding is bad. */
1593 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001594 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001595 pad_done |= if_int( buf[i], 0, 1 );
1596 pad_count += if_int( pad_done, 0, 1 );
1597 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001599 }
1600
Gilles Peskine0b330f72018-10-05 15:06:12 +02001601 /* If pad_done is still zero, there's no data, only unfinished padding. */
1602 bad |= if_int( pad_done, 0, 1 );
1603
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001604 /* There must be at least 8 bytes of padding. */
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001605 bad |= size_greater_than( 8, pad_count );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001606
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001607 /* If the padding is valid, set plaintext_size to the number of
1608 * remaining bytes after stripping the padding. If the padding
1609 * is invalid, avoid leaking this fact through the size of the
1610 * output: use the maximum message size that fits in the output
1611 * buffer. Do it without branches to avoid leaking the padding
1612 * validity through timing. RSA keys are small enough that all the
1613 * size_t values involved fit in unsigned int. */
Gilles Peskineb4739162018-10-04 18:32:29 +02001614 plaintext_size = if_int( bad,
1615 (unsigned) plaintext_max_size,
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001616 (unsigned) ( ilen - pad_count - 3 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001617
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001618 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001619 * buffer and to 1 otherwise. */
1620 output_too_large = size_greater_than( plaintext_size,
1621 plaintext_max_size );
Paul Bakker060c5682009-01-12 21:48:39 +00001622
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001623 /* Set ret without branches to avoid timing attacks. Return:
1624 * - INVALID_PADDING if the padding is bad (bad != 0).
1625 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1626 * plaintext does not fit in the output buffer.
1627 * - 0 if the padding is correct. */
Gilles Peskine84a21d52018-10-12 19:19:12 +02001628 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1629 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1630 0 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001631
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001632 /* If the padding is bad or the plaintext is too large, zero the
1633 * data that we're about to copy to the output buffer.
1634 * We need to copy the same amount of data
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001635 * from the same buffer whether the padding is good or not to
1636 * avoid leaking the padding validity through overall timing or
1637 * through memory or cache access patterns. */
Gilles Peskine0b330f72018-10-05 15:06:12 +02001638 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001639 for( i = 11; i < ilen; i++ )
Gilles Peskine0b330f72018-10-05 15:06:12 +02001640 buf[i] &= ~bad;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001641
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001642 /* If the plaintext is too large, truncate it to the buffer size.
1643 * Copy anyway to avoid revealing the length through timing, because
1644 * revealing the length is as bad as revealing the padding validity
1645 * for a Bleichenbacher attack. */
1646 plaintext_size = if_int( output_too_large,
1647 (unsigned) plaintext_max_size,
1648 (unsigned) plaintext_size );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001649
Gilles Peskine087544b2018-10-04 22:45:13 +02001650 /* Move the plaintext to the leftmost position where it can start in
1651 * the working buffer, i.e. make it start plaintext_max_size from
1652 * the end of the buffer. Do this with a memory access trace that
1653 * does not depend on the plaintext size. After this move, the
1654 * starting location of the plaintext is no longer sensitive
1655 * information. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001656 mem_move_to_left( buf + ilen - plaintext_max_size,
1657 plaintext_max_size,
Gilles Peskine087544b2018-10-04 22:45:13 +02001658 plaintext_max_size - plaintext_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001659
Gilles Peskine087544b2018-10-04 22:45:13 +02001660 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001661 * into the output buffer. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001662 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001663
1664 /* Report the amount of data we copied to the output buffer. In case
1665 * of errors (bad padding or output too large), the value of *olen
1666 * when this function returns is not specified. Making it equivalent
1667 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001668 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001670cleanup:
1671 mbedtls_zeroize( buf, sizeof( buf ) );
1672
1673 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001674}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
1677/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001678 * Do an RSA operation, then remove the message padding
1679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001681 int (*f_rng)(void *, unsigned char *, size_t),
1682 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001683 int mode, size_t *olen,
1684 const unsigned char *input,
1685 unsigned char *output,
1686 size_t output_max_len)
1687{
1688 switch( ctx->padding )
1689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#if defined(MBEDTLS_PKCS1_V15)
1691 case MBEDTLS_RSA_PKCS_V15:
1692 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001693 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001694#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#if defined(MBEDTLS_PKCS1_V21)
1697 case MBEDTLS_RSA_PKCS_V21:
1698 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001699 olen, input, output,
1700 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001701#endif
1702
1703 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001705 }
1706}
1707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001709/*
1710 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001713 int (*f_rng)(void *, unsigned char *, size_t),
1714 void *p_rng,
1715 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001717 unsigned int hashlen,
1718 const unsigned char *hash,
1719 unsigned char *sig )
1720{
1721 size_t olen;
1722 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001724 unsigned int slen, hlen, offset = 0;
1725 int ret;
1726 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 const mbedtls_md_info_t *md_info;
1728 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1731 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001732
1733 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001735
1736 olen = ctx->len;
1737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001739 {
Simon Butcher02037452016-03-01 21:19:12 +00001740 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001742 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001746 }
1747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001749 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001753 slen = hlen;
1754
1755 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001757
1758 memset( sig, 0, olen );
1759
Simon Butcher02037452016-03-01 21:19:12 +00001760 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001761 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001763
Simon Butcher02037452016-03-01 21:19:12 +00001764 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001765 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001766 p += olen - hlen * 2 - 2;
1767 *p++ = 0x01;
1768 memcpy( p, salt, slen );
1769 p += slen;
1770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001772 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001773 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001774
Simon Butcher02037452016-03-01 21:19:12 +00001775 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001776 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1777 goto exit;
1778 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1779 goto exit;
1780 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1781 goto exit;
1782 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1783 goto exit;
1784 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1785 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001786
Simon Butcher02037452016-03-01 21:19:12 +00001787 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001788 if( msb % 8 == 0 )
1789 offset = 1;
1790
Simon Butcher02037452016-03-01 21:19:12 +00001791 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001792 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1793 &md_ctx ) ) != 0 )
1794 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001795
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001796 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001797 sig[0] &= 0xFF >> ( olen * 8 - msb );
1798
1799 p += hlen;
1800 *p++ = 0xBC;
1801
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001802 mbedtls_zeroize( salt, sizeof( salt ) );
1803
1804exit:
1805 mbedtls_md_free( &md_ctx );
1806
1807 if( ret != 0 )
1808 return( ret );
1809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 return( ( mode == MBEDTLS_RSA_PUBLIC )
1811 ? mbedtls_rsa_public( ctx, sig, sig )
1812 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001813}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001817/*
1818 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1819 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001820
1821/* Construct a PKCS v1.5 encoding of a hashed message
1822 *
1823 * This is used both for signature generation and verification.
1824 *
1825 * Parameters:
1826 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001827 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001828 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001829 * - hash: Buffer containing the hashed message or the raw data.
1830 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001831 * - dst: Buffer to hold the encoded message.
1832 *
1833 * Assumptions:
1834 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1835 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001836 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001837 *
1838 */
1839static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1840 unsigned int hashlen,
1841 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001842 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001843 unsigned char *dst )
1844{
1845 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001846 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001847 unsigned char *p = dst;
1848 const char *oid = NULL;
1849
1850 /* Are we signing hashed or raw data? */
1851 if( md_alg != MBEDTLS_MD_NONE )
1852 {
1853 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1854 if( md_info == NULL )
1855 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1856
1857 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1858 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1859
1860 hashlen = mbedtls_md_get_size( md_info );
1861
1862 /* Double-check that 8 + hashlen + oid_size can be used as a
1863 * 1-byte ASN.1 length encoding and that there's no overflow. */
1864 if( 8 + hashlen + oid_size >= 0x80 ||
1865 10 + hashlen < hashlen ||
1866 10 + hashlen + oid_size < 10 + hashlen )
1867 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1868
1869 /*
1870 * Static bounds check:
1871 * - Need 10 bytes for five tag-length pairs.
1872 * (Insist on 1-byte length encodings to protect against variants of
1873 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1874 * - Need hashlen bytes for hash
1875 * - Need oid_size bytes for hash alg OID.
1876 */
1877 if( nb_pad < 10 + hashlen + oid_size )
1878 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1879 nb_pad -= 10 + hashlen + oid_size;
1880 }
1881 else
1882 {
1883 if( nb_pad < hashlen )
1884 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1885
1886 nb_pad -= hashlen;
1887 }
1888
Hanno Becker2b2f8982017-09-27 17:10:03 +01001889 /* Need space for signature header and padding delimiter (3 bytes),
1890 * and 8 bytes for the minimal padding */
1891 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001892 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1893 nb_pad -= 3;
1894
1895 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001896 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001897
1898 /* Write signature header and padding */
1899 *p++ = 0;
1900 *p++ = MBEDTLS_RSA_SIGN;
1901 memset( p, 0xFF, nb_pad );
1902 p += nb_pad;
1903 *p++ = 0;
1904
1905 /* Are we signing raw data? */
1906 if( md_alg == MBEDTLS_MD_NONE )
1907 {
1908 memcpy( p, hash, hashlen );
1909 return( 0 );
1910 }
1911
1912 /* Signing hashed data, add corresponding ASN.1 structure
1913 *
1914 * DigestInfo ::= SEQUENCE {
1915 * digestAlgorithm DigestAlgorithmIdentifier,
1916 * digest Digest }
1917 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1918 * Digest ::= OCTET STRING
1919 *
1920 * Schematic:
1921 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1922 * TAG-NULL + LEN [ NULL ] ]
1923 * TAG-OCTET + LEN [ HASH ] ]
1924 */
1925 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001926 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001927 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001928 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001929 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001930 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001931 memcpy( p, oid, oid_size );
1932 p += oid_size;
1933 *p++ = MBEDTLS_ASN1_NULL;
1934 *p++ = 0x00;
1935 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001936 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001937 memcpy( p, hash, hashlen );
1938 p += hashlen;
1939
1940 /* Just a sanity-check, should be automatic
1941 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001942 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001943 {
Hanno Beckere58d38c2017-09-27 17:09:00 +01001944 mbedtls_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001945 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1946 }
1947
1948 return( 0 );
1949}
1950
Paul Bakkerb3869132013-02-28 17:21:01 +01001951/*
1952 * Do an RSA operation to sign the message digest
1953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001955 int (*f_rng)(void *, unsigned char *, size_t),
1956 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001957 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001959 unsigned int hashlen,
1960 const unsigned char *hash,
1961 unsigned char *sig )
1962{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001963 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001964 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1967 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001968
Hanno Beckerfdf38032017-09-06 12:35:55 +01001969 /*
1970 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1971 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001972
Hanno Beckerfdf38032017-09-06 12:35:55 +01001973 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1974 ctx->len, sig ) ) != 0 )
1975 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001976
1977 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001978 * Call respective RSA primitive
1979 */
1980
1981 if( mode == MBEDTLS_RSA_PUBLIC )
1982 {
1983 /* Skip verification on a public key operation */
1984 return( mbedtls_rsa_public( ctx, sig, sig ) );
1985 }
1986
1987 /* Private key operation
1988 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001989 * In order to prevent Lenstra's attack, make the signature in a
1990 * temporary buffer and check it before returning it.
1991 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001992
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001993 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001994 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001995 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1996
Hanno Beckerfdf38032017-09-06 12:35:55 +01001997 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001998 if( verif == NULL )
1999 {
2000 mbedtls_free( sig_try );
2001 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2002 }
2003
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002004 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2005 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2006
Hanno Becker171a8f12017-09-06 12:32:16 +01002007 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002008 {
2009 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2010 goto cleanup;
2011 }
2012
2013 memcpy( sig, sig_try, ctx->len );
2014
2015cleanup:
2016 mbedtls_free( sig_try );
2017 mbedtls_free( verif );
2018
2019 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002020}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002022
2023/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 * Do an RSA operation to sign the message digest
2025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002027 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002028 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002031 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002032 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002033 unsigned char *sig )
2034{
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 switch( ctx->padding )
2036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#if defined(MBEDTLS_PKCS1_V15)
2038 case MBEDTLS_RSA_PKCS_V15:
2039 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002040 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002041#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#if defined(MBEDTLS_PKCS1_V21)
2044 case MBEDTLS_RSA_PKCS_V21:
2045 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002046 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002047#endif
2048
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002052}
2053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002055/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002056 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002059 int (*f_rng)(void *, unsigned char *, size_t),
2060 void *p_rng,
2061 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002063 unsigned int hashlen,
2064 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002066 int expected_salt_len,
2067 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002068{
Paul Bakker23986e52011-04-24 08:57:21 +00002069 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002070 size_t siglen;
2071 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002072 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002074 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002075 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002076 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 const mbedtls_md_info_t *md_info;
2078 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002079 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2082 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002083
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 siglen = ctx->len;
2085
Paul Bakker27fdf462011-06-09 13:55:13 +00002086 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2090 ? mbedtls_rsa_public( ctx, sig, buf )
2091 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002092
2093 if( ret != 0 )
2094 return( ret );
2095
2096 p = buf;
2097
Paul Bakkerb3869132013-02-28 17:21:01 +01002098 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 {
Simon Butcher02037452016-03-01 21:19:12 +00002103 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002105 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002109 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002112 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002116
Paul Bakkerb3869132013-02-28 17:21:01 +01002117 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002118
Simon Butcher02037452016-03-01 21:19:12 +00002119 /*
2120 * Note: EMSA-PSS verification is over the length of N - 1 bits
2121 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002122 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002123
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002124 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2125 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2126
Simon Butcher02037452016-03-01 21:19:12 +00002127 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002128 if( msb % 8 == 0 )
2129 {
2130 p++;
2131 siglen -= 1;
2132 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002133
Gilles Peskine139108a2017-10-18 19:03:42 +02002134 if( siglen < hlen + 2 )
2135 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2136 hash_start = p + siglen - hlen - 1;
2137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002139 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002140 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002141
Jaeden Amero66954e12018-01-25 16:05:54 +00002142 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2143 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002144 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002145
Paul Bakkerb3869132013-02-28 17:21:01 +01002146 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002147
Gilles Peskine6a54b022017-10-17 19:02:13 +02002148 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002149 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002150
Gilles Peskine91048a32017-10-19 17:46:14 +02002151 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002152 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002153 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2154 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002155 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002156
Gilles Peskine6a54b022017-10-17 19:02:13 +02002157 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002160 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002161 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002162 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2163 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002164 }
2165
Simon Butcher02037452016-03-01 21:19:12 +00002166 /*
2167 * Generate H = Hash( M' )
2168 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002169 ret = mbedtls_md_starts( &md_ctx );
2170 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002171 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002172 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2173 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002174 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002175 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2176 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002177 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002178 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2179 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002180 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002181 ret = mbedtls_md_finish( &md_ctx, result );
2182 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002183 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002184
Jaeden Amero66954e12018-01-25 16:05:54 +00002185 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002186 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002187 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002188 goto exit;
2189 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002190
2191exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002193
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002194 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002195}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002196
2197/*
2198 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002201 int (*f_rng)(void *, unsigned char *, size_t),
2202 void *p_rng,
2203 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002205 unsigned int hashlen,
2206 const unsigned char *hash,
2207 const unsigned char *sig )
2208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2210 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002211 : md_alg;
2212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002214 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002216 sig ) );
2217
2218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002222/*
2223 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002226 int (*f_rng)(void *, unsigned char *, size_t),
2227 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002228 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002230 unsigned int hashlen,
2231 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002232 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002233{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002234 int ret = 0;
2235 const size_t sig_len = ctx->len;
2236 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2239 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002240
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002241 /*
2242 * Prepare expected PKCS1 v1.5 encoding of hash.
2243 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002244
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002245 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2246 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2247 {
2248 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2249 goto cleanup;
2250 }
2251
2252 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2253 encoded_expected ) ) != 0 )
2254 goto cleanup;
2255
2256 /*
2257 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2258 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002261 ? mbedtls_rsa_public( ctx, sig, encoded )
2262 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002263 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002264 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002265
Simon Butcher02037452016-03-01 21:19:12 +00002266 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002267 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002268 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002269
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002270 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2271 sig_len ) ) != 0 )
2272 {
2273 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2274 goto cleanup;
2275 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002276
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002277cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002278
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002279 if( encoded != NULL )
2280 {
2281 mbedtls_zeroize( encoded, sig_len );
2282 mbedtls_free( encoded );
2283 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002284
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002285 if( encoded_expected != NULL )
2286 {
2287 mbedtls_zeroize( encoded_expected, sig_len );
2288 mbedtls_free( encoded_expected );
2289 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002290
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002291 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
2295/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002296 * Do an RSA operation and check the message digest
2297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002299 int (*f_rng)(void *, unsigned char *, size_t),
2300 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002301 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002303 unsigned int hashlen,
2304 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002305 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002306{
2307 switch( ctx->padding )
2308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#if defined(MBEDTLS_PKCS1_V15)
2310 case MBEDTLS_RSA_PKCS_V15:
2311 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002312 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002313#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315#if defined(MBEDTLS_PKCS1_V21)
2316 case MBEDTLS_RSA_PKCS_V21:
2317 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002318 hashlen, hash, sig );
2319#endif
2320
2321 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002323 }
2324}
2325
2326/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002327 * Copy the components of an RSA key
2328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002330{
2331 int ret;
2332
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002333 dst->len = src->len;
2334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2336 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2339 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2340 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002341
2342#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2344 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2345 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2347 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002348#endif
2349
2350 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2353 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002354
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002355 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002356 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002357
2358cleanup:
2359 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002361
2362 return( ret );
2363}
2364
2365/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 * Free the components of an RSA key
2367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002369{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002371 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2372 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002374
Hanno Becker33c30a02017-08-23 07:00:22 +01002375#if !defined(MBEDTLS_RSA_NO_CRT)
2376 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2377 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2378 mbedtls_mpi_free( &ctx->DP );
2379#endif /* MBEDTLS_RSA_NO_CRT */
2380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381#if defined(MBEDTLS_THREADING_C)
Gilles Peskined7e82ad2021-02-01 17:57:41 +01002382 /* Free the mutex, but only if it hasn't been freed already. */
2383 if( ctx->ver != 0 )
2384 {
2385 mbedtls_mutex_free( &ctx->mutex );
2386 ctx->ver = 0;
2387 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002388#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002389}
2390
Hanno Beckerab377312017-08-23 16:24:51 +01002391#endif /* !MBEDTLS_RSA_ALT */
2392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002395#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
2397/*
2398 * Example RSA-1024 keypair, for test purposes
2399 */
2400#define KEY_LEN 128
2401
2402#define RSA_N "9292758453063D803DD603D5E777D788" \
2403 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2404 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2405 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2406 "93A89813FBF3C4F8066D2D800F7C38A8" \
2407 "1AE31942917403FF4946B0A83D3D3E05" \
2408 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2409 "5E94BB77B07507233A0BC7BAC8F90F79"
2410
2411#define RSA_E "10001"
2412
2413#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2414 "66CA472BC44D253102F8B4A9D3BFA750" \
2415 "91386C0077937FE33FA3252D28855837" \
2416 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2417 "DF79C5CE07EE72C7F123142198164234" \
2418 "CABB724CF78B8173B9F880FC86322407" \
2419 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2420 "071513A1E85B5DFA031F21ECAE91A34D"
2421
2422#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2423 "2C01CAD19EA484A87EA4377637E75500" \
2424 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2425 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2426
2427#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2428 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2429 "910E4168387E3C30AA1E00C339A79508" \
2430 "8452DD96A9A5EA5D9DCA68DA636032AF"
2431
Paul Bakker5121ce52009-01-03 21:22:43 +00002432#define PT_LEN 24
2433#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2434 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002437static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002438{
gufe44206cb392020-08-03 17:56:50 +02002439#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002440 size_t i;
2441
Paul Bakker545570e2010-07-18 09:00:25 +00002442 if( rng_state != NULL )
2443 rng_state = NULL;
2444
Paul Bakkera3d195c2011-11-27 21:07:34 +00002445 for( i = 0; i < len; ++i )
2446 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002447#else
2448 if( rng_state != NULL )
2449 rng_state = NULL;
2450
2451 arc4random_buf( output, len );
gufe44206cb392020-08-03 17:56:50 +02002452#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002453
Paul Bakkera3d195c2011-11-27 21:07:34 +00002454 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002455}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002457
Paul Bakker5121ce52009-01-03 21:22:43 +00002458/*
2459 * Checkup routine
2460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002462{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002463 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002465 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 unsigned char rsa_plaintext[PT_LEN];
2468 unsigned char rsa_decrypted[PT_LEN];
2469 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002471 unsigned char sha1sum[20];
2472#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Hanno Becker3a701162017-08-22 13:52:43 +01002474 mbedtls_mpi K;
2475
2476 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
Hanno Becker3a701162017-08-22 13:52:43 +01002479 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2480 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2481 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2482 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2483 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2484 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2485 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2486 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2487 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2488 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2489
Hanno Becker7f25f852017-10-10 16:56:22 +01002490 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
2492 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2496 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 {
2498 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Hanno Beckera5fa0792018-03-09 10:42:23 +00002501 ret = 1;
2502 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 }
2504
2505 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
2508 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2509
Hanno Becker98838b02017-10-02 13:16:10 +01002510 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2511 PT_LEN, rsa_plaintext,
2512 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 {
2514 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
Hanno Beckera5fa0792018-03-09 10:42:23 +00002517 ret = 1;
2518 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 }
2520
2521 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Hanno Becker98838b02017-10-02 13:16:10 +01002524 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2525 &len, rsa_ciphertext, rsa_decrypted,
2526 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 {
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
2535 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2536 {
2537 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Hanno Beckera5fa0792018-03-09 10:42:23 +00002540 ret = 1;
2541 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 }
2543
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002544 if( verbose != 0 )
2545 mbedtls_printf( "passed\n" );
2546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002549 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002551 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002552 {
2553 if( verbose != 0 )
2554 mbedtls_printf( "failed\n" );
2555
2556 return( 1 );
2557 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
Hanno Becker98838b02017-10-02 13:16:10 +01002559 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2560 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2561 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 {
2563 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Hanno Beckera5fa0792018-03-09 10:42:23 +00002566 ret = 1;
2567 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 }
2569
2570 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Hanno Becker98838b02017-10-02 13:16:10 +01002573 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2574 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2575 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 {
2577 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Hanno Beckera5fa0792018-03-09 10:42:23 +00002580 ret = 1;
2581 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 }
2583
2584 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002585 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002588 if( verbose != 0 )
2589 mbedtls_printf( "\n" );
2590
Paul Bakker3d8fb632014-04-17 12:42:41 +02002591cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002592 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 mbedtls_rsa_free( &rsa );
2594#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002595 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002597 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598}
2599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#endif /* MBEDTLS_RSA_C */