blob: 8c0d8c360358faec34a175be0248284d8b11601b [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 */
Paul Bakker48377d92013-08-30 12:06:24 +0200945static void 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;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000955 memset( counter, 0, 4 );
956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000958
Simon Butcher02037452016-03-01 21:19:12 +0000959 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000960 p = dst;
961
962 while( dlen > 0 )
963 {
964 use_len = hlen;
965 if( dlen < hlen )
966 use_len = dlen;
967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 mbedtls_md_starts( md_ctx );
969 mbedtls_md_update( md_ctx, src, slen );
970 mbedtls_md_update( md_ctx, counter, 4 );
971 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000972
973 for( i = 0; i < use_len; ++i )
974 *p++ ^= mask[i];
975
976 counter[3]++;
977
978 dlen -= use_len;
979 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200980
981 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000982}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100986/*
987 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100990 int (*f_rng)(void *, unsigned char *, size_t),
991 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100992 int mode,
993 const unsigned char *label, size_t label_len,
994 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100995 const unsigned char *input,
996 unsigned char *output )
997{
998 size_t olen;
999 int ret;
1000 unsigned char *p = output;
1001 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 const mbedtls_md_info_t *md_info;
1003 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1006 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001007
1008 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001012 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001014
1015 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001017
Simon Butcher02037452016-03-01 21:19:12 +00001018 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001019 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001021
1022 memset( output, 0, olen );
1023
1024 *p++ = 0;
1025
Simon Butcher02037452016-03-01 21:19:12 +00001026 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001027 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001029
1030 p += hlen;
1031
Simon Butcher02037452016-03-01 21:19:12 +00001032 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001034 p += hlen;
1035 p += olen - 2 * hlen - 2 - ilen;
1036 *p++ = 1;
1037 memcpy( p, input, ilen );
1038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001040 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1041 {
1042 mbedtls_md_free( &md_ctx );
1043 return( ret );
1044 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001045
Simon Butcher02037452016-03-01 21:19:12 +00001046 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001047 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1048 &md_ctx );
1049
Simon Butcher02037452016-03-01 21:19:12 +00001050 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001051 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1052 &md_ctx );
1053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 return( ( mode == MBEDTLS_RSA_PUBLIC )
1057 ? mbedtls_rsa_public( ctx, output, output )
1058 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001063/*
1064 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001067 int (*f_rng)(void *, unsigned char *, size_t),
1068 void *p_rng,
1069 int mode, size_t ilen,
1070 const unsigned char *input,
1071 unsigned char *output )
1072{
1073 size_t nb_pad, olen;
1074 int ret;
1075 unsigned char *p = output;
1076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1078 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001079
Janos Follath1ed9f992016-03-18 11:45:44 +00001080 // We don't check p_rng because it won't be dereferenced here
1081 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001083
1084 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001085
Simon Butcher02037452016-03-01 21:19:12 +00001086 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001087 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001089
1090 nb_pad = olen - 3 - ilen;
1091
1092 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001096
1097 while( nb_pad-- > 0 )
1098 {
1099 int rng_dl = 100;
1100
1101 do {
1102 ret = f_rng( p_rng, p, 1 );
1103 } while( *p == 0 && --rng_dl && ret == 0 );
1104
Simon Butcher02037452016-03-01 21:19:12 +00001105 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001106 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001108
1109 p++;
1110 }
1111 }
1112 else
1113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001115
1116 while( nb_pad-- > 0 )
1117 *p++ = 0xFF;
1118 }
1119
1120 *p++ = 0;
1121 memcpy( p, input, ilen );
1122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 return( ( mode == MBEDTLS_RSA_PUBLIC )
1124 ? mbedtls_rsa_public( ctx, output, output )
1125 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001128
Paul Bakker5121ce52009-01-03 21:22:43 +00001129/*
1130 * Add the message padding, then do an RSA operation
1131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001133 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001134 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001135 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001136 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 unsigned char *output )
1138{
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 switch( ctx->padding )
1140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_PKCS1_V15)
1142 case MBEDTLS_RSA_PKCS_V15:
1143 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001144 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001145#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#if defined(MBEDTLS_PKCS1_V21)
1148 case MBEDTLS_RSA_PKCS_V21:
1149 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001150 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001151#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
1153 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001156}
1157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001159/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001160 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001163 int (*f_rng)(void *, unsigned char *, size_t),
1164 void *p_rng,
1165 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001166 const unsigned char *label, size_t label_len,
1167 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001168 const unsigned char *input,
1169 unsigned char *output,
1170 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001171{
Paul Bakker23986e52011-04-24 08:57:21 +00001172 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001173 size_t ilen, i, pad_len;
1174 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1176 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001177 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 const mbedtls_md_info_t *md_info;
1179 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001180
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001181 /*
1182 * Parameters sanity checks
1183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1185 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001186
1187 ilen = ctx->len;
1188
Paul Bakker27fdf462011-06-09 13:55:13 +00001189 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001193 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001195
Janos Follathc17cda12016-02-11 11:08:18 +00001196 hlen = mbedtls_md_get_size( md_info );
1197
1198 // checking for integer underflow
1199 if( 2 * hlen + 2 > ilen )
1200 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1201
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001202 /*
1203 * RSA operation
1204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1206 ? mbedtls_rsa_public( ctx, input, buf )
1207 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001208
1209 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001210 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001211
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001212 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001213 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001216 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1217 {
1218 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001219 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001220 }
1221
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001222
1223 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001225
1226 /* seed: Apply seedMask to maskedSeed */
1227 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1228 &md_ctx );
1229
1230 /* DB: Apply dbMask to maskedDB */
1231 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1232 &md_ctx );
1233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001235
1236 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001237 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001238 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001240 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001242 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001243
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001244 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001245
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001246 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001247 for( i = 0; i < hlen; i++ )
1248 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001249
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001250 /* Get zero-padding len, but always read till end of buffer
1251 * (minus one, for the 01 byte) */
1252 pad_len = 0;
1253 pad_done = 0;
1254 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1255 {
1256 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001257 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001258 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001259
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001260 p += pad_len;
1261 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001262
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001263 /*
1264 * The only information "leaked" is whether the padding was correct or not
1265 * (eg, no data is copied if it was not correct). This meets the
1266 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1267 * the different error conditions.
1268 */
1269 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001270 {
1271 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1272 goto cleanup;
1273 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001274
Paul Bakker66d5d072014-06-17 16:39:18 +02001275 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001276 {
1277 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1278 goto cleanup;
1279 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001280
1281 *olen = ilen - (p - buf);
1282 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001283 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001284
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001285cleanup:
1286 mbedtls_zeroize( buf, sizeof( buf ) );
1287 mbedtls_zeroize( lhash, sizeof( lhash ) );
1288
1289 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001290}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001294/*
1295 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001298 int (*f_rng)(void *, unsigned char *, size_t),
1299 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001300 int mode, size_t *olen,
1301 const unsigned char *input,
1302 unsigned char *output,
1303 size_t output_max_len)
1304{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001305 int ret;
1306 size_t ilen, pad_count = 0, i;
1307 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1311 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001312
1313 ilen = ctx->len;
1314
1315 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1319 ? mbedtls_rsa_public( ctx, input, buf )
1320 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001321
1322 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001323 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001324
1325 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001326 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001327
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001328 /*
1329 * Check and get padding len in "constant-time"
1330 */
1331 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001332
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001333 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001338 /* Get padding len, but always read till end of buffer
1339 * (minus one, for the 00 byte) */
1340 for( i = 0; i < ilen - 3; i++ )
1341 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001342 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1343 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001344 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001345
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001346 p += pad_count;
1347 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001348 }
1349 else
1350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001352
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 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001357 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001358 pad_count += ( pad_done == 0 );
1359 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001360
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001361 p += pad_count;
1362 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 }
1364
Janos Follathc69fa502016-02-12 13:30:09 +00001365 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001366
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001367 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001368 {
1369 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1370 goto cleanup;
1371 }
Paul Bakker8804f692013-02-28 18:06:26 +01001372
Paul Bakker66d5d072014-06-17 16:39:18 +02001373 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001374 {
1375 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1376 goto cleanup;
1377 }
Paul Bakker060c5682009-01-12 21:48:39 +00001378
Paul Bakker27fdf462011-06-09 13:55:13 +00001379 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001381 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001383cleanup:
1384 mbedtls_zeroize( buf, sizeof( buf ) );
1385
1386 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
1390/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001391 * Do an RSA operation, then remove the message padding
1392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001394 int (*f_rng)(void *, unsigned char *, size_t),
1395 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001396 int mode, size_t *olen,
1397 const unsigned char *input,
1398 unsigned char *output,
1399 size_t output_max_len)
1400{
1401 switch( ctx->padding )
1402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#if defined(MBEDTLS_PKCS1_V15)
1404 case MBEDTLS_RSA_PKCS_V15:
1405 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001406 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001407#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_PKCS1_V21)
1410 case MBEDTLS_RSA_PKCS_V21:
1411 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001412 olen, input, output,
1413 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001414#endif
1415
1416 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001418 }
1419}
1420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001422/*
1423 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001426 int (*f_rng)(void *, unsigned char *, size_t),
1427 void *p_rng,
1428 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001430 unsigned int hashlen,
1431 const unsigned char *hash,
1432 unsigned char *sig )
1433{
1434 size_t olen;
1435 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001437 unsigned int slen, hlen, offset = 0;
1438 int ret;
1439 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 const mbedtls_md_info_t *md_info;
1441 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1444 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001445
1446 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001448
1449 olen = ctx->len;
1450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001452 {
Simon Butcher02037452016-03-01 21:19:12 +00001453 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001455 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001459 }
1460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001462 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001466 slen = hlen;
1467
1468 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001470
1471 memset( sig, 0, olen );
1472
Simon Butcher02037452016-03-01 21:19:12 +00001473 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001474 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001476
Simon Butcher02037452016-03-01 21:19:12 +00001477 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001478 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001479 p += olen - hlen * 2 - 2;
1480 *p++ = 0x01;
1481 memcpy( p, salt, slen );
1482 p += slen;
1483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001485 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1486 {
1487 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001488 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001489 return( ret );
1490 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001491
Simon Butcher02037452016-03-01 21:19:12 +00001492 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 mbedtls_md_starts( &md_ctx );
1494 mbedtls_md_update( &md_ctx, p, 8 );
1495 mbedtls_md_update( &md_ctx, hash, hashlen );
1496 mbedtls_md_update( &md_ctx, salt, slen );
1497 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001498 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001499
Simon Butcher02037452016-03-01 21:19:12 +00001500 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001501 if( msb % 8 == 0 )
1502 offset = 1;
1503
Simon Butcher02037452016-03-01 21:19:12 +00001504 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001505 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001508
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001509 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001510 sig[0] &= 0xFF >> ( olen * 8 - msb );
1511
1512 p += hlen;
1513 *p++ = 0xBC;
1514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 return( ( mode == MBEDTLS_RSA_PUBLIC )
1516 ? mbedtls_rsa_public( ctx, sig, sig )
1517 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001518}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001522/*
1523 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1524 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001525
1526/* Construct a PKCS v1.5 encoding of a hashed message
1527 *
1528 * This is used both for signature generation and verification.
1529 *
1530 * Parameters:
1531 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001532 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001533 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001534 * - hash: Buffer containing the hashed message or the raw data.
1535 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001536 * - dst: Buffer to hold the encoded message.
1537 *
1538 * Assumptions:
1539 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1540 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001541 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001542 *
1543 */
1544static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1545 unsigned int hashlen,
1546 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001547 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001548 unsigned char *dst )
1549{
1550 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001551 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001552 unsigned char *p = dst;
1553 const char *oid = NULL;
1554
1555 /* Are we signing hashed or raw data? */
1556 if( md_alg != MBEDTLS_MD_NONE )
1557 {
1558 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1559 if( md_info == NULL )
1560 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1561
1562 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1563 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1564
1565 hashlen = mbedtls_md_get_size( md_info );
1566
1567 /* Double-check that 8 + hashlen + oid_size can be used as a
1568 * 1-byte ASN.1 length encoding and that there's no overflow. */
1569 if( 8 + hashlen + oid_size >= 0x80 ||
1570 10 + hashlen < hashlen ||
1571 10 + hashlen + oid_size < 10 + hashlen )
1572 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1573
1574 /*
1575 * Static bounds check:
1576 * - Need 10 bytes for five tag-length pairs.
1577 * (Insist on 1-byte length encodings to protect against variants of
1578 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1579 * - Need hashlen bytes for hash
1580 * - Need oid_size bytes for hash alg OID.
1581 */
1582 if( nb_pad < 10 + hashlen + oid_size )
1583 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1584 nb_pad -= 10 + hashlen + oid_size;
1585 }
1586 else
1587 {
1588 if( nb_pad < hashlen )
1589 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1590
1591 nb_pad -= hashlen;
1592 }
1593
Hanno Becker2b2f8982017-09-27 17:10:03 +01001594 /* Need space for signature header and padding delimiter (3 bytes),
1595 * and 8 bytes for the minimal padding */
1596 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001597 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1598 nb_pad -= 3;
1599
1600 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001601 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001602
1603 /* Write signature header and padding */
1604 *p++ = 0;
1605 *p++ = MBEDTLS_RSA_SIGN;
1606 memset( p, 0xFF, nb_pad );
1607 p += nb_pad;
1608 *p++ = 0;
1609
1610 /* Are we signing raw data? */
1611 if( md_alg == MBEDTLS_MD_NONE )
1612 {
1613 memcpy( p, hash, hashlen );
1614 return( 0 );
1615 }
1616
1617 /* Signing hashed data, add corresponding ASN.1 structure
1618 *
1619 * DigestInfo ::= SEQUENCE {
1620 * digestAlgorithm DigestAlgorithmIdentifier,
1621 * digest Digest }
1622 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1623 * Digest ::= OCTET STRING
1624 *
1625 * Schematic:
1626 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1627 * TAG-NULL + LEN [ NULL ] ]
1628 * TAG-OCTET + LEN [ HASH ] ]
1629 */
1630 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001631 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001632 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001633 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001634 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001635 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001636 memcpy( p, oid, oid_size );
1637 p += oid_size;
1638 *p++ = MBEDTLS_ASN1_NULL;
1639 *p++ = 0x00;
1640 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001641 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001642 memcpy( p, hash, hashlen );
1643 p += hashlen;
1644
1645 /* Just a sanity-check, should be automatic
1646 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001647 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001648 {
Hanno Beckere58d38c2017-09-27 17:09:00 +01001649 mbedtls_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001650 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1651 }
1652
1653 return( 0 );
1654}
1655
Paul Bakkerb3869132013-02-28 17:21:01 +01001656/*
1657 * Do an RSA operation to sign the message digest
1658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001660 int (*f_rng)(void *, unsigned char *, size_t),
1661 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001662 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001664 unsigned int hashlen,
1665 const unsigned char *hash,
1666 unsigned char *sig )
1667{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001668 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001669 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1672 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001673
Hanno Beckerfdf38032017-09-06 12:35:55 +01001674 /*
1675 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1676 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001677
Hanno Beckerfdf38032017-09-06 12:35:55 +01001678 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1679 ctx->len, sig ) ) != 0 )
1680 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001681
1682 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001683 * Call respective RSA primitive
1684 */
1685
1686 if( mode == MBEDTLS_RSA_PUBLIC )
1687 {
1688 /* Skip verification on a public key operation */
1689 return( mbedtls_rsa_public( ctx, sig, sig ) );
1690 }
1691
1692 /* Private key operation
1693 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001694 * In order to prevent Lenstra's attack, make the signature in a
1695 * temporary buffer and check it before returning it.
1696 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001697
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001698 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001699 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001700 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1701
Hanno Beckerfdf38032017-09-06 12:35:55 +01001702 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001703 if( verif == NULL )
1704 {
1705 mbedtls_free( sig_try );
1706 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1707 }
1708
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001709 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1710 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1711
Hanno Becker171a8f12017-09-06 12:32:16 +01001712 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001713 {
1714 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1715 goto cleanup;
1716 }
1717
1718 memcpy( sig, sig_try, ctx->len );
1719
1720cleanup:
1721 mbedtls_free( sig_try );
1722 mbedtls_free( verif );
1723
1724 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001727
1728/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001729 * Do an RSA operation to sign the message digest
1730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001732 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001733 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001734 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001736 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001737 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001738 unsigned char *sig )
1739{
Paul Bakker5121ce52009-01-03 21:22:43 +00001740 switch( ctx->padding )
1741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#if defined(MBEDTLS_PKCS1_V15)
1743 case MBEDTLS_RSA_PKCS_V15:
1744 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001745 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001746#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748#if defined(MBEDTLS_PKCS1_V21)
1749 case MBEDTLS_RSA_PKCS_V21:
1750 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001751 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001752#endif
1753
Paul Bakker5121ce52009-01-03 21:22:43 +00001754 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001756 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001757}
1758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001760/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001761 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001764 int (*f_rng)(void *, unsigned char *, size_t),
1765 void *p_rng,
1766 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001768 unsigned int hashlen,
1769 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001771 int expected_salt_len,
1772 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001773{
Paul Bakker23986e52011-04-24 08:57:21 +00001774 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001775 size_t siglen;
1776 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001778 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001779 unsigned int hlen;
1780 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 const mbedtls_md_info_t *md_info;
1782 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001783 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1786 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001787
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 siglen = ctx->len;
1789
Paul Bakker27fdf462011-06-09 13:55:13 +00001790 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1794 ? mbedtls_rsa_public( ctx, sig, buf )
1795 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001796
1797 if( ret != 0 )
1798 return( ret );
1799
1800 p = buf;
1801
Paul Bakkerb3869132013-02-28 17:21:01 +01001802 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001806 {
Simon Butcher02037452016-03-01 21:19:12 +00001807 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001809 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001813 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001816 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001820 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001821
Paul Bakkerb3869132013-02-28 17:21:01 +01001822 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001823
Simon Butcher02037452016-03-01 21:19:12 +00001824 /*
1825 * Note: EMSA-PSS verification is over the length of N - 1 bits
1826 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001827 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001828
Simon Butcher02037452016-03-01 21:19:12 +00001829 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001830 if( msb % 8 == 0 )
1831 {
1832 p++;
1833 siglen -= 1;
1834 }
1835 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001839 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1840 {
1841 mbedtls_md_free( &md_ctx );
1842 return( ret );
1843 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001844
Paul Bakkerb3869132013-02-28 17:21:01 +01001845 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001846
Paul Bakkerb3869132013-02-28 17:21:01 +01001847 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001848
Paul Bakker4de44aa2013-12-31 11:43:01 +01001849 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001850 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001851
Paul Bakkerb3869132013-02-28 17:21:01 +01001852 if( p == buf + siglen ||
1853 *p++ != 0x01 )
1854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 mbedtls_md_free( &md_ctx );
1856 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001857 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001858
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001859 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001860 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001863 slen != (size_t) expected_salt_len )
1864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 mbedtls_md_free( &md_ctx );
1866 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001867 }
1868
Simon Butcher02037452016-03-01 21:19:12 +00001869 /*
1870 * Generate H = Hash( M' )
1871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 mbedtls_md_starts( &md_ctx );
1873 mbedtls_md_update( &md_ctx, zeros, 8 );
1874 mbedtls_md_update( &md_ctx, hash, hashlen );
1875 mbedtls_md_update( &md_ctx, p, slen );
1876 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001879
Paul Bakkerb3869132013-02-28 17:21:01 +01001880 if( memcmp( p + slen, result, hlen ) == 0 )
1881 return( 0 );
1882 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001884}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001885
1886/*
1887 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001890 int (*f_rng)(void *, unsigned char *, size_t),
1891 void *p_rng,
1892 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001894 unsigned int hashlen,
1895 const unsigned char *hash,
1896 const unsigned char *sig )
1897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1899 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001900 : md_alg;
1901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001903 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001905 sig ) );
1906
1907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001911/*
1912 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001915 int (*f_rng)(void *, unsigned char *, size_t),
1916 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001917 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001919 unsigned int hashlen,
1920 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001921 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001922{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001923 int ret = 0;
1924 const size_t sig_len = ctx->len;
1925 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1928 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001929
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001930 /*
1931 * Prepare expected PKCS1 v1.5 encoding of hash.
1932 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001933
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001934 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
1935 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
1936 {
1937 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1938 goto cleanup;
1939 }
1940
1941 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
1942 encoded_expected ) ) != 0 )
1943 goto cleanup;
1944
1945 /*
1946 * Apply RSA primitive to get what should be PKCS1 encoded hash.
1947 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001950 ? mbedtls_rsa_public( ctx, sig, encoded )
1951 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01001952 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001953 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001954
Simon Butcher02037452016-03-01 21:19:12 +00001955 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001956 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00001957 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001958
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001959 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
1960 sig_len ) ) != 0 )
1961 {
1962 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1963 goto cleanup;
1964 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001965
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001966cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02001967
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001968 if( encoded != NULL )
1969 {
1970 mbedtls_zeroize( encoded, sig_len );
1971 mbedtls_free( encoded );
1972 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001973
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001974 if( encoded_expected != NULL )
1975 {
1976 mbedtls_zeroize( encoded_expected, sig_len );
1977 mbedtls_free( encoded_expected );
1978 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001979
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001980 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001981}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001983
1984/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001985 * Do an RSA operation and check the message digest
1986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001988 int (*f_rng)(void *, unsigned char *, size_t),
1989 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001990 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001992 unsigned int hashlen,
1993 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001994 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001995{
1996 switch( ctx->padding )
1997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998#if defined(MBEDTLS_PKCS1_V15)
1999 case MBEDTLS_RSA_PKCS_V15:
2000 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002001 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002002#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#if defined(MBEDTLS_PKCS1_V21)
2005 case MBEDTLS_RSA_PKCS_V21:
2006 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002007 hashlen, hash, sig );
2008#endif
2009
2010 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002012 }
2013}
2014
2015/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002016 * Copy the components of an RSA key
2017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002019{
2020 int ret;
2021
2022 dst->ver = src->ver;
2023 dst->len = src->len;
2024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2026 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2029 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2030 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002031
2032#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2034 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2035 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2037 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002038#endif
2039
2040 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2043 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002044
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002045 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002046 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002047
2048cleanup:
2049 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002051
2052 return( ret );
2053}
2054
2055/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002056 * Free the components of an RSA key
2057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002061 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2062 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002064
Hanno Becker33c30a02017-08-23 07:00:22 +01002065#if !defined(MBEDTLS_RSA_NO_CRT)
2066 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2067 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2068 mbedtls_mpi_free( &ctx->DP );
2069#endif /* MBEDTLS_RSA_NO_CRT */
2070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#if defined(MBEDTLS_THREADING_C)
2072 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002073#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002074}
2075
Hanno Beckerab377312017-08-23 16:24:51 +01002076#endif /* !MBEDTLS_RSA_ALT */
2077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002079
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002080#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
2082/*
2083 * Example RSA-1024 keypair, for test purposes
2084 */
2085#define KEY_LEN 128
2086
2087#define RSA_N "9292758453063D803DD603D5E777D788" \
2088 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2089 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2090 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2091 "93A89813FBF3C4F8066D2D800F7C38A8" \
2092 "1AE31942917403FF4946B0A83D3D3E05" \
2093 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2094 "5E94BB77B07507233A0BC7BAC8F90F79"
2095
2096#define RSA_E "10001"
2097
2098#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2099 "66CA472BC44D253102F8B4A9D3BFA750" \
2100 "91386C0077937FE33FA3252D28855837" \
2101 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2102 "DF79C5CE07EE72C7F123142198164234" \
2103 "CABB724CF78B8173B9F880FC86322407" \
2104 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2105 "071513A1E85B5DFA031F21ECAE91A34D"
2106
2107#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2108 "2C01CAD19EA484A87EA4377637E75500" \
2109 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2110 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2111
2112#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2113 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2114 "910E4168387E3C30AA1E00C339A79508" \
2115 "8452DD96A9A5EA5D9DCA68DA636032AF"
2116
Paul Bakker5121ce52009-01-03 21:22:43 +00002117#define PT_LEN 24
2118#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2119 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002122static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002123{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002124#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002125 size_t i;
2126
Paul Bakker545570e2010-07-18 09:00:25 +00002127 if( rng_state != NULL )
2128 rng_state = NULL;
2129
Paul Bakkera3d195c2011-11-27 21:07:34 +00002130 for( i = 0; i < len; ++i )
2131 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002132#else
2133 if( rng_state != NULL )
2134 rng_state = NULL;
2135
2136 arc4random_buf( output, len );
2137#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002138
Paul Bakkera3d195c2011-11-27 21:07:34 +00002139 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002142
Paul Bakker5121ce52009-01-03 21:22:43 +00002143/*
2144 * Checkup routine
2145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002147{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002148 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002150 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 unsigned char rsa_plaintext[PT_LEN];
2153 unsigned char rsa_decrypted[PT_LEN];
2154 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002156 unsigned char sha1sum[20];
2157#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002158
Hanno Becker3a701162017-08-22 13:52:43 +01002159 mbedtls_mpi K;
2160
2161 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002163
Hanno Becker3a701162017-08-22 13:52:43 +01002164 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2165 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2166 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2167 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2168 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2169 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2170 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2171 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2172 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2173 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2174
Hanno Becker7f25f852017-10-10 16:56:22 +01002175 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002176
2177 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2181 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 {
2183 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
2186 return( 1 );
2187 }
2188
2189 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191
2192 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2193
Hanno Becker98838b02017-10-02 13:16:10 +01002194 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2195 PT_LEN, rsa_plaintext,
2196 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 {
2198 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002200
2201 return( 1 );
2202 }
2203
2204 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206
Hanno Becker98838b02017-10-02 13:16:10 +01002207 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2208 &len, rsa_ciphertext, rsa_decrypted,
2209 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 {
2211 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002213
2214 return( 1 );
2215 }
2216
2217 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2218 {
2219 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002221
2222 return( 1 );
2223 }
2224
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002225 if( verbose != 0 )
2226 mbedtls_printf( "passed\n" );
2227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002230 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002233
Hanno Becker98838b02017-10-02 13:16:10 +01002234 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2235 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2236 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 {
2238 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
2241 return( 1 );
2242 }
2243
2244 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002246
Hanno Becker98838b02017-10-02 13:16:10 +01002247 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2248 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2249 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 {
2251 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002253
2254 return( 1 );
2255 }
2256
2257 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002258 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002260
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002261 if( verbose != 0 )
2262 mbedtls_printf( "\n" );
2263
Paul Bakker3d8fb632014-04-17 12:42:41 +02002264cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002265 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 mbedtls_rsa_free( &rsa );
2267#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002268 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002270 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002271}
2272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#endif /* MBEDTLS_RSA_C */