blob: 376afa4cad1966be060ee3994a379395e18a5429 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
Hanno Becker74716312017-10-02 10:00:37 +010046
Paul Bakker5121ce52009-01-03 21:22:43 +000047/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000048 * The following sources were referenced in the design of this implementation
49 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000050 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000051 * [1] A method for obtaining digital signatures and public-key cryptosystems
52 * R Rivest, A Shamir, and L Adleman
53 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
54 *
55 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
56 * Menezes, van Oorschot and Vanstone
57 *
Janos Follathe81102e2017-03-22 13:38:28 +000058 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
59 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
60 * Stefan Mangard
61 * https://arxiv.org/abs/1702.08719v2
62 *
Paul Bakker5121ce52009-01-03 21:22:43 +000063 */
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020067#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020069#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010074#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000076
Rich Evans00ab4702015-02-06 13:43:58 +000077#include <string.h>
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000081#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000082
gufe44206cb392020-08-03 17:56:50 +020083#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000084#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000085#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000088#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010089#else
Rich Evans00ab4702015-02-06 13:43:58 +000090#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020092#define mbedtls_calloc calloc
93#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010094#endif
95
Hanno Beckera565f542017-10-11 11:00:19 +010096#if !defined(MBEDTLS_RSA_ALT)
97
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010098/* Implementation that should never be optimized out by the compiler */
99static void mbedtls_zeroize( void *v, size_t n ) {
100 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
101}
102
Manuel Pégourié-Gonnardb0ba5bc2018-03-13 13:27:14 +0100103#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +0100104/* constant-time buffer comparison */
105static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
106{
107 size_t i;
108 const unsigned char *A = (const unsigned char *) a;
109 const unsigned char *B = (const unsigned char *) b;
110 unsigned char diff = 0;
111
112 for( i = 0; i < n; i++ )
113 diff |= A[i] ^ B[i];
114
115 return( diff );
116}
Manuel Pégourié-Gonnardb0ba5bc2018-03-13 13:27:14 +0100117#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +0100118
Hanno Becker617c1ae2017-08-23 14:11:24 +0100119int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
120 const mbedtls_mpi *N,
121 const mbedtls_mpi *P, const mbedtls_mpi *Q,
122 const mbedtls_mpi *D, const mbedtls_mpi *E )
123{
124 int ret;
125
126 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
127 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
128 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
129 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
130 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
131 {
132 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
133 }
134
135 if( N != NULL )
136 ctx->len = mbedtls_mpi_size( &ctx->N );
137
138 return( 0 );
139}
140
141int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100142 unsigned char const *N, size_t N_len,
143 unsigned char const *P, size_t P_len,
144 unsigned char const *Q, size_t Q_len,
145 unsigned char const *D, size_t D_len,
146 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100147{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000148 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100149
150 if( N != NULL )
151 {
152 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
153 ctx->len = mbedtls_mpi_size( &ctx->N );
154 }
155
156 if( P != NULL )
157 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
158
159 if( Q != NULL )
160 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
161
162 if( D != NULL )
163 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
164
165 if( E != NULL )
166 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
167
168cleanup:
169
170 if( ret != 0 )
171 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
172
173 return( 0 );
174}
175
Hanno Becker705fc682017-10-10 17:57:02 +0100176/*
177 * Checks whether the context fields are set in such a way
178 * that the RSA primitives will be able to execute without error.
179 * It does *not* make guarantees for consistency of the parameters.
180 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100181static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
182 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100183{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100184#if !defined(MBEDTLS_RSA_NO_CRT)
185 /* blinding_needed is only used for NO_CRT to decide whether
186 * P,Q need to be present or not. */
187 ((void) blinding_needed);
188#endif
189
Hanno Becker3a760a12018-01-05 08:14:49 +0000190 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
191 ctx->len > MBEDTLS_MPI_MAX_SIZE )
192 {
Hanno Becker705fc682017-10-10 17:57:02 +0100193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000194 }
Hanno Becker705fc682017-10-10 17:57:02 +0100195
196 /*
197 * 1. Modular exponentiation needs positive, odd moduli.
198 */
199
200 /* Modular exponentiation wrt. N is always used for
201 * RSA public key operations. */
202 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
203 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
204 {
205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
206 }
207
208#if !defined(MBEDTLS_RSA_NO_CRT)
209 /* Modular exponentiation for P and Q is only
210 * used for private key operations and if CRT
211 * is used. */
212 if( is_priv &&
213 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
214 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
215 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
216 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
217 {
218 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
219 }
220#endif /* !MBEDTLS_RSA_NO_CRT */
221
222 /*
223 * 2. Exponents must be positive
224 */
225
226 /* Always need E for public key operations */
227 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
228 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
229
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100230#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100231 /* For private key operations, use D or DP & DQ
232 * as (unblinded) exponents. */
233 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
234 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
235#else
236 if( is_priv &&
237 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
238 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
239 {
240 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
241 }
242#endif /* MBEDTLS_RSA_NO_CRT */
243
244 /* Blinding shouldn't make exponents negative either,
245 * so check that P, Q >= 1 if that hasn't yet been
246 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100247#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100248 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100249 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
250 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
251 {
252 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
253 }
254#endif
255
256 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100257 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100258#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100259 if( is_priv &&
260 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
261 {
262 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
263 }
264#endif
265
266 return( 0 );
267}
268
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100269int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100270{
271 int ret = 0;
272
Hanno Becker617c1ae2017-08-23 14:11:24 +0100273 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
274 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
275 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
276 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
277 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100278
Jack Lloyd100e1472020-01-29 13:13:04 -0500279#if !defined(MBEDTLS_RSA_NO_CRT)
280 const int have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
281 const int have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
282 const int have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
283#endif
284
Hanno Becker617c1ae2017-08-23 14:11:24 +0100285 /*
286 * Check whether provided parameters are enough
287 * to deduce all others. The following incomplete
288 * parameter sets for private keys are supported:
289 *
290 * (1) P, Q missing.
291 * (2) D and potentially N missing.
292 *
293 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100294
Hanno Becker2cca6f32017-09-29 11:46:40 +0100295 const int n_missing = have_P && have_Q && have_D && have_E;
296 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
297 const int d_missing = have_P && have_Q && !have_D && have_E;
298 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
299
300 /* These three alternatives are mutually exclusive */
301 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100302
Hanno Becker617c1ae2017-08-23 14:11:24 +0100303 if( !is_priv && !is_pub )
304 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
305
306 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100307 * Step 1: Deduce N if P, Q are provided.
308 */
309
310 if( !have_N && have_P && have_Q )
311 {
312 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
313 &ctx->Q ) ) != 0 )
314 {
315 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
316 }
317
318 ctx->len = mbedtls_mpi_size( &ctx->N );
319 }
320
321 /*
322 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100323 */
324
325 if( pq_missing )
326 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100327 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100328 &ctx->P, &ctx->Q );
329 if( ret != 0 )
330 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
331
332 }
333 else if( d_missing )
334 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100335 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
336 &ctx->Q,
337 &ctx->E,
338 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100339 {
340 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
341 }
342 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100343
Hanno Becker617c1ae2017-08-23 14:11:24 +0100344 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100345 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100346 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100347 */
348
Hanno Becker23344b52017-08-23 07:43:27 +0100349#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd100e1472020-01-29 13:13:04 -0500350 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100351 {
352 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
353 &ctx->DP, &ctx->DQ, &ctx->QP );
354 if( ret != 0 )
355 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
356 }
Hanno Becker23344b52017-08-23 07:43:27 +0100357#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100358
359 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100360 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100361 */
362
Hanno Beckerebd2c022017-10-12 10:54:53 +0100363 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100364}
365
Hanno Becker617c1ae2017-08-23 14:11:24 +0100366int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
367 unsigned char *N, size_t N_len,
368 unsigned char *P, size_t P_len,
369 unsigned char *Q, size_t Q_len,
370 unsigned char *D, size_t D_len,
371 unsigned char *E, size_t E_len )
372{
373 int ret = 0;
374
375 /* Check if key is private or public */
376 const int is_priv =
377 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
378 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
379 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
380 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
381 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
382
383 if( !is_priv )
384 {
385 /* If we're trying to export private parameters for a public key,
386 * something must be wrong. */
387 if( P != NULL || Q != NULL || D != NULL )
388 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
389
390 }
391
392 if( N != NULL )
393 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
394
395 if( P != NULL )
396 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
397
398 if( Q != NULL )
399 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
400
401 if( D != NULL )
402 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
403
404 if( E != NULL )
405 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100406
407cleanup:
408
409 return( ret );
410}
411
Hanno Becker617c1ae2017-08-23 14:11:24 +0100412int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
413 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
414 mbedtls_mpi *D, mbedtls_mpi *E )
415{
416 int ret;
417
418 /* Check if key is private or public */
419 int is_priv =
420 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
421 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
422 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
423 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
424 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
425
426 if( !is_priv )
427 {
428 /* If we're trying to export private parameters for a public key,
429 * something must be wrong. */
430 if( P != NULL || Q != NULL || D != NULL )
431 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
432
433 }
434
435 /* Export all requested core parameters. */
436
437 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
438 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
439 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
440 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
441 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
442 {
443 return( ret );
444 }
445
446 return( 0 );
447}
448
449/*
450 * Export CRT parameters
451 * This must also be implemented if CRT is not used, for being able to
452 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
453 * can be used in this case.
454 */
455int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
456 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
457{
458 int ret;
459
460 /* Check if key is private or public */
461 int is_priv =
462 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
463 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
464 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
465 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
466 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
467
468 if( !is_priv )
469 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
470
Hanno Beckerdc95c892017-08-23 06:57:02 +0100471#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100472 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100473 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
474 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
475 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
476 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100477 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100478 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100479#else
480 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
481 DP, DQ, QP ) ) != 0 )
482 {
483 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
484 }
485#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100486
487 return( 0 );
488}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100489
Paul Bakker5121ce52009-01-03 21:22:43 +0000490/*
491 * Initialize an RSA context
492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000495 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#if defined(MBEDTLS_THREADING_C)
502 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200503#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000504}
505
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100506/*
507 * Set padding for an existing RSA context
508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100510{
511 ctx->padding = padding;
512 ctx->hash_id = hash_id;
513}
514
Hanno Becker617c1ae2017-08-23 14:11:24 +0100515/*
516 * Get length in bytes of RSA modulus
517 */
518
519size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
520{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100521 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100522}
523
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527/*
528 * Generate an RSA keypair
529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000531 int (*f_rng)(void *, unsigned char *, size_t),
532 void *p_rng,
533 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000534{
535 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100536 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
Paul Bakker21eb2802010-08-16 11:10:02 +0000538 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Janos Follathef441782016-09-21 13:18:12 +0100541 if( nbits % 2 )
542 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
543
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100544 mbedtls_mpi_init( &H );
545 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547 /*
548 * find primes P and Q with Q < P so that:
549 * GCD( E, (P-1)*(Q-1) ) == 1
550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553 do
554 {
Janos Follath10c575b2016-02-23 14:42:48 +0000555 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100556 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
Janos Follathef441782016-09-21 13:18:12 +0100558 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100559 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 continue;
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200565 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 continue;
567
Janos Follathef441782016-09-21 13:18:12 +0100568 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100569 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100570
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100571 /* Temporarily replace P,Q by P-1, Q-1 */
572 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
573 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
574 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100579 /* Restore P,Q */
580 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
581 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
582
583 ctx->len = mbedtls_mpi_size( &ctx->N );
584
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 /*
586 * D = E^-1 mod ((P-1)*(Q-1))
587 * DP = D mod (P - 1)
588 * DQ = D mod (Q - 1)
589 * QP = Q^-1 mod P
590 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100592 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
593
594#if !defined(MBEDTLS_RSA_NO_CRT)
595 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
596 &ctx->DP, &ctx->DQ, &ctx->QP ) );
597#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
Hanno Becker83aad1f2017-08-23 06:45:10 +0100599 /* Double-check */
600 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602cleanup:
603
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100604 mbedtls_mpi_free( &H );
605 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
607 if( ret != 0 )
608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_rsa_free( ctx );
610 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 }
612
Paul Bakker48377d92013-08-30 12:06:24 +0200613 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000614}
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
618/*
619 * Check a public RSA key
620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000622{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100623 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000625
Hanno Becker3a760a12018-01-05 08:14:49 +0000626 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100629 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
Hanno Becker705fc682017-10-10 17:57:02 +0100631 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
632 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100636 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
638 return( 0 );
639}
640
641/*
Hanno Becker705fc682017-10-10 17:57:02 +0100642 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000645{
Hanno Becker705fc682017-10-10 17:57:02 +0100646 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100647 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 {
Hanno Becker98838b02017-10-02 13:16:10 +0100649 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 }
Paul Bakker48377d92013-08-30 12:06:24 +0200651
Hanno Becker98838b02017-10-02 13:16:10 +0100652 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100653 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100655 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000657
Hanno Beckerb269a852017-08-25 08:03:21 +0100658#if !defined(MBEDTLS_RSA_NO_CRT)
659 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
660 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
661 {
662 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
663 }
664#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000665
666 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000667}
668
669/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100670 * Check if contexts holding a public and private key match
671 */
Hanno Becker98838b02017-10-02 13:16:10 +0100672int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
673 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100674{
Hanno Becker98838b02017-10-02 13:16:10 +0100675 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100679 }
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
682 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100685 }
686
687 return( 0 );
688}
689
690/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 * Do an RSA public key operation
692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000694 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 unsigned char *output )
696{
Paul Bakker23986e52011-04-24 08:57:21 +0000697 int ret;
698 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
Hanno Beckerebd2c022017-10-12 10:54:53 +0100701 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100702 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200706#if defined(MBEDTLS_THREADING_C)
707 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
708 return( ret );
709#endif
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200715 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
716 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 }
718
719 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
721 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
723cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200725 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
726 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100727#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
731 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
734 return( 0 );
735}
736
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200737/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200738 * Generate or update blinding values, see section 10 of:
739 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200740 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200741 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200742 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200743static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200744 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
745{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200746 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200747
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200748 if( ctx->Vf.p != NULL )
749 {
750 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
752 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
753 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
754 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200755
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200756 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200757 }
758
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200759 /* Unblinding value: Vf = random number, invertible mod N */
760 do {
761 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
765 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
766 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200767
768 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
770 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 +0200771
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200772
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200773cleanup:
774 return( ret );
775}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200776
Paul Bakker5121ce52009-01-03 21:22:43 +0000777/*
Janos Follathe81102e2017-03-22 13:38:28 +0000778 * Exponent blinding supposed to prevent side-channel attacks using multiple
779 * traces of measurements to recover the RSA key. The more collisions are there,
780 * the more bits of the key can be recovered. See [3].
781 *
782 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
783 * observations on avarage.
784 *
785 * For example with 28 byte blinding to achieve 2 collisions the adversary has
786 * to make 2^112 observations on avarage.
787 *
788 * (With the currently (as of 2017 April) known best algorithms breaking 2048
789 * bit RSA requires approximately as much time as trying out 2^112 random keys.
790 * Thus in this sense with 28 byte blinding the security is not reduced by
791 * side-channel attacks like the one in [3])
792 *
793 * This countermeasure does not help if the key recovery is possible with a
794 * single trace.
795 */
796#define RSA_EXPONENT_BLINDING 28
797
798/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000799 * Do an RSA private key operation
800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200802 int (*f_rng)(void *, unsigned char *, size_t),
803 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000804 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000805 unsigned char *output )
806{
Paul Bakker23986e52011-04-24 08:57:21 +0000807 int ret;
808 size_t olen;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000809
810 /* Temporary holding the result */
811 mbedtls_mpi T;
812
813 /* Temporaries holding P-1, Q-1 and the
814 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000815 mbedtls_mpi P1, Q1, R;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000816
817#if !defined(MBEDTLS_RSA_NO_CRT)
818 /* Temporaries holding the results mod p resp. mod q. */
819 mbedtls_mpi TP, TQ;
820
821 /* Temporaries holding the blinded exponents for
822 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000823 mbedtls_mpi DP_blind, DQ_blind;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000824
825 /* Pointers to actual exponents to be used - either the unblinded
826 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000827 mbedtls_mpi *DP = &ctx->DP;
828 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000829#else
830 /* Temporary holding the blinded exponent (if used). */
831 mbedtls_mpi D_blind;
832
833 /* Pointer to actual exponent to be used - either the unblinded
834 * or the blinded one, depending on the presence of a PRNG. */
835 mbedtls_mpi *D = &ctx->D;
836#endif /* MBEDTLS_RSA_NO_CRT */
837
838 /* Temporaries holding the initial input and the double
839 * checked result; should be the same in the end. */
840 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Hanno Beckerebd2c022017-10-12 10:54:53 +0100842 if( rsa_check_context( ctx, 1 /* private key checks */,
843 f_rng != NULL /* blinding y/n */ ) != 0 )
844 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100845 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100846 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100847
Hanno Beckera5fa0792018-03-09 10:42:23 +0000848#if defined(MBEDTLS_THREADING_C)
849 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
850 return( ret );
851#endif
852
853 /* MPI Initialization */
854 mbedtls_mpi_init( &T );
855
856 mbedtls_mpi_init( &P1 );
857 mbedtls_mpi_init( &Q1 );
858 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000859
Janos Follathf9203b42017-03-22 15:13:15 +0000860 if( f_rng != NULL )
861 {
Janos Follathe81102e2017-03-22 13:38:28 +0000862#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000863 mbedtls_mpi_init( &D_blind );
864#else
865 mbedtls_mpi_init( &DP_blind );
866 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000867#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000868 }
Janos Follathe81102e2017-03-22 13:38:28 +0000869
Hanno Beckera5fa0792018-03-09 10:42:23 +0000870#if !defined(MBEDTLS_RSA_NO_CRT)
871 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200872#endif
873
Hanno Beckera5fa0792018-03-09 10:42:23 +0000874 mbedtls_mpi_init( &I );
875 mbedtls_mpi_init( &C );
876
877 /* End of MPI initialization */
878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
880 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000881 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200882 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
883 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000884 }
885
Hanno Beckera5fa0792018-03-09 10:42:23 +0000886 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
887
Paul Bakkerf451bac2013-08-30 15:37:02 +0200888 if( f_rng != NULL )
889 {
890 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200891 * Blinding
892 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200893 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200894 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
895 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000897
Janos Follathe81102e2017-03-22 13:38:28 +0000898 /*
899 * Exponent blinding
900 */
901 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
902 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
903
Janos Follathf9203b42017-03-22 15:13:15 +0000904#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000905 /*
906 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
907 */
908 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
909 f_rng, p_rng ) );
910 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
911 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
912 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
913
914 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000915#else
916 /*
917 * DP_blind = ( P - 1 ) * R + DP
918 */
919 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
920 f_rng, p_rng ) );
921 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
922 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
923 &ctx->DP ) );
924
925 DP = &DP_blind;
926
927 /*
928 * DQ_blind = ( Q - 1 ) * R + DQ
929 */
930 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
931 f_rng, p_rng ) );
932 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
933 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
934 &ctx->DQ ) );
935
936 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000937#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200938 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000941 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100942#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200943 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000944 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000945 *
Hanno Beckera5fa0792018-03-09 10:42:23 +0000946 * TP = input ^ dP mod P
947 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000948 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000949
950 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
951 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
953 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000954 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000956 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
957 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
958 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
960 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000961 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000962 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000963 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
964 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200966
Paul Bakkerf451bac2013-08-30 15:37:02 +0200967 if( f_rng != NULL )
968 {
969 /*
970 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200971 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200972 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200973 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200975 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Hanno Beckera5fa0792018-03-09 10:42:23 +0000977 /* Verify the result to prevent glitching attacks. */
978 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
979 &ctx->N, &ctx->RN ) );
980 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
981 {
982 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
983 goto cleanup;
984 }
985
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000988
989cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200991 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
992 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200993#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200994
Hanno Beckera5fa0792018-03-09 10:42:23 +0000995 mbedtls_mpi_free( &P1 );
996 mbedtls_mpi_free( &Q1 );
997 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000998
999 if( f_rng != NULL )
1000 {
Janos Follathe81102e2017-03-22 13:38:28 +00001001#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001002 mbedtls_mpi_free( &D_blind );
1003#else
1004 mbedtls_mpi_free( &DP_blind );
1005 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001006#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001007 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
Hanno Beckera5fa0792018-03-09 10:42:23 +00001009 mbedtls_mpi_free( &T );
1010
1011#if !defined(MBEDTLS_RSA_NO_CRT)
1012 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1013#endif
1014
1015 mbedtls_mpi_free( &C );
1016 mbedtls_mpi_free( &I );
1017
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
1021 return( 0 );
1022}
1023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001025/**
1026 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1027 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001028 * \param dst buffer to mask
1029 * \param dlen length of destination buffer
1030 * \param src source of the mask generation
1031 * \param slen length of the source buffer
1032 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001033 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001034static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001036{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001038 unsigned char counter[4];
1039 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001040 unsigned int hlen;
1041 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001042 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001045 memset( counter, 0, 4 );
1046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001048
Simon Butcher02037452016-03-01 21:19:12 +00001049 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001050 p = dst;
1051
1052 while( dlen > 0 )
1053 {
1054 use_len = hlen;
1055 if( dlen < hlen )
1056 use_len = dlen;
1057
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001058 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1059 goto exit;
1060 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1061 goto exit;
1062 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1063 goto exit;
1064 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1065 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001066
1067 for( i = 0; i < use_len; ++i )
1068 *p++ ^= mask[i];
1069
1070 counter[3]++;
1071
1072 dlen -= use_len;
1073 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001074
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001075exit:
Gilles Peskine18ac7162017-05-05 19:24:06 +02001076 mbedtls_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001077
1078 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001079}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001083/*
1084 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001087 int (*f_rng)(void *, unsigned char *, size_t),
1088 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001089 int mode,
1090 const unsigned char *label, size_t label_len,
1091 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001092 const unsigned char *input,
1093 unsigned char *output )
1094{
1095 size_t olen;
1096 int ret;
1097 unsigned char *p = output;
1098 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 const mbedtls_md_info_t *md_info;
1100 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1103 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001104
1105 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001109 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001111
1112 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001114
Simon Butcher02037452016-03-01 21:19:12 +00001115 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001116 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001118
1119 memset( output, 0, olen );
1120
1121 *p++ = 0;
1122
Simon Butcher02037452016-03-01 21:19:12 +00001123 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001124 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001126
1127 p += hlen;
1128
Simon Butcher02037452016-03-01 21:19:12 +00001129 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001130 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1131 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001132 p += hlen;
1133 p += olen - 2 * hlen - 2 - ilen;
1134 *p++ = 1;
1135 memcpy( p, input, ilen );
1136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001138 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001139 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001140
Simon Butcher02037452016-03-01 21:19:12 +00001141 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001142 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1143 &md_ctx ) ) != 0 )
1144 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001145
Simon Butcher02037452016-03-01 21:19:12 +00001146 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001147 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1148 &md_ctx ) ) != 0 )
1149 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001150
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001151exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001153
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001154 if( ret != 0 )
1155 return( ret );
1156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 return( ( mode == MBEDTLS_RSA_PUBLIC )
1158 ? mbedtls_rsa_public( ctx, output, output )
1159 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001164/*
1165 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001168 int (*f_rng)(void *, unsigned char *, size_t),
1169 void *p_rng,
1170 int mode, size_t ilen,
1171 const unsigned char *input,
1172 unsigned char *output )
1173{
1174 size_t nb_pad, olen;
1175 int ret;
1176 unsigned char *p = output;
1177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001180
Janos Follath1ed9f992016-03-18 11:45:44 +00001181 // We don't check p_rng because it won't be dereferenced here
1182 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001184
1185 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001186
Simon Butcher02037452016-03-01 21:19:12 +00001187 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001188 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001190
1191 nb_pad = olen - 3 - ilen;
1192
1193 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001197
1198 while( nb_pad-- > 0 )
1199 {
1200 int rng_dl = 100;
1201
1202 do {
1203 ret = f_rng( p_rng, p, 1 );
1204 } while( *p == 0 && --rng_dl && ret == 0 );
1205
Simon Butcher02037452016-03-01 21:19:12 +00001206 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001207 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001209
1210 p++;
1211 }
1212 }
1213 else
1214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001216
1217 while( nb_pad-- > 0 )
1218 *p++ = 0xFF;
1219 }
1220
1221 *p++ = 0;
1222 memcpy( p, input, ilen );
1223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 return( ( mode == MBEDTLS_RSA_PUBLIC )
1225 ? mbedtls_rsa_public( ctx, output, output )
1226 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001229
Paul Bakker5121ce52009-01-03 21:22:43 +00001230/*
1231 * Add the message padding, then do an RSA operation
1232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001234 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001235 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001236 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001237 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 unsigned char *output )
1239{
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 switch( ctx->padding )
1241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_PKCS1_V15)
1243 case MBEDTLS_RSA_PKCS_V15:
1244 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001245 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001246#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248#if defined(MBEDTLS_PKCS1_V21)
1249 case MBEDTLS_RSA_PKCS_V21:
1250 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001251 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001252#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
1254 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001257}
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001260/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001261 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001264 int (*f_rng)(void *, unsigned char *, size_t),
1265 void *p_rng,
1266 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001267 const unsigned char *label, size_t label_len,
1268 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001269 const unsigned char *input,
1270 unsigned char *output,
1271 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001272{
Paul Bakker23986e52011-04-24 08:57:21 +00001273 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001274 size_t ilen, i, pad_len;
1275 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1277 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001278 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 const mbedtls_md_info_t *md_info;
1280 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001281
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001282 /*
1283 * Parameters sanity checks
1284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1286 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
1288 ilen = ctx->len;
1289
Paul Bakker27fdf462011-06-09 13:55:13 +00001290 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001294 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001296
Janos Follathc17cda12016-02-11 11:08:18 +00001297 hlen = mbedtls_md_get_size( md_info );
1298
1299 // checking for integer underflow
1300 if( 2 * hlen + 2 > ilen )
1301 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1302
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001303 /*
1304 * RSA operation
1305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1307 ? mbedtls_rsa_public( ctx, input, buf )
1308 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
1310 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001311 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001312
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001313 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001314 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001317 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1318 {
1319 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001320 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001321 }
1322
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001323 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001324 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1325 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001326 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001327 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1328 &md_ctx ) ) != 0 )
1329 {
1330 mbedtls_md_free( &md_ctx );
1331 goto cleanup;
1332 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001335
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001336 /* Generate lHash */
1337 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1338 goto cleanup;
1339
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001340 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001341 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001342 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001344 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001346 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001347
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001348 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001349
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001350 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001351 for( i = 0; i < hlen; i++ )
1352 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001353
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001354 /* Get zero-padding len, but always read till end of buffer
1355 * (minus one, for the 01 byte) */
1356 pad_len = 0;
1357 pad_done = 0;
1358 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1359 {
1360 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001361 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001362 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001363
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001364 p += pad_len;
1365 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001366
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001367 /*
1368 * The only information "leaked" is whether the padding was correct or not
1369 * (eg, no data is copied if it was not correct). This meets the
1370 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1371 * the different error conditions.
1372 */
1373 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001374 {
1375 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1376 goto cleanup;
1377 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001378
Paul Bakker66d5d072014-06-17 16:39:18 +02001379 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001380 {
1381 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1382 goto cleanup;
1383 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001384
1385 *olen = ilen - (p - buf);
1386 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001387 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001388
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001389cleanup:
1390 mbedtls_zeroize( buf, sizeof( buf ) );
1391 mbedtls_zeroize( lhash, sizeof( lhash ) );
1392
1393 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001398/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1399 *
1400 * \param value The value to analyze.
Gilles Peskineb4739162018-10-04 18:32:29 +02001401 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001402 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001403static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001404{
1405 /* MSVC has a warning about unary minus on unsigned, but this is
1406 * well-defined and precisely what we want to do here */
1407#if defined(_MSC_VER)
1408#pragma warning( push )
1409#pragma warning( disable : 4146 )
1410#endif
1411 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1412#if defined(_MSC_VER)
1413#pragma warning( pop )
1414#endif
1415}
1416
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001417/** Check whether a size is out of bounds, without branches.
1418 *
1419 * This is equivalent to `size > max`, but is likely to be compiled to
1420 * to code using bitwise operation rather than a branch.
1421 *
1422 * \param size Size to check.
1423 * \param max Maximum desired value for \p size.
1424 * \return \c 0 if `size <= max`.
1425 * \return \c 1 if `size > max`.
1426 */
1427static unsigned size_greater_than( size_t size, size_t max )
1428{
1429 /* Return the sign bit (1 for negative) of (max - size). */
1430 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1431}
1432
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001433/** Choose between two integer values, without branches.
1434 *
Gilles Peskineb4739162018-10-04 18:32:29 +02001435 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1436 * to code using bitwise operation rather than a branch.
1437 *
1438 * \param cond Condition to test.
1439 * \param if1 Value to use if \p cond is nonzero.
1440 * \param if0 Value to use if \p cond is zero.
1441 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001442 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001443static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001444{
Gilles Peskineb4739162018-10-04 18:32:29 +02001445 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001446 return( ( mask & if1 ) | (~mask & if0 ) );
1447}
1448
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001449/** Shift some data towards the left inside a buffer without leaking
1450 * the length of the data through side channels.
1451 *
1452 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1453 * ```
1454 * memmove(start, start + offset, total - offset);
1455 * memset(start + offset, 0, total - offset);
1456 * ```
1457 * but it strives to use a memory access pattern (and thus total timing)
1458 * that does not depend on \p offset. This timing independence comes at
1459 * the expense of performance.
1460 *
1461 * \param start Pointer to the start of the buffer.
1462 * \param total Total size of the buffer.
1463 * \param offset Offset from which to copy \p total - \p offset bytes.
1464 */
1465static void mem_move_to_left( void *start,
1466 size_t total,
1467 size_t offset )
1468{
1469 volatile unsigned char *buf = start;
1470 size_t i, n;
1471 if( total == 0 )
1472 return;
1473 for( i = 0; i < total; i++ )
1474 {
1475 unsigned no_op = size_greater_than( total - offset, i );
1476 /* The first `total - offset` passes are a no-op. The last
1477 * `offset` passes shift the data one byte to the left and
1478 * zero out the last byte. */
1479 for( n = 0; n < total - 1; n++ )
Gilles Peskine66a28e92018-10-12 19:15:34 +02001480 {
1481 unsigned char current = buf[n];
1482 unsigned char next = buf[n+1];
1483 buf[n] = if_int( no_op, current, next );
1484 }
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001485 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1486 }
1487}
1488
Paul Bakkerb3869132013-02-28 17:21:01 +01001489/*
1490 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001493 int (*f_rng)(void *, unsigned char *, size_t),
1494 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001495 int mode, size_t *olen,
1496 const unsigned char *input,
1497 unsigned char *output,
Gilles Peskinecd500f32018-10-02 22:43:06 +02001498 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001499{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001500 int ret;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001501 size_t ilen = ctx->len;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001502 size_t i;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001503 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1504 ilen - 11 :
1505 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001507 /* The following variables take sensitive values: their value must
1508 * not leak into the observable behavior of the function other than
1509 * the designated outputs (output, olen, return value). Otherwise
1510 * this would open the execution of the function to
1511 * side-channel-based variants of the Bleichenbacher padding oracle
1512 * attack. Potential side channels include overall timing, memory
1513 * access patterns (especially visible to an adversary who has access
1514 * to a shared memory cache), and branches (especially visible to
1515 * an adversary who has access to a shared code cache or to a shared
1516 * branch predictor). */
1517 size_t pad_count = 0;
1518 unsigned bad = 0;
1519 unsigned char pad_done = 0;
1520 size_t plaintext_size = 0;
1521 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1524 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001525
Paul Bakkerb3869132013-02-28 17:21:01 +01001526 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1530 ? mbedtls_rsa_public( ctx, input, buf )
1531 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001532
1533 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001534 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001535
Gilles Peskine0b330f72018-10-05 15:06:12 +02001536 /* Check and get padding length in constant time and constant
1537 * memory trace. The first byte must be 0. */
1538 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001541 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001542 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1543 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001544 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001545
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001546 /* Read the whole buffer. Set pad_done to nonzero if we find
1547 * the 0x00 byte and remember the padding length in pad_count. */
1548 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001549 {
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001550 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001551 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001552 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001553 }
1554 else
1555 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001556 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1557 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001558 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001559
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001560 /* Read the whole buffer. Set pad_done to nonzero if we find
1561 * the 0x00 byte and remember the padding length in pad_count.
1562 * If there's a non-0xff byte in the padding, the padding is bad. */
1563 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001564 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001565 pad_done |= if_int( buf[i], 0, 1 );
1566 pad_count += if_int( pad_done, 0, 1 );
1567 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001568 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 }
1570
Gilles Peskine0b330f72018-10-05 15:06:12 +02001571 /* If pad_done is still zero, there's no data, only unfinished padding. */
1572 bad |= if_int( pad_done, 0, 1 );
1573
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001574 /* There must be at least 8 bytes of padding. */
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001575 bad |= size_greater_than( 8, pad_count );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001576
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001577 /* If the padding is valid, set plaintext_size to the number of
1578 * remaining bytes after stripping the padding. If the padding
1579 * is invalid, avoid leaking this fact through the size of the
1580 * output: use the maximum message size that fits in the output
1581 * buffer. Do it without branches to avoid leaking the padding
1582 * validity through timing. RSA keys are small enough that all the
1583 * size_t values involved fit in unsigned int. */
Gilles Peskineb4739162018-10-04 18:32:29 +02001584 plaintext_size = if_int( bad,
1585 (unsigned) plaintext_max_size,
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001586 (unsigned) ( ilen - pad_count - 3 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001587
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001588 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001589 * buffer and to 1 otherwise. */
1590 output_too_large = size_greater_than( plaintext_size,
1591 plaintext_max_size );
Paul Bakker060c5682009-01-12 21:48:39 +00001592
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001593 /* Set ret without branches to avoid timing attacks. Return:
1594 * - INVALID_PADDING if the padding is bad (bad != 0).
1595 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1596 * plaintext does not fit in the output buffer.
1597 * - 0 if the padding is correct. */
Gilles Peskine84a21d52018-10-12 19:19:12 +02001598 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1599 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1600 0 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001601
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001602 /* If the padding is bad or the plaintext is too large, zero the
1603 * data that we're about to copy to the output buffer.
1604 * We need to copy the same amount of data
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001605 * from the same buffer whether the padding is good or not to
1606 * avoid leaking the padding validity through overall timing or
1607 * through memory or cache access patterns. */
Gilles Peskine0b330f72018-10-05 15:06:12 +02001608 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001609 for( i = 11; i < ilen; i++ )
Gilles Peskine0b330f72018-10-05 15:06:12 +02001610 buf[i] &= ~bad;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001611
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001612 /* If the plaintext is too large, truncate it to the buffer size.
1613 * Copy anyway to avoid revealing the length through timing, because
1614 * revealing the length is as bad as revealing the padding validity
1615 * for a Bleichenbacher attack. */
1616 plaintext_size = if_int( output_too_large,
1617 (unsigned) plaintext_max_size,
1618 (unsigned) plaintext_size );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001619
Gilles Peskine087544b2018-10-04 22:45:13 +02001620 /* Move the plaintext to the leftmost position where it can start in
1621 * the working buffer, i.e. make it start plaintext_max_size from
1622 * the end of the buffer. Do this with a memory access trace that
1623 * does not depend on the plaintext size. After this move, the
1624 * starting location of the plaintext is no longer sensitive
1625 * information. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001626 mem_move_to_left( buf + ilen - plaintext_max_size,
1627 plaintext_max_size,
Gilles Peskine087544b2018-10-04 22:45:13 +02001628 plaintext_max_size - plaintext_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001629
Gilles Peskine087544b2018-10-04 22:45:13 +02001630 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001631 * into the output buffer. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001632 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001633
1634 /* Report the amount of data we copied to the output buffer. In case
1635 * of errors (bad padding or output too large), the value of *olen
1636 * when this function returns is not specified. Making it equivalent
1637 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001638 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001639
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001640cleanup:
1641 mbedtls_zeroize( buf, sizeof( buf ) );
1642
1643 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001644}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001646
1647/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001648 * Do an RSA operation, then remove the message padding
1649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001651 int (*f_rng)(void *, unsigned char *, size_t),
1652 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001653 int mode, size_t *olen,
1654 const unsigned char *input,
1655 unsigned char *output,
1656 size_t output_max_len)
1657{
1658 switch( ctx->padding )
1659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#if defined(MBEDTLS_PKCS1_V15)
1661 case MBEDTLS_RSA_PKCS_V15:
1662 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001663 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001664#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#if defined(MBEDTLS_PKCS1_V21)
1667 case MBEDTLS_RSA_PKCS_V21:
1668 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001669 olen, input, output,
1670 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001671#endif
1672
1673 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001675 }
1676}
1677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001679/*
1680 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001683 int (*f_rng)(void *, unsigned char *, size_t),
1684 void *p_rng,
1685 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001687 unsigned int hashlen,
1688 const unsigned char *hash,
1689 unsigned char *sig )
1690{
1691 size_t olen;
1692 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001694 unsigned int slen, hlen, offset = 0;
1695 int ret;
1696 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 const mbedtls_md_info_t *md_info;
1698 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1701 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001702
1703 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001705
1706 olen = ctx->len;
1707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001709 {
Simon Butcher02037452016-03-01 21:19:12 +00001710 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001712 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001716 }
1717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001719 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001723 slen = hlen;
1724
1725 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001727
1728 memset( sig, 0, olen );
1729
Simon Butcher02037452016-03-01 21:19:12 +00001730 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001731 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001733
Simon Butcher02037452016-03-01 21:19:12 +00001734 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001735 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001736 p += olen - hlen * 2 - 2;
1737 *p++ = 0x01;
1738 memcpy( p, salt, slen );
1739 p += slen;
1740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001742 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001743 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001744
Simon Butcher02037452016-03-01 21:19:12 +00001745 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001746 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1747 goto exit;
1748 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1749 goto exit;
1750 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1751 goto exit;
1752 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1753 goto exit;
1754 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1755 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001756
Simon Butcher02037452016-03-01 21:19:12 +00001757 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001758 if( msb % 8 == 0 )
1759 offset = 1;
1760
Simon Butcher02037452016-03-01 21:19:12 +00001761 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001762 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1763 &md_ctx ) ) != 0 )
1764 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001765
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001766 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001767 sig[0] &= 0xFF >> ( olen * 8 - msb );
1768
1769 p += hlen;
1770 *p++ = 0xBC;
1771
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001772 mbedtls_zeroize( salt, sizeof( salt ) );
1773
1774exit:
1775 mbedtls_md_free( &md_ctx );
1776
1777 if( ret != 0 )
1778 return( ret );
1779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 return( ( mode == MBEDTLS_RSA_PUBLIC )
1781 ? mbedtls_rsa_public( ctx, sig, sig )
1782 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001783}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001787/*
1788 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1789 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001790
1791/* Construct a PKCS v1.5 encoding of a hashed message
1792 *
1793 * This is used both for signature generation and verification.
1794 *
1795 * Parameters:
1796 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001797 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001798 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001799 * - hash: Buffer containing the hashed message or the raw data.
1800 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001801 * - dst: Buffer to hold the encoded message.
1802 *
1803 * Assumptions:
1804 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1805 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001806 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001807 *
1808 */
1809static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1810 unsigned int hashlen,
1811 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001812 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001813 unsigned char *dst )
1814{
1815 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001816 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001817 unsigned char *p = dst;
1818 const char *oid = NULL;
1819
1820 /* Are we signing hashed or raw data? */
1821 if( md_alg != MBEDTLS_MD_NONE )
1822 {
1823 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1824 if( md_info == NULL )
1825 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1826
1827 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1828 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1829
1830 hashlen = mbedtls_md_get_size( md_info );
1831
1832 /* Double-check that 8 + hashlen + oid_size can be used as a
1833 * 1-byte ASN.1 length encoding and that there's no overflow. */
1834 if( 8 + hashlen + oid_size >= 0x80 ||
1835 10 + hashlen < hashlen ||
1836 10 + hashlen + oid_size < 10 + hashlen )
1837 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1838
1839 /*
1840 * Static bounds check:
1841 * - Need 10 bytes for five tag-length pairs.
1842 * (Insist on 1-byte length encodings to protect against variants of
1843 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1844 * - Need hashlen bytes for hash
1845 * - Need oid_size bytes for hash alg OID.
1846 */
1847 if( nb_pad < 10 + hashlen + oid_size )
1848 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1849 nb_pad -= 10 + hashlen + oid_size;
1850 }
1851 else
1852 {
1853 if( nb_pad < hashlen )
1854 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1855
1856 nb_pad -= hashlen;
1857 }
1858
Hanno Becker2b2f8982017-09-27 17:10:03 +01001859 /* Need space for signature header and padding delimiter (3 bytes),
1860 * and 8 bytes for the minimal padding */
1861 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001862 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1863 nb_pad -= 3;
1864
1865 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001866 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001867
1868 /* Write signature header and padding */
1869 *p++ = 0;
1870 *p++ = MBEDTLS_RSA_SIGN;
1871 memset( p, 0xFF, nb_pad );
1872 p += nb_pad;
1873 *p++ = 0;
1874
1875 /* Are we signing raw data? */
1876 if( md_alg == MBEDTLS_MD_NONE )
1877 {
1878 memcpy( p, hash, hashlen );
1879 return( 0 );
1880 }
1881
1882 /* Signing hashed data, add corresponding ASN.1 structure
1883 *
1884 * DigestInfo ::= SEQUENCE {
1885 * digestAlgorithm DigestAlgorithmIdentifier,
1886 * digest Digest }
1887 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1888 * Digest ::= OCTET STRING
1889 *
1890 * Schematic:
1891 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1892 * TAG-NULL + LEN [ NULL ] ]
1893 * TAG-OCTET + LEN [ HASH ] ]
1894 */
1895 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001896 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001897 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001898 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001899 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001900 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001901 memcpy( p, oid, oid_size );
1902 p += oid_size;
1903 *p++ = MBEDTLS_ASN1_NULL;
1904 *p++ = 0x00;
1905 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001906 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001907 memcpy( p, hash, hashlen );
1908 p += hashlen;
1909
1910 /* Just a sanity-check, should be automatic
1911 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001912 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001913 {
Hanno Beckere58d38c2017-09-27 17:09:00 +01001914 mbedtls_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001915 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1916 }
1917
1918 return( 0 );
1919}
1920
Paul Bakkerb3869132013-02-28 17:21:01 +01001921/*
1922 * Do an RSA operation to sign the message digest
1923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001925 int (*f_rng)(void *, unsigned char *, size_t),
1926 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001927 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001929 unsigned int hashlen,
1930 const unsigned char *hash,
1931 unsigned char *sig )
1932{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001933 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001934 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1937 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001938
Hanno Beckerfdf38032017-09-06 12:35:55 +01001939 /*
1940 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1941 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001942
Hanno Beckerfdf38032017-09-06 12:35:55 +01001943 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1944 ctx->len, sig ) ) != 0 )
1945 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001946
1947 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001948 * Call respective RSA primitive
1949 */
1950
1951 if( mode == MBEDTLS_RSA_PUBLIC )
1952 {
1953 /* Skip verification on a public key operation */
1954 return( mbedtls_rsa_public( ctx, sig, sig ) );
1955 }
1956
1957 /* Private key operation
1958 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001959 * In order to prevent Lenstra's attack, make the signature in a
1960 * temporary buffer and check it before returning it.
1961 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001962
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001963 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001964 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001965 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1966
Hanno Beckerfdf38032017-09-06 12:35:55 +01001967 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001968 if( verif == NULL )
1969 {
1970 mbedtls_free( sig_try );
1971 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1972 }
1973
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001974 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1975 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1976
Hanno Becker171a8f12017-09-06 12:32:16 +01001977 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001978 {
1979 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1980 goto cleanup;
1981 }
1982
1983 memcpy( sig, sig_try, ctx->len );
1984
1985cleanup:
1986 mbedtls_free( sig_try );
1987 mbedtls_free( verif );
1988
1989 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001992
1993/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 * Do an RSA operation to sign the message digest
1995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001997 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001998 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002001 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002002 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 unsigned char *sig )
2004{
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 switch( ctx->padding )
2006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007#if defined(MBEDTLS_PKCS1_V15)
2008 case MBEDTLS_RSA_PKCS_V15:
2009 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002010 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002011#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013#if defined(MBEDTLS_PKCS1_V21)
2014 case MBEDTLS_RSA_PKCS_V21:
2015 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002016 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002017#endif
2018
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002022}
2023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002025/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002026 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002029 int (*f_rng)(void *, unsigned char *, size_t),
2030 void *p_rng,
2031 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002033 unsigned int hashlen,
2034 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002036 int expected_salt_len,
2037 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002038{
Paul Bakker23986e52011-04-24 08:57:21 +00002039 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002040 size_t siglen;
2041 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002042 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002044 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002045 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002046 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 const mbedtls_md_info_t *md_info;
2048 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002049 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2052 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002053
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 siglen = ctx->len;
2055
Paul Bakker27fdf462011-06-09 13:55:13 +00002056 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2060 ? mbedtls_rsa_public( ctx, sig, buf )
2061 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002062
2063 if( ret != 0 )
2064 return( ret );
2065
2066 p = buf;
2067
Paul Bakkerb3869132013-02-28 17:21:01 +01002068 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 {
Simon Butcher02037452016-03-01 21:19:12 +00002073 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002075 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002079 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002082 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002086
Paul Bakkerb3869132013-02-28 17:21:01 +01002087 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002088
Simon Butcher02037452016-03-01 21:19:12 +00002089 /*
2090 * Note: EMSA-PSS verification is over the length of N - 1 bits
2091 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002092 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002093
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002094 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2095 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2096
Simon Butcher02037452016-03-01 21:19:12 +00002097 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002098 if( msb % 8 == 0 )
2099 {
2100 p++;
2101 siglen -= 1;
2102 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002103
Gilles Peskine139108a2017-10-18 19:03:42 +02002104 if( siglen < hlen + 2 )
2105 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2106 hash_start = p + siglen - hlen - 1;
2107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002109 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002110 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002111
Jaeden Amero66954e12018-01-25 16:05:54 +00002112 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2113 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002114 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002115
Paul Bakkerb3869132013-02-28 17:21:01 +01002116 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002117
Gilles Peskine6a54b022017-10-17 19:02:13 +02002118 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002119 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002120
Gilles Peskine91048a32017-10-19 17:46:14 +02002121 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002122 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002123 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2124 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002125 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002126
Gilles Peskine6a54b022017-10-17 19:02:13 +02002127 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002130 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002131 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002132 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2133 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002134 }
2135
Simon Butcher02037452016-03-01 21:19:12 +00002136 /*
2137 * Generate H = Hash( M' )
2138 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002139 ret = mbedtls_md_starts( &md_ctx );
2140 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002141 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002142 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2143 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002144 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002145 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2146 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002147 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002148 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2149 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002150 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002151 ret = mbedtls_md_finish( &md_ctx, result );
2152 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002153 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002154
Jaeden Amero66954e12018-01-25 16:05:54 +00002155 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002156 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002157 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002158 goto exit;
2159 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002160
2161exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002163
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002164 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002165}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002166
2167/*
2168 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002171 int (*f_rng)(void *, unsigned char *, size_t),
2172 void *p_rng,
2173 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002175 unsigned int hashlen,
2176 const unsigned char *hash,
2177 const unsigned char *sig )
2178{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2180 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002181 : md_alg;
2182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002184 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002186 sig ) );
2187
2188}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002192/*
2193 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002196 int (*f_rng)(void *, unsigned char *, size_t),
2197 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002198 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002200 unsigned int hashlen,
2201 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002202 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002203{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002204 int ret = 0;
2205 const size_t sig_len = ctx->len;
2206 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002210
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002211 /*
2212 * Prepare expected PKCS1 v1.5 encoding of hash.
2213 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002214
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002215 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2216 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2217 {
2218 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2219 goto cleanup;
2220 }
2221
2222 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2223 encoded_expected ) ) != 0 )
2224 goto cleanup;
2225
2226 /*
2227 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2228 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002231 ? mbedtls_rsa_public( ctx, sig, encoded )
2232 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002233 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002234 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002235
Simon Butcher02037452016-03-01 21:19:12 +00002236 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002237 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002238 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002239
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002240 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2241 sig_len ) ) != 0 )
2242 {
2243 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2244 goto cleanup;
2245 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002246
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002247cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002248
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002249 if( encoded != NULL )
2250 {
2251 mbedtls_zeroize( encoded, sig_len );
2252 mbedtls_free( encoded );
2253 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002254
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002255 if( encoded_expected != NULL )
2256 {
2257 mbedtls_zeroize( encoded_expected, sig_len );
2258 mbedtls_free( encoded_expected );
2259 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002260
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002261 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002264
2265/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002266 * Do an RSA operation and check the message digest
2267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002269 int (*f_rng)(void *, unsigned char *, size_t),
2270 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002271 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002273 unsigned int hashlen,
2274 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002275 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002276{
2277 switch( ctx->padding )
2278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279#if defined(MBEDTLS_PKCS1_V15)
2280 case MBEDTLS_RSA_PKCS_V15:
2281 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002282 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002283#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#if defined(MBEDTLS_PKCS1_V21)
2286 case MBEDTLS_RSA_PKCS_V21:
2287 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002288 hashlen, hash, sig );
2289#endif
2290
2291 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002293 }
2294}
2295
2296/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002297 * Copy the components of an RSA key
2298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002300{
2301 int ret;
2302
2303 dst->ver = src->ver;
2304 dst->len = src->len;
2305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2307 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2310 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2311 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002312
2313#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2315 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2316 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2318 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002319#endif
2320
2321 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2324 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002325
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002326 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002327 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002328
2329cleanup:
2330 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002332
2333 return( ret );
2334}
2335
2336/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002337 * Free the components of an RSA key
2338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002340{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002342 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2343 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002345
Hanno Becker33c30a02017-08-23 07:00:22 +01002346#if !defined(MBEDTLS_RSA_NO_CRT)
2347 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2348 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2349 mbedtls_mpi_free( &ctx->DP );
2350#endif /* MBEDTLS_RSA_NO_CRT */
2351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352#if defined(MBEDTLS_THREADING_C)
2353 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002354#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002355}
2356
Hanno Beckerab377312017-08-23 16:24:51 +01002357#endif /* !MBEDTLS_RSA_ALT */
2358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002361#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002362
2363/*
2364 * Example RSA-1024 keypair, for test purposes
2365 */
2366#define KEY_LEN 128
2367
2368#define RSA_N "9292758453063D803DD603D5E777D788" \
2369 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2370 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2371 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2372 "93A89813FBF3C4F8066D2D800F7C38A8" \
2373 "1AE31942917403FF4946B0A83D3D3E05" \
2374 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2375 "5E94BB77B07507233A0BC7BAC8F90F79"
2376
2377#define RSA_E "10001"
2378
2379#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2380 "66CA472BC44D253102F8B4A9D3BFA750" \
2381 "91386C0077937FE33FA3252D28855837" \
2382 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2383 "DF79C5CE07EE72C7F123142198164234" \
2384 "CABB724CF78B8173B9F880FC86322407" \
2385 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2386 "071513A1E85B5DFA031F21ECAE91A34D"
2387
2388#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2389 "2C01CAD19EA484A87EA4377637E75500" \
2390 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2391 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2392
2393#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2394 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2395 "910E4168387E3C30AA1E00C339A79508" \
2396 "8452DD96A9A5EA5D9DCA68DA636032AF"
2397
Paul Bakker5121ce52009-01-03 21:22:43 +00002398#define PT_LEN 24
2399#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2400 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002403static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002404{
gufe44206cb392020-08-03 17:56:50 +02002405#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002406 size_t i;
2407
Paul Bakker545570e2010-07-18 09:00:25 +00002408 if( rng_state != NULL )
2409 rng_state = NULL;
2410
Paul Bakkera3d195c2011-11-27 21:07:34 +00002411 for( i = 0; i < len; ++i )
2412 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002413#else
2414 if( rng_state != NULL )
2415 rng_state = NULL;
2416
2417 arc4random_buf( output, len );
gufe44206cb392020-08-03 17:56:50 +02002418#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002419
Paul Bakkera3d195c2011-11-27 21:07:34 +00002420 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002421}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002423
Paul Bakker5121ce52009-01-03 21:22:43 +00002424/*
2425 * Checkup routine
2426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002428{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002429 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002431 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 unsigned char rsa_plaintext[PT_LEN];
2434 unsigned char rsa_decrypted[PT_LEN];
2435 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002437 unsigned char sha1sum[20];
2438#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002439
Hanno Becker3a701162017-08-22 13:52:43 +01002440 mbedtls_mpi K;
2441
2442 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
Hanno Becker3a701162017-08-22 13:52:43 +01002445 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2446 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2447 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2448 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2449 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2450 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2451 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2452 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2453 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2454 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2455
Hanno Becker7f25f852017-10-10 16:56:22 +01002456 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
2458 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2462 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 {
2464 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002466
Hanno Beckera5fa0792018-03-09 10:42:23 +00002467 ret = 1;
2468 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 }
2470
2471 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
2474 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2475
Hanno Becker98838b02017-10-02 13:16:10 +01002476 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2477 PT_LEN, rsa_plaintext,
2478 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 {
2480 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
Hanno Beckera5fa0792018-03-09 10:42:23 +00002483 ret = 1;
2484 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 }
2486
2487 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
Hanno Becker98838b02017-10-02 13:16:10 +01002490 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2491 &len, rsa_ciphertext, rsa_decrypted,
2492 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 {
2494 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Hanno Beckera5fa0792018-03-09 10:42:23 +00002497 ret = 1;
2498 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 }
2500
2501 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2502 {
2503 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
Hanno Beckera5fa0792018-03-09 10:42:23 +00002506 ret = 1;
2507 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 }
2509
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002510 if( verbose != 0 )
2511 mbedtls_printf( "passed\n" );
2512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002515 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002517 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002518 {
2519 if( verbose != 0 )
2520 mbedtls_printf( "failed\n" );
2521
2522 return( 1 );
2523 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Hanno Becker98838b02017-10-02 13:16:10 +01002525 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2526 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2527 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 {
2529 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Hanno Beckera5fa0792018-03-09 10:42:23 +00002532 ret = 1;
2533 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 }
2535
2536 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002538
Hanno Becker98838b02017-10-02 13:16:10 +01002539 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2540 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2541 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 {
2543 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Hanno Beckera5fa0792018-03-09 10:42:23 +00002546 ret = 1;
2547 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 }
2549
2550 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002551 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002554 if( verbose != 0 )
2555 mbedtls_printf( "\n" );
2556
Paul Bakker3d8fb632014-04-17 12:42:41 +02002557cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002558 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 mbedtls_rsa_free( &rsa );
2560#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002561 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002563 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002564}
2565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568#endif /* MBEDTLS_RSA_C */