blob: 98c529f00509dd522e5c44c8ab8d0904d89f4224 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Hanno Becker74716312017-10-02 10:00:37 +010021
Paul Bakker5121ce52009-01-03 21:22:43 +000022/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000023 * The following sources were referenced in the design of this implementation
24 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000025 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000026 * [1] A method for obtaining digital signatures and public-key cryptosystems
27 * R Rivest, A Shamir, and L Adleman
28 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
29 *
30 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
31 * Menezes, van Oorschot and Vanstone
32 *
Janos Follathe81102e2017-03-22 13:38:28 +000033 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
34 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
35 * Stefan Mangard
36 * https://arxiv.org/abs/1702.08719v2
37 *
Paul Bakker5121ce52009-01-03 21:22:43 +000038 */
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020042#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010049#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000059#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000060#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010064#else
Rich Evans00ab4702015-02-06 13:43:58 +000065#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020067#define mbedtls_calloc calloc
68#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010069#endif
70
Hanno Beckera565f542017-10-11 11:00:19 +010071#if !defined(MBEDTLS_RSA_ALT)
72
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010073/* Implementation that should never be optimized out by the compiler */
74static void mbedtls_zeroize( void *v, size_t n ) {
75 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
76}
77
Manuel Pégourié-Gonnardb0ba5bc2018-03-13 13:27:14 +010078#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +010079/* constant-time buffer comparison */
80static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
81{
82 size_t i;
83 const unsigned char *A = (const unsigned char *) a;
84 const unsigned char *B = (const unsigned char *) b;
85 unsigned char diff = 0;
86
87 for( i = 0; i < n; i++ )
88 diff |= A[i] ^ B[i];
89
90 return( diff );
91}
Manuel Pégourié-Gonnardb0ba5bc2018-03-13 13:27:14 +010092#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +010093
Hanno Becker617c1ae2017-08-23 14:11:24 +010094int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
95 const mbedtls_mpi *N,
96 const mbedtls_mpi *P, const mbedtls_mpi *Q,
97 const mbedtls_mpi *D, const mbedtls_mpi *E )
98{
99 int ret;
100
101 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
102 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
103 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
104 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
105 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
106 {
107 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
108 }
109
110 if( N != NULL )
111 ctx->len = mbedtls_mpi_size( &ctx->N );
112
113 return( 0 );
114}
115
116int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100117 unsigned char const *N, size_t N_len,
118 unsigned char const *P, size_t P_len,
119 unsigned char const *Q, size_t Q_len,
120 unsigned char const *D, size_t D_len,
121 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100122{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000123 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100124
125 if( N != NULL )
126 {
127 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
128 ctx->len = mbedtls_mpi_size( &ctx->N );
129 }
130
131 if( P != NULL )
132 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
133
134 if( Q != NULL )
135 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
136
137 if( D != NULL )
138 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
139
140 if( E != NULL )
141 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
142
143cleanup:
144
145 if( ret != 0 )
146 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
147
148 return( 0 );
149}
150
Hanno Becker705fc682017-10-10 17:57:02 +0100151/*
152 * Checks whether the context fields are set in such a way
153 * that the RSA primitives will be able to execute without error.
154 * It does *not* make guarantees for consistency of the parameters.
155 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100156static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
157 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100158{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100159#if !defined(MBEDTLS_RSA_NO_CRT)
160 /* blinding_needed is only used for NO_CRT to decide whether
161 * P,Q need to be present or not. */
162 ((void) blinding_needed);
163#endif
164
Hanno Becker3a760a12018-01-05 08:14:49 +0000165 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
166 ctx->len > MBEDTLS_MPI_MAX_SIZE )
167 {
Hanno Becker705fc682017-10-10 17:57:02 +0100168 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000169 }
Hanno Becker705fc682017-10-10 17:57:02 +0100170
171 /*
172 * 1. Modular exponentiation needs positive, odd moduli.
173 */
174
175 /* Modular exponentiation wrt. N is always used for
176 * RSA public key operations. */
177 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
178 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
179 {
180 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
181 }
182
183#if !defined(MBEDTLS_RSA_NO_CRT)
184 /* Modular exponentiation for P and Q is only
185 * used for private key operations and if CRT
186 * is used. */
187 if( is_priv &&
188 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
189 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
190 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
191 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
192 {
193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
194 }
195#endif /* !MBEDTLS_RSA_NO_CRT */
196
197 /*
198 * 2. Exponents must be positive
199 */
200
201 /* Always need E for public key operations */
202 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
203 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
204
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100205#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100206 /* For private key operations, use D or DP & DQ
207 * as (unblinded) exponents. */
208 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
210#else
211 if( is_priv &&
212 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
213 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
214 {
215 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
216 }
217#endif /* MBEDTLS_RSA_NO_CRT */
218
219 /* Blinding shouldn't make exponents negative either,
220 * so check that P, Q >= 1 if that hasn't yet been
221 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100222#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100223 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100224 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
225 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
226 {
227 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
228 }
229#endif
230
231 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100232 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100233#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100234 if( is_priv &&
235 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
236 {
237 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
238 }
239#endif
240
241 return( 0 );
242}
243
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100244int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100245{
246 int ret = 0;
247
Hanno Becker617c1ae2017-08-23 14:11:24 +0100248 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
249 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
250 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
251 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
252 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100253
Jack Lloyd100e1472020-01-29 13:13:04 -0500254#if !defined(MBEDTLS_RSA_NO_CRT)
255 const int have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
256 const int have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
257 const int have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
258#endif
259
Hanno Becker617c1ae2017-08-23 14:11:24 +0100260 /*
261 * Check whether provided parameters are enough
262 * to deduce all others. The following incomplete
263 * parameter sets for private keys are supported:
264 *
265 * (1) P, Q missing.
266 * (2) D and potentially N missing.
267 *
268 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100269
Hanno Becker2cca6f32017-09-29 11:46:40 +0100270 const int n_missing = have_P && have_Q && have_D && have_E;
271 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
272 const int d_missing = have_P && have_Q && !have_D && have_E;
273 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
274
275 /* These three alternatives are mutually exclusive */
276 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100277
Hanno Becker617c1ae2017-08-23 14:11:24 +0100278 if( !is_priv && !is_pub )
279 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
280
281 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100282 * Step 1: Deduce N if P, Q are provided.
283 */
284
285 if( !have_N && have_P && have_Q )
286 {
287 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
288 &ctx->Q ) ) != 0 )
289 {
290 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
291 }
292
293 ctx->len = mbedtls_mpi_size( &ctx->N );
294 }
295
296 /*
297 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100298 */
299
300 if( pq_missing )
301 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100302 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100303 &ctx->P, &ctx->Q );
304 if( ret != 0 )
305 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
306
307 }
308 else if( d_missing )
309 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100310 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
311 &ctx->Q,
312 &ctx->E,
313 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100314 {
315 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
316 }
317 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100318
Hanno Becker617c1ae2017-08-23 14:11:24 +0100319 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100320 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100321 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100322 */
323
Hanno Becker23344b52017-08-23 07:43:27 +0100324#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd100e1472020-01-29 13:13:04 -0500325 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100326 {
327 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
328 &ctx->DP, &ctx->DQ, &ctx->QP );
329 if( ret != 0 )
330 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
331 }
Hanno Becker23344b52017-08-23 07:43:27 +0100332#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100333
334 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100335 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100336 */
337
Hanno Beckerebd2c022017-10-12 10:54:53 +0100338 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100339}
340
Hanno Becker617c1ae2017-08-23 14:11:24 +0100341int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
342 unsigned char *N, size_t N_len,
343 unsigned char *P, size_t P_len,
344 unsigned char *Q, size_t Q_len,
345 unsigned char *D, size_t D_len,
346 unsigned char *E, size_t E_len )
347{
348 int ret = 0;
349
350 /* Check if key is private or public */
351 const int is_priv =
352 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
353 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
354 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
355 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
356 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
357
358 if( !is_priv )
359 {
360 /* If we're trying to export private parameters for a public key,
361 * something must be wrong. */
362 if( P != NULL || Q != NULL || D != NULL )
363 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
364
365 }
366
367 if( N != NULL )
368 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
369
370 if( P != NULL )
371 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
372
373 if( Q != NULL )
374 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
375
376 if( D != NULL )
377 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
378
379 if( E != NULL )
380 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100381
382cleanup:
383
384 return( ret );
385}
386
Hanno Becker617c1ae2017-08-23 14:11:24 +0100387int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
388 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
389 mbedtls_mpi *D, mbedtls_mpi *E )
390{
391 int ret;
392
393 /* Check if key is private or public */
394 int is_priv =
395 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
396 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
397 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
398 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
399 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
400
401 if( !is_priv )
402 {
403 /* If we're trying to export private parameters for a public key,
404 * something must be wrong. */
405 if( P != NULL || Q != NULL || D != NULL )
406 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
407
408 }
409
410 /* Export all requested core parameters. */
411
412 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
413 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
414 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
415 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
416 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
417 {
418 return( ret );
419 }
420
421 return( 0 );
422}
423
424/*
425 * Export CRT parameters
426 * This must also be implemented if CRT is not used, for being able to
427 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
428 * can be used in this case.
429 */
430int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
431 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
432{
433 int ret;
434
435 /* Check if key is private or public */
436 int is_priv =
437 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
438 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
439 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
440 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
441 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
442
443 if( !is_priv )
444 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
445
Hanno Beckerdc95c892017-08-23 06:57:02 +0100446#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100447 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100448 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
449 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
450 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
451 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100452 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100453 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100454#else
455 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
456 DP, DQ, QP ) ) != 0 )
457 {
458 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
459 }
460#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100461
462 return( 0 );
463}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100464
Paul Bakker5121ce52009-01-03 21:22:43 +0000465/*
466 * Initialize an RSA context
467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000470 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#if defined(MBEDTLS_THREADING_C)
477 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200478#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000479}
480
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100481/*
482 * Set padding for an existing RSA context
483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100485{
486 ctx->padding = padding;
487 ctx->hash_id = hash_id;
488}
489
Hanno Becker617c1ae2017-08-23 14:11:24 +0100490/*
491 * Get length in bytes of RSA modulus
492 */
493
494size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
495{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100496 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100497}
498
499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502/*
503 * Generate an RSA keypair
504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000506 int (*f_rng)(void *, unsigned char *, size_t),
507 void *p_rng,
508 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000509{
510 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100511 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Paul Bakker21eb2802010-08-16 11:10:02 +0000513 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Janos Follathef441782016-09-21 13:18:12 +0100516 if( nbits % 2 )
517 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
518
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100519 mbedtls_mpi_init( &H );
520 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522 /*
523 * find primes P and Q with Q < P so that:
524 * GCD( E, (P-1)*(Q-1) ) == 1
525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528 do
529 {
Janos Follath10c575b2016-02-23 14:42:48 +0000530 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100531 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
Janos Follathef441782016-09-21 13:18:12 +0100533 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100534 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 continue;
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200540 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 continue;
542
Janos Follathef441782016-09-21 13:18:12 +0100543 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100544 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100545
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100546 /* Temporarily replace P,Q by P-1, Q-1 */
547 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
548 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
549 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100554 /* Restore P,Q */
555 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
556 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
557
558 ctx->len = mbedtls_mpi_size( &ctx->N );
559
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 /*
561 * D = E^-1 mod ((P-1)*(Q-1))
562 * DP = D mod (P - 1)
563 * DQ = D mod (Q - 1)
564 * QP = Q^-1 mod P
565 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100567 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
568
569#if !defined(MBEDTLS_RSA_NO_CRT)
570 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
571 &ctx->DP, &ctx->DQ, &ctx->QP ) );
572#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Hanno Becker83aad1f2017-08-23 06:45:10 +0100574 /* Double-check */
575 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577cleanup:
578
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100579 mbedtls_mpi_free( &H );
580 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582 if( ret != 0 )
583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_rsa_free( ctx );
585 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 }
587
Paul Bakker48377d92013-08-30 12:06:24 +0200588 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589}
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
593/*
594 * Check a public RSA key
595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000597{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100598 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000600
Hanno Becker3a760a12018-01-05 08:14:49 +0000601 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100604 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
Hanno Becker705fc682017-10-10 17:57:02 +0100606 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
607 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100611 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
613 return( 0 );
614}
615
616/*
Hanno Becker705fc682017-10-10 17:57:02 +0100617 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000620{
Hanno Becker705fc682017-10-10 17:57:02 +0100621 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100622 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 {
Hanno Becker98838b02017-10-02 13:16:10 +0100624 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 }
Paul Bakker48377d92013-08-30 12:06:24 +0200626
Hanno Becker98838b02017-10-02 13:16:10 +0100627 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100628 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100630 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000632
Hanno Beckerb269a852017-08-25 08:03:21 +0100633#if !defined(MBEDTLS_RSA_NO_CRT)
634 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
635 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
636 {
637 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
638 }
639#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000640
641 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642}
643
644/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100645 * Check if contexts holding a public and private key match
646 */
Hanno Becker98838b02017-10-02 13:16:10 +0100647int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
648 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100649{
Hanno Becker98838b02017-10-02 13:16:10 +0100650 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100654 }
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
657 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100660 }
661
662 return( 0 );
663}
664
665/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 * Do an RSA public key operation
667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000669 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 unsigned char *output )
671{
Paul Bakker23986e52011-04-24 08:57:21 +0000672 int ret;
673 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
Hanno Beckerebd2c022017-10-12 10:54:53 +0100676 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100677 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000680
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200681#if defined(MBEDTLS_THREADING_C)
682 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
683 return( ret );
684#endif
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200690 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
691 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000692 }
693
694 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
696 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000697
698cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200700 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
701 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100702#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
706 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
709 return( 0 );
710}
711
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200712/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200713 * Generate or update blinding values, see section 10 of:
714 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200715 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200716 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200717 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200718static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200719 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
720{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200721 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200722
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200723 if( ctx->Vf.p != NULL )
724 {
725 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
727 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
728 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
729 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200730
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200731 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200732 }
733
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200734 /* Unblinding value: Vf = random number, invertible mod N */
735 do {
736 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
740 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
741 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200742
743 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
745 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 +0200746
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200747
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200748cleanup:
749 return( ret );
750}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200751
Paul Bakker5121ce52009-01-03 21:22:43 +0000752/*
Janos Follathe81102e2017-03-22 13:38:28 +0000753 * Exponent blinding supposed to prevent side-channel attacks using multiple
754 * traces of measurements to recover the RSA key. The more collisions are there,
755 * the more bits of the key can be recovered. See [3].
756 *
757 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
758 * observations on avarage.
759 *
760 * For example with 28 byte blinding to achieve 2 collisions the adversary has
761 * to make 2^112 observations on avarage.
762 *
763 * (With the currently (as of 2017 April) known best algorithms breaking 2048
764 * bit RSA requires approximately as much time as trying out 2^112 random keys.
765 * Thus in this sense with 28 byte blinding the security is not reduced by
766 * side-channel attacks like the one in [3])
767 *
768 * This countermeasure does not help if the key recovery is possible with a
769 * single trace.
770 */
771#define RSA_EXPONENT_BLINDING 28
772
773/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 * Do an RSA private key operation
775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200777 int (*f_rng)(void *, unsigned char *, size_t),
778 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000779 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000780 unsigned char *output )
781{
Paul Bakker23986e52011-04-24 08:57:21 +0000782 int ret;
783 size_t olen;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000784
785 /* Temporary holding the result */
786 mbedtls_mpi T;
787
788 /* Temporaries holding P-1, Q-1 and the
789 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000790 mbedtls_mpi P1, Q1, R;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000791
792#if !defined(MBEDTLS_RSA_NO_CRT)
793 /* Temporaries holding the results mod p resp. mod q. */
794 mbedtls_mpi TP, TQ;
795
796 /* Temporaries holding the blinded exponents for
797 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000798 mbedtls_mpi DP_blind, DQ_blind;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000799
800 /* Pointers to actual exponents to be used - either the unblinded
801 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000802 mbedtls_mpi *DP = &ctx->DP;
803 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000804#else
805 /* Temporary holding the blinded exponent (if used). */
806 mbedtls_mpi D_blind;
807
808 /* Pointer to actual exponent to be used - either the unblinded
809 * or the blinded one, depending on the presence of a PRNG. */
810 mbedtls_mpi *D = &ctx->D;
811#endif /* MBEDTLS_RSA_NO_CRT */
812
813 /* Temporaries holding the initial input and the double
814 * checked result; should be the same in the end. */
815 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000816
Hanno Beckerebd2c022017-10-12 10:54:53 +0100817 if( rsa_check_context( ctx, 1 /* private key checks */,
818 f_rng != NULL /* blinding y/n */ ) != 0 )
819 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100820 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100821 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100822
Hanno Beckera5fa0792018-03-09 10:42:23 +0000823#if defined(MBEDTLS_THREADING_C)
824 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
825 return( ret );
826#endif
827
828 /* MPI Initialization */
829 mbedtls_mpi_init( &T );
830
831 mbedtls_mpi_init( &P1 );
832 mbedtls_mpi_init( &Q1 );
833 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000834
Janos Follathf9203b42017-03-22 15:13:15 +0000835 if( f_rng != NULL )
836 {
Janos Follathe81102e2017-03-22 13:38:28 +0000837#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000838 mbedtls_mpi_init( &D_blind );
839#else
840 mbedtls_mpi_init( &DP_blind );
841 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000842#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000843 }
Janos Follathe81102e2017-03-22 13:38:28 +0000844
Hanno Beckera5fa0792018-03-09 10:42:23 +0000845#if !defined(MBEDTLS_RSA_NO_CRT)
846 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200847#endif
848
Hanno Beckera5fa0792018-03-09 10:42:23 +0000849 mbedtls_mpi_init( &I );
850 mbedtls_mpi_init( &C );
851
852 /* End of MPI initialization */
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
855 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200857 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
858 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000859 }
860
Hanno Beckera5fa0792018-03-09 10:42:23 +0000861 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
862
Paul Bakkerf451bac2013-08-30 15:37:02 +0200863 if( f_rng != NULL )
864 {
865 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200866 * Blinding
867 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200868 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200869 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
870 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000872
Janos Follathe81102e2017-03-22 13:38:28 +0000873 /*
874 * Exponent blinding
875 */
876 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
877 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
878
Janos Follathf9203b42017-03-22 15:13:15 +0000879#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000880 /*
881 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
882 */
883 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
884 f_rng, p_rng ) );
885 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
886 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
887 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
888
889 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000890#else
891 /*
892 * DP_blind = ( P - 1 ) * R + DP
893 */
894 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
895 f_rng, p_rng ) );
896 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
897 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
898 &ctx->DP ) );
899
900 DP = &DP_blind;
901
902 /*
903 * DQ_blind = ( Q - 1 ) * R + DQ
904 */
905 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
906 f_rng, p_rng ) );
907 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
908 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
909 &ctx->DQ ) );
910
911 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000912#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200913 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000916 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100917#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200918 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000919 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 *
Hanno Beckera5fa0792018-03-09 10:42:23 +0000921 * TP = input ^ dP mod P
922 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000923 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000924
925 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
926 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
928 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000929 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000931 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
932 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
933 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
935 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000936 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000937 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000938 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
939 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200941
Paul Bakkerf451bac2013-08-30 15:37:02 +0200942 if( f_rng != NULL )
943 {
944 /*
945 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200946 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200947 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200948 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200950 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Hanno Beckera5fa0792018-03-09 10:42:23 +0000952 /* Verify the result to prevent glitching attacks. */
953 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
954 &ctx->N, &ctx->RN ) );
955 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
956 {
957 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
958 goto cleanup;
959 }
960
Paul Bakker5121ce52009-01-03 21:22:43 +0000961 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
964cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200966 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
967 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200968#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200969
Hanno Beckera5fa0792018-03-09 10:42:23 +0000970 mbedtls_mpi_free( &P1 );
971 mbedtls_mpi_free( &Q1 );
972 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000973
974 if( f_rng != NULL )
975 {
Janos Follathe81102e2017-03-22 13:38:28 +0000976#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000977 mbedtls_mpi_free( &D_blind );
978#else
979 mbedtls_mpi_free( &DP_blind );
980 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000981#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000982 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
Hanno Beckera5fa0792018-03-09 10:42:23 +0000984 mbedtls_mpi_free( &T );
985
986#if !defined(MBEDTLS_RSA_NO_CRT)
987 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
988#endif
989
990 mbedtls_mpi_free( &C );
991 mbedtls_mpi_free( &I );
992
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
996 return( 0 );
997}
998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001000/**
1001 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1002 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001003 * \param dst buffer to mask
1004 * \param dlen length of destination buffer
1005 * \param src source of the mask generation
1006 * \param slen length of the source buffer
1007 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001008 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001009static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001011{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001013 unsigned char counter[4];
1014 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001015 unsigned int hlen;
1016 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001017 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001020 memset( counter, 0, 4 );
1021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001023
Simon Butcher02037452016-03-01 21:19:12 +00001024 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001025 p = dst;
1026
1027 while( dlen > 0 )
1028 {
1029 use_len = hlen;
1030 if( dlen < hlen )
1031 use_len = dlen;
1032
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001033 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1034 goto exit;
1035 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1036 goto exit;
1037 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1038 goto exit;
1039 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1040 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001041
1042 for( i = 0; i < use_len; ++i )
1043 *p++ ^= mask[i];
1044
1045 counter[3]++;
1046
1047 dlen -= use_len;
1048 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001049
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001050exit:
Gilles Peskine18ac7162017-05-05 19:24:06 +02001051 mbedtls_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001052
1053 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001054}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001058/*
1059 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001062 int (*f_rng)(void *, unsigned char *, size_t),
1063 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001064 int mode,
1065 const unsigned char *label, size_t label_len,
1066 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001067 const unsigned char *input,
1068 unsigned char *output )
1069{
1070 size_t olen;
1071 int ret;
1072 unsigned char *p = output;
1073 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 const mbedtls_md_info_t *md_info;
1075 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1078 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001079
1080 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001084 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001086
1087 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001089
Simon Butcher02037452016-03-01 21:19:12 +00001090 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001091 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001093
1094 memset( output, 0, olen );
1095
1096 *p++ = 0;
1097
Simon Butcher02037452016-03-01 21:19:12 +00001098 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001099 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001101
1102 p += hlen;
1103
Simon Butcher02037452016-03-01 21:19:12 +00001104 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001105 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1106 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001107 p += hlen;
1108 p += olen - 2 * hlen - 2 - ilen;
1109 *p++ = 1;
1110 memcpy( p, input, ilen );
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001113 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001114 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001115
Simon Butcher02037452016-03-01 21:19:12 +00001116 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001117 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1118 &md_ctx ) ) != 0 )
1119 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001120
Simon Butcher02037452016-03-01 21:19:12 +00001121 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001122 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1123 &md_ctx ) ) != 0 )
1124 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001125
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001126exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001128
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001129 if( ret != 0 )
1130 return( ret );
1131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 return( ( mode == MBEDTLS_RSA_PUBLIC )
1133 ? mbedtls_rsa_public( ctx, output, output )
1134 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001139/*
1140 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001143 int (*f_rng)(void *, unsigned char *, size_t),
1144 void *p_rng,
1145 int mode, size_t ilen,
1146 const unsigned char *input,
1147 unsigned char *output )
1148{
1149 size_t nb_pad, olen;
1150 int ret;
1151 unsigned char *p = output;
1152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1154 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001155
Janos Follath1ed9f992016-03-18 11:45:44 +00001156 // We don't check p_rng because it won't be dereferenced here
1157 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001159
1160 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001161
Simon Butcher02037452016-03-01 21:19:12 +00001162 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001163 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001165
1166 nb_pad = olen - 3 - ilen;
1167
1168 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001172
1173 while( nb_pad-- > 0 )
1174 {
1175 int rng_dl = 100;
1176
1177 do {
1178 ret = f_rng( p_rng, p, 1 );
1179 } while( *p == 0 && --rng_dl && ret == 0 );
1180
Simon Butcher02037452016-03-01 21:19:12 +00001181 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001182 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001184
1185 p++;
1186 }
1187 }
1188 else
1189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001191
1192 while( nb_pad-- > 0 )
1193 *p++ = 0xFF;
1194 }
1195
1196 *p++ = 0;
1197 memcpy( p, input, ilen );
1198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 return( ( mode == MBEDTLS_RSA_PUBLIC )
1200 ? mbedtls_rsa_public( ctx, output, output )
1201 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001204
Paul Bakker5121ce52009-01-03 21:22:43 +00001205/*
1206 * Add the message padding, then do an RSA operation
1207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001209 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001210 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001211 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001212 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 unsigned char *output )
1214{
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 switch( ctx->padding )
1216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_PKCS1_V15)
1218 case MBEDTLS_RSA_PKCS_V15:
1219 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001220 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001221#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#if defined(MBEDTLS_PKCS1_V21)
1224 case MBEDTLS_RSA_PKCS_V21:
1225 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001226 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001227#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001228
1229 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001232}
1233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001235/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001236 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001239 int (*f_rng)(void *, unsigned char *, size_t),
1240 void *p_rng,
1241 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001242 const unsigned char *label, size_t label_len,
1243 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001244 const unsigned char *input,
1245 unsigned char *output,
1246 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001247{
Paul Bakker23986e52011-04-24 08:57:21 +00001248 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001249 size_t ilen, i, pad_len;
1250 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1252 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001253 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 const mbedtls_md_info_t *md_info;
1255 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001256
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001257 /*
1258 * Parameters sanity checks
1259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1261 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
1263 ilen = ctx->len;
1264
Paul Bakker27fdf462011-06-09 13:55:13 +00001265 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001269 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001271
Janos Follathc17cda12016-02-11 11:08:18 +00001272 hlen = mbedtls_md_get_size( md_info );
1273
1274 // checking for integer underflow
1275 if( 2 * hlen + 2 > ilen )
1276 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1277
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001278 /*
1279 * RSA operation
1280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1282 ? mbedtls_rsa_public( ctx, input, buf )
1283 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
1285 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001286 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001288 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001289 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001292 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1293 {
1294 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001295 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001296 }
1297
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001298 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001299 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1300 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001301 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001302 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1303 &md_ctx ) ) != 0 )
1304 {
1305 mbedtls_md_free( &md_ctx );
1306 goto cleanup;
1307 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001310
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001311 /* Generate lHash */
1312 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1313 goto cleanup;
1314
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001315 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001316 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001317 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001319 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001321 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001322
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001323 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001324
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001325 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001326 for( i = 0; i < hlen; i++ )
1327 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001328
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001329 /* Get zero-padding len, but always read till end of buffer
1330 * (minus one, for the 01 byte) */
1331 pad_len = 0;
1332 pad_done = 0;
1333 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1334 {
1335 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001336 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001337 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001338
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001339 p += pad_len;
1340 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001341
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001342 /*
1343 * The only information "leaked" is whether the padding was correct or not
1344 * (eg, no data is copied if it was not correct). This meets the
1345 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1346 * the different error conditions.
1347 */
1348 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001349 {
1350 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1351 goto cleanup;
1352 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001353
Paul Bakker66d5d072014-06-17 16:39:18 +02001354 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001355 {
1356 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1357 goto cleanup;
1358 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001359
1360 *olen = ilen - (p - buf);
1361 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001362 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001363
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001364cleanup:
1365 mbedtls_zeroize( buf, sizeof( buf ) );
1366 mbedtls_zeroize( lhash, sizeof( lhash ) );
1367
1368 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001373/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1374 *
1375 * \param value The value to analyze.
Gilles Peskineb4739162018-10-04 18:32:29 +02001376 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001377 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001378static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001379{
1380 /* MSVC has a warning about unary minus on unsigned, but this is
1381 * well-defined and precisely what we want to do here */
1382#if defined(_MSC_VER)
1383#pragma warning( push )
1384#pragma warning( disable : 4146 )
1385#endif
1386 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1387#if defined(_MSC_VER)
1388#pragma warning( pop )
1389#endif
1390}
1391
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001392/** Check whether a size is out of bounds, without branches.
1393 *
1394 * This is equivalent to `size > max`, but is likely to be compiled to
1395 * to code using bitwise operation rather than a branch.
1396 *
1397 * \param size Size to check.
1398 * \param max Maximum desired value for \p size.
1399 * \return \c 0 if `size <= max`.
1400 * \return \c 1 if `size > max`.
1401 */
1402static unsigned size_greater_than( size_t size, size_t max )
1403{
1404 /* Return the sign bit (1 for negative) of (max - size). */
1405 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1406}
1407
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001408/** Choose between two integer values, without branches.
1409 *
Gilles Peskineb4739162018-10-04 18:32:29 +02001410 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1411 * to code using bitwise operation rather than a branch.
1412 *
1413 * \param cond Condition to test.
1414 * \param if1 Value to use if \p cond is nonzero.
1415 * \param if0 Value to use if \p cond is zero.
1416 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001417 */
Gilles Peskineb4739162018-10-04 18:32:29 +02001418static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001419{
Gilles Peskineb4739162018-10-04 18:32:29 +02001420 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001421 return( ( mask & if1 ) | (~mask & if0 ) );
1422}
1423
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001424/** Shift some data towards the left inside a buffer without leaking
1425 * the length of the data through side channels.
1426 *
1427 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1428 * ```
1429 * memmove(start, start + offset, total - offset);
1430 * memset(start + offset, 0, total - offset);
1431 * ```
1432 * but it strives to use a memory access pattern (and thus total timing)
1433 * that does not depend on \p offset. This timing independence comes at
1434 * the expense of performance.
1435 *
1436 * \param start Pointer to the start of the buffer.
1437 * \param total Total size of the buffer.
1438 * \param offset Offset from which to copy \p total - \p offset bytes.
1439 */
1440static void mem_move_to_left( void *start,
1441 size_t total,
1442 size_t offset )
1443{
1444 volatile unsigned char *buf = start;
1445 size_t i, n;
1446 if( total == 0 )
1447 return;
1448 for( i = 0; i < total; i++ )
1449 {
1450 unsigned no_op = size_greater_than( total - offset, i );
1451 /* The first `total - offset` passes are a no-op. The last
1452 * `offset` passes shift the data one byte to the left and
1453 * zero out the last byte. */
1454 for( n = 0; n < total - 1; n++ )
Gilles Peskine66a28e92018-10-12 19:15:34 +02001455 {
1456 unsigned char current = buf[n];
1457 unsigned char next = buf[n+1];
1458 buf[n] = if_int( no_op, current, next );
1459 }
Gilles Peskinea04f8bb2018-10-04 21:18:30 +02001460 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1461 }
1462}
1463
Paul Bakkerb3869132013-02-28 17:21:01 +01001464/*
1465 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001468 int (*f_rng)(void *, unsigned char *, size_t),
1469 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001470 int mode, size_t *olen,
1471 const unsigned char *input,
1472 unsigned char *output,
Gilles Peskinecd500f32018-10-02 22:43:06 +02001473 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001474{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001475 int ret;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001476 size_t ilen = ctx->len;
Gilles Peskinecd500f32018-10-02 22:43:06 +02001477 size_t i;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001478 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1479 ilen - 11 :
1480 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001482 /* The following variables take sensitive values: their value must
1483 * not leak into the observable behavior of the function other than
1484 * the designated outputs (output, olen, return value). Otherwise
1485 * this would open the execution of the function to
1486 * side-channel-based variants of the Bleichenbacher padding oracle
1487 * attack. Potential side channels include overall timing, memory
1488 * access patterns (especially visible to an adversary who has access
1489 * to a shared memory cache), and branches (especially visible to
1490 * an adversary who has access to a shared code cache or to a shared
1491 * branch predictor). */
1492 size_t pad_count = 0;
1493 unsigned bad = 0;
1494 unsigned char pad_done = 0;
1495 size_t plaintext_size = 0;
1496 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1499 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001500
Paul Bakkerb3869132013-02-28 17:21:01 +01001501 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1505 ? mbedtls_rsa_public( ctx, input, buf )
1506 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001507
1508 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001509 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001510
Gilles Peskine0b330f72018-10-05 15:06:12 +02001511 /* Check and get padding length in constant time and constant
1512 * memory trace. The first byte must be 0. */
1513 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001516 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001517 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1518 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001519 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001520
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001521 /* Read the whole buffer. Set pad_done to nonzero if we find
1522 * the 0x00 byte and remember the padding length in pad_count. */
1523 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001524 {
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001525 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001526 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001527 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001528 }
1529 else
1530 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001531 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1532 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001533 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001534
Gilles Peskine23d7cea2018-10-05 18:11:27 +02001535 /* Read the whole buffer. Set pad_done to nonzero if we find
1536 * the 0x00 byte and remember the padding length in pad_count.
1537 * If there's a non-0xff byte in the padding, the padding is bad. */
1538 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001539 {
Gilles Peskine0b330f72018-10-05 15:06:12 +02001540 pad_done |= if_int( buf[i], 0, 1 );
1541 pad_count += if_int( pad_done, 0, 1 );
1542 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 }
1545
Gilles Peskine0b330f72018-10-05 15:06:12 +02001546 /* If pad_done is still zero, there's no data, only unfinished padding. */
1547 bad |= if_int( pad_done, 0, 1 );
1548
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001549 /* There must be at least 8 bytes of padding. */
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001550 bad |= size_greater_than( 8, pad_count );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001551
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001552 /* If the padding is valid, set plaintext_size to the number of
1553 * remaining bytes after stripping the padding. If the padding
1554 * is invalid, avoid leaking this fact through the size of the
1555 * output: use the maximum message size that fits in the output
1556 * buffer. Do it without branches to avoid leaking the padding
1557 * validity through timing. RSA keys are small enough that all the
1558 * size_t values involved fit in unsigned int. */
Gilles Peskineb4739162018-10-04 18:32:29 +02001559 plaintext_size = if_int( bad,
1560 (unsigned) plaintext_max_size,
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001561 (unsigned) ( ilen - pad_count - 3 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001562
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001563 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskinecf1253e2018-10-04 21:24:21 +02001564 * buffer and to 1 otherwise. */
1565 output_too_large = size_greater_than( plaintext_size,
1566 plaintext_max_size );
Paul Bakker060c5682009-01-12 21:48:39 +00001567
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001568 /* Set ret without branches to avoid timing attacks. Return:
1569 * - INVALID_PADDING if the padding is bad (bad != 0).
1570 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1571 * plaintext does not fit in the output buffer.
1572 * - 0 if the padding is correct. */
Gilles Peskine84a21d52018-10-12 19:19:12 +02001573 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1574 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1575 0 ) );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001576
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001577 /* If the padding is bad or the plaintext is too large, zero the
1578 * data that we're about to copy to the output buffer.
1579 * We need to copy the same amount of data
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001580 * from the same buffer whether the padding is good or not to
1581 * avoid leaking the padding validity through overall timing or
1582 * through memory or cache access patterns. */
Gilles Peskine0b330f72018-10-05 15:06:12 +02001583 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001584 for( i = 11; i < ilen; i++ )
Gilles Peskine0b330f72018-10-05 15:06:12 +02001585 buf[i] &= ~bad;
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001586
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001587 /* If the plaintext is too large, truncate it to the buffer size.
1588 * Copy anyway to avoid revealing the length through timing, because
1589 * revealing the length is as bad as revealing the padding validity
1590 * for a Bleichenbacher attack. */
1591 plaintext_size = if_int( output_too_large,
1592 (unsigned) plaintext_max_size,
1593 (unsigned) plaintext_size );
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001594
Gilles Peskine087544b2018-10-04 22:45:13 +02001595 /* Move the plaintext to the leftmost position where it can start in
1596 * the working buffer, i.e. make it start plaintext_max_size from
1597 * the end of the buffer. Do this with a memory access trace that
1598 * does not depend on the plaintext size. After this move, the
1599 * starting location of the plaintext is no longer sensitive
1600 * information. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001601 mem_move_to_left( buf + ilen - plaintext_max_size,
1602 plaintext_max_size,
Gilles Peskine087544b2018-10-04 22:45:13 +02001603 plaintext_max_size - plaintext_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001604
Gilles Peskine087544b2018-10-04 22:45:13 +02001605 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001606 * into the output buffer. */
Gilles Peskine03fb3e32018-10-05 14:50:21 +02001607 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskinef9dd29e2018-10-04 19:13:43 +02001608
1609 /* Report the amount of data we copied to the output buffer. In case
1610 * of errors (bad padding or output too large), the value of *olen
1611 * when this function returns is not specified. Making it equivalent
1612 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinedabe87c2018-10-02 22:44:41 +02001613 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001615cleanup:
1616 mbedtls_zeroize( buf, sizeof( buf ) );
1617
1618 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001619}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001621
1622/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001623 * Do an RSA operation, then remove the message padding
1624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001626 int (*f_rng)(void *, unsigned char *, size_t),
1627 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001628 int mode, size_t *olen,
1629 const unsigned char *input,
1630 unsigned char *output,
1631 size_t output_max_len)
1632{
1633 switch( ctx->padding )
1634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_PKCS1_V15)
1636 case MBEDTLS_RSA_PKCS_V15:
1637 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001638 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001639#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#if defined(MBEDTLS_PKCS1_V21)
1642 case MBEDTLS_RSA_PKCS_V21:
1643 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001644 olen, input, output,
1645 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001646#endif
1647
1648 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001650 }
1651}
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001654/*
1655 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001658 int (*f_rng)(void *, unsigned char *, size_t),
1659 void *p_rng,
1660 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001662 unsigned int hashlen,
1663 const unsigned char *hash,
1664 unsigned char *sig )
1665{
1666 size_t olen;
1667 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001669 unsigned int slen, hlen, offset = 0;
1670 int ret;
1671 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 const mbedtls_md_info_t *md_info;
1673 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1676 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001677
1678 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001680
1681 olen = ctx->len;
1682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001684 {
Simon Butcher02037452016-03-01 21:19:12 +00001685 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001687 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001691 }
1692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001694 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001698 slen = hlen;
1699
1700 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001702
1703 memset( sig, 0, olen );
1704
Simon Butcher02037452016-03-01 21:19:12 +00001705 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001706 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001708
Simon Butcher02037452016-03-01 21:19:12 +00001709 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001710 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001711 p += olen - hlen * 2 - 2;
1712 *p++ = 0x01;
1713 memcpy( p, salt, slen );
1714 p += slen;
1715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001717 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001718 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001719
Simon Butcher02037452016-03-01 21:19:12 +00001720 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001721 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1722 goto exit;
1723 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1724 goto exit;
1725 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1726 goto exit;
1727 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1728 goto exit;
1729 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1730 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001731
Simon Butcher02037452016-03-01 21:19:12 +00001732 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001733 if( msb % 8 == 0 )
1734 offset = 1;
1735
Simon Butcher02037452016-03-01 21:19:12 +00001736 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001737 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1738 &md_ctx ) ) != 0 )
1739 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001740
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001741 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001742 sig[0] &= 0xFF >> ( olen * 8 - msb );
1743
1744 p += hlen;
1745 *p++ = 0xBC;
1746
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001747 mbedtls_zeroize( salt, sizeof( salt ) );
1748
1749exit:
1750 mbedtls_md_free( &md_ctx );
1751
1752 if( ret != 0 )
1753 return( ret );
1754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 return( ( mode == MBEDTLS_RSA_PUBLIC )
1756 ? mbedtls_rsa_public( ctx, sig, sig )
1757 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001762/*
1763 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1764 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001765
1766/* Construct a PKCS v1.5 encoding of a hashed message
1767 *
1768 * This is used both for signature generation and verification.
1769 *
1770 * Parameters:
1771 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001772 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001773 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001774 * - hash: Buffer containing the hashed message or the raw data.
1775 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001776 * - dst: Buffer to hold the encoded message.
1777 *
1778 * Assumptions:
1779 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1780 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001781 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001782 *
1783 */
1784static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1785 unsigned int hashlen,
1786 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001787 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001788 unsigned char *dst )
1789{
1790 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001791 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001792 unsigned char *p = dst;
1793 const char *oid = NULL;
1794
1795 /* Are we signing hashed or raw data? */
1796 if( md_alg != MBEDTLS_MD_NONE )
1797 {
1798 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1799 if( md_info == NULL )
1800 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1801
1802 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1803 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1804
1805 hashlen = mbedtls_md_get_size( md_info );
1806
1807 /* Double-check that 8 + hashlen + oid_size can be used as a
1808 * 1-byte ASN.1 length encoding and that there's no overflow. */
1809 if( 8 + hashlen + oid_size >= 0x80 ||
1810 10 + hashlen < hashlen ||
1811 10 + hashlen + oid_size < 10 + hashlen )
1812 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1813
1814 /*
1815 * Static bounds check:
1816 * - Need 10 bytes for five tag-length pairs.
1817 * (Insist on 1-byte length encodings to protect against variants of
1818 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1819 * - Need hashlen bytes for hash
1820 * - Need oid_size bytes for hash alg OID.
1821 */
1822 if( nb_pad < 10 + hashlen + oid_size )
1823 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1824 nb_pad -= 10 + hashlen + oid_size;
1825 }
1826 else
1827 {
1828 if( nb_pad < hashlen )
1829 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1830
1831 nb_pad -= hashlen;
1832 }
1833
Hanno Becker2b2f8982017-09-27 17:10:03 +01001834 /* Need space for signature header and padding delimiter (3 bytes),
1835 * and 8 bytes for the minimal padding */
1836 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001837 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1838 nb_pad -= 3;
1839
1840 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001841 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001842
1843 /* Write signature header and padding */
1844 *p++ = 0;
1845 *p++ = MBEDTLS_RSA_SIGN;
1846 memset( p, 0xFF, nb_pad );
1847 p += nb_pad;
1848 *p++ = 0;
1849
1850 /* Are we signing raw data? */
1851 if( md_alg == MBEDTLS_MD_NONE )
1852 {
1853 memcpy( p, hash, hashlen );
1854 return( 0 );
1855 }
1856
1857 /* Signing hashed data, add corresponding ASN.1 structure
1858 *
1859 * DigestInfo ::= SEQUENCE {
1860 * digestAlgorithm DigestAlgorithmIdentifier,
1861 * digest Digest }
1862 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1863 * Digest ::= OCTET STRING
1864 *
1865 * Schematic:
1866 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1867 * TAG-NULL + LEN [ NULL ] ]
1868 * TAG-OCTET + LEN [ HASH ] ]
1869 */
1870 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001871 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001872 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001873 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001874 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001875 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001876 memcpy( p, oid, oid_size );
1877 p += oid_size;
1878 *p++ = MBEDTLS_ASN1_NULL;
1879 *p++ = 0x00;
1880 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001881 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001882 memcpy( p, hash, hashlen );
1883 p += hashlen;
1884
1885 /* Just a sanity-check, should be automatic
1886 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001887 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001888 {
Hanno Beckere58d38c2017-09-27 17:09:00 +01001889 mbedtls_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001890 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1891 }
1892
1893 return( 0 );
1894}
1895
Paul Bakkerb3869132013-02-28 17:21:01 +01001896/*
1897 * Do an RSA operation to sign the message digest
1898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001900 int (*f_rng)(void *, unsigned char *, size_t),
1901 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001902 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001904 unsigned int hashlen,
1905 const unsigned char *hash,
1906 unsigned char *sig )
1907{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001908 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001909 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1912 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001913
Hanno Beckerfdf38032017-09-06 12:35:55 +01001914 /*
1915 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1916 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001917
Hanno Beckerfdf38032017-09-06 12:35:55 +01001918 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1919 ctx->len, sig ) ) != 0 )
1920 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001921
1922 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001923 * Call respective RSA primitive
1924 */
1925
1926 if( mode == MBEDTLS_RSA_PUBLIC )
1927 {
1928 /* Skip verification on a public key operation */
1929 return( mbedtls_rsa_public( ctx, sig, sig ) );
1930 }
1931
1932 /* Private key operation
1933 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001934 * In order to prevent Lenstra's attack, make the signature in a
1935 * temporary buffer and check it before returning it.
1936 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001937
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001938 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001939 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001940 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1941
Hanno Beckerfdf38032017-09-06 12:35:55 +01001942 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001943 if( verif == NULL )
1944 {
1945 mbedtls_free( sig_try );
1946 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1947 }
1948
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001949 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1950 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1951
Hanno Becker171a8f12017-09-06 12:32:16 +01001952 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001953 {
1954 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1955 goto cleanup;
1956 }
1957
1958 memcpy( sig, sig_try, ctx->len );
1959
1960cleanup:
1961 mbedtls_free( sig_try );
1962 mbedtls_free( verif );
1963
1964 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001965}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001967
1968/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 * Do an RSA operation to sign the message digest
1970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001972 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001973 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001976 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001977 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 unsigned char *sig )
1979{
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 switch( ctx->padding )
1981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#if defined(MBEDTLS_PKCS1_V15)
1983 case MBEDTLS_RSA_PKCS_V15:
1984 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001985 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001986#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988#if defined(MBEDTLS_PKCS1_V21)
1989 case MBEDTLS_RSA_PKCS_V21:
1990 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001991 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001992#endif
1993
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001996 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001997}
1998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002000/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002001 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002004 int (*f_rng)(void *, unsigned char *, size_t),
2005 void *p_rng,
2006 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002008 unsigned int hashlen,
2009 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002011 int expected_salt_len,
2012 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002013{
Paul Bakker23986e52011-04-24 08:57:21 +00002014 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002015 size_t siglen;
2016 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002017 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002019 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002020 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002021 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 const mbedtls_md_info_t *md_info;
2023 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002024 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2027 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002028
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 siglen = ctx->len;
2030
Paul Bakker27fdf462011-06-09 13:55:13 +00002031 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2035 ? mbedtls_rsa_public( ctx, sig, buf )
2036 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002037
2038 if( ret != 0 )
2039 return( ret );
2040
2041 p = buf;
2042
Paul Bakkerb3869132013-02-28 17:21:01 +01002043 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 {
Simon Butcher02037452016-03-01 21:19:12 +00002048 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002050 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002054 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002057 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002061
Paul Bakkerb3869132013-02-28 17:21:01 +01002062 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002063
Simon Butcher02037452016-03-01 21:19:12 +00002064 /*
2065 * Note: EMSA-PSS verification is over the length of N - 1 bits
2066 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002067 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002068
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002069 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2070 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2071
Simon Butcher02037452016-03-01 21:19:12 +00002072 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002073 if( msb % 8 == 0 )
2074 {
2075 p++;
2076 siglen -= 1;
2077 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002078
Gilles Peskine139108a2017-10-18 19:03:42 +02002079 if( siglen < hlen + 2 )
2080 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2081 hash_start = p + siglen - hlen - 1;
2082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002084 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002085 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002086
Jaeden Amero66954e12018-01-25 16:05:54 +00002087 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2088 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002089 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002090
Paul Bakkerb3869132013-02-28 17:21:01 +01002091 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002092
Gilles Peskine6a54b022017-10-17 19:02:13 +02002093 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002094 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002095
Gilles Peskine91048a32017-10-19 17:46:14 +02002096 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002097 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002098 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2099 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002100 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002101
Gilles Peskine6a54b022017-10-17 19:02:13 +02002102 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002105 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002106 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002107 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2108 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002109 }
2110
Simon Butcher02037452016-03-01 21:19:12 +00002111 /*
2112 * Generate H = Hash( M' )
2113 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002114 ret = mbedtls_md_starts( &md_ctx );
2115 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002116 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002117 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2118 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002119 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002120 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2121 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002122 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002123 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2124 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002125 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002126 ret = mbedtls_md_finish( &md_ctx, result );
2127 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002128 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002129
Jaeden Amero66954e12018-01-25 16:05:54 +00002130 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002131 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002132 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002133 goto exit;
2134 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002135
2136exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002138
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002139 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002140}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002141
2142/*
2143 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002146 int (*f_rng)(void *, unsigned char *, size_t),
2147 void *p_rng,
2148 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002150 unsigned int hashlen,
2151 const unsigned char *hash,
2152 const unsigned char *sig )
2153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2155 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002156 : md_alg;
2157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002159 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002161 sig ) );
2162
2163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002167/*
2168 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002171 int (*f_rng)(void *, unsigned char *, size_t),
2172 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002173 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002175 unsigned int hashlen,
2176 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002177 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002178{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002179 int ret = 0;
2180 const size_t sig_len = ctx->len;
2181 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002185
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002186 /*
2187 * Prepare expected PKCS1 v1.5 encoding of hash.
2188 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002189
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002190 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2191 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2192 {
2193 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2194 goto cleanup;
2195 }
2196
2197 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2198 encoded_expected ) ) != 0 )
2199 goto cleanup;
2200
2201 /*
2202 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2203 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002206 ? mbedtls_rsa_public( ctx, sig, encoded )
2207 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002208 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002209 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002210
Simon Butcher02037452016-03-01 21:19:12 +00002211 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002212 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002213 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002214
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002215 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2216 sig_len ) ) != 0 )
2217 {
2218 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2219 goto cleanup;
2220 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002221
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002222cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002223
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002224 if( encoded != NULL )
2225 {
2226 mbedtls_zeroize( encoded, sig_len );
2227 mbedtls_free( encoded );
2228 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002229
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002230 if( encoded_expected != NULL )
2231 {
2232 mbedtls_zeroize( encoded_expected, sig_len );
2233 mbedtls_free( encoded_expected );
2234 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002235
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002236 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002237}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002239
2240/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002241 * Do an RSA operation and check the message digest
2242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002244 int (*f_rng)(void *, unsigned char *, size_t),
2245 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002246 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002248 unsigned int hashlen,
2249 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002250 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002251{
2252 switch( ctx->padding )
2253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254#if defined(MBEDTLS_PKCS1_V15)
2255 case MBEDTLS_RSA_PKCS_V15:
2256 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002257 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002258#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260#if defined(MBEDTLS_PKCS1_V21)
2261 case MBEDTLS_RSA_PKCS_V21:
2262 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002263 hashlen, hash, sig );
2264#endif
2265
2266 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002268 }
2269}
2270
2271/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002272 * Copy the components of an RSA key
2273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002275{
2276 int ret;
2277
2278 dst->ver = src->ver;
2279 dst->len = src->len;
2280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2282 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2285 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2286 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002287
2288#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2290 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2291 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2293 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002294#endif
2295
2296 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2299 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002300
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002301 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002302 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002303
2304cleanup:
2305 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002307
2308 return( ret );
2309}
2310
2311/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002312 * Free the components of an RSA key
2313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002315{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002317 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2318 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002320
Hanno Becker33c30a02017-08-23 07:00:22 +01002321#if !defined(MBEDTLS_RSA_NO_CRT)
2322 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2323 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2324 mbedtls_mpi_free( &ctx->DP );
2325#endif /* MBEDTLS_RSA_NO_CRT */
2326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#if defined(MBEDTLS_THREADING_C)
2328 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002329#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002330}
2331
Hanno Beckerab377312017-08-23 16:24:51 +01002332#endif /* !MBEDTLS_RSA_ALT */
2333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002335
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002336#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002337
2338/*
2339 * Example RSA-1024 keypair, for test purposes
2340 */
2341#define KEY_LEN 128
2342
2343#define RSA_N "9292758453063D803DD603D5E777D788" \
2344 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2345 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2346 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2347 "93A89813FBF3C4F8066D2D800F7C38A8" \
2348 "1AE31942917403FF4946B0A83D3D3E05" \
2349 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2350 "5E94BB77B07507233A0BC7BAC8F90F79"
2351
2352#define RSA_E "10001"
2353
2354#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2355 "66CA472BC44D253102F8B4A9D3BFA750" \
2356 "91386C0077937FE33FA3252D28855837" \
2357 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2358 "DF79C5CE07EE72C7F123142198164234" \
2359 "CABB724CF78B8173B9F880FC86322407" \
2360 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2361 "071513A1E85B5DFA031F21ECAE91A34D"
2362
2363#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2364 "2C01CAD19EA484A87EA4377637E75500" \
2365 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2366 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2367
2368#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2369 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2370 "910E4168387E3C30AA1E00C339A79508" \
2371 "8452DD96A9A5EA5D9DCA68DA636032AF"
2372
Paul Bakker5121ce52009-01-03 21:22:43 +00002373#define PT_LEN 24
2374#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2375 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002378static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002379{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002380#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002381 size_t i;
2382
Paul Bakker545570e2010-07-18 09:00:25 +00002383 if( rng_state != NULL )
2384 rng_state = NULL;
2385
Paul Bakkera3d195c2011-11-27 21:07:34 +00002386 for( i = 0; i < len; ++i )
2387 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002388#else
2389 if( rng_state != NULL )
2390 rng_state = NULL;
2391
2392 arc4random_buf( output, len );
2393#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002394
Paul Bakkera3d195c2011-11-27 21:07:34 +00002395 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002398
Paul Bakker5121ce52009-01-03 21:22:43 +00002399/*
2400 * Checkup routine
2401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002403{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002404 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002406 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 unsigned char rsa_plaintext[PT_LEN];
2409 unsigned char rsa_decrypted[PT_LEN];
2410 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002412 unsigned char sha1sum[20];
2413#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
Hanno Becker3a701162017-08-22 13:52:43 +01002415 mbedtls_mpi K;
2416
2417 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
Hanno Becker3a701162017-08-22 13:52:43 +01002420 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2421 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2422 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2423 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2424 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2425 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2426 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2427 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2428 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2429 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2430
Hanno Becker7f25f852017-10-10 16:56:22 +01002431 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
2433 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2437 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002438 {
2439 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Hanno Beckera5fa0792018-03-09 10:42:23 +00002442 ret = 1;
2443 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 }
2445
2446 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
2449 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2450
Hanno Becker98838b02017-10-02 13:16:10 +01002451 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2452 PT_LEN, rsa_plaintext,
2453 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 {
2455 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
Hanno Beckera5fa0792018-03-09 10:42:23 +00002458 ret = 1;
2459 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 }
2461
2462 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
Hanno Becker98838b02017-10-02 13:16:10 +01002465 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2466 &len, rsa_ciphertext, rsa_decrypted,
2467 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 {
2469 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
Hanno Beckera5fa0792018-03-09 10:42:23 +00002472 ret = 1;
2473 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 }
2475
2476 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2477 {
2478 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Hanno Beckera5fa0792018-03-09 10:42:23 +00002481 ret = 1;
2482 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 }
2484
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002485 if( verbose != 0 )
2486 mbedtls_printf( "passed\n" );
2487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002490 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002492 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002493 {
2494 if( verbose != 0 )
2495 mbedtls_printf( "failed\n" );
2496
2497 return( 1 );
2498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Hanno Becker98838b02017-10-02 13:16:10 +01002500 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2501 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2502 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 {
2504 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002506
Hanno Beckera5fa0792018-03-09 10:42:23 +00002507 ret = 1;
2508 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 }
2510
2511 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002513
Hanno Becker98838b02017-10-02 13:16:10 +01002514 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2515 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2516 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 {
2518 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
Hanno Beckera5fa0792018-03-09 10:42:23 +00002521 ret = 1;
2522 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 }
2524
2525 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002526 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002529 if( verbose != 0 )
2530 mbedtls_printf( "\n" );
2531
Paul Bakker3d8fb632014-04-17 12:42:41 +02002532cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002533 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 mbedtls_rsa_free( &rsa );
2535#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002536 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002538 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539}
2540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543#endif /* MBEDTLS_RSA_C */