blob: c9f7ba91b6b97ab8249a47fc0a1934d8a03b3cf5 [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
Hanno Becker171a8f12017-09-06 12:32:16 +010078/* constant-time buffer comparison */
79static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
80{
81 size_t i;
82 const unsigned char *A = (const unsigned char *) a;
83 const unsigned char *B = (const unsigned char *) b;
84 unsigned char diff = 0;
85
86 for( i = 0; i < n; i++ )
87 diff |= A[i] ^ B[i];
88
89 return( diff );
90}
91
Hanno Becker617c1ae2017-08-23 14:11:24 +010092int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
93 const mbedtls_mpi *N,
94 const mbedtls_mpi *P, const mbedtls_mpi *Q,
95 const mbedtls_mpi *D, const mbedtls_mpi *E )
96{
97 int ret;
98
99 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
100 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
101 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
102 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
103 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
104 {
105 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
106 }
107
108 if( N != NULL )
109 ctx->len = mbedtls_mpi_size( &ctx->N );
110
111 return( 0 );
112}
113
114int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100115 unsigned char const *N, size_t N_len,
116 unsigned char const *P, size_t P_len,
117 unsigned char const *Q, size_t Q_len,
118 unsigned char const *D, size_t D_len,
119 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100120{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000121 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100122
123 if( N != NULL )
124 {
125 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
126 ctx->len = mbedtls_mpi_size( &ctx->N );
127 }
128
129 if( P != NULL )
130 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
131
132 if( Q != NULL )
133 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
134
135 if( D != NULL )
136 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
137
138 if( E != NULL )
139 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
140
141cleanup:
142
143 if( ret != 0 )
144 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
145
146 return( 0 );
147}
148
Hanno Becker705fc682017-10-10 17:57:02 +0100149/*
150 * Checks whether the context fields are set in such a way
151 * that the RSA primitives will be able to execute without error.
152 * It does *not* make guarantees for consistency of the parameters.
153 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100154static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
155 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100156{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100157#if !defined(MBEDTLS_RSA_NO_CRT)
158 /* blinding_needed is only used for NO_CRT to decide whether
159 * P,Q need to be present or not. */
160 ((void) blinding_needed);
161#endif
162
Hanno Becker3a760a12018-01-05 08:14:49 +0000163 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
164 ctx->len > MBEDTLS_MPI_MAX_SIZE )
165 {
Hanno Becker705fc682017-10-10 17:57:02 +0100166 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000167 }
Hanno Becker705fc682017-10-10 17:57:02 +0100168
169 /*
170 * 1. Modular exponentiation needs positive, odd moduli.
171 */
172
173 /* Modular exponentiation wrt. N is always used for
174 * RSA public key operations. */
175 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
176 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
177 {
178 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
179 }
180
181#if !defined(MBEDTLS_RSA_NO_CRT)
182 /* Modular exponentiation for P and Q is only
183 * used for private key operations and if CRT
184 * is used. */
185 if( is_priv &&
186 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
187 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
188 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
189 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
190 {
191 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
192 }
193#endif /* !MBEDTLS_RSA_NO_CRT */
194
195 /*
196 * 2. Exponents must be positive
197 */
198
199 /* Always need E for public key operations */
200 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
201 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
202
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100203#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100204 /* For private key operations, use D or DP & DQ
205 * as (unblinded) exponents. */
206 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
208#else
209 if( is_priv &&
210 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
211 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
212 {
213 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
214 }
215#endif /* MBEDTLS_RSA_NO_CRT */
216
217 /* Blinding shouldn't make exponents negative either,
218 * so check that P, Q >= 1 if that hasn't yet been
219 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100220#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100221 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100222 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
223 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
224 {
225 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
226 }
227#endif
228
229 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100230 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100231#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100232 if( is_priv &&
233 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
234 {
235 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
236 }
237#endif
238
239 return( 0 );
240}
241
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100242int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100243{
244 int ret = 0;
245
Hanno Becker617c1ae2017-08-23 14:11:24 +0100246 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
247 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
248 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
249 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
250 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100251
Hanno Becker617c1ae2017-08-23 14:11:24 +0100252 /*
253 * Check whether provided parameters are enough
254 * to deduce all others. The following incomplete
255 * parameter sets for private keys are supported:
256 *
257 * (1) P, Q missing.
258 * (2) D and potentially N missing.
259 *
260 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100261
Hanno Becker2cca6f32017-09-29 11:46:40 +0100262 const int n_missing = have_P && have_Q && have_D && have_E;
263 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
264 const int d_missing = have_P && have_Q && !have_D && have_E;
265 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
266
267 /* These three alternatives are mutually exclusive */
268 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100269
Hanno Becker617c1ae2017-08-23 14:11:24 +0100270 if( !is_priv && !is_pub )
271 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
272
273 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100274 * Step 1: Deduce N if P, Q are provided.
275 */
276
277 if( !have_N && have_P && have_Q )
278 {
279 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
280 &ctx->Q ) ) != 0 )
281 {
282 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
283 }
284
285 ctx->len = mbedtls_mpi_size( &ctx->N );
286 }
287
288 /*
289 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100290 */
291
292 if( pq_missing )
293 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100294 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100295 &ctx->P, &ctx->Q );
296 if( ret != 0 )
297 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
298
299 }
300 else if( d_missing )
301 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100302 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
303 &ctx->Q,
304 &ctx->E,
305 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100306 {
307 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
308 }
309 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100310
Hanno Becker617c1ae2017-08-23 14:11:24 +0100311 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100312 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100313 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100314 */
315
Hanno Becker23344b52017-08-23 07:43:27 +0100316#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100317 if( is_priv )
318 {
319 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
320 &ctx->DP, &ctx->DQ, &ctx->QP );
321 if( ret != 0 )
322 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
323 }
Hanno Becker23344b52017-08-23 07:43:27 +0100324#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100325
326 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100327 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100328 */
329
Hanno Beckerebd2c022017-10-12 10:54:53 +0100330 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100331}
332
Hanno Becker617c1ae2017-08-23 14:11:24 +0100333int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
334 unsigned char *N, size_t N_len,
335 unsigned char *P, size_t P_len,
336 unsigned char *Q, size_t Q_len,
337 unsigned char *D, size_t D_len,
338 unsigned char *E, size_t E_len )
339{
340 int ret = 0;
341
342 /* Check if key is private or public */
343 const int is_priv =
344 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
345 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
346 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
347 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
348 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
349
350 if( !is_priv )
351 {
352 /* If we're trying to export private parameters for a public key,
353 * something must be wrong. */
354 if( P != NULL || Q != NULL || D != NULL )
355 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
356
357 }
358
359 if( N != NULL )
360 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
361
362 if( P != NULL )
363 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
364
365 if( Q != NULL )
366 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
367
368 if( D != NULL )
369 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
370
371 if( E != NULL )
372 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100373
374cleanup:
375
376 return( ret );
377}
378
Hanno Becker617c1ae2017-08-23 14:11:24 +0100379int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
380 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
381 mbedtls_mpi *D, mbedtls_mpi *E )
382{
383 int ret;
384
385 /* Check if key is private or public */
386 int is_priv =
387 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
388 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
389 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
390 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
391 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
392
393 if( !is_priv )
394 {
395 /* If we're trying to export private parameters for a public key,
396 * something must be wrong. */
397 if( P != NULL || Q != NULL || D != NULL )
398 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
399
400 }
401
402 /* Export all requested core parameters. */
403
404 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
405 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
406 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
407 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
408 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
409 {
410 return( ret );
411 }
412
413 return( 0 );
414}
415
416/*
417 * Export CRT parameters
418 * This must also be implemented if CRT is not used, for being able to
419 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
420 * can be used in this case.
421 */
422int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
423 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
424{
425 int ret;
426
427 /* Check if key is private or public */
428 int is_priv =
429 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
430 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
431 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
432 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
433 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
434
435 if( !is_priv )
436 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
437
Hanno Beckerdc95c892017-08-23 06:57:02 +0100438#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100439 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100440 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
441 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
442 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
443 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100444 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100445 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100446#else
447 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
448 DP, DQ, QP ) ) != 0 )
449 {
450 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
451 }
452#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100453
454 return( 0 );
455}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100456
Paul Bakker5121ce52009-01-03 21:22:43 +0000457/*
458 * Initialize an RSA context
459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000462 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468#if defined(MBEDTLS_THREADING_C)
469 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200470#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000471}
472
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100473/*
474 * Set padding for an existing RSA context
475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100477{
478 ctx->padding = padding;
479 ctx->hash_id = hash_id;
480}
481
Hanno Becker617c1ae2017-08-23 14:11:24 +0100482/*
483 * Get length in bytes of RSA modulus
484 */
485
486size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
487{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100488 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100489}
490
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494/*
495 * Generate an RSA keypair
496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000498 int (*f_rng)(void *, unsigned char *, size_t),
499 void *p_rng,
500 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501{
502 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100503 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Paul Bakker21eb2802010-08-16 11:10:02 +0000505 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Janos Follathef441782016-09-21 13:18:12 +0100508 if( nbits % 2 )
509 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
510
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100511 mbedtls_mpi_init( &H );
512 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
514 /*
515 * find primes P and Q with Q < P so that:
516 * GCD( E, (P-1)*(Q-1) ) == 1
517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520 do
521 {
Janos Follath10c575b2016-02-23 14:42:48 +0000522 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100523 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
Janos Follathef441782016-09-21 13:18:12 +0100525 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100526 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 continue;
530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200532 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 continue;
534
Janos Follathef441782016-09-21 13:18:12 +0100535 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100536 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100537
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100538 /* Temporarily replace P,Q by P-1, Q-1 */
539 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
540 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
541 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100546 /* Restore P,Q */
547 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
548 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
549
550 ctx->len = mbedtls_mpi_size( &ctx->N );
551
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 /*
553 * D = E^-1 mod ((P-1)*(Q-1))
554 * DP = D mod (P - 1)
555 * DQ = D mod (Q - 1)
556 * QP = Q^-1 mod P
557 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100559 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
560
561#if !defined(MBEDTLS_RSA_NO_CRT)
562 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
563 &ctx->DP, &ctx->DQ, &ctx->QP ) );
564#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Hanno Becker83aad1f2017-08-23 06:45:10 +0100566 /* Double-check */
567 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569cleanup:
570
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100571 mbedtls_mpi_free( &H );
572 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 if( ret != 0 )
575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_rsa_free( ctx );
577 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 }
579
Paul Bakker48377d92013-08-30 12:06:24 +0200580 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581}
582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585/*
586 * Check a public RSA key
587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000589{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100590 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000592
Hanno Becker3a760a12018-01-05 08:14:49 +0000593 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100596 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
Hanno Becker705fc682017-10-10 17:57:02 +0100598 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
599 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100603 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
605 return( 0 );
606}
607
608/*
Hanno Becker705fc682017-10-10 17:57:02 +0100609 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000612{
Hanno Becker705fc682017-10-10 17:57:02 +0100613 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100614 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 {
Hanno Becker98838b02017-10-02 13:16:10 +0100616 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 }
Paul Bakker48377d92013-08-30 12:06:24 +0200618
Hanno Becker98838b02017-10-02 13:16:10 +0100619 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100620 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100622 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000624
Hanno Beckerb269a852017-08-25 08:03:21 +0100625#if !defined(MBEDTLS_RSA_NO_CRT)
626 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
627 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
628 {
629 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
630 }
631#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000632
633 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634}
635
636/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100637 * Check if contexts holding a public and private key match
638 */
Hanno Becker98838b02017-10-02 13:16:10 +0100639int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
640 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100641{
Hanno Becker98838b02017-10-02 13:16:10 +0100642 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100646 }
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
649 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100652 }
653
654 return( 0 );
655}
656
657/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 * Do an RSA public key operation
659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000661 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 unsigned char *output )
663{
Paul Bakker23986e52011-04-24 08:57:21 +0000664 int ret;
665 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
Hanno Beckerebd2c022017-10-12 10:54:53 +0100668 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100669 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200673#if defined(MBEDTLS_THREADING_C)
674 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
675 return( ret );
676#endif
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200682 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
683 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000684 }
685
686 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
688 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000689
690cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200692 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
693 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100694#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000697
698 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
701 return( 0 );
702}
703
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200704/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200705 * Generate or update blinding values, see section 10 of:
706 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200707 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200708 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200709 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200710static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200711 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
712{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200713 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200714
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200715 if( ctx->Vf.p != NULL )
716 {
717 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
719 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
720 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
721 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200722
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200723 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200724 }
725
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200726 /* Unblinding value: Vf = random number, invertible mod N */
727 do {
728 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
732 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
733 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200734
735 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
737 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 +0200738
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200739
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200740cleanup:
741 return( ret );
742}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200743
Paul Bakker5121ce52009-01-03 21:22:43 +0000744/*
Janos Follathe81102e2017-03-22 13:38:28 +0000745 * Exponent blinding supposed to prevent side-channel attacks using multiple
746 * traces of measurements to recover the RSA key. The more collisions are there,
747 * the more bits of the key can be recovered. See [3].
748 *
749 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
750 * observations on avarage.
751 *
752 * For example with 28 byte blinding to achieve 2 collisions the adversary has
753 * to make 2^112 observations on avarage.
754 *
755 * (With the currently (as of 2017 April) known best algorithms breaking 2048
756 * bit RSA requires approximately as much time as trying out 2^112 random keys.
757 * Thus in this sense with 28 byte blinding the security is not reduced by
758 * side-channel attacks like the one in [3])
759 *
760 * This countermeasure does not help if the key recovery is possible with a
761 * single trace.
762 */
763#define RSA_EXPONENT_BLINDING 28
764
765/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000766 * Do an RSA private key operation
767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200769 int (*f_rng)(void *, unsigned char *, size_t),
770 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000771 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 unsigned char *output )
773{
Paul Bakker23986e52011-04-24 08:57:21 +0000774 int ret;
775 size_t olen;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000776
777 /* Temporary holding the result */
778 mbedtls_mpi T;
779
780 /* Temporaries holding P-1, Q-1 and the
781 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000782 mbedtls_mpi P1, Q1, R;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000783
784#if !defined(MBEDTLS_RSA_NO_CRT)
785 /* Temporaries holding the results mod p resp. mod q. */
786 mbedtls_mpi TP, TQ;
787
788 /* Temporaries holding the blinded exponents for
789 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000790 mbedtls_mpi DP_blind, DQ_blind;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000791
792 /* Pointers to actual exponents to be used - either the unblinded
793 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000794 mbedtls_mpi *DP = &ctx->DP;
795 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Beckera5fa0792018-03-09 10:42:23 +0000796#else
797 /* Temporary holding the blinded exponent (if used). */
798 mbedtls_mpi D_blind;
799
800 /* Pointer to actual exponent to be used - either the unblinded
801 * or the blinded one, depending on the presence of a PRNG. */
802 mbedtls_mpi *D = &ctx->D;
803#endif /* MBEDTLS_RSA_NO_CRT */
804
805 /* Temporaries holding the initial input and the double
806 * checked result; should be the same in the end. */
807 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000808
Hanno Beckerebd2c022017-10-12 10:54:53 +0100809 if( rsa_check_context( ctx, 1 /* private key checks */,
810 f_rng != NULL /* blinding y/n */ ) != 0 )
811 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100812 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100813 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100814
Hanno Beckera5fa0792018-03-09 10:42:23 +0000815#if defined(MBEDTLS_THREADING_C)
816 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
817 return( ret );
818#endif
819
820 /* MPI Initialization */
821 mbedtls_mpi_init( &T );
822
823 mbedtls_mpi_init( &P1 );
824 mbedtls_mpi_init( &Q1 );
825 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000826
Janos Follathf9203b42017-03-22 15:13:15 +0000827 if( f_rng != NULL )
828 {
Janos Follathe81102e2017-03-22 13:38:28 +0000829#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000830 mbedtls_mpi_init( &D_blind );
831#else
832 mbedtls_mpi_init( &DP_blind );
833 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000834#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000835 }
Janos Follathe81102e2017-03-22 13:38:28 +0000836
Hanno Beckera5fa0792018-03-09 10:42:23 +0000837#if !defined(MBEDTLS_RSA_NO_CRT)
838 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200839#endif
840
Hanno Beckera5fa0792018-03-09 10:42:23 +0000841 mbedtls_mpi_init( &I );
842 mbedtls_mpi_init( &C );
843
844 /* End of MPI initialization */
845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
847 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000848 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200849 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
850 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000851 }
852
Hanno Beckera5fa0792018-03-09 10:42:23 +0000853 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
854
Paul Bakkerf451bac2013-08-30 15:37:02 +0200855 if( f_rng != NULL )
856 {
857 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200858 * Blinding
859 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200860 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200861 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
862 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000864
Janos Follathe81102e2017-03-22 13:38:28 +0000865 /*
866 * Exponent blinding
867 */
868 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
869 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
870
Janos Follathf9203b42017-03-22 15:13:15 +0000871#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000872 /*
873 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
874 */
875 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
876 f_rng, p_rng ) );
877 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
878 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
879 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
880
881 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000882#else
883 /*
884 * DP_blind = ( P - 1 ) * R + DP
885 */
886 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
887 f_rng, p_rng ) );
888 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
889 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
890 &ctx->DP ) );
891
892 DP = &DP_blind;
893
894 /*
895 * DQ_blind = ( Q - 1 ) * R + DQ
896 */
897 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
898 f_rng, p_rng ) );
899 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
900 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
901 &ctx->DQ ) );
902
903 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000904#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200905 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000908 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100909#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200910 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000911 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000912 *
Hanno Beckera5fa0792018-03-09 10:42:23 +0000913 * TP = input ^ dP mod P
914 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000915 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000916
917 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
918 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
920 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000921 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000922 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000923 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
924 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
925 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
927 /*
Hanno Beckera5fa0792018-03-09 10:42:23 +0000928 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 */
Hanno Beckera5fa0792018-03-09 10:42:23 +0000930 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
931 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200933
Paul Bakkerf451bac2013-08-30 15:37:02 +0200934 if( f_rng != NULL )
935 {
936 /*
937 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200938 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200939 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200940 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200942 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000943
Hanno Beckera5fa0792018-03-09 10:42:23 +0000944 /* Verify the result to prevent glitching attacks. */
945 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
946 &ctx->N, &ctx->RN ) );
947 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
948 {
949 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
950 goto cleanup;
951 }
952
Paul Bakker5121ce52009-01-03 21:22:43 +0000953 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
956cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200958 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
959 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200960#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200961
Hanno Beckera5fa0792018-03-09 10:42:23 +0000962 mbedtls_mpi_free( &P1 );
963 mbedtls_mpi_free( &Q1 );
964 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000965
966 if( f_rng != NULL )
967 {
Janos Follathe81102e2017-03-22 13:38:28 +0000968#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000969 mbedtls_mpi_free( &D_blind );
970#else
971 mbedtls_mpi_free( &DP_blind );
972 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000973#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000974 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
Hanno Beckera5fa0792018-03-09 10:42:23 +0000976 mbedtls_mpi_free( &T );
977
978#if !defined(MBEDTLS_RSA_NO_CRT)
979 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
980#endif
981
982 mbedtls_mpi_free( &C );
983 mbedtls_mpi_free( &I );
984
Paul Bakker5121ce52009-01-03 21:22:43 +0000985 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
988 return( 0 );
989}
990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000992/**
993 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
994 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000995 * \param dst buffer to mask
996 * \param dlen length of destination buffer
997 * \param src source of the mask generation
998 * \param slen length of the source buffer
999 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001000 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001001static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001003{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001005 unsigned char counter[4];
1006 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001007 unsigned int hlen;
1008 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001009 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001012 memset( counter, 0, 4 );
1013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001015
Simon Butcher02037452016-03-01 21:19:12 +00001016 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001017 p = dst;
1018
1019 while( dlen > 0 )
1020 {
1021 use_len = hlen;
1022 if( dlen < hlen )
1023 use_len = dlen;
1024
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001025 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1026 goto exit;
1027 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1028 goto exit;
1029 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1030 goto exit;
1031 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1032 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001033
1034 for( i = 0; i < use_len; ++i )
1035 *p++ ^= mask[i];
1036
1037 counter[3]++;
1038
1039 dlen -= use_len;
1040 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001041
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001042exit:
Gilles Peskine18ac7162017-05-05 19:24:06 +02001043 mbedtls_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001044
1045 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001046}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001050/*
1051 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001054 int (*f_rng)(void *, unsigned char *, size_t),
1055 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001056 int mode,
1057 const unsigned char *label, size_t label_len,
1058 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001059 const unsigned char *input,
1060 unsigned char *output )
1061{
1062 size_t olen;
1063 int ret;
1064 unsigned char *p = output;
1065 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 const mbedtls_md_info_t *md_info;
1067 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1070 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001071
1072 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001076 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001078
1079 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001081
Simon Butcher02037452016-03-01 21:19:12 +00001082 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001083 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001085
1086 memset( output, 0, olen );
1087
1088 *p++ = 0;
1089
Simon Butcher02037452016-03-01 21:19:12 +00001090 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001091 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001093
1094 p += hlen;
1095
Simon Butcher02037452016-03-01 21:19:12 +00001096 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001097 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1098 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001099 p += hlen;
1100 p += olen - 2 * hlen - 2 - ilen;
1101 *p++ = 1;
1102 memcpy( p, input, ilen );
1103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001105 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001106 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001107
Simon Butcher02037452016-03-01 21:19:12 +00001108 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001109 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1110 &md_ctx ) ) != 0 )
1111 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001112
Simon Butcher02037452016-03-01 21:19:12 +00001113 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001114 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1115 &md_ctx ) ) != 0 )
1116 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001117
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001118exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001120
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001121 if( ret != 0 )
1122 return( ret );
1123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 return( ( mode == MBEDTLS_RSA_PUBLIC )
1125 ? mbedtls_rsa_public( ctx, output, output )
1126 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001127}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001131/*
1132 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001135 int (*f_rng)(void *, unsigned char *, size_t),
1136 void *p_rng,
1137 int mode, size_t ilen,
1138 const unsigned char *input,
1139 unsigned char *output )
1140{
1141 size_t nb_pad, olen;
1142 int ret;
1143 unsigned char *p = output;
1144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1146 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001147
Janos Follath1ed9f992016-03-18 11:45:44 +00001148 // We don't check p_rng because it won't be dereferenced here
1149 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001151
1152 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001153
Simon Butcher02037452016-03-01 21:19:12 +00001154 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001155 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001157
1158 nb_pad = olen - 3 - ilen;
1159
1160 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001164
1165 while( nb_pad-- > 0 )
1166 {
1167 int rng_dl = 100;
1168
1169 do {
1170 ret = f_rng( p_rng, p, 1 );
1171 } while( *p == 0 && --rng_dl && ret == 0 );
1172
Simon Butcher02037452016-03-01 21:19:12 +00001173 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001174 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001176
1177 p++;
1178 }
1179 }
1180 else
1181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001183
1184 while( nb_pad-- > 0 )
1185 *p++ = 0xFF;
1186 }
1187
1188 *p++ = 0;
1189 memcpy( p, input, ilen );
1190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 return( ( mode == MBEDTLS_RSA_PUBLIC )
1192 ? mbedtls_rsa_public( ctx, output, output )
1193 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001194}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001196
Paul Bakker5121ce52009-01-03 21:22:43 +00001197/*
1198 * Add the message padding, then do an RSA operation
1199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001201 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001202 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001203 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001204 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001205 unsigned char *output )
1206{
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 switch( ctx->padding )
1208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#if defined(MBEDTLS_PKCS1_V15)
1210 case MBEDTLS_RSA_PKCS_V15:
1211 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001212 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001213#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#if defined(MBEDTLS_PKCS1_V21)
1216 case MBEDTLS_RSA_PKCS_V21:
1217 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001218 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001219#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001220
1221 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001224}
1225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001227/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001228 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001231 int (*f_rng)(void *, unsigned char *, size_t),
1232 void *p_rng,
1233 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001234 const unsigned char *label, size_t label_len,
1235 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001236 const unsigned char *input,
1237 unsigned char *output,
1238 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001239{
Paul Bakker23986e52011-04-24 08:57:21 +00001240 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001241 size_t ilen, i, pad_len;
1242 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1244 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001245 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 const mbedtls_md_info_t *md_info;
1247 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001248
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001249 /*
1250 * Parameters sanity checks
1251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1253 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
1255 ilen = ctx->len;
1256
Paul Bakker27fdf462011-06-09 13:55:13 +00001257 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001261 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001263
Janos Follathc17cda12016-02-11 11:08:18 +00001264 hlen = mbedtls_md_get_size( md_info );
1265
1266 // checking for integer underflow
1267 if( 2 * hlen + 2 > ilen )
1268 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1269
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001270 /*
1271 * RSA operation
1272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1274 ? mbedtls_rsa_public( ctx, input, buf )
1275 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001276
1277 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001278 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001280 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001281 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001284 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1285 {
1286 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001287 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001288 }
1289
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001290 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001291 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1292 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001293 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001294 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1295 &md_ctx ) ) != 0 )
1296 {
1297 mbedtls_md_free( &md_ctx );
1298 goto cleanup;
1299 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001302
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001303 /* Generate lHash */
1304 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1305 goto cleanup;
1306
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001307 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001308 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001309 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001311 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001312
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001313 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001314
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001315 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001316
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001317 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001318 for( i = 0; i < hlen; i++ )
1319 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001320
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001321 /* Get zero-padding len, but always read till end of buffer
1322 * (minus one, for the 01 byte) */
1323 pad_len = 0;
1324 pad_done = 0;
1325 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1326 {
1327 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001328 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001329 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001330
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001331 p += pad_len;
1332 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001333
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001334 /*
1335 * The only information "leaked" is whether the padding was correct or not
1336 * (eg, no data is copied if it was not correct). This meets the
1337 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1338 * the different error conditions.
1339 */
1340 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001341 {
1342 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1343 goto cleanup;
1344 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001345
Paul Bakker66d5d072014-06-17 16:39:18 +02001346 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001347 {
1348 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1349 goto cleanup;
1350 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001351
1352 *olen = ilen - (p - buf);
1353 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001354 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001355
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001356cleanup:
1357 mbedtls_zeroize( buf, sizeof( buf ) );
1358 mbedtls_zeroize( lhash, sizeof( lhash ) );
1359
1360 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001365/*
1366 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001369 int (*f_rng)(void *, unsigned char *, size_t),
1370 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001371 int mode, size_t *olen,
1372 const unsigned char *input,
1373 unsigned char *output,
1374 size_t output_max_len)
1375{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001376 int ret;
1377 size_t ilen, pad_count = 0, i;
1378 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1382 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001383
1384 ilen = ctx->len;
1385
1386 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1390 ? mbedtls_rsa_public( ctx, input, buf )
1391 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001392
1393 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001394 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001395
1396 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001397 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001398
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001399 /*
1400 * Check and get padding len in "constant-time"
1401 */
1402 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001403
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001404 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001408
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001409 /* Get padding len, but always read till end of buffer
1410 * (minus one, for the 00 byte) */
1411 for( i = 0; i < ilen - 3; i++ )
1412 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001413 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1414 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001415 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001416
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001417 p += pad_count;
1418 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001419 }
1420 else
1421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001423
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001424 /* Get padding len, but always read till end of buffer
1425 * (minus one, for the 00 byte) */
1426 for( i = 0; i < ilen - 3; i++ )
1427 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001428 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001429 pad_count += ( pad_done == 0 );
1430 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001431
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001432 p += pad_count;
1433 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 }
1435
Janos Follathc69fa502016-02-12 13:30:09 +00001436 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001437
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001438 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001439 {
1440 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1441 goto cleanup;
1442 }
Paul Bakker8804f692013-02-28 18:06:26 +01001443
Paul Bakker66d5d072014-06-17 16:39:18 +02001444 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001445 {
1446 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1447 goto cleanup;
1448 }
Paul Bakker060c5682009-01-12 21:48:39 +00001449
Paul Bakker27fdf462011-06-09 13:55:13 +00001450 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001452 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001454cleanup:
1455 mbedtls_zeroize( buf, sizeof( buf ) );
1456
1457 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001460
1461/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001462 * Do an RSA operation, then remove the message padding
1463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001465 int (*f_rng)(void *, unsigned char *, size_t),
1466 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001467 int mode, size_t *olen,
1468 const unsigned char *input,
1469 unsigned char *output,
1470 size_t output_max_len)
1471{
1472 switch( ctx->padding )
1473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_PKCS1_V15)
1475 case MBEDTLS_RSA_PKCS_V15:
1476 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001477 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001478#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_PKCS1_V21)
1481 case MBEDTLS_RSA_PKCS_V21:
1482 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001483 olen, input, output,
1484 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001485#endif
1486
1487 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001489 }
1490}
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001493/*
1494 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001497 int (*f_rng)(void *, unsigned char *, size_t),
1498 void *p_rng,
1499 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001501 unsigned int hashlen,
1502 const unsigned char *hash,
1503 unsigned char *sig )
1504{
1505 size_t olen;
1506 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001508 unsigned int slen, hlen, offset = 0;
1509 int ret;
1510 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 const mbedtls_md_info_t *md_info;
1512 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1515 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001516
1517 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001519
1520 olen = ctx->len;
1521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001523 {
Simon Butcher02037452016-03-01 21:19:12 +00001524 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001526 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001530 }
1531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001533 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001537 slen = hlen;
1538
1539 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001541
1542 memset( sig, 0, olen );
1543
Simon Butcher02037452016-03-01 21:19:12 +00001544 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001545 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001547
Simon Butcher02037452016-03-01 21:19:12 +00001548 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001549 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001550 p += olen - hlen * 2 - 2;
1551 *p++ = 0x01;
1552 memcpy( p, salt, slen );
1553 p += slen;
1554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001556 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001557 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001558
Simon Butcher02037452016-03-01 21:19:12 +00001559 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001560 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1561 goto exit;
1562 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1563 goto exit;
1564 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1565 goto exit;
1566 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1567 goto exit;
1568 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1569 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001570
Simon Butcher02037452016-03-01 21:19:12 +00001571 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001572 if( msb % 8 == 0 )
1573 offset = 1;
1574
Simon Butcher02037452016-03-01 21:19:12 +00001575 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001576 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1577 &md_ctx ) ) != 0 )
1578 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001579
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001580 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001581 sig[0] &= 0xFF >> ( olen * 8 - msb );
1582
1583 p += hlen;
1584 *p++ = 0xBC;
1585
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001586 mbedtls_zeroize( salt, sizeof( salt ) );
1587
1588exit:
1589 mbedtls_md_free( &md_ctx );
1590
1591 if( ret != 0 )
1592 return( ret );
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 return( ( mode == MBEDTLS_RSA_PUBLIC )
1595 ? mbedtls_rsa_public( ctx, sig, sig )
1596 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001597}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001601/*
1602 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1603 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001604
1605/* Construct a PKCS v1.5 encoding of a hashed message
1606 *
1607 * This is used both for signature generation and verification.
1608 *
1609 * Parameters:
1610 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001611 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001612 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001613 * - hash: Buffer containing the hashed message or the raw data.
1614 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001615 * - dst: Buffer to hold the encoded message.
1616 *
1617 * Assumptions:
1618 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1619 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001620 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001621 *
1622 */
1623static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1624 unsigned int hashlen,
1625 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001626 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001627 unsigned char *dst )
1628{
1629 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001630 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001631 unsigned char *p = dst;
1632 const char *oid = NULL;
1633
1634 /* Are we signing hashed or raw data? */
1635 if( md_alg != MBEDTLS_MD_NONE )
1636 {
1637 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1638 if( md_info == NULL )
1639 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1640
1641 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1642 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1643
1644 hashlen = mbedtls_md_get_size( md_info );
1645
1646 /* Double-check that 8 + hashlen + oid_size can be used as a
1647 * 1-byte ASN.1 length encoding and that there's no overflow. */
1648 if( 8 + hashlen + oid_size >= 0x80 ||
1649 10 + hashlen < hashlen ||
1650 10 + hashlen + oid_size < 10 + hashlen )
1651 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1652
1653 /*
1654 * Static bounds check:
1655 * - Need 10 bytes for five tag-length pairs.
1656 * (Insist on 1-byte length encodings to protect against variants of
1657 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1658 * - Need hashlen bytes for hash
1659 * - Need oid_size bytes for hash alg OID.
1660 */
1661 if( nb_pad < 10 + hashlen + oid_size )
1662 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1663 nb_pad -= 10 + hashlen + oid_size;
1664 }
1665 else
1666 {
1667 if( nb_pad < hashlen )
1668 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1669
1670 nb_pad -= hashlen;
1671 }
1672
Hanno Becker2b2f8982017-09-27 17:10:03 +01001673 /* Need space for signature header and padding delimiter (3 bytes),
1674 * and 8 bytes for the minimal padding */
1675 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001676 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1677 nb_pad -= 3;
1678
1679 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001680 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001681
1682 /* Write signature header and padding */
1683 *p++ = 0;
1684 *p++ = MBEDTLS_RSA_SIGN;
1685 memset( p, 0xFF, nb_pad );
1686 p += nb_pad;
1687 *p++ = 0;
1688
1689 /* Are we signing raw data? */
1690 if( md_alg == MBEDTLS_MD_NONE )
1691 {
1692 memcpy( p, hash, hashlen );
1693 return( 0 );
1694 }
1695
1696 /* Signing hashed data, add corresponding ASN.1 structure
1697 *
1698 * DigestInfo ::= SEQUENCE {
1699 * digestAlgorithm DigestAlgorithmIdentifier,
1700 * digest Digest }
1701 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1702 * Digest ::= OCTET STRING
1703 *
1704 * Schematic:
1705 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1706 * TAG-NULL + LEN [ NULL ] ]
1707 * TAG-OCTET + LEN [ HASH ] ]
1708 */
1709 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001710 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001711 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001712 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001713 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001714 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001715 memcpy( p, oid, oid_size );
1716 p += oid_size;
1717 *p++ = MBEDTLS_ASN1_NULL;
1718 *p++ = 0x00;
1719 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001720 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001721 memcpy( p, hash, hashlen );
1722 p += hashlen;
1723
1724 /* Just a sanity-check, should be automatic
1725 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001726 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001727 {
Hanno Beckere58d38c2017-09-27 17:09:00 +01001728 mbedtls_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001729 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1730 }
1731
1732 return( 0 );
1733}
1734
Paul Bakkerb3869132013-02-28 17:21:01 +01001735/*
1736 * Do an RSA operation to sign the message digest
1737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001739 int (*f_rng)(void *, unsigned char *, size_t),
1740 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001741 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001743 unsigned int hashlen,
1744 const unsigned char *hash,
1745 unsigned char *sig )
1746{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001747 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001748 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1751 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001752
Hanno Beckerfdf38032017-09-06 12:35:55 +01001753 /*
1754 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1755 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001756
Hanno Beckerfdf38032017-09-06 12:35:55 +01001757 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1758 ctx->len, sig ) ) != 0 )
1759 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001760
1761 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001762 * Call respective RSA primitive
1763 */
1764
1765 if( mode == MBEDTLS_RSA_PUBLIC )
1766 {
1767 /* Skip verification on a public key operation */
1768 return( mbedtls_rsa_public( ctx, sig, sig ) );
1769 }
1770
1771 /* Private key operation
1772 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001773 * In order to prevent Lenstra's attack, make the signature in a
1774 * temporary buffer and check it before returning it.
1775 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001776
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001777 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001778 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001779 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1780
Hanno Beckerfdf38032017-09-06 12:35:55 +01001781 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001782 if( verif == NULL )
1783 {
1784 mbedtls_free( sig_try );
1785 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1786 }
1787
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001788 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1789 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1790
Hanno Becker171a8f12017-09-06 12:32:16 +01001791 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001792 {
1793 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1794 goto cleanup;
1795 }
1796
1797 memcpy( sig, sig_try, ctx->len );
1798
1799cleanup:
1800 mbedtls_free( sig_try );
1801 mbedtls_free( verif );
1802
1803 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001804}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001806
1807/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001808 * Do an RSA operation to sign the message digest
1809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001811 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001812 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001813 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001815 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001816 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001817 unsigned char *sig )
1818{
Paul Bakker5121ce52009-01-03 21:22:43 +00001819 switch( ctx->padding )
1820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821#if defined(MBEDTLS_PKCS1_V15)
1822 case MBEDTLS_RSA_PKCS_V15:
1823 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001824 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001825#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827#if defined(MBEDTLS_PKCS1_V21)
1828 case MBEDTLS_RSA_PKCS_V21:
1829 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001830 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001831#endif
1832
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001836}
1837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001839/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001840 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001843 int (*f_rng)(void *, unsigned char *, size_t),
1844 void *p_rng,
1845 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001847 unsigned int hashlen,
1848 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001850 int expected_salt_len,
1851 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001852{
Paul Bakker23986e52011-04-24 08:57:21 +00001853 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001854 size_t siglen;
1855 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001856 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001858 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001859 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001860 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 const mbedtls_md_info_t *md_info;
1862 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001863 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1866 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001867
Paul Bakker5121ce52009-01-03 21:22:43 +00001868 siglen = ctx->len;
1869
Paul Bakker27fdf462011-06-09 13:55:13 +00001870 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1874 ? mbedtls_rsa_public( ctx, sig, buf )
1875 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
1877 if( ret != 0 )
1878 return( ret );
1879
1880 p = buf;
1881
Paul Bakkerb3869132013-02-28 17:21:01 +01001882 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 {
Simon Butcher02037452016-03-01 21:19:12 +00001887 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001889 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001893 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001896 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001900
Paul Bakkerb3869132013-02-28 17:21:01 +01001901 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001902
Simon Butcher02037452016-03-01 21:19:12 +00001903 /*
1904 * Note: EMSA-PSS verification is over the length of N - 1 bits
1905 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001906 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001907
Gilles Peskineb00b0da2017-10-19 15:23:49 +02001908 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1909 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1910
Simon Butcher02037452016-03-01 21:19:12 +00001911 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001912 if( msb % 8 == 0 )
1913 {
1914 p++;
1915 siglen -= 1;
1916 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001917
Gilles Peskine139108a2017-10-18 19:03:42 +02001918 if( siglen < hlen + 2 )
1919 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1920 hash_start = p + siglen - hlen - 1;
1921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001923 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001924 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001925
Jaeden Amero66954e12018-01-25 16:05:54 +00001926 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
1927 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001928 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01001929
Paul Bakkerb3869132013-02-28 17:21:01 +01001930 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001931
Gilles Peskine6a54b022017-10-17 19:02:13 +02001932 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001933 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001934
Gilles Peskine91048a32017-10-19 17:46:14 +02001935 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001936 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001937 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1938 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001939 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001940
Gilles Peskine6a54b022017-10-17 19:02:13 +02001941 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02001944 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001945 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001946 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1947 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001948 }
1949
Simon Butcher02037452016-03-01 21:19:12 +00001950 /*
1951 * Generate H = Hash( M' )
1952 */
Jaeden Amero66954e12018-01-25 16:05:54 +00001953 ret = mbedtls_md_starts( &md_ctx );
1954 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001955 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00001956 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
1957 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001958 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00001959 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
1960 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001961 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00001962 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
1963 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001964 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00001965 ret = mbedtls_md_finish( &md_ctx, result );
1966 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001967 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00001968
Jaeden Amero66954e12018-01-25 16:05:54 +00001969 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01001970 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001971 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01001972 goto exit;
1973 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001974
1975exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001977
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001978 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001979}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001980
1981/*
1982 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001985 int (*f_rng)(void *, unsigned char *, size_t),
1986 void *p_rng,
1987 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001989 unsigned int hashlen,
1990 const unsigned char *hash,
1991 const unsigned char *sig )
1992{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1994 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001995 : md_alg;
1996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001998 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002000 sig ) );
2001
2002}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002006/*
2007 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002010 int (*f_rng)(void *, unsigned char *, size_t),
2011 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002012 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002014 unsigned int hashlen,
2015 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002016 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002017{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002018 int ret = 0;
2019 const size_t sig_len = ctx->len;
2020 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2023 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002024
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002025 /*
2026 * Prepare expected PKCS1 v1.5 encoding of hash.
2027 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002028
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002029 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2030 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2031 {
2032 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2033 goto cleanup;
2034 }
2035
2036 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2037 encoded_expected ) ) != 0 )
2038 goto cleanup;
2039
2040 /*
2041 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2042 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002045 ? mbedtls_rsa_public( ctx, sig, encoded )
2046 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002047 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002048 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002049
Simon Butcher02037452016-03-01 21:19:12 +00002050 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002051 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002052 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002053
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002054 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2055 sig_len ) ) != 0 )
2056 {
2057 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2058 goto cleanup;
2059 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002060
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002061cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002062
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002063 if( encoded != NULL )
2064 {
2065 mbedtls_zeroize( encoded, sig_len );
2066 mbedtls_free( encoded );
2067 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002068
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002069 if( encoded_expected != NULL )
2070 {
2071 mbedtls_zeroize( encoded_expected, sig_len );
2072 mbedtls_free( encoded_expected );
2073 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002074
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002075 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002076}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002078
2079/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002080 * Do an RSA operation and check the message digest
2081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002083 int (*f_rng)(void *, unsigned char *, size_t),
2084 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002085 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002087 unsigned int hashlen,
2088 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002089 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002090{
2091 switch( ctx->padding )
2092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#if defined(MBEDTLS_PKCS1_V15)
2094 case MBEDTLS_RSA_PKCS_V15:
2095 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002096 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002097#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_PKCS1_V21)
2100 case MBEDTLS_RSA_PKCS_V21:
2101 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002102 hashlen, hash, sig );
2103#endif
2104
2105 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002107 }
2108}
2109
2110/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002111 * Copy the components of an RSA key
2112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002114{
2115 int ret;
2116
2117 dst->ver = src->ver;
2118 dst->len = src->len;
2119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2121 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2124 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2125 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002126
2127#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2129 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2130 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2132 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002133#endif
2134
2135 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2138 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002139
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002140 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002141 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002142
2143cleanup:
2144 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002146
2147 return( ret );
2148}
2149
2150/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 * Free the components of an RSA key
2152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002156 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2157 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002159
Hanno Becker33c30a02017-08-23 07:00:22 +01002160#if !defined(MBEDTLS_RSA_NO_CRT)
2161 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2162 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2163 mbedtls_mpi_free( &ctx->DP );
2164#endif /* MBEDTLS_RSA_NO_CRT */
2165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#if defined(MBEDTLS_THREADING_C)
2167 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002168#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002169}
2170
Hanno Beckerab377312017-08-23 16:24:51 +01002171#endif /* !MBEDTLS_RSA_ALT */
2172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002174
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002175#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002176
2177/*
2178 * Example RSA-1024 keypair, for test purposes
2179 */
2180#define KEY_LEN 128
2181
2182#define RSA_N "9292758453063D803DD603D5E777D788" \
2183 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2184 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2185 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2186 "93A89813FBF3C4F8066D2D800F7C38A8" \
2187 "1AE31942917403FF4946B0A83D3D3E05" \
2188 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2189 "5E94BB77B07507233A0BC7BAC8F90F79"
2190
2191#define RSA_E "10001"
2192
2193#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2194 "66CA472BC44D253102F8B4A9D3BFA750" \
2195 "91386C0077937FE33FA3252D28855837" \
2196 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2197 "DF79C5CE07EE72C7F123142198164234" \
2198 "CABB724CF78B8173B9F880FC86322407" \
2199 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2200 "071513A1E85B5DFA031F21ECAE91A34D"
2201
2202#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2203 "2C01CAD19EA484A87EA4377637E75500" \
2204 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2205 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2206
2207#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2208 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2209 "910E4168387E3C30AA1E00C339A79508" \
2210 "8452DD96A9A5EA5D9DCA68DA636032AF"
2211
Paul Bakker5121ce52009-01-03 21:22:43 +00002212#define PT_LEN 24
2213#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2214 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002217static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002218{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002219#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002220 size_t i;
2221
Paul Bakker545570e2010-07-18 09:00:25 +00002222 if( rng_state != NULL )
2223 rng_state = NULL;
2224
Paul Bakkera3d195c2011-11-27 21:07:34 +00002225 for( i = 0; i < len; ++i )
2226 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002227#else
2228 if( rng_state != NULL )
2229 rng_state = NULL;
2230
2231 arc4random_buf( output, len );
2232#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002233
Paul Bakkera3d195c2011-11-27 21:07:34 +00002234 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002237
Paul Bakker5121ce52009-01-03 21:22:43 +00002238/*
2239 * Checkup routine
2240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002242{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002243 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002245 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 unsigned char rsa_plaintext[PT_LEN];
2248 unsigned char rsa_decrypted[PT_LEN];
2249 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002251 unsigned char sha1sum[20];
2252#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002253
Hanno Becker3a701162017-08-22 13:52:43 +01002254 mbedtls_mpi K;
2255
2256 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002258
Hanno Becker3a701162017-08-22 13:52:43 +01002259 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2260 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2261 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2262 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2263 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2264 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2265 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2266 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2267 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2268 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2269
Hanno Becker7f25f852017-10-10 16:56:22 +01002270 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002271
2272 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2276 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 {
2278 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
Hanno Beckera5fa0792018-03-09 10:42:23 +00002281 ret = 1;
2282 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002283 }
2284
2285 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002287
2288 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2289
Hanno Becker98838b02017-10-02 13:16:10 +01002290 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2291 PT_LEN, rsa_plaintext,
2292 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002293 {
2294 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002296
Hanno Beckera5fa0792018-03-09 10:42:23 +00002297 ret = 1;
2298 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002299 }
2300
2301 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
Hanno Becker98838b02017-10-02 13:16:10 +01002304 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2305 &len, rsa_ciphertext, rsa_decrypted,
2306 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002307 {
2308 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002310
Hanno Beckera5fa0792018-03-09 10:42:23 +00002311 ret = 1;
2312 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 }
2314
2315 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2316 {
2317 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002319
Hanno Beckera5fa0792018-03-09 10:42:23 +00002320 ret = 1;
2321 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 }
2323
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002324 if( verbose != 0 )
2325 mbedtls_printf( "passed\n" );
2326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002329 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002331 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002332 {
2333 if( verbose != 0 )
2334 mbedtls_printf( "failed\n" );
2335
2336 return( 1 );
2337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002338
Hanno Becker98838b02017-10-02 13:16:10 +01002339 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2340 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2341 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 {
2343 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002345
Hanno Beckera5fa0792018-03-09 10:42:23 +00002346 ret = 1;
2347 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 }
2349
2350 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352
Hanno Becker98838b02017-10-02 13:16:10 +01002353 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2354 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2355 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002356 {
2357 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
Hanno Beckera5fa0792018-03-09 10:42:23 +00002360 ret = 1;
2361 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002362 }
2363
2364 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002365 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002368 if( verbose != 0 )
2369 mbedtls_printf( "\n" );
2370
Paul Bakker3d8fb632014-04-17 12:42:41 +02002371cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002372 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 mbedtls_rsa_free( &rsa );
2374#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002375 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002377 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002378}
2379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382#endif /* MBEDTLS_RSA_C */