blob: 65b75d60f04949c51e1228bd018ee448e9364670 [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
Paul Bakker21eb2802010-08-16 11:10:02 +0000541 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Janos Follathef441782016-09-21 13:18:12 +0100544 if( nbits % 2 )
545 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
546
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100547 mbedtls_mpi_init( &H );
548 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 /*
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 );
613 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 }
615
Paul Bakker48377d92013-08-30 12:06:24 +0200616 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000617}
618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
621/*
622 * Check a public RSA key
623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000625{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100626 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000628
Hanno Becker3a760a12018-01-05 08:14:49 +0000629 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100632 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Hanno Becker705fc682017-10-10 17:57:02 +0100634 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
635 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100639 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
641 return( 0 );
642}
643
644/*
Hanno Becker705fc682017-10-10 17:57:02 +0100645 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000648{
Hanno Becker705fc682017-10-10 17:57:02 +0100649 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100650 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 {
Hanno Becker98838b02017-10-02 13:16:10 +0100652 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 }
Paul Bakker48377d92013-08-30 12:06:24 +0200654
Hanno Becker98838b02017-10-02 13:16:10 +0100655 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100656 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100658 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000660
Hanno Beckerb269a852017-08-25 08:03:21 +0100661#if !defined(MBEDTLS_RSA_NO_CRT)
662 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
663 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
664 {
665 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
666 }
667#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000668
669 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670}
671
672/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100673 * Check if contexts holding a public and private key match
674 */
Hanno Becker98838b02017-10-02 13:16:10 +0100675int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
676 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100677{
Hanno Becker98838b02017-10-02 13:16:10 +0100678 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100682 }
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
685 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100688 }
689
690 return( 0 );
691}
692
693/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 * Do an RSA public key operation
695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000697 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 unsigned char *output )
699{
Paul Bakker23986e52011-04-24 08:57:21 +0000700 int ret;
701 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Hanno Beckerebd2c022017-10-12 10:54:53 +0100704 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100705 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200709#if defined(MBEDTLS_THREADING_C)
710 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
711 return( ret );
712#endif
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200718 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
719 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 }
721
722 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
724 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
726cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200728 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
729 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100730#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
734 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
737 return( 0 );
738}
739
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200740/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200741 * Generate or update blinding values, see section 10 of:
742 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200743 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200744 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200745 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200746static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200747 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
748{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200749 int ret, count = 0;
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200750 mbedtls_mpi R;
751
752 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200753
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200754 if( ctx->Vf.p != NULL )
755 {
756 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
758 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
759 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
760 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200761
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200762 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200763 }
764
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200765 /* Unblinding value: Vf = random number, invertible mod N */
766 do {
767 if( count++ > 10 )
Manuel Pégourié-Gonnardab601d62020-07-16 09:23:30 +0200768 {
769 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
770 goto cleanup;
771 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 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 +0200774
Manuel Pégourié-Gonnardb2b1d8e2020-07-16 09:48:54 +0200775 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200776 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
777 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
778 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
779
Manuel Pégourié-Gonnardb2b1d8e2020-07-16 09:48:54 +0200780 /* At this point, Vi is invertible mod N if and only if both Vf and R
781 * are invertible mod N. If one of them isn't, we don't need to know
782 * which one, we just loop and choose new values for both of them.
783 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200784 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbuse6345642020-09-24 11:11:50 -0500785 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnard6ab924d2020-06-26 11:03:19 +0200786 goto cleanup;
787
Peter Kolbuse6345642020-09-24 11:11:50 -0500788 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
789
790 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
791 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
792 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200793
Manuel Pégourié-Gonnardb2b1d8e2020-07-16 09:48:54 +0200794 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200795 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 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 +0200797
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200798
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200799cleanup:
Manuel Pégourié-Gonnard406c7ae2020-06-26 11:19:12 +0200800 mbedtls_mpi_free( &R );
801
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200802 return( ret );
803}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200804
Paul Bakker5121ce52009-01-03 21:22:43 +0000805/*
Janos Follathe81102e2017-03-22 13:38:28 +0000806 * Exponent blinding supposed to prevent side-channel attacks using multiple
807 * traces of measurements to recover the RSA key. The more collisions are there,
808 * the more bits of the key can be recovered. See [3].
809 *
810 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
811 * observations on avarage.
812 *
813 * For example with 28 byte blinding to achieve 2 collisions the adversary has
814 * to make 2^112 observations on avarage.
815 *
816 * (With the currently (as of 2017 April) known best algorithms breaking 2048
817 * bit RSA requires approximately as much time as trying out 2^112 random keys.
818 * Thus in this sense with 28 byte blinding the security is not reduced by
819 * side-channel attacks like the one in [3])
820 *
821 * This countermeasure does not help if the key recovery is possible with a
822 * single trace.
823 */
824#define RSA_EXPONENT_BLINDING 28
825
826/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000827 * Do an RSA private key operation
828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200830 int (*f_rng)(void *, unsigned char *, size_t),
831 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000832 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 unsigned char *output )
834{
Paul Bakker23986e52011-04-24 08:57:21 +0000835 int ret;
836 size_t olen;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000837
838 /* Temporary holding the result */
839 mbedtls_mpi T;
840
841 /* Temporaries holding P-1, Q-1 and the
842 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000843 mbedtls_mpi P1, Q1, R;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000844
845#if !defined(MBEDTLS_RSA_NO_CRT)
846 /* Temporaries holding the results mod p resp. mod q. */
847 mbedtls_mpi TP, TQ;
848
849 /* Temporaries holding the blinded exponents for
850 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000851 mbedtls_mpi DP_blind, DQ_blind;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000852
853 /* Pointers to actual exponents to be used - either the unblinded
854 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000855 mbedtls_mpi *DP = &ctx->DP;
856 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000857#else
858 /* Temporary holding the blinded exponent (if used). */
859 mbedtls_mpi D_blind;
860
861 /* Pointer to actual exponent to be used - either the unblinded
862 * or the blinded one, depending on the presence of a PRNG. */
863 mbedtls_mpi *D = &ctx->D;
864#endif /* MBEDTLS_RSA_NO_CRT */
865
866 /* Temporaries holding the initial input and the double
867 * checked result; should be the same in the end. */
868 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000869
Hanno Beckerebd2c022017-10-12 10:54:53 +0100870 if( rsa_check_context( ctx, 1 /* private key checks */,
871 f_rng != NULL /* blinding y/n */ ) != 0 )
872 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100873 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100874 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100875
Hanno Beckera5fa0792018-03-09 10:42:23 +0000876#if defined(MBEDTLS_THREADING_C)
877 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
878 return( ret );
879#endif
880
881 /* MPI Initialization */
882 mbedtls_mpi_init( &T );
883
884 mbedtls_mpi_init( &P1 );
885 mbedtls_mpi_init( &Q1 );
886 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000887
Janos Follathf9203b42017-03-22 15:13:15 +0000888 if( f_rng != NULL )
889 {
Janos Follathe81102e2017-03-22 13:38:28 +0000890#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000891 mbedtls_mpi_init( &D_blind );
892#else
893 mbedtls_mpi_init( &DP_blind );
894 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000895#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000896 }
Janos Follathe81102e2017-03-22 13:38:28 +0000897
Hanno Beckera5fa0792018-03-09 10:42:23 +0000898#if !defined(MBEDTLS_RSA_NO_CRT)
899 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200900#endif
901
Hanno Beckera5fa0792018-03-09 10:42:23 +0000902 mbedtls_mpi_init( &I );
903 mbedtls_mpi_init( &C );
904
905 /* End of MPI initialization */
906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
908 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200910 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
911 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000912 }
913
Hanno Beckera5fa0792018-03-09 10:42:23 +0000914 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
915
Paul Bakkerf451bac2013-08-30 15:37:02 +0200916 if( f_rng != NULL )
917 {
918 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200919 * Blinding
920 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200921 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200922 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
923 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000925
Janos Follathe81102e2017-03-22 13:38:28 +0000926 /*
927 * Exponent blinding
928 */
929 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
930 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
931
Janos Follathf9203b42017-03-22 15:13:15 +0000932#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000933 /*
934 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
935 */
936 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
937 f_rng, p_rng ) );
938 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
939 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
940 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
941
942 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000943#else
944 /*
945 * DP_blind = ( P - 1 ) * R + DP
946 */
947 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
948 f_rng, p_rng ) );
949 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
950 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
951 &ctx->DP ) );
952
953 DP = &DP_blind;
954
955 /*
956 * DQ_blind = ( Q - 1 ) * R + DQ
957 */
958 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
959 f_rng, p_rng ) );
960 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
961 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
962 &ctx->DQ ) );
963
964 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000965#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200966 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000969 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100970#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200971 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000972 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 *
Hanno Beckera5fa0792018-03-09 10:42:23 +0000974 * TP = input ^ dP mod P
975 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000977
978 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
979 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
981 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000982 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000984 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
985 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
986 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
988 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000989 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000991 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
992 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200994
Paul Bakkerf451bac2013-08-30 15:37:02 +0200995 if( f_rng != NULL )
996 {
997 /*
998 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200999 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001000 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001001 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001003 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Hanno Beckera5fa0792018-03-09 10:42:23 +00001005 /* Verify the result to prevent glitching attacks. */
1006 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1007 &ctx->N, &ctx->RN ) );
1008 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
1009 {
1010 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1011 goto cleanup;
1012 }
1013
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
1017cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001019 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1020 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001021#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001022
Hanno Beckera5fa0792018-03-09 10:42:23 +00001023 mbedtls_mpi_free( &P1 );
1024 mbedtls_mpi_free( &Q1 );
1025 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001026
1027 if( f_rng != NULL )
1028 {
Janos Follathe81102e2017-03-22 13:38:28 +00001029#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001030 mbedtls_mpi_free( &D_blind );
1031#else
1032 mbedtls_mpi_free( &DP_blind );
1033 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001034#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Hanno Beckera5fa0792018-03-09 10:42:23 +00001037 mbedtls_mpi_free( &T );
1038
1039#if !defined(MBEDTLS_RSA_NO_CRT)
1040 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1041#endif
1042
1043 mbedtls_mpi_free( &C );
1044 mbedtls_mpi_free( &I );
1045
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001048
1049 return( 0 );
1050}
1051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001053/**
1054 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1055 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001056 * \param dst buffer to mask
1057 * \param dlen length of destination buffer
1058 * \param src source of the mask generation
1059 * \param slen length of the source buffer
1060 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001061 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001062static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001066 unsigned char counter[4];
1067 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001068 unsigned int hlen;
1069 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001070 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001073 memset( counter, 0, 4 );
1074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001076
Simon Butcher02037452016-03-01 21:19:12 +00001077 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001078 p = dst;
1079
1080 while( dlen > 0 )
1081 {
1082 use_len = hlen;
1083 if( dlen < hlen )
1084 use_len = dlen;
1085
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001086 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1087 goto exit;
1088 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1089 goto exit;
1090 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1091 goto exit;
1092 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1093 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001094
1095 for( i = 0; i < use_len; ++i )
1096 *p++ ^= mask[i];
1097
1098 counter[3]++;
1099
1100 dlen -= use_len;
1101 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001102
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001103exit:
Gilles Peskine18ac7162017-05-05 19:24:06 +02001104 mbedtls_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001105
1106 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001107}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001111/*
1112 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001115 int (*f_rng)(void *, unsigned char *, size_t),
1116 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001117 int mode,
1118 const unsigned char *label, size_t label_len,
1119 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001120 const unsigned char *input,
1121 unsigned char *output )
1122{
1123 size_t olen;
1124 int ret;
1125 unsigned char *p = output;
1126 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 const mbedtls_md_info_t *md_info;
1128 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1131 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001132
1133 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001137 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001139
1140 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001142
Simon Butcher02037452016-03-01 21:19:12 +00001143 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001144 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001146
1147 memset( output, 0, olen );
1148
1149 *p++ = 0;
1150
Simon Butcher02037452016-03-01 21:19:12 +00001151 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001152 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001154
1155 p += hlen;
1156
Simon Butcher02037452016-03-01 21:19:12 +00001157 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001158 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1159 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001160 p += hlen;
1161 p += olen - 2 * hlen - 2 - ilen;
1162 *p++ = 1;
1163 memcpy( p, input, ilen );
1164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001166 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001167 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001168
Simon Butcher02037452016-03-01 21:19:12 +00001169 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001170 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1171 &md_ctx ) ) != 0 )
1172 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001173
Simon Butcher02037452016-03-01 21:19:12 +00001174 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001175 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1176 &md_ctx ) ) != 0 )
1177 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001178
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001179exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001181
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001182 if( ret != 0 )
1183 return( ret );
1184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 return( ( mode == MBEDTLS_RSA_PUBLIC )
1186 ? mbedtls_rsa_public( ctx, output, output )
1187 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001188}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001192/*
1193 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001196 int (*f_rng)(void *, unsigned char *, size_t),
1197 void *p_rng,
1198 int mode, size_t ilen,
1199 const unsigned char *input,
1200 unsigned char *output )
1201{
1202 size_t nb_pad, olen;
1203 int ret;
1204 unsigned char *p = output;
1205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001208
Janos Follath1ed9f992016-03-18 11:45:44 +00001209 // We don't check p_rng because it won't be dereferenced here
1210 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001212
1213 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001214
Simon Butcher02037452016-03-01 21:19:12 +00001215 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001216 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001218
1219 nb_pad = olen - 3 - ilen;
1220
1221 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001225
1226 while( nb_pad-- > 0 )
1227 {
1228 int rng_dl = 100;
1229
1230 do {
1231 ret = f_rng( p_rng, p, 1 );
1232 } while( *p == 0 && --rng_dl && ret == 0 );
1233
Simon Butcher02037452016-03-01 21:19:12 +00001234 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001235 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001237
1238 p++;
1239 }
1240 }
1241 else
1242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001244
1245 while( nb_pad-- > 0 )
1246 *p++ = 0xFF;
1247 }
1248
1249 *p++ = 0;
1250 memcpy( p, input, ilen );
1251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 return( ( mode == MBEDTLS_RSA_PUBLIC )
1253 ? mbedtls_rsa_public( ctx, output, output )
1254 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001257
Paul Bakker5121ce52009-01-03 21:22:43 +00001258/*
1259 * Add the message padding, then do an RSA operation
1260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001262 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001263 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001264 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001265 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 unsigned char *output )
1267{
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 switch( ctx->padding )
1269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270#if defined(MBEDTLS_PKCS1_V15)
1271 case MBEDTLS_RSA_PKCS_V15:
1272 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001273 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001274#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#if defined(MBEDTLS_PKCS1_V21)
1277 case MBEDTLS_RSA_PKCS_V21:
1278 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001279 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001281
1282 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001285}
1286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001288/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001289 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001292 int (*f_rng)(void *, unsigned char *, size_t),
1293 void *p_rng,
1294 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001295 const unsigned char *label, size_t label_len,
1296 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001297 const unsigned char *input,
1298 unsigned char *output,
1299 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001300{
Paul Bakker23986e52011-04-24 08:57:21 +00001301 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001302 size_t ilen, i, pad_len;
1303 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1305 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001306 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 const mbedtls_md_info_t *md_info;
1308 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001309
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001310 /*
1311 * Parameters sanity checks
1312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1314 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315
1316 ilen = ctx->len;
1317
Paul Bakker27fdf462011-06-09 13:55:13 +00001318 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001322 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001324
Janos Follathc17cda12016-02-11 11:08:18 +00001325 hlen = mbedtls_md_get_size( md_info );
1326
1327 // checking for integer underflow
1328 if( 2 * hlen + 2 > ilen )
1329 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1330
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001331 /*
1332 * RSA operation
1333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1335 ? mbedtls_rsa_public( ctx, input, buf )
1336 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
1338 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001339 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001341 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001342 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001345 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1346 {
1347 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001348 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001349 }
1350
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001351 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001352 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1353 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001354 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001355 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1356 &md_ctx ) ) != 0 )
1357 {
1358 mbedtls_md_free( &md_ctx );
1359 goto cleanup;
1360 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001363
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001364 /* Generate lHash */
1365 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1366 goto cleanup;
1367
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001368 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001369 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001370 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001372 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001374 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001375
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001376 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001377
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001378 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001379 for( i = 0; i < hlen; i++ )
1380 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001381
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001382 /* Get zero-padding len, but always read till end of buffer
1383 * (minus one, for the 01 byte) */
1384 pad_len = 0;
1385 pad_done = 0;
1386 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1387 {
1388 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001389 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001390 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001391
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001392 p += pad_len;
1393 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001394
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001395 /*
1396 * The only information "leaked" is whether the padding was correct or not
1397 * (eg, no data is copied if it was not correct). This meets the
1398 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1399 * the different error conditions.
1400 */
1401 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001402 {
1403 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1404 goto cleanup;
1405 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001406
Paul Bakker66d5d072014-06-17 16:39:18 +02001407 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001408 {
1409 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1410 goto cleanup;
1411 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001412
1413 *olen = ilen - (p - buf);
1414 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001415 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001416
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001417cleanup:
1418 mbedtls_zeroize( buf, sizeof( buf ) );
1419 mbedtls_zeroize( lhash, sizeof( lhash ) );
1420
1421 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001426/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1427 *
1428 * \param value The value to analyze.
Gilles Peskineb4739162018-10-04 18:32:29 +02001429 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001430 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001431static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001432{
1433 /* MSVC has a warning about unary minus on unsigned, but this is
1434 * well-defined and precisely what we want to do here */
1435#if defined(_MSC_VER)
1436#pragma warning( push )
1437#pragma warning( disable : 4146 )
1438#endif
1439 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1440#if defined(_MSC_VER)
1441#pragma warning( pop )
1442#endif
1443}
1444
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001445/** Check whether a size is out of bounds, without branches.
1446 *
1447 * This is equivalent to `size > max`, but is likely to be compiled to
1448 * to code using bitwise operation rather than a branch.
1449 *
1450 * \param size Size to check.
1451 * \param max Maximum desired value for \p size.
1452 * \return \c 0 if `size <= max`.
1453 * \return \c 1 if `size > max`.
1454 */
1455static unsigned size_greater_than( size_t size, size_t max )
1456{
1457 /* Return the sign bit (1 for negative) of (max - size). */
1458 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1459}
1460
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001461/** Choose between two integer values, without branches.
1462 *
Gilles Peskineb4739162018-10-04 18:32:29 +02001463 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1464 * to code using bitwise operation rather than a branch.
1465 *
1466 * \param cond Condition to test.
1467 * \param if1 Value to use if \p cond is nonzero.
1468 * \param if0 Value to use if \p cond is zero.
1469 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001470 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001471static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001472{
Gilles Peskineb4739162018-10-04 18:32:29 +02001473 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001474 return( ( mask & if1 ) | (~mask & if0 ) );
1475}
1476
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001477/** Shift some data towards the left inside a buffer without leaking
1478 * the length of the data through side channels.
1479 *
1480 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1481 * ```
1482 * memmove(start, start + offset, total - offset);
1483 * memset(start + offset, 0, total - offset);
1484 * ```
1485 * but it strives to use a memory access pattern (and thus total timing)
1486 * that does not depend on \p offset. This timing independence comes at
1487 * the expense of performance.
1488 *
1489 * \param start Pointer to the start of the buffer.
1490 * \param total Total size of the buffer.
1491 * \param offset Offset from which to copy \p total - \p offset bytes.
1492 */
1493static void mem_move_to_left( void *start,
1494 size_t total,
1495 size_t offset )
1496{
1497 volatile unsigned char *buf = start;
1498 size_t i, n;
1499 if( total == 0 )
1500 return;
1501 for( i = 0; i < total; i++ )
1502 {
1503 unsigned no_op = size_greater_than( total - offset, i );
1504 /* The first `total - offset` passes are a no-op. The last
1505 * `offset` passes shift the data one byte to the left and
1506 * zero out the last byte. */
1507 for( n = 0; n < total - 1; n++ )
Gilles Peskine66a28e92018-10-12 19:15:34 +02001508 {
1509 unsigned char current = buf[n];
1510 unsigned char next = buf[n+1];
1511 buf[n] = if_int( no_op, current, next );
1512 }
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001513 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1514 }
1515}
1516
Paul Bakkerb3869132013-02-28 17:21:01 +01001517/*
1518 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001521 int (*f_rng)(void *, unsigned char *, size_t),
1522 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001523 int mode, size_t *olen,
1524 const unsigned char *input,
1525 unsigned char *output,
Gilles Peskinecd500f32018-10-02 22:43:06 +02001526 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001527{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001528 int ret;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001529 size_t ilen = ctx->len;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001530 size_t i;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001531 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1532 ilen - 11 :
1533 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001535 /* The following variables take sensitive values: their value must
1536 * not leak into the observable behavior of the function other than
1537 * the designated outputs (output, olen, return value). Otherwise
1538 * this would open the execution of the function to
1539 * side-channel-based variants of the Bleichenbacher padding oracle
1540 * attack. Potential side channels include overall timing, memory
1541 * access patterns (especially visible to an adversary who has access
1542 * to a shared memory cache), and branches (especially visible to
1543 * an adversary who has access to a shared code cache or to a shared
1544 * branch predictor). */
1545 size_t pad_count = 0;
1546 unsigned bad = 0;
1547 unsigned char pad_done = 0;
1548 size_t plaintext_size = 0;
1549 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1552 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001553
Paul Bakkerb3869132013-02-28 17:21:01 +01001554 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1558 ? mbedtls_rsa_public( ctx, input, buf )
1559 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001560
1561 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001562 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001563
Gilles Peskine0b330f72018-10-05 15:06:12 +02001564 /* Check and get padding length in constant time and constant
1565 * memory trace. The first byte must be 0. */
1566 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001570 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1571 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001572 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001573
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001574 /* Read the whole buffer. Set pad_done to nonzero if we find
1575 * the 0x00 byte and remember the padding length in pad_count. */
1576 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001577 {
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001578 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001579 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001580 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001581 }
1582 else
1583 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001584 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1585 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001586 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001587
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001588 /* Read the whole buffer. Set pad_done to nonzero if we find
1589 * the 0x00 byte and remember the padding length in pad_count.
1590 * If there's a non-0xff byte in the padding, the padding is bad. */
1591 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001592 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001593 pad_done |= if_int( buf[i], 0, 1 );
1594 pad_count += if_int( pad_done, 0, 1 );
1595 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 }
1598
Gilles Peskine0b330f72018-10-05 15:06:12 +02001599 /* If pad_done is still zero, there's no data, only unfinished padding. */
1600 bad |= if_int( pad_done, 0, 1 );
1601
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001602 /* There must be at least 8 bytes of padding. */
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001603 bad |= size_greater_than( 8, pad_count );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001604
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001605 /* If the padding is valid, set plaintext_size to the number of
1606 * remaining bytes after stripping the padding. If the padding
1607 * is invalid, avoid leaking this fact through the size of the
1608 * output: use the maximum message size that fits in the output
1609 * buffer. Do it without branches to avoid leaking the padding
1610 * validity through timing. RSA keys are small enough that all the
1611 * size_t values involved fit in unsigned int. */
Gilles Peskineb4739162018-10-04 18:32:29 +02001612 plaintext_size = if_int( bad,
1613 (unsigned) plaintext_max_size,
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001614 (unsigned) ( ilen - pad_count - 3 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001615
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001616 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001617 * buffer and to 1 otherwise. */
1618 output_too_large = size_greater_than( plaintext_size,
1619 plaintext_max_size );
Paul Bakker060c5682009-01-12 21:48:39 +00001620
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001621 /* Set ret without branches to avoid timing attacks. Return:
1622 * - INVALID_PADDING if the padding is bad (bad != 0).
1623 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1624 * plaintext does not fit in the output buffer.
1625 * - 0 if the padding is correct. */
Gilles Peskine84a21d52018-10-12 19:19:12 +02001626 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1627 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1628 0 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001629
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001630 /* If the padding is bad or the plaintext is too large, zero the
1631 * data that we're about to copy to the output buffer.
1632 * We need to copy the same amount of data
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001633 * from the same buffer whether the padding is good or not to
1634 * avoid leaking the padding validity through overall timing or
1635 * through memory or cache access patterns. */
Gilles Peskine0b330f72018-10-05 15:06:12 +02001636 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001637 for( i = 11; i < ilen; i++ )
Gilles Peskine0b330f72018-10-05 15:06:12 +02001638 buf[i] &= ~bad;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001639
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001640 /* If the plaintext is too large, truncate it to the buffer size.
1641 * Copy anyway to avoid revealing the length through timing, because
1642 * revealing the length is as bad as revealing the padding validity
1643 * for a Bleichenbacher attack. */
1644 plaintext_size = if_int( output_too_large,
1645 (unsigned) plaintext_max_size,
1646 (unsigned) plaintext_size );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001647
Gilles Peskine087544b2018-10-04 22:45:13 +02001648 /* Move the plaintext to the leftmost position where it can start in
1649 * the working buffer, i.e. make it start plaintext_max_size from
1650 * the end of the buffer. Do this with a memory access trace that
1651 * does not depend on the plaintext size. After this move, the
1652 * starting location of the plaintext is no longer sensitive
1653 * information. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001654 mem_move_to_left( buf + ilen - plaintext_max_size,
1655 plaintext_max_size,
Gilles Peskine087544b2018-10-04 22:45:13 +02001656 plaintext_max_size - plaintext_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001657
Gilles Peskine087544b2018-10-04 22:45:13 +02001658 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001659 * into the output buffer. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001660 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001661
1662 /* Report the amount of data we copied to the output buffer. In case
1663 * of errors (bad padding or output too large), the value of *olen
1664 * when this function returns is not specified. Making it equivalent
1665 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001666 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001668cleanup:
1669 mbedtls_zeroize( buf, sizeof( buf ) );
1670
1671 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001672}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001674
1675/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001676 * Do an RSA operation, then remove the message padding
1677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001679 int (*f_rng)(void *, unsigned char *, size_t),
1680 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001681 int mode, size_t *olen,
1682 const unsigned char *input,
1683 unsigned char *output,
1684 size_t output_max_len)
1685{
1686 switch( ctx->padding )
1687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#if defined(MBEDTLS_PKCS1_V15)
1689 case MBEDTLS_RSA_PKCS_V15:
1690 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001691 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001692#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#if defined(MBEDTLS_PKCS1_V21)
1695 case MBEDTLS_RSA_PKCS_V21:
1696 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001697 olen, input, output,
1698 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001699#endif
1700
1701 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001703 }
1704}
1705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001707/*
1708 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001711 int (*f_rng)(void *, unsigned char *, size_t),
1712 void *p_rng,
1713 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001715 unsigned int hashlen,
1716 const unsigned char *hash,
1717 unsigned char *sig )
1718{
1719 size_t olen;
1720 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001722 unsigned int slen, hlen, offset = 0;
1723 int ret;
1724 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 const mbedtls_md_info_t *md_info;
1726 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1729 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001730
1731 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001733
1734 olen = ctx->len;
1735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001737 {
Simon Butcher02037452016-03-01 21:19:12 +00001738 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001740 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001744 }
1745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001747 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001751 slen = hlen;
1752
1753 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001755
1756 memset( sig, 0, olen );
1757
Simon Butcher02037452016-03-01 21:19:12 +00001758 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001759 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001761
Simon Butcher02037452016-03-01 21:19:12 +00001762 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001763 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001764 p += olen - hlen * 2 - 2;
1765 *p++ = 0x01;
1766 memcpy( p, salt, slen );
1767 p += slen;
1768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001770 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001771 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001772
Simon Butcher02037452016-03-01 21:19:12 +00001773 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001774 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1775 goto exit;
1776 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1777 goto exit;
1778 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1779 goto exit;
1780 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1781 goto exit;
1782 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1783 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001784
Simon Butcher02037452016-03-01 21:19:12 +00001785 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001786 if( msb % 8 == 0 )
1787 offset = 1;
1788
Simon Butcher02037452016-03-01 21:19:12 +00001789 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001790 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1791 &md_ctx ) ) != 0 )
1792 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001793
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001794 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001795 sig[0] &= 0xFF >> ( olen * 8 - msb );
1796
1797 p += hlen;
1798 *p++ = 0xBC;
1799
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001800 mbedtls_zeroize( salt, sizeof( salt ) );
1801
1802exit:
1803 mbedtls_md_free( &md_ctx );
1804
1805 if( ret != 0 )
1806 return( ret );
1807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 return( ( mode == MBEDTLS_RSA_PUBLIC )
1809 ? mbedtls_rsa_public( ctx, sig, sig )
1810 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001811}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001815/*
1816 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1817 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001818
1819/* Construct a PKCS v1.5 encoding of a hashed message
1820 *
1821 * This is used both for signature generation and verification.
1822 *
1823 * Parameters:
1824 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001825 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001826 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001827 * - hash: Buffer containing the hashed message or the raw data.
1828 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001829 * - dst: Buffer to hold the encoded message.
1830 *
1831 * Assumptions:
1832 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1833 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001834 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001835 *
1836 */
1837static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1838 unsigned int hashlen,
1839 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001840 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001841 unsigned char *dst )
1842{
1843 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001844 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001845 unsigned char *p = dst;
1846 const char *oid = NULL;
1847
1848 /* Are we signing hashed or raw data? */
1849 if( md_alg != MBEDTLS_MD_NONE )
1850 {
1851 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1852 if( md_info == NULL )
1853 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1854
1855 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1856 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1857
1858 hashlen = mbedtls_md_get_size( md_info );
1859
1860 /* Double-check that 8 + hashlen + oid_size can be used as a
1861 * 1-byte ASN.1 length encoding and that there's no overflow. */
1862 if( 8 + hashlen + oid_size >= 0x80 ||
1863 10 + hashlen < hashlen ||
1864 10 + hashlen + oid_size < 10 + hashlen )
1865 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1866
1867 /*
1868 * Static bounds check:
1869 * - Need 10 bytes for five tag-length pairs.
1870 * (Insist on 1-byte length encodings to protect against variants of
1871 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1872 * - Need hashlen bytes for hash
1873 * - Need oid_size bytes for hash alg OID.
1874 */
1875 if( nb_pad < 10 + hashlen + oid_size )
1876 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1877 nb_pad -= 10 + hashlen + oid_size;
1878 }
1879 else
1880 {
1881 if( nb_pad < hashlen )
1882 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1883
1884 nb_pad -= hashlen;
1885 }
1886
Hanno Becker2b2f8982017-09-27 17:10:03 +01001887 /* Need space for signature header and padding delimiter (3 bytes),
1888 * and 8 bytes for the minimal padding */
1889 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001890 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1891 nb_pad -= 3;
1892
1893 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001894 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001895
1896 /* Write signature header and padding */
1897 *p++ = 0;
1898 *p++ = MBEDTLS_RSA_SIGN;
1899 memset( p, 0xFF, nb_pad );
1900 p += nb_pad;
1901 *p++ = 0;
1902
1903 /* Are we signing raw data? */
1904 if( md_alg == MBEDTLS_MD_NONE )
1905 {
1906 memcpy( p, hash, hashlen );
1907 return( 0 );
1908 }
1909
1910 /* Signing hashed data, add corresponding ASN.1 structure
1911 *
1912 * DigestInfo ::= SEQUENCE {
1913 * digestAlgorithm DigestAlgorithmIdentifier,
1914 * digest Digest }
1915 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1916 * Digest ::= OCTET STRING
1917 *
1918 * Schematic:
1919 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1920 * TAG-NULL + LEN [ NULL ] ]
1921 * TAG-OCTET + LEN [ HASH ] ]
1922 */
1923 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001924 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001925 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001926 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001927 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001928 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001929 memcpy( p, oid, oid_size );
1930 p += oid_size;
1931 *p++ = MBEDTLS_ASN1_NULL;
1932 *p++ = 0x00;
1933 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001934 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001935 memcpy( p, hash, hashlen );
1936 p += hashlen;
1937
1938 /* Just a sanity-check, should be automatic
1939 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001940 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001941 {
Hanno Beckere58d38c2017-09-27 17:09:00 +01001942 mbedtls_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001943 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1944 }
1945
1946 return( 0 );
1947}
1948
Paul Bakkerb3869132013-02-28 17:21:01 +01001949/*
1950 * Do an RSA operation to sign the message digest
1951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001953 int (*f_rng)(void *, unsigned char *, size_t),
1954 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001955 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001957 unsigned int hashlen,
1958 const unsigned char *hash,
1959 unsigned char *sig )
1960{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001961 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001962 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1965 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001966
Hanno Beckerfdf38032017-09-06 12:35:55 +01001967 /*
1968 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1969 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001970
Hanno Beckerfdf38032017-09-06 12:35:55 +01001971 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1972 ctx->len, sig ) ) != 0 )
1973 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001974
1975 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001976 * Call respective RSA primitive
1977 */
1978
1979 if( mode == MBEDTLS_RSA_PUBLIC )
1980 {
1981 /* Skip verification on a public key operation */
1982 return( mbedtls_rsa_public( ctx, sig, sig ) );
1983 }
1984
1985 /* Private key operation
1986 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001987 * In order to prevent Lenstra's attack, make the signature in a
1988 * temporary buffer and check it before returning it.
1989 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001990
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001991 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001992 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001993 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1994
Hanno Beckerfdf38032017-09-06 12:35:55 +01001995 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001996 if( verif == NULL )
1997 {
1998 mbedtls_free( sig_try );
1999 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2000 }
2001
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002002 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2003 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2004
Hanno Becker171a8f12017-09-06 12:32:16 +01002005 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002006 {
2007 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2008 goto cleanup;
2009 }
2010
2011 memcpy( sig, sig_try, ctx->len );
2012
2013cleanup:
2014 mbedtls_free( sig_try );
2015 mbedtls_free( verif );
2016
2017 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002018}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002020
2021/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 * Do an RSA operation to sign the message digest
2023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002025 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002026 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002029 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002030 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 unsigned char *sig )
2032{
Paul Bakker5121ce52009-01-03 21:22:43 +00002033 switch( ctx->padding )
2034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035#if defined(MBEDTLS_PKCS1_V15)
2036 case MBEDTLS_RSA_PKCS_V15:
2037 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002038 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002039#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041#if defined(MBEDTLS_PKCS1_V21)
2042 case MBEDTLS_RSA_PKCS_V21:
2043 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002044 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002045#endif
2046
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002050}
2051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002053/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002054 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002057 int (*f_rng)(void *, unsigned char *, size_t),
2058 void *p_rng,
2059 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002061 unsigned int hashlen,
2062 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002064 int expected_salt_len,
2065 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002066{
Paul Bakker23986e52011-04-24 08:57:21 +00002067 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002068 size_t siglen;
2069 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002070 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002072 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002073 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002074 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 const mbedtls_md_info_t *md_info;
2076 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002077 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2080 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002081
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 siglen = ctx->len;
2083
Paul Bakker27fdf462011-06-09 13:55:13 +00002084 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2088 ? mbedtls_rsa_public( ctx, sig, buf )
2089 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002090
2091 if( ret != 0 )
2092 return( ret );
2093
2094 p = buf;
2095
Paul Bakkerb3869132013-02-28 17:21:01 +01002096 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 {
Simon Butcher02037452016-03-01 21:19:12 +00002101 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002103 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002107 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002110 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002114
Paul Bakkerb3869132013-02-28 17:21:01 +01002115 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002116
Simon Butcher02037452016-03-01 21:19:12 +00002117 /*
2118 * Note: EMSA-PSS verification is over the length of N - 1 bits
2119 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002120 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002121
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002122 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2123 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2124
Simon Butcher02037452016-03-01 21:19:12 +00002125 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002126 if( msb % 8 == 0 )
2127 {
2128 p++;
2129 siglen -= 1;
2130 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002131
Gilles Peskine139108a2017-10-18 19:03:42 +02002132 if( siglen < hlen + 2 )
2133 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2134 hash_start = p + siglen - hlen - 1;
2135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002137 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002138 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002139
Jaeden Amero66954e12018-01-25 16:05:54 +00002140 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2141 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002142 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002143
Paul Bakkerb3869132013-02-28 17:21:01 +01002144 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002145
Gilles Peskine6a54b022017-10-17 19:02:13 +02002146 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002147 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002148
Gilles Peskine91048a32017-10-19 17:46:14 +02002149 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002150 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002151 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2152 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002153 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002154
Gilles Peskine6a54b022017-10-17 19:02:13 +02002155 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002158 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002159 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002160 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2161 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002162 }
2163
Simon Butcher02037452016-03-01 21:19:12 +00002164 /*
2165 * Generate H = Hash( M' )
2166 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002167 ret = mbedtls_md_starts( &md_ctx );
2168 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002169 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002170 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2171 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002172 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002173 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2174 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002175 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002176 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2177 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002178 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002179 ret = mbedtls_md_finish( &md_ctx, result );
2180 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002181 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002182
Jaeden Amero66954e12018-01-25 16:05:54 +00002183 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002184 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002185 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002186 goto exit;
2187 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002188
2189exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002191
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002192 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002193}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002194
2195/*
2196 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002199 int (*f_rng)(void *, unsigned char *, size_t),
2200 void *p_rng,
2201 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002203 unsigned int hashlen,
2204 const unsigned char *hash,
2205 const unsigned char *sig )
2206{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2208 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002209 : md_alg;
2210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002212 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002214 sig ) );
2215
2216}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002220/*
2221 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002224 int (*f_rng)(void *, unsigned char *, size_t),
2225 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002226 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002228 unsigned int hashlen,
2229 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002230 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002231{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002232 int ret = 0;
2233 const size_t sig_len = ctx->len;
2234 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2237 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002238
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002239 /*
2240 * Prepare expected PKCS1 v1.5 encoding of hash.
2241 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002242
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002243 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2244 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2245 {
2246 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2247 goto cleanup;
2248 }
2249
2250 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2251 encoded_expected ) ) != 0 )
2252 goto cleanup;
2253
2254 /*
2255 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2256 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002259 ? mbedtls_rsa_public( ctx, sig, encoded )
2260 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002261 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002262 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002263
Simon Butcher02037452016-03-01 21:19:12 +00002264 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002265 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002266 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002267
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002268 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2269 sig_len ) ) != 0 )
2270 {
2271 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2272 goto cleanup;
2273 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002274
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002275cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002276
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002277 if( encoded != NULL )
2278 {
2279 mbedtls_zeroize( encoded, sig_len );
2280 mbedtls_free( encoded );
2281 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002282
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002283 if( encoded_expected != NULL )
2284 {
2285 mbedtls_zeroize( encoded_expected, sig_len );
2286 mbedtls_free( encoded_expected );
2287 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002288
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002289 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002290}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
2293/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002294 * Do an RSA operation and check the message digest
2295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002297 int (*f_rng)(void *, unsigned char *, size_t),
2298 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002299 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002301 unsigned int hashlen,
2302 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002303 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002304{
2305 switch( ctx->padding )
2306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307#if defined(MBEDTLS_PKCS1_V15)
2308 case MBEDTLS_RSA_PKCS_V15:
2309 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002310 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002311#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#if defined(MBEDTLS_PKCS1_V21)
2314 case MBEDTLS_RSA_PKCS_V21:
2315 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002316 hashlen, hash, sig );
2317#endif
2318
2319 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002321 }
2322}
2323
2324/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002325 * Copy the components of an RSA key
2326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002328{
2329 int ret;
2330
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002331 dst->len = src->len;
2332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2334 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2337 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2338 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002339
2340#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2342 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2343 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2345 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002346#endif
2347
2348 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2351 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002352
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002353 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002354 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002355
2356cleanup:
2357 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002359
2360 return( ret );
2361}
2362
2363/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 * Free the components of an RSA key
2365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002369 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2370 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002372
Hanno Becker33c30a02017-08-23 07:00:22 +01002373#if !defined(MBEDTLS_RSA_NO_CRT)
2374 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2375 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2376 mbedtls_mpi_free( &ctx->DP );
2377#endif /* MBEDTLS_RSA_NO_CRT */
2378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379#if defined(MBEDTLS_THREADING_C)
Gilles Peskined7e82ad2021-02-01 17:57:41 +01002380 /* Free the mutex, but only if it hasn't been freed already. */
2381 if( ctx->ver != 0 )
2382 {
2383 mbedtls_mutex_free( &ctx->mutex );
2384 ctx->ver = 0;
2385 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002386#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002387}
2388
Hanno Beckerab377312017-08-23 16:24:51 +01002389#endif /* !MBEDTLS_RSA_ALT */
2390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002393#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
2395/*
2396 * Example RSA-1024 keypair, for test purposes
2397 */
2398#define KEY_LEN 128
2399
2400#define RSA_N "9292758453063D803DD603D5E777D788" \
2401 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2402 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2403 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2404 "93A89813FBF3C4F8066D2D800F7C38A8" \
2405 "1AE31942917403FF4946B0A83D3D3E05" \
2406 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2407 "5E94BB77B07507233A0BC7BAC8F90F79"
2408
2409#define RSA_E "10001"
2410
2411#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2412 "66CA472BC44D253102F8B4A9D3BFA750" \
2413 "91386C0077937FE33FA3252D28855837" \
2414 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2415 "DF79C5CE07EE72C7F123142198164234" \
2416 "CABB724CF78B8173B9F880FC86322407" \
2417 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2418 "071513A1E85B5DFA031F21ECAE91A34D"
2419
2420#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2421 "2C01CAD19EA484A87EA4377637E75500" \
2422 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2423 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2424
2425#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2426 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2427 "910E4168387E3C30AA1E00C339A79508" \
2428 "8452DD96A9A5EA5D9DCA68DA636032AF"
2429
Paul Bakker5121ce52009-01-03 21:22:43 +00002430#define PT_LEN 24
2431#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2432 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002435static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002436{
gufe44206cb392020-08-03 17:56:50 +02002437#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002438 size_t i;
2439
Paul Bakker545570e2010-07-18 09:00:25 +00002440 if( rng_state != NULL )
2441 rng_state = NULL;
2442
Paul Bakkera3d195c2011-11-27 21:07:34 +00002443 for( i = 0; i < len; ++i )
2444 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002445#else
2446 if( rng_state != NULL )
2447 rng_state = NULL;
2448
2449 arc4random_buf( output, len );
gufe44206cb392020-08-03 17:56:50 +02002450#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002451
Paul Bakkera3d195c2011-11-27 21:07:34 +00002452 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002455
Paul Bakker5121ce52009-01-03 21:22:43 +00002456/*
2457 * Checkup routine
2458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002460{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002461 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002463 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 unsigned char rsa_plaintext[PT_LEN];
2466 unsigned char rsa_decrypted[PT_LEN];
2467 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002469 unsigned char sha1sum[20];
2470#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
Hanno Becker3a701162017-08-22 13:52:43 +01002472 mbedtls_mpi K;
2473
2474 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
Hanno Becker3a701162017-08-22 13:52:43 +01002477 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2478 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2479 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2480 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2481 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2482 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2483 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2484 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2485 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2486 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2487
Hanno Becker7f25f852017-10-10 16:56:22 +01002488 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
2490 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2494 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 {
2496 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498
Hanno Beckera5fa0792018-03-09 10:42:23 +00002499 ret = 1;
2500 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 }
2502
2503 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
2506 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2507
Hanno Becker98838b02017-10-02 13:16:10 +01002508 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2509 PT_LEN, rsa_plaintext,
2510 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 {
2512 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002514
Hanno Beckera5fa0792018-03-09 10:42:23 +00002515 ret = 1;
2516 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 }
2518
2519 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Hanno Becker98838b02017-10-02 13:16:10 +01002522 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2523 &len, rsa_ciphertext, rsa_decrypted,
2524 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 {
2526 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
Hanno Beckera5fa0792018-03-09 10:42:23 +00002529 ret = 1;
2530 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 }
2532
2533 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2534 {
2535 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
Hanno Beckera5fa0792018-03-09 10:42:23 +00002538 ret = 1;
2539 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 }
2541
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002542 if( verbose != 0 )
2543 mbedtls_printf( "passed\n" );
2544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002547 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002549 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002550 {
2551 if( verbose != 0 )
2552 mbedtls_printf( "failed\n" );
2553
2554 return( 1 );
2555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Hanno Becker98838b02017-10-02 13:16:10 +01002557 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2558 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2559 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 {
2561 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Hanno Beckera5fa0792018-03-09 10:42:23 +00002564 ret = 1;
2565 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
2567
2568 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Hanno Becker98838b02017-10-02 13:16:10 +01002571 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2572 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2573 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 {
2575 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Hanno Beckera5fa0792018-03-09 10:42:23 +00002578 ret = 1;
2579 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 }
2581
2582 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002583 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002586 if( verbose != 0 )
2587 mbedtls_printf( "\n" );
2588
Paul Bakker3d8fb632014-04-17 12:42:41 +02002589cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002590 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 mbedtls_rsa_free( &rsa );
2592#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002593 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002595 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596}
2597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600#endif /* MBEDTLS_RSA_C */