blob: 1909744a7672fbd3751b84282327d616be55b3af [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;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +0000777 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +0000778#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000779 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000780 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +0000781#else
782 mbedtls_mpi DP_blind, DQ_blind;
783 mbedtls_mpi *DP = &ctx->DP;
784 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +0000785#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
Hanno Beckerebd2c022017-10-12 10:54:53 +0100787 if( rsa_check_context( ctx, 1 /* private key checks */,
788 f_rng != NULL /* blinding y/n */ ) != 0 )
789 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100790 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100791 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000794 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
795
Janos Follathf9203b42017-03-22 15:13:15 +0000796 if( f_rng != NULL )
797 {
Janos Follathe81102e2017-03-22 13:38:28 +0000798#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000799 mbedtls_mpi_init( &D_blind );
800#else
801 mbedtls_mpi_init( &DP_blind );
802 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000803#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000804 }
Janos Follathe81102e2017-03-22 13:38:28 +0000805
Paul Bakker5121ce52009-01-03 21:22:43 +0000806
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200807#if defined(MBEDTLS_THREADING_C)
808 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
809 return( ret );
810#endif
811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
813 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000814 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200815 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
816 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000817 }
818
Paul Bakkerf451bac2013-08-30 15:37:02 +0200819 if( f_rng != NULL )
820 {
821 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200822 * Blinding
823 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200824 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200825 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
826 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000828
Janos Follathe81102e2017-03-22 13:38:28 +0000829 /*
830 * Exponent blinding
831 */
832 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
833 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
834
Janos Follathf9203b42017-03-22 15:13:15 +0000835#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000836 /*
837 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
838 */
839 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
840 f_rng, p_rng ) );
841 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
842 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
843 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
844
845 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000846#else
847 /*
848 * DP_blind = ( P - 1 ) * R + DP
849 */
850 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
851 f_rng, p_rng ) );
852 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
853 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
854 &ctx->DP ) );
855
856 DP = &DP_blind;
857
858 /*
859 * DQ_blind = ( Q - 1 ) * R + DQ
860 */
861 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
862 f_rng, p_rng ) );
863 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
864 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
865 &ctx->DQ ) );
866
867 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000868#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200869 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000872 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100873#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200874 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000875 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 *
877 * T1 = input ^ dP mod P
878 * T2 = input ^ dQ mod Q
879 */
Janos Follathf9203b42017-03-22 15:13:15 +0000880 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
881 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000882
883 /*
884 * T = (T1 - T2) * (Q^-1 mod P) mod P
885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
887 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
888 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000889
890 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200891 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
894 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
895#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200896
Paul Bakkerf451bac2013-08-30 15:37:02 +0200897 if( f_rng != NULL )
898 {
899 /*
900 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200901 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200902 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200903 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200905 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000906
907 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000909
910cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200912 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
913 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200914#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000917 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
918
919 if( f_rng != NULL )
920 {
Janos Follathe81102e2017-03-22 13:38:28 +0000921#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000922 mbedtls_mpi_free( &D_blind );
923#else
924 mbedtls_mpi_free( &DP_blind );
925 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000926#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000927 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
929 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
932 return( 0 );
933}
934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000936/**
937 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
938 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000939 * \param dst buffer to mask
940 * \param dlen length of destination buffer
941 * \param src source of the mask generation
942 * \param slen length of the source buffer
943 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000944 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100945static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000947{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000949 unsigned char counter[4];
950 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000951 unsigned int hlen;
952 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +0100953 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000956 memset( counter, 0, 4 );
957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000959
Simon Butcher02037452016-03-01 21:19:12 +0000960 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000961 p = dst;
962
963 while( dlen > 0 )
964 {
965 use_len = hlen;
966 if( dlen < hlen )
967 use_len = dlen;
968
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100969 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
970 goto exit;
971 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
972 goto exit;
973 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
974 goto exit;
975 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
976 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000977
978 for( i = 0; i < use_len; ++i )
979 *p++ ^= mask[i];
980
981 counter[3]++;
982
983 dlen -= use_len;
984 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200985
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100986exit:
Gilles Peskine18ac7162017-05-05 19:24:06 +0200987 mbedtls_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100988
989 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100994/*
995 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100998 int (*f_rng)(void *, unsigned char *, size_t),
999 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001000 int mode,
1001 const unsigned char *label, size_t label_len,
1002 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001003 const unsigned char *input,
1004 unsigned char *output )
1005{
1006 size_t olen;
1007 int ret;
1008 unsigned char *p = output;
1009 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 const mbedtls_md_info_t *md_info;
1011 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1014 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001015
1016 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001020 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001022
1023 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001025
Simon Butcher02037452016-03-01 21:19:12 +00001026 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001027 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001029
1030 memset( output, 0, olen );
1031
1032 *p++ = 0;
1033
Simon Butcher02037452016-03-01 21:19:12 +00001034 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001035 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001037
1038 p += hlen;
1039
Simon Butcher02037452016-03-01 21:19:12 +00001040 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001041 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1042 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001043 p += hlen;
1044 p += olen - 2 * hlen - 2 - ilen;
1045 *p++ = 1;
1046 memcpy( p, input, ilen );
1047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001049 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001050 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001051
Simon Butcher02037452016-03-01 21:19:12 +00001052 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001053 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1054 &md_ctx ) ) != 0 )
1055 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001056
Simon Butcher02037452016-03-01 21:19:12 +00001057 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001058 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1059 &md_ctx ) ) != 0 )
1060 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001061
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001062exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001064
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001065 if( ret != 0 )
1066 return( ret );
1067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 return( ( mode == MBEDTLS_RSA_PUBLIC )
1069 ? mbedtls_rsa_public( ctx, output, output )
1070 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001071}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001075/*
1076 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001079 int (*f_rng)(void *, unsigned char *, size_t),
1080 void *p_rng,
1081 int mode, size_t ilen,
1082 const unsigned char *input,
1083 unsigned char *output )
1084{
1085 size_t nb_pad, olen;
1086 int ret;
1087 unsigned char *p = output;
1088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1090 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001091
Janos Follath1ed9f992016-03-18 11:45:44 +00001092 // We don't check p_rng because it won't be dereferenced here
1093 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001095
1096 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001097
Simon Butcher02037452016-03-01 21:19:12 +00001098 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001099 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001101
1102 nb_pad = olen - 3 - ilen;
1103
1104 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001108
1109 while( nb_pad-- > 0 )
1110 {
1111 int rng_dl = 100;
1112
1113 do {
1114 ret = f_rng( p_rng, p, 1 );
1115 } while( *p == 0 && --rng_dl && ret == 0 );
1116
Simon Butcher02037452016-03-01 21:19:12 +00001117 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001118 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001120
1121 p++;
1122 }
1123 }
1124 else
1125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001127
1128 while( nb_pad-- > 0 )
1129 *p++ = 0xFF;
1130 }
1131
1132 *p++ = 0;
1133 memcpy( p, input, ilen );
1134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 return( ( mode == MBEDTLS_RSA_PUBLIC )
1136 ? mbedtls_rsa_public( ctx, output, output )
1137 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001138}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001140
Paul Bakker5121ce52009-01-03 21:22:43 +00001141/*
1142 * Add the message padding, then do an RSA operation
1143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001145 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001146 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001147 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001148 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 unsigned char *output )
1150{
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 switch( ctx->padding )
1152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#if defined(MBEDTLS_PKCS1_V15)
1154 case MBEDTLS_RSA_PKCS_V15:
1155 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001156 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001157#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#if defined(MBEDTLS_PKCS1_V21)
1160 case MBEDTLS_RSA_PKCS_V21:
1161 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001162 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001163#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
1165 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001168}
1169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001171/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001172 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001175 int (*f_rng)(void *, unsigned char *, size_t),
1176 void *p_rng,
1177 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001178 const unsigned char *label, size_t label_len,
1179 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001180 const unsigned char *input,
1181 unsigned char *output,
1182 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001183{
Paul Bakker23986e52011-04-24 08:57:21 +00001184 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001185 size_t ilen, i, pad_len;
1186 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1188 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001189 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 const mbedtls_md_info_t *md_info;
1191 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001192
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001193 /*
1194 * Parameters sanity checks
1195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1197 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001198
1199 ilen = ctx->len;
1200
Paul Bakker27fdf462011-06-09 13:55:13 +00001201 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001205 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001207
Janos Follathc17cda12016-02-11 11:08:18 +00001208 hlen = mbedtls_md_get_size( md_info );
1209
1210 // checking for integer underflow
1211 if( 2 * hlen + 2 > ilen )
1212 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1213
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001214 /*
1215 * RSA operation
1216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1218 ? mbedtls_rsa_public( ctx, input, buf )
1219 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220
1221 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001222 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001223
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001224 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001225 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001228 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1229 {
1230 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001231 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001232 }
1233
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001234 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001235 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1236 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001237 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001238 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1239 &md_ctx ) ) != 0 )
1240 {
1241 mbedtls_md_free( &md_ctx );
1242 goto cleanup;
1243 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001246
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001247 /* Generate lHash */
1248 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1249 goto cleanup;
1250
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001251 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001252 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001253 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001255 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001257 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001258
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001259 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001260
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001261 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001262 for( i = 0; i < hlen; i++ )
1263 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001264
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001265 /* Get zero-padding len, but always read till end of buffer
1266 * (minus one, for the 01 byte) */
1267 pad_len = 0;
1268 pad_done = 0;
1269 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1270 {
1271 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001272 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001273 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001274
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001275 p += pad_len;
1276 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001277
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001278 /*
1279 * The only information "leaked" is whether the padding was correct or not
1280 * (eg, no data is copied if it was not correct). This meets the
1281 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1282 * the different error conditions.
1283 */
1284 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001285 {
1286 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1287 goto cleanup;
1288 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001289
Paul Bakker66d5d072014-06-17 16:39:18 +02001290 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001291 {
1292 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1293 goto cleanup;
1294 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001295
1296 *olen = ilen - (p - buf);
1297 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001298 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001299
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001300cleanup:
1301 mbedtls_zeroize( buf, sizeof( buf ) );
1302 mbedtls_zeroize( lhash, sizeof( lhash ) );
1303
1304 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001305}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001309/*
1310 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001313 int (*f_rng)(void *, unsigned char *, size_t),
1314 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001315 int mode, size_t *olen,
1316 const unsigned char *input,
1317 unsigned char *output,
1318 size_t output_max_len)
1319{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001320 int ret;
1321 size_t ilen, pad_count = 0, i;
1322 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1326 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001327
1328 ilen = ctx->len;
1329
1330 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1334 ? mbedtls_rsa_public( ctx, input, buf )
1335 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001336
1337 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001338 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001339
1340 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001341 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001342
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001343 /*
1344 * Check and get padding len in "constant-time"
1345 */
1346 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001347
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001348 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001353 /* Get padding len, but always read till end of buffer
1354 * (minus one, for the 00 byte) */
1355 for( i = 0; i < ilen - 3; i++ )
1356 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001357 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1358 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001359 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001360
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001361 p += pad_count;
1362 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001363 }
1364 else
1365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001367
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001368 /* Get padding len, but always read till end of buffer
1369 * (minus one, for the 00 byte) */
1370 for( i = 0; i < ilen - 3; i++ )
1371 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001372 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001373 pad_count += ( pad_done == 0 );
1374 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001375
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001376 p += pad_count;
1377 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 }
1379
Janos Follathc69fa502016-02-12 13:30:09 +00001380 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001381
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001382 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001383 {
1384 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1385 goto cleanup;
1386 }
Paul Bakker8804f692013-02-28 18:06:26 +01001387
Paul Bakker66d5d072014-06-17 16:39:18 +02001388 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001389 {
1390 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1391 goto cleanup;
1392 }
Paul Bakker060c5682009-01-12 21:48:39 +00001393
Paul Bakker27fdf462011-06-09 13:55:13 +00001394 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001396 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001398cleanup:
1399 mbedtls_zeroize( buf, sizeof( buf ) );
1400
1401 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001402}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
1405/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001406 * Do an RSA operation, then remove the message padding
1407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001409 int (*f_rng)(void *, unsigned char *, size_t),
1410 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001411 int mode, size_t *olen,
1412 const unsigned char *input,
1413 unsigned char *output,
1414 size_t output_max_len)
1415{
1416 switch( ctx->padding )
1417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#if defined(MBEDTLS_PKCS1_V15)
1419 case MBEDTLS_RSA_PKCS_V15:
1420 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001421 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001422#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#if defined(MBEDTLS_PKCS1_V21)
1425 case MBEDTLS_RSA_PKCS_V21:
1426 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001427 olen, input, output,
1428 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001429#endif
1430
1431 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001433 }
1434}
1435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001437/*
1438 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001441 int (*f_rng)(void *, unsigned char *, size_t),
1442 void *p_rng,
1443 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001445 unsigned int hashlen,
1446 const unsigned char *hash,
1447 unsigned char *sig )
1448{
1449 size_t olen;
1450 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001452 unsigned int slen, hlen, offset = 0;
1453 int ret;
1454 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 const mbedtls_md_info_t *md_info;
1456 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1459 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001460
1461 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001463
1464 olen = ctx->len;
1465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001467 {
Simon Butcher02037452016-03-01 21:19:12 +00001468 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001470 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001474 }
1475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001477 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001481 slen = hlen;
1482
1483 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001485
1486 memset( sig, 0, olen );
1487
Simon Butcher02037452016-03-01 21:19:12 +00001488 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001489 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001491
Simon Butcher02037452016-03-01 21:19:12 +00001492 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001493 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001494 p += olen - hlen * 2 - 2;
1495 *p++ = 0x01;
1496 memcpy( p, salt, slen );
1497 p += slen;
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001500 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001501 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001502
Simon Butcher02037452016-03-01 21:19:12 +00001503 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001504 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1505 goto exit;
1506 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1507 goto exit;
1508 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1509 goto exit;
1510 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1511 goto exit;
1512 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1513 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001514
Simon Butcher02037452016-03-01 21:19:12 +00001515 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001516 if( msb % 8 == 0 )
1517 offset = 1;
1518
Simon Butcher02037452016-03-01 21:19:12 +00001519 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001520 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1521 &md_ctx ) ) != 0 )
1522 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001523
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001524 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001525 sig[0] &= 0xFF >> ( olen * 8 - msb );
1526
1527 p += hlen;
1528 *p++ = 0xBC;
1529
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001530 mbedtls_zeroize( salt, sizeof( salt ) );
1531
1532exit:
1533 mbedtls_md_free( &md_ctx );
1534
1535 if( ret != 0 )
1536 return( ret );
1537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 return( ( mode == MBEDTLS_RSA_PUBLIC )
1539 ? mbedtls_rsa_public( ctx, sig, sig )
1540 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001545/*
1546 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1547 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001548
1549/* Construct a PKCS v1.5 encoding of a hashed message
1550 *
1551 * This is used both for signature generation and verification.
1552 *
1553 * Parameters:
1554 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001555 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001556 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001557 * - hash: Buffer containing the hashed message or the raw data.
1558 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001559 * - dst: Buffer to hold the encoded message.
1560 *
1561 * Assumptions:
1562 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1563 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001564 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001565 *
1566 */
1567static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1568 unsigned int hashlen,
1569 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001570 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001571 unsigned char *dst )
1572{
1573 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001574 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001575 unsigned char *p = dst;
1576 const char *oid = NULL;
1577
1578 /* Are we signing hashed or raw data? */
1579 if( md_alg != MBEDTLS_MD_NONE )
1580 {
1581 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1582 if( md_info == NULL )
1583 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1584
1585 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1586 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1587
1588 hashlen = mbedtls_md_get_size( md_info );
1589
1590 /* Double-check that 8 + hashlen + oid_size can be used as a
1591 * 1-byte ASN.1 length encoding and that there's no overflow. */
1592 if( 8 + hashlen + oid_size >= 0x80 ||
1593 10 + hashlen < hashlen ||
1594 10 + hashlen + oid_size < 10 + hashlen )
1595 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1596
1597 /*
1598 * Static bounds check:
1599 * - Need 10 bytes for five tag-length pairs.
1600 * (Insist on 1-byte length encodings to protect against variants of
1601 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1602 * - Need hashlen bytes for hash
1603 * - Need oid_size bytes for hash alg OID.
1604 */
1605 if( nb_pad < 10 + hashlen + oid_size )
1606 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1607 nb_pad -= 10 + hashlen + oid_size;
1608 }
1609 else
1610 {
1611 if( nb_pad < hashlen )
1612 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1613
1614 nb_pad -= hashlen;
1615 }
1616
Hanno Becker2b2f8982017-09-27 17:10:03 +01001617 /* Need space for signature header and padding delimiter (3 bytes),
1618 * and 8 bytes for the minimal padding */
1619 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001620 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1621 nb_pad -= 3;
1622
1623 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001624 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001625
1626 /* Write signature header and padding */
1627 *p++ = 0;
1628 *p++ = MBEDTLS_RSA_SIGN;
1629 memset( p, 0xFF, nb_pad );
1630 p += nb_pad;
1631 *p++ = 0;
1632
1633 /* Are we signing raw data? */
1634 if( md_alg == MBEDTLS_MD_NONE )
1635 {
1636 memcpy( p, hash, hashlen );
1637 return( 0 );
1638 }
1639
1640 /* Signing hashed data, add corresponding ASN.1 structure
1641 *
1642 * DigestInfo ::= SEQUENCE {
1643 * digestAlgorithm DigestAlgorithmIdentifier,
1644 * digest Digest }
1645 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1646 * Digest ::= OCTET STRING
1647 *
1648 * Schematic:
1649 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1650 * TAG-NULL + LEN [ NULL ] ]
1651 * TAG-OCTET + LEN [ HASH ] ]
1652 */
1653 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001654 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001655 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001656 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001657 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001658 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001659 memcpy( p, oid, oid_size );
1660 p += oid_size;
1661 *p++ = MBEDTLS_ASN1_NULL;
1662 *p++ = 0x00;
1663 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001664 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001665 memcpy( p, hash, hashlen );
1666 p += hashlen;
1667
1668 /* Just a sanity-check, should be automatic
1669 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001670 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001671 {
Hanno Beckere58d38c2017-09-27 17:09:00 +01001672 mbedtls_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001673 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1674 }
1675
1676 return( 0 );
1677}
1678
Paul Bakkerb3869132013-02-28 17:21:01 +01001679/*
1680 * Do an RSA operation to sign the message digest
1681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001683 int (*f_rng)(void *, unsigned char *, size_t),
1684 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001685 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001687 unsigned int hashlen,
1688 const unsigned char *hash,
1689 unsigned char *sig )
1690{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001691 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001692 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1695 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001696
Hanno Beckerfdf38032017-09-06 12:35:55 +01001697 /*
1698 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1699 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001700
Hanno Beckerfdf38032017-09-06 12:35:55 +01001701 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1702 ctx->len, sig ) ) != 0 )
1703 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001704
1705 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001706 * Call respective RSA primitive
1707 */
1708
1709 if( mode == MBEDTLS_RSA_PUBLIC )
1710 {
1711 /* Skip verification on a public key operation */
1712 return( mbedtls_rsa_public( ctx, sig, sig ) );
1713 }
1714
1715 /* Private key operation
1716 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001717 * In order to prevent Lenstra's attack, make the signature in a
1718 * temporary buffer and check it before returning it.
1719 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001720
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001721 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001722 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001723 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1724
Hanno Beckerfdf38032017-09-06 12:35:55 +01001725 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001726 if( verif == NULL )
1727 {
1728 mbedtls_free( sig_try );
1729 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1730 }
1731
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001732 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1733 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1734
Hanno Becker171a8f12017-09-06 12:32:16 +01001735 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001736 {
1737 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1738 goto cleanup;
1739 }
1740
1741 memcpy( sig, sig_try, ctx->len );
1742
1743cleanup:
1744 mbedtls_free( sig_try );
1745 mbedtls_free( verif );
1746
1747 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001748}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001750
1751/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001752 * Do an RSA operation to sign the message digest
1753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001755 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001756 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001759 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001760 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 unsigned char *sig )
1762{
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 switch( ctx->padding )
1764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765#if defined(MBEDTLS_PKCS1_V15)
1766 case MBEDTLS_RSA_PKCS_V15:
1767 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001768 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001769#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771#if defined(MBEDTLS_PKCS1_V21)
1772 case MBEDTLS_RSA_PKCS_V21:
1773 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001774 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001775#endif
1776
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001780}
1781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001783/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001784 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001787 int (*f_rng)(void *, unsigned char *, size_t),
1788 void *p_rng,
1789 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001791 unsigned int hashlen,
1792 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001794 int expected_salt_len,
1795 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001796{
Paul Bakker23986e52011-04-24 08:57:21 +00001797 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001798 size_t siglen;
1799 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001801 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001802 unsigned int hlen;
1803 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 const mbedtls_md_info_t *md_info;
1805 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001806 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1809 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001810
Paul Bakker5121ce52009-01-03 21:22:43 +00001811 siglen = ctx->len;
1812
Paul Bakker27fdf462011-06-09 13:55:13 +00001813 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1817 ? mbedtls_rsa_public( ctx, sig, buf )
1818 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001819
1820 if( ret != 0 )
1821 return( ret );
1822
1823 p = buf;
1824
Paul Bakkerb3869132013-02-28 17:21:01 +01001825 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 {
Simon Butcher02037452016-03-01 21:19:12 +00001830 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001832 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001836 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001839 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001843 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001844
Paul Bakkerb3869132013-02-28 17:21:01 +01001845 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001846
Simon Butcher02037452016-03-01 21:19:12 +00001847 /*
1848 * Note: EMSA-PSS verification is over the length of N - 1 bits
1849 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001850 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001851
Simon Butcher02037452016-03-01 21:19:12 +00001852 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001853 if( msb % 8 == 0 )
1854 {
1855 p++;
1856 siglen -= 1;
1857 }
1858 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001862 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001863 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001864
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001865 if( ( ret = mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen,
1866 &md_ctx ) ) != 0 )
1867 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01001868
Paul Bakkerb3869132013-02-28 17:21:01 +01001869 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001870
Paul Bakker4de44aa2013-12-31 11:43:01 +01001871 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001872 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001873
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001874 if( p == buf + siglen || *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001875 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001876 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1877 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001878 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001879
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001880 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001881 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001884 slen != (size_t) expected_salt_len )
1885 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001886 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1887 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001888 }
1889
Simon Butcher02037452016-03-01 21:19:12 +00001890 /*
1891 * Generate H = Hash( M' )
1892 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001893 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1894 goto exit;
1895 if( ( ret = mbedtls_md_update( &md_ctx, zeros, 8 ) ) != 0 )
1896 goto exit;
1897 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1898 goto exit;
1899 if( ( ret = mbedtls_md_update( &md_ctx, p, slen ) ) != 0 )
1900 goto exit;
1901 if( ( ret = mbedtls_md_finish( &md_ctx, result ) ) != 0 )
1902 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00001903
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01001904 if( memcmp( p + slen, result, hlen ) != 0 )
1905 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001906 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01001907 goto exit;
1908 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001909
1910exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001912
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001913 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001914}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001915
1916/*
1917 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001920 int (*f_rng)(void *, unsigned char *, size_t),
1921 void *p_rng,
1922 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001924 unsigned int hashlen,
1925 const unsigned char *hash,
1926 const unsigned char *sig )
1927{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1929 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001930 : md_alg;
1931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001933 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001935 sig ) );
1936
1937}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001941/*
1942 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001945 int (*f_rng)(void *, unsigned char *, size_t),
1946 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001947 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001949 unsigned int hashlen,
1950 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001951 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001952{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001953 int ret = 0;
1954 const size_t sig_len = ctx->len;
1955 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1958 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001959
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001960 /*
1961 * Prepare expected PKCS1 v1.5 encoding of hash.
1962 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001963
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001964 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
1965 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
1966 {
1967 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1968 goto cleanup;
1969 }
1970
1971 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
1972 encoded_expected ) ) != 0 )
1973 goto cleanup;
1974
1975 /*
1976 * Apply RSA primitive to get what should be PKCS1 encoded hash.
1977 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001980 ? mbedtls_rsa_public( ctx, sig, encoded )
1981 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01001982 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001983 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001984
Simon Butcher02037452016-03-01 21:19:12 +00001985 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001986 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00001987 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001988
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001989 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
1990 sig_len ) ) != 0 )
1991 {
1992 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1993 goto cleanup;
1994 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001995
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001996cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02001997
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001998 if( encoded != NULL )
1999 {
2000 mbedtls_zeroize( encoded, sig_len );
2001 mbedtls_free( encoded );
2002 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002003
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002004 if( encoded_expected != NULL )
2005 {
2006 mbedtls_zeroize( encoded_expected, sig_len );
2007 mbedtls_free( encoded_expected );
2008 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002009
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002010 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002011}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
2014/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002015 * Do an RSA operation and check the message digest
2016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002018 int (*f_rng)(void *, unsigned char *, size_t),
2019 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002020 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002022 unsigned int hashlen,
2023 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002024 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002025{
2026 switch( ctx->padding )
2027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028#if defined(MBEDTLS_PKCS1_V15)
2029 case MBEDTLS_RSA_PKCS_V15:
2030 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002031 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002032#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#if defined(MBEDTLS_PKCS1_V21)
2035 case MBEDTLS_RSA_PKCS_V21:
2036 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002037 hashlen, hash, sig );
2038#endif
2039
2040 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002042 }
2043}
2044
2045/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002046 * Copy the components of an RSA key
2047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002049{
2050 int ret;
2051
2052 dst->ver = src->ver;
2053 dst->len = src->len;
2054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2056 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2059 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2060 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002061
2062#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2064 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2065 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2067 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002068#endif
2069
2070 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2073 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002074
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002075 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002076 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002077
2078cleanup:
2079 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002081
2082 return( ret );
2083}
2084
2085/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 * Free the components of an RSA key
2087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002091 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2092 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002094
Hanno Becker33c30a02017-08-23 07:00:22 +01002095#if !defined(MBEDTLS_RSA_NO_CRT)
2096 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2097 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2098 mbedtls_mpi_free( &ctx->DP );
2099#endif /* MBEDTLS_RSA_NO_CRT */
2100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101#if defined(MBEDTLS_THREADING_C)
2102 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002103#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002104}
2105
Hanno Beckerab377312017-08-23 16:24:51 +01002106#endif /* !MBEDTLS_RSA_ALT */
2107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002109
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002110#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002111
2112/*
2113 * Example RSA-1024 keypair, for test purposes
2114 */
2115#define KEY_LEN 128
2116
2117#define RSA_N "9292758453063D803DD603D5E777D788" \
2118 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2119 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2120 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2121 "93A89813FBF3C4F8066D2D800F7C38A8" \
2122 "1AE31942917403FF4946B0A83D3D3E05" \
2123 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2124 "5E94BB77B07507233A0BC7BAC8F90F79"
2125
2126#define RSA_E "10001"
2127
2128#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2129 "66CA472BC44D253102F8B4A9D3BFA750" \
2130 "91386C0077937FE33FA3252D28855837" \
2131 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2132 "DF79C5CE07EE72C7F123142198164234" \
2133 "CABB724CF78B8173B9F880FC86322407" \
2134 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2135 "071513A1E85B5DFA031F21ECAE91A34D"
2136
2137#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2138 "2C01CAD19EA484A87EA4377637E75500" \
2139 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2140 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2141
2142#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2143 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2144 "910E4168387E3C30AA1E00C339A79508" \
2145 "8452DD96A9A5EA5D9DCA68DA636032AF"
2146
Paul Bakker5121ce52009-01-03 21:22:43 +00002147#define PT_LEN 24
2148#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2149 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002152static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002153{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002154#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002155 size_t i;
2156
Paul Bakker545570e2010-07-18 09:00:25 +00002157 if( rng_state != NULL )
2158 rng_state = NULL;
2159
Paul Bakkera3d195c2011-11-27 21:07:34 +00002160 for( i = 0; i < len; ++i )
2161 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002162#else
2163 if( rng_state != NULL )
2164 rng_state = NULL;
2165
2166 arc4random_buf( output, len );
2167#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002168
Paul Bakkera3d195c2011-11-27 21:07:34 +00002169 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002172
Paul Bakker5121ce52009-01-03 21:22:43 +00002173/*
2174 * Checkup routine
2175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002177{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002178 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002180 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 unsigned char rsa_plaintext[PT_LEN];
2183 unsigned char rsa_decrypted[PT_LEN];
2184 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002186 unsigned char sha1sum[20];
2187#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002188
Hanno Becker3a701162017-08-22 13:52:43 +01002189 mbedtls_mpi K;
2190
2191 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002193
Hanno Becker3a701162017-08-22 13:52:43 +01002194 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2195 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2196 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2197 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2198 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2199 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2200 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2201 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2202 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2203 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2204
Hanno Becker7f25f852017-10-10 16:56:22 +01002205 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206
2207 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2211 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 {
2213 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215
2216 return( 1 );
2217 }
2218
2219 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002221
2222 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2223
Hanno Becker98838b02017-10-02 13:16:10 +01002224 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2225 PT_LEN, rsa_plaintext,
2226 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 {
2228 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002230
2231 return( 1 );
2232 }
2233
2234 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
Hanno Becker98838b02017-10-02 13:16:10 +01002237 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2238 &len, rsa_ciphertext, rsa_decrypted,
2239 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 {
2241 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002243
2244 return( 1 );
2245 }
2246
2247 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2248 {
2249 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002251
2252 return( 1 );
2253 }
2254
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002255 if( verbose != 0 )
2256 mbedtls_printf( "passed\n" );
2257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002260 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002262 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002263 {
2264 if( verbose != 0 )
2265 mbedtls_printf( "failed\n" );
2266
2267 return( 1 );
2268 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002269
Hanno Becker98838b02017-10-02 13:16:10 +01002270 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2271 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2272 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 {
2274 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
2277 return( 1 );
2278 }
2279
2280 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
Hanno Becker98838b02017-10-02 13:16:10 +01002283 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2284 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2285 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 {
2287 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002289
2290 return( 1 );
2291 }
2292
2293 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002294 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002296
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002297 if( verbose != 0 )
2298 mbedtls_printf( "\n" );
2299
Paul Bakker3d8fb632014-04-17 12:42:41 +02002300cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002301 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 mbedtls_rsa_free( &rsa );
2303#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002304 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002306 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002307}
2308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#endif /* MBEDTLS_RSA_C */