blob: 72f661061cf6b3450cf12a23cd622e6ba76dad3e [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 */
21/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000022 * The following sources were referenced in the design of this implementation
23 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000024 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000025 * [1] A method for obtaining digital signatures and public-key cryptosystems
26 * R Rivest, A Shamir, and L Adleman
27 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
28 *
29 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
30 * Menezes, van Oorschot and Vanstone
31 *
Janos Follathe81102e2017-03-22 13:38:28 +000032 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
33 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
34 * Stefan Mangard
35 * https://arxiv.org/abs/1702.08719v2
36 *
Paul Bakker5121ce52009-01-03 21:22:43 +000037 */
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020041#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/rsa.h"
48#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#else
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020065#define mbedtls_calloc calloc
66#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#endif
68
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010069/* Implementation that should never be optimized out by the compiler */
70static void mbedtls_zeroize( void *v, size_t n ) {
71 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
72}
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010075 * Context-independent RSA helper functions.
76 *
77 * The following three functions
78 * - mbedtls_rsa_deduce_moduli
79 * - mbedtls_rsa_deduce_private
80 * - mbedtls_rsa_check_params
81 * are helper functions operating on the core RSA parameters
82 * (represented as MPI's). They do not use the RSA context structure
83 * and therefore need not be replaced when providing an alternative
84 * RSA implementation.
85 *
86 * Their purpose is to provide common MPI operations in the context
87 * of RSA that can be easily shared across multiple implementations.
88 */
89
90/*
91 * mbedtls_rsa_deduce_moduli
92 *
93 * Given the modulus N=PQ and a pair of public and private
94 * exponents E and D, respectively, factor N.
95 *
96 * Setting F := lcm(P-1,Q-1), the idea is as follows:
97 *
98 * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
99 * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
100 * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
101 * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
102 * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
103 * factors of N.
104 *
105 * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
106 * construction still applies since (-)^K is the identity on the set of
107 * roots of 1 in Z/NZ.
108 *
109 * The public and private key primitives (-)^E and (-)^D are mutually inverse
110 * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
111 * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
112 * Splitting L = 2^t * K with K odd, we have
113 *
114 * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
115 *
116 * so (F / 2) * K is among the numbers
117 *
118 * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
119 *
120 * where ord is the order of 2 in (DE - 1).
121 * We can therefore iterate through these numbers apply the construction
122 * of (a) and (b) above to attempt to factor N.
123 *
124 */
125int mbedtls_rsa_deduce_moduli( mbedtls_mpi *N, mbedtls_mpi *D, mbedtls_mpi *E,
126 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
127 mbedtls_mpi *P, mbedtls_mpi *Q )
128{
129 /* Implementation note:
130 *
131 * Space-efficiency is given preference over time-efficiency here:
132 * several calculations are done in place and temporarily change
133 * the values of D and E.
134 *
135 * Specifically, D is replaced the largest odd divisor of DE - 1
136 * throughout the calculations.
137 */
138
139 int ret = 0;
140
141 uint16_t attempt; /* Number of current attempt */
142 uint16_t iter; /* Number of squares computed in the current attempt */
143
144 uint16_t bitlen_half; /* Half the bitsize of the modulus N */
145 uint16_t order; /* Order of 2 in DE - 1 */
146
147 mbedtls_mpi K; /* Temporary used for two purposes:
148 * - During factorization attempts, stores a andom integer
149 * in the range of [0,..,N]
150 * - During verification, holding intermediate results.
151 */
152
153 if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
154 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
155
156 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
157 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
158 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
159 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
160 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
161 {
162 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
163 }
164
165 /*
166 * Initializations and temporary changes
167 */
168
169 mbedtls_mpi_init( &K );
170 mbedtls_mpi_init( P );
171 mbedtls_mpi_init( Q );
172
173 /* Replace D by DE - 1 */
174 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( D, D, E ) );
175 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( D, D, 1 ) );
176
177 if( ( order = mbedtls_mpi_lsb( D ) ) == 0 )
178 {
179 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
180 goto cleanup;
181 }
182
183 /* After this operation, D holds the largest odd divisor
184 * of DE - 1 for the original values of D and E. */
185 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( D, order ) );
186
187 /* This is used to generate a few numbers around N / 2
188 * if no PRNG is provided. */
189 if( f_rng == NULL )
190 bitlen_half = mbedtls_mpi_bitlen( N ) / 2;
191
192 /*
193 * Actual work
194 */
195
196 for( attempt = 0; attempt < 30; ++attempt )
197 {
198 /* Generate some number in [0,N], either randomly
199 * if a PRNG is given, or try numbers around N/2 */
200 if( f_rng != NULL )
201 {
202 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &K,
203 mbedtls_mpi_size( N ),
204 f_rng, p_rng ) );
205 }
206 else
207 {
208 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &K, 1 ) ) ;
209 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &K, bitlen_half ) ) ;
210 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, attempt + 1 ) );
211 }
212
213 /* Check if gcd(K,N) = 1 */
214 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
215 if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
216 continue;
217
218 /* Go through K^X + 1, K^(2X) + 1, K^(4X) + 1, ...
219 * and check whether they have nontrivial GCD with N. */
220 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, D, N,
221 Q /* temporarily use Q for storing Montgomery
222 * multiplication helper values */ ) );
223
224 for( iter = 1; iter < order; ++iter )
225 {
226 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
227 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
228
229 if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
230 mbedtls_mpi_cmp_mpi( P, N ) == -1 )
231 {
232 /*
233 * Have found a nontrivial divisor P of N.
234 * Set Q := N / P and verify D, E.
235 */
236
237 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, &K, N, P ) );
238
239 /*
240 * Verify that DE - 1 is indeed a multiple of
241 * lcm(P-1, Q-1), i.e. that it's a multiple of both
242 * P-1 and Q-1.
243 */
244
245 /* Restore DE - 1 and temporarily replace P, Q by P-1, Q-1. */
246 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( D, order ) );
247 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) );
248 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) );
249
250 /* Compute DE-1 mod P-1 */
251 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, D, P ) );
252 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
253 {
254 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
255 goto cleanup;
256 }
257
258 /* Compute DE-1 mod Q-1 */
259 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, D, Q ) );
260 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
261 {
262 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
263 goto cleanup;
264 }
265
266 /*
267 * All good, restore P, Q and D and return.
268 */
269
270 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) );
271 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) );
272 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( D, D, 1 ) );
273 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( D, NULL, D, E ) );
274
275 goto cleanup;
276 }
277
278 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
279 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
280 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
281 }
282 }
283
284 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
285
286cleanup:
287
288 mbedtls_mpi_free( &K );
289 return( ret );
290}
291
292/*
293 * Given P, Q and the public exponent E, deduce D.
294 * This is essentially a modular inversion.
295 */
296
297int mbedtls_rsa_deduce_private( mbedtls_mpi *P, mbedtls_mpi *Q,
298 mbedtls_mpi *D, mbedtls_mpi *E )
299{
300 int ret = 0;
301 mbedtls_mpi K;
302
303 if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
304 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
305
306 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
307 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
308 mbedtls_mpi_cmp_int( E, 0 ) == 0 )
309 {
310 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
311 }
312
313 mbedtls_mpi_init( &K );
314
315 /* Temporarily replace P and Q by P-1 and Q-1, respectively. */
316 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) );
317 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) );
318
319 /* Temporarily compute the gcd(P-1, Q-1) in D. */
320 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, P, Q ) );
321
322 /* Compute LCM(P-1, Q-1) in K */
323 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
324 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
325
326 /* Compute modular inverse of E in LCM(P-1, Q-1) */
327 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
328
329 /* Restore P and Q. */
330 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) );
331 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) );
332
333 /* Double-check result */
334 MBEDTLS_MPI_CHK( mbedtls_rsa_check_params( NULL, P, Q, D, E, NULL, NULL ) );
335
336cleanup:
337
338 mbedtls_mpi_free( &K );
339
340 return( ret );
341}
342
343/*
344 * Check that core RSA parameters are sane.
345 *
346 * Note that the inputs are not declared const and may be
347 * altered on an unsuccessful run.
348 */
349
350int mbedtls_rsa_check_params( mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
351 mbedtls_mpi *D, mbedtls_mpi *E,
352 int (*f_rng)(void *, unsigned char *, size_t),
353 void *p_rng )
354{
355 int ret = 0;
356 mbedtls_mpi K;
357
358 mbedtls_mpi_init( &K );
359
360 /*
361 * Step 1: If PRNG provided, check that P and Q are prime
362 */
363
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100364#if defined(MBEDTLS_GENPRIME)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100365 if( f_rng != NULL && P != NULL &&
366 ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
367 {
368 goto cleanup;
369 }
370
371 if( f_rng != NULL && Q != NULL &&
372 ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
373 {
374 goto cleanup;
375 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100376#else
377 ((void) f_rng);
378 ((void) p_rng);
379#endif /* MBEDTLS_GENPRIME */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100380
381 /*
382 * Step 2: Check that N = PQ
383 */
384
385 if( P != NULL && Q != NULL && N != NULL )
386 {
387 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
388 if( mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
389 {
390 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
391 goto cleanup;
392 }
393 }
394
395 /*
396 * Step 3: Check that D, E are inverse modulo P-1 and Q-1
397 */
398
399 if( P != NULL && Q != NULL && D != NULL && E != NULL )
400 {
401 /* Temporarily replace P, Q by P-1, Q-1. */
402 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) );
403 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) );
404
405 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
406 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
407
408 /* Compute DE-1 mod P-1 */
409 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
410 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
411 {
412 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
413 goto cleanup;
414 }
415
416 /* Compute DE-1 mod Q-1 */
417 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, Q ) );
418 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
419 {
420 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
421 goto cleanup;
422 }
423
424 /* Restore P, Q. */
425 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) );
426 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) );
427 }
428
429cleanup:
430
431 mbedtls_mpi_free( &K );
432
433 return( ret );
434}
435
436int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
437 const mbedtls_mpi *D, mbedtls_mpi *DP,
438 mbedtls_mpi *DQ, mbedtls_mpi *QP )
439{
440 int ret = 0;
441 mbedtls_mpi K;
442 mbedtls_mpi_init( &K );
443
444 if( DP != NULL )
445 {
446 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
447 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
448 }
449
450 if( DQ != NULL )
451 {
452 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
453 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
454 }
455
456 if( QP != NULL )
457 {
458 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
459 }
460
461cleanup:
462 mbedtls_mpi_free( &K );
463
464 return( ret );
465}
466
Hanno Becker617c1ae2017-08-23 14:11:24 +0100467
468/*
469 * Default RSA interface implementation
470 */
471
Hanno Beckerab377312017-08-23 16:24:51 +0100472#if !defined(MBEDTLS_RSA_ALT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100473
474int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
475 const mbedtls_mpi *N,
476 const mbedtls_mpi *P, const mbedtls_mpi *Q,
477 const mbedtls_mpi *D, const mbedtls_mpi *E )
478{
479 int ret;
480
481 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
482 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
483 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
484 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
485 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
486 {
487 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
488 }
489
490 if( N != NULL )
491 ctx->len = mbedtls_mpi_size( &ctx->N );
492
493 return( 0 );
494}
495
496int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
497 unsigned char *N, size_t N_len,
498 unsigned char *P, size_t P_len,
499 unsigned char *Q, size_t Q_len,
500 unsigned char *D, size_t D_len,
501 unsigned char *E, size_t E_len )
502{
503 int ret;
504
505 if( N != NULL )
506 {
507 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
508 ctx->len = mbedtls_mpi_size( &ctx->N );
509 }
510
511 if( P != NULL )
512 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
513
514 if( Q != NULL )
515 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
516
517 if( D != NULL )
518 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
519
520 if( E != NULL )
521 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
522
523cleanup:
524
525 if( ret != 0 )
526 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
527
528 return( 0 );
529}
530
531int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
532 int (*f_rng)(void *, unsigned char *, size_t),
533 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100534{
535 int ret = 0;
536
Hanno Becker617c1ae2017-08-23 14:11:24 +0100537 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
538 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
539 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
540 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
541 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100542
Hanno Becker617c1ae2017-08-23 14:11:24 +0100543 /*
544 * Check whether provided parameters are enough
545 * to deduce all others. The following incomplete
546 * parameter sets for private keys are supported:
547 *
548 * (1) P, Q missing.
549 * (2) D and potentially N missing.
550 *
551 */
552 const int complete = have_N && have_P && have_Q && have_D && have_E;
553 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
554 const int d_missing = have_P && have_Q && !have_D && have_E;
555 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100556
Hanno Becker617c1ae2017-08-23 14:11:24 +0100557 const int is_priv = complete || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100558
Hanno Becker617c1ae2017-08-23 14:11:24 +0100559 if( !is_priv && !is_pub )
560 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
561
562 /*
563 * Step 1: Deduce and verify all core parameters.
564 */
565
566 if( pq_missing )
567 {
568 /* This includes sanity checking of core parameters,
569 * so no further checks necessary. */
570 ret = mbedtls_rsa_deduce_moduli( &ctx->N, &ctx->D, &ctx->E,
571 f_rng, p_rng,
572 &ctx->P, &ctx->Q );
573 if( ret != 0 )
574 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
575
576 }
577 else if( d_missing )
578 {
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100579#if defined(MBEDTLS_GENPRIME)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100580 /* If a PRNG is provided, check if P, Q are prime. */
581 if( f_rng != NULL &&
582 ( ( ret = mbedtls_mpi_is_prime( &ctx->P, f_rng, p_rng ) ) != 0 ||
583 ( ret = mbedtls_mpi_is_prime( &ctx->Q, f_rng, p_rng ) ) != 0 ) )
584 {
585 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
586 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100587#endif /* MBEDTLS_GENPRIME */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100588
589 /* Compute N if missing. */
590 if( !have_N &&
591 ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) ) != 0 )
592 {
593 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
594 }
595
596 /* Deduce private exponent. This includes double-checking of the result,
597 * so together with the primality test above all core parameters are
598 * guaranteed to be sane if this call succeeds. */
599 if( ( ret = mbedtls_rsa_deduce_private( &ctx->P, &ctx->Q,
600 &ctx->D, &ctx->E ) ) != 0 )
601 {
602 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
603 }
604 }
605 else if( complete )
606 {
607 /* Check complete set of imported core parameters. */
608 if( ( ret = mbedtls_rsa_check_params( &ctx->N, &ctx->P, &ctx->Q,
609 &ctx->D, &ctx->E,
610 f_rng, p_rng ) ) != 0 )
611 {
612 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
613 }
614 }
615
616 /* In the remaining case of a public key, there's nothing to check for. */
617
618 /*
619 * Step 2: Deduce all additional parameters specific
620 * to our current RSA implementaiton.
621 */
622
Hanno Becker23344b52017-08-23 07:43:27 +0100623#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100624 if( is_priv )
625 {
626 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
627 &ctx->DP, &ctx->DQ, &ctx->QP );
628 if( ret != 0 )
629 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
630 }
Hanno Becker23344b52017-08-23 07:43:27 +0100631#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100632
633 /*
634 * Step 3: Double check
635 */
636
637 if( is_priv )
638 {
639 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
640 return( ret );
641 }
642 else
643 {
644 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
645 return( ret );
646 }
647
648 return( 0 );
649}
650
651/*
652 * Check if CRT parameters match RSA context.
653 * This has to be implemented even if CRT is not used,
654 * in order to be able to validate DER encoded RSA keys,
655 * which always contain CRT parameters.
656 */
657int mbedtls_rsa_check_crt( mbedtls_rsa_context *ctx, mbedtls_mpi *DP,
658 mbedtls_mpi *DQ, mbedtls_mpi *QP )
659{
Hanno Becker23344b52017-08-23 07:43:27 +0100660 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100661
Hanno Becker23344b52017-08-23 07:43:27 +0100662 /* Check if key is private or public */
663 const int is_priv =
664 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
665 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
666 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
667 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
668 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
669
670 if( !is_priv )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100671 {
672 /* Checking optional parameters only makes sense for private keys. */
673 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
674 }
675
Hanno Becker23344b52017-08-23 07:43:27 +0100676#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100677 if( ( DP != NULL && mbedtls_mpi_cmp_mpi( DP, &ctx->DP ) != 0 ) ||
678 ( DQ != NULL && mbedtls_mpi_cmp_mpi( DQ, &ctx->DQ ) != 0 ) ||
679 ( QP != NULL && mbedtls_mpi_cmp_mpi( QP, &ctx->QP ) != 0 ) )
680 {
681 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
682 }
Hanno Becker23344b52017-08-23 07:43:27 +0100683#else /* MBEDTLS_RSA_NO_CRT */
684
685 /*
686 * Check that DP, DQ and QP are in accordance with core parameters.
687 * (1) Check that DP - P == 0 mod P - 1
688 * (2) Check that DQ - Q == 0 mod Q - 1
689 * (3) Check that QP * P - 1 == 0 mod P
690
691 * Alternative implementation also not using DP, DQ and QP
692 * should be able to reuse this codepath.
693 */
694
695 /* Check (1) */
696 if( DP != NULL )
697 {
698 /* Temporarily replace P by P-1 and compute DP - D mod P-1 */
699 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
700 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( DP, DP, &ctx->D ) );
701 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, DP, &ctx->P ) );
702 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
703
704 if( mbedtls_mpi_cmp_int( DP, 0 ) != 0 )
705 {
706 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
707 }
708 }
709
710 /* Check (1) */
711 if( DQ != NULL )
712 {
713 /* Temporarily replace Q by Q-1 and compute DQ - D mod Q-1 */
714 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
715 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( DQ, DQ, &ctx->D ) );
716 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, DQ, &ctx->Q ) );
717 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
718
719 if( mbedtls_mpi_cmp_int( DQ, 0 ) != 0 )
720 {
721 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
722 }
723 }
724
725 /* Check (3) */
726 if( QP != NULL )
727 {
728 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( QP, QP, &ctx->Q ) );
729 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( QP, QP, 1 ) );
730 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( QP, QP, &ctx->P ) );
731 if( mbedtls_mpi_cmp_int( QP, 0 ) != 0 )
732 {
733 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
734 }
735 }
736
737cleanup:
738
739#endif
740
741 if( ret != 0 )
742 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100743
744 return( 0 );
745}
746
747int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
748 unsigned char *N, size_t N_len,
749 unsigned char *P, size_t P_len,
750 unsigned char *Q, size_t Q_len,
751 unsigned char *D, size_t D_len,
752 unsigned char *E, size_t E_len )
753{
754 int ret = 0;
755
756 /* Check if key is private or public */
757 const int is_priv =
758 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
759 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
760 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
761 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
762 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
763
764 if( !is_priv )
765 {
766 /* If we're trying to export private parameters for a public key,
767 * something must be wrong. */
768 if( P != NULL || Q != NULL || D != NULL )
769 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
770
771 }
772
773 if( N != NULL )
774 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
775
776 if( P != NULL )
777 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
778
779 if( Q != NULL )
780 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
781
782 if( D != NULL )
783 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
784
785 if( E != NULL )
786 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100787
788cleanup:
789
790 return( ret );
791}
792
Hanno Becker617c1ae2017-08-23 14:11:24 +0100793int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
794 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
795 mbedtls_mpi *D, mbedtls_mpi *E )
796{
797 int ret;
798
799 /* Check if key is private or public */
800 int is_priv =
801 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
802 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
803 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
804 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
805 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
806
807 if( !is_priv )
808 {
809 /* If we're trying to export private parameters for a public key,
810 * something must be wrong. */
811 if( P != NULL || Q != NULL || D != NULL )
812 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
813
814 }
815
816 /* Export all requested core parameters. */
817
818 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
819 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
820 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
821 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
822 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
823 {
824 return( ret );
825 }
826
827 return( 0 );
828}
829
830/*
831 * Export CRT parameters
832 * This must also be implemented if CRT is not used, for being able to
833 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
834 * can be used in this case.
835 */
836int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
837 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
838{
839 int ret;
840
841 /* Check if key is private or public */
842 int is_priv =
843 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
844 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
845 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
846 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
847 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
848
849 if( !is_priv )
850 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
851
Hanno Beckerdc95c892017-08-23 06:57:02 +0100852#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100853 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100854 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
855 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
856 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
857 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100858 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100859 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100860#else
861 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
862 DP, DQ, QP ) ) != 0 )
863 {
864 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
865 }
866#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100867
868 return( 0 );
869}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100870
871/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 * Initialize an RSA context
873 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000876 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000877{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#if defined(MBEDTLS_THREADING_C)
883 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200884#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000885}
886
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100887/*
888 * Set padding for an existing RSA context
889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100891{
892 ctx->padding = padding;
893 ctx->hash_id = hash_id;
894}
895
Hanno Becker617c1ae2017-08-23 14:11:24 +0100896/*
897 * Get length in bytes of RSA modulus
898 */
899
900size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
901{
902 return( mbedtls_mpi_size( &ctx->N ) );
903}
904
905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000907
908/*
909 * Generate an RSA keypair
910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000912 int (*f_rng)(void *, unsigned char *, size_t),
913 void *p_rng,
914 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000915{
916 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100917 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
Paul Bakker21eb2802010-08-16 11:10:02 +0000919 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000921
Janos Follathef441782016-09-21 13:18:12 +0100922 if( nbits % 2 )
923 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
924
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100925 mbedtls_mpi_init( &H );
926 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
928 /*
929 * find primes P and Q with Q < P so that:
930 * GCD( E, (P-1)*(Q-1) ) == 1
931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
934 do
935 {
Janos Follath10c575b2016-02-23 14:42:48 +0000936 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100937 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000938
Janos Follathef441782016-09-21 13:18:12 +0100939 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100940 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000943 continue;
944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200946 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 continue;
948
Janos Follathef441782016-09-21 13:18:12 +0100949 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100950 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100951
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100952 /* Temporarily replace P,Q by P-1, Q-1 */
953 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
954 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
955 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000957 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100960 /* Restore P,Q */
961 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
962 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
963
964 ctx->len = mbedtls_mpi_size( &ctx->N );
965
Paul Bakker5121ce52009-01-03 21:22:43 +0000966 /*
967 * D = E^-1 mod ((P-1)*(Q-1))
968 * DP = D mod (P - 1)
969 * DQ = D mod (Q - 1)
970 * QP = Q^-1 mod P
971 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000972
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100973 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
974
975#if !defined(MBEDTLS_RSA_NO_CRT)
976 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
977 &ctx->DP, &ctx->DQ, &ctx->QP ) );
978#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
Hanno Becker83aad1f2017-08-23 06:45:10 +0100980 /* Double-check */
981 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
982
Paul Bakker5121ce52009-01-03 21:22:43 +0000983cleanup:
984
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100985 mbedtls_mpi_free( &H );
986 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
988 if( ret != 0 )
989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_rsa_free( ctx );
991 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 }
993
Paul Bakker48377d92013-08-30 12:06:24 +0200994 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995}
996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000998
999/*
1000 * Check a public RSA key
1001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001003{
Paul Bakker37940d92009-07-10 22:38:58 +00001004 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +00001006
Paul Bakker48377d92013-08-30 12:06:24 +02001007 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +00001008 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001011 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
1012 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001015 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
1017 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
1019 return( 0 );
1020}
1021
1022/*
1023 * Check a private RSA key
1024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001026{
1027 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 return( ret );
1032
Paul Bakker37940d92009-07-10 22:38:58 +00001033 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +00001035
Hanno Becker6345dd32017-08-23 06:59:48 +01001036 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 );
1037 mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I );
1038 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 ); mbedtls_mpi_init( &L1 );
1039 mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
1043 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
1044 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1045 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1046 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
1047 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
1050 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
1051 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +00001052
Hanno Becker6345dd32017-08-23 06:59:48 +01001053#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
1055 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
1056 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Hanno Becker6345dd32017-08-23 06:59:48 +01001057#endif
1058
Paul Bakkerb572adf2010-07-18 08:29:32 +00001059 /*
1060 * Check for a valid PKCS1v2 private key
1061 */
Hanno Becker6345dd32017-08-23 06:59:48 +01001062 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
1063#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
1065 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
1066 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
Hanno Becker6345dd32017-08-23 06:59:48 +01001067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
Hanno Becker6345dd32017-08-23 06:59:48 +01001069 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 }
Paul Bakker48377d92013-08-30 12:06:24 +02001074
Paul Bakker5121ce52009-01-03 21:22:43 +00001075cleanup:
Hanno Becker6345dd32017-08-23 06:59:48 +01001076 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 );
1077 mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I );
1078 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 ); mbedtls_mpi_free( &L1 );
1079 mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +00001083 return( ret );
1084
Paul Bakker6c591fa2011-05-05 11:49:20 +00001085 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001087
1088 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089}
1090
1091/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001092 * Check if contexts holding a public and private key match
1093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1097 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001100 }
1101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1103 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001106 }
1107
1108 return( 0 );
1109}
1110
1111/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 * Do an RSA public key operation
1113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001115 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 unsigned char *output )
1117{
Paul Bakker23986e52011-04-24 08:57:21 +00001118 int ret;
1119 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001123
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001124#if defined(MBEDTLS_THREADING_C)
1125 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1126 return( ret );
1127#endif
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001133 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1134 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 }
1136
1137 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1139 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
1141cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001143 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1144 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001145#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
1149 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
1152 return( 0 );
1153}
1154
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001155/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001156 * Generate or update blinding values, see section 10 of:
1157 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001158 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001159 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001160 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001161static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001162 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1163{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001164 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001165
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001166 if( ctx->Vf.p != NULL )
1167 {
1168 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1170 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1171 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1172 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001173
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001174 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001175 }
1176
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001177 /* Unblinding value: Vf = random number, invertible mod N */
1178 do {
1179 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1183 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1184 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001185
1186 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1188 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 +02001189
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001190
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001191cleanup:
1192 return( ret );
1193}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001194
Paul Bakker5121ce52009-01-03 21:22:43 +00001195/*
Janos Follathe81102e2017-03-22 13:38:28 +00001196 * Exponent blinding supposed to prevent side-channel attacks using multiple
1197 * traces of measurements to recover the RSA key. The more collisions are there,
1198 * the more bits of the key can be recovered. See [3].
1199 *
1200 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1201 * observations on avarage.
1202 *
1203 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1204 * to make 2^112 observations on avarage.
1205 *
1206 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1207 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1208 * Thus in this sense with 28 byte blinding the security is not reduced by
1209 * side-channel attacks like the one in [3])
1210 *
1211 * This countermeasure does not help if the key recovery is possible with a
1212 * single trace.
1213 */
1214#define RSA_EXPONENT_BLINDING 28
1215
1216/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 * Do an RSA private key operation
1218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001220 int (*f_rng)(void *, unsigned char *, size_t),
1221 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001222 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 unsigned char *output )
1224{
Paul Bakker23986e52011-04-24 08:57:21 +00001225 int ret;
1226 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001228 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001229#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001230 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001231 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001232#else
1233 mbedtls_mpi DP_blind, DQ_blind;
1234 mbedtls_mpi *DP = &ctx->DP;
1235 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001236#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001237
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001238 /* Make sure we have private key info, prevent possible misuse */
1239 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
1240 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001243 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1244
1245
1246 if( f_rng != NULL )
1247 {
Janos Follathe81102e2017-03-22 13:38:28 +00001248#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001249 mbedtls_mpi_init( &D_blind );
1250#else
1251 mbedtls_mpi_init( &DP_blind );
1252 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001253#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001254 }
Janos Follathe81102e2017-03-22 13:38:28 +00001255
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001257#if defined(MBEDTLS_THREADING_C)
1258 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1259 return( ret );
1260#endif
1261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1263 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001265 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1266 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 }
1268
Paul Bakkerf451bac2013-08-30 15:37:02 +02001269 if( f_rng != NULL )
1270 {
1271 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001272 * Blinding
1273 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001274 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001275 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1276 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001278
Janos Follathe81102e2017-03-22 13:38:28 +00001279 /*
1280 * Exponent blinding
1281 */
1282 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1283 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1284
Janos Follathf9203b42017-03-22 15:13:15 +00001285#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001286 /*
1287 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1288 */
1289 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1290 f_rng, p_rng ) );
1291 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1292 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1293 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1294
1295 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001296#else
1297 /*
1298 * DP_blind = ( P - 1 ) * R + DP
1299 */
1300 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1301 f_rng, p_rng ) );
1302 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1303 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1304 &ctx->DP ) );
1305
1306 DP = &DP_blind;
1307
1308 /*
1309 * DQ_blind = ( Q - 1 ) * R + DQ
1310 */
1311 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1312 f_rng, p_rng ) );
1313 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1314 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1315 &ctx->DQ ) );
1316
1317 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001318#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001319 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001322 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001323#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001324 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001325 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 *
1327 * T1 = input ^ dP mod P
1328 * T2 = input ^ dQ mod Q
1329 */
Janos Follathf9203b42017-03-22 15:13:15 +00001330 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1331 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
1333 /*
1334 * T = (T1 - T2) * (Q^-1 mod P) mod P
1335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1337 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1338 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
1340 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001341 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1344 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1345#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001346
Paul Bakkerf451bac2013-08-30 15:37:02 +02001347 if( f_rng != NULL )
1348 {
1349 /*
1350 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001351 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001352 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001353 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001355 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001356
1357 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
1360cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001362 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1363 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001364#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001367 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1368
1369 if( f_rng != NULL )
1370 {
Janos Follathe81102e2017-03-22 13:38:28 +00001371#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001372 mbedtls_mpi_free( &D_blind );
1373#else
1374 mbedtls_mpi_free( &DP_blind );
1375 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001376#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
1379 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
1382 return( 0 );
1383}
1384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001386/**
1387 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1388 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001389 * \param dst buffer to mask
1390 * \param dlen length of destination buffer
1391 * \param src source of the mask generation
1392 * \param slen length of the source buffer
1393 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001394 */
Paul Bakker48377d92013-08-30 12:06:24 +02001395static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001397{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001399 unsigned char counter[4];
1400 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001401 unsigned int hlen;
1402 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001405 memset( counter, 0, 4 );
1406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001408
Simon Butcher02037452016-03-01 21:19:12 +00001409 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001410 p = dst;
1411
1412 while( dlen > 0 )
1413 {
1414 use_len = hlen;
1415 if( dlen < hlen )
1416 use_len = dlen;
1417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 mbedtls_md_starts( md_ctx );
1419 mbedtls_md_update( md_ctx, src, slen );
1420 mbedtls_md_update( md_ctx, counter, 4 );
1421 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001422
1423 for( i = 0; i < use_len; ++i )
1424 *p++ ^= mask[i];
1425
1426 counter[3]++;
1427
1428 dlen -= use_len;
1429 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001430
1431 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001432}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001436/*
1437 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001440 int (*f_rng)(void *, unsigned char *, size_t),
1441 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001442 int mode,
1443 const unsigned char *label, size_t label_len,
1444 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001445 const unsigned char *input,
1446 unsigned char *output )
1447{
1448 size_t olen;
1449 int ret;
1450 unsigned char *p = output;
1451 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 const mbedtls_md_info_t *md_info;
1453 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1456 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001457
1458 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001460
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
1465 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001467
Simon Butcher02037452016-03-01 21:19:12 +00001468 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001469 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001471
1472 memset( output, 0, olen );
1473
1474 *p++ = 0;
1475
Simon Butcher02037452016-03-01 21:19:12 +00001476 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001477 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001479
1480 p += hlen;
1481
Simon Butcher02037452016-03-01 21:19:12 +00001482 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001484 p += hlen;
1485 p += olen - 2 * hlen - 2 - ilen;
1486 *p++ = 1;
1487 memcpy( p, input, ilen );
1488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001490 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1491 {
1492 mbedtls_md_free( &md_ctx );
1493 return( ret );
1494 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001495
Simon Butcher02037452016-03-01 21:19:12 +00001496 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001497 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1498 &md_ctx );
1499
Simon Butcher02037452016-03-01 21:19:12 +00001500 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001501 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1502 &md_ctx );
1503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 return( ( mode == MBEDTLS_RSA_PUBLIC )
1507 ? mbedtls_rsa_public( ctx, output, output )
1508 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001513/*
1514 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001517 int (*f_rng)(void *, unsigned char *, size_t),
1518 void *p_rng,
1519 int mode, size_t ilen,
1520 const unsigned char *input,
1521 unsigned char *output )
1522{
1523 size_t nb_pad, olen;
1524 int ret;
1525 unsigned char *p = output;
1526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1528 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001529
Janos Follath1ed9f992016-03-18 11:45:44 +00001530 // We don't check p_rng because it won't be dereferenced here
1531 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001533
1534 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001535
Simon Butcher02037452016-03-01 21:19:12 +00001536 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001537 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001539
1540 nb_pad = olen - 3 - ilen;
1541
1542 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001546
1547 while( nb_pad-- > 0 )
1548 {
1549 int rng_dl = 100;
1550
1551 do {
1552 ret = f_rng( p_rng, p, 1 );
1553 } while( *p == 0 && --rng_dl && ret == 0 );
1554
Simon Butcher02037452016-03-01 21:19:12 +00001555 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001556 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001558
1559 p++;
1560 }
1561 }
1562 else
1563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001565
1566 while( nb_pad-- > 0 )
1567 *p++ = 0xFF;
1568 }
1569
1570 *p++ = 0;
1571 memcpy( p, input, ilen );
1572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 return( ( mode == MBEDTLS_RSA_PUBLIC )
1574 ? mbedtls_rsa_public( ctx, output, output )
1575 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001578
Paul Bakker5121ce52009-01-03 21:22:43 +00001579/*
1580 * Add the message padding, then do an RSA operation
1581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001583 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001584 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001585 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001586 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001587 unsigned char *output )
1588{
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 switch( ctx->padding )
1590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#if defined(MBEDTLS_PKCS1_V15)
1592 case MBEDTLS_RSA_PKCS_V15:
1593 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001594 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001595#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597#if defined(MBEDTLS_PKCS1_V21)
1598 case MBEDTLS_RSA_PKCS_V21:
1599 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001600 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001601#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001602
1603 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001606}
1607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001609/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001610 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001613 int (*f_rng)(void *, unsigned char *, size_t),
1614 void *p_rng,
1615 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001616 const unsigned char *label, size_t label_len,
1617 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001618 const unsigned char *input,
1619 unsigned char *output,
1620 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001621{
Paul Bakker23986e52011-04-24 08:57:21 +00001622 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001623 size_t ilen, i, pad_len;
1624 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1626 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001627 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 const mbedtls_md_info_t *md_info;
1629 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001630
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001631 /*
1632 * Parameters sanity checks
1633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1635 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
1637 ilen = ctx->len;
1638
Paul Bakker27fdf462011-06-09 13:55:13 +00001639 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001643 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001645
Janos Follathc17cda12016-02-11 11:08:18 +00001646 hlen = mbedtls_md_get_size( md_info );
1647
1648 // checking for integer underflow
1649 if( 2 * hlen + 2 > ilen )
1650 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1651
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001652 /*
1653 * RSA operation
1654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1656 ? mbedtls_rsa_public( ctx, input, buf )
1657 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
1659 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001660 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001661
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001662 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001663 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001666 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1667 {
1668 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001669 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001670 }
1671
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001672
1673 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001675
1676 /* seed: Apply seedMask to maskedSeed */
1677 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1678 &md_ctx );
1679
1680 /* DB: Apply dbMask to maskedDB */
1681 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1682 &md_ctx );
1683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001685
1686 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001687 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001688 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001689 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001690 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001692 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001693
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001694 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001695
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001696 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001697 for( i = 0; i < hlen; i++ )
1698 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001699
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001700 /* Get zero-padding len, but always read till end of buffer
1701 * (minus one, for the 01 byte) */
1702 pad_len = 0;
1703 pad_done = 0;
1704 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1705 {
1706 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001707 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001708 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001709
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001710 p += pad_len;
1711 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001712
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001713 /*
1714 * The only information "leaked" is whether the padding was correct or not
1715 * (eg, no data is copied if it was not correct). This meets the
1716 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1717 * the different error conditions.
1718 */
1719 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001720 {
1721 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1722 goto cleanup;
1723 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001724
Paul Bakker66d5d072014-06-17 16:39:18 +02001725 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001726 {
1727 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1728 goto cleanup;
1729 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001730
1731 *olen = ilen - (p - buf);
1732 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001733 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001734
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001735cleanup:
1736 mbedtls_zeroize( buf, sizeof( buf ) );
1737 mbedtls_zeroize( lhash, sizeof( lhash ) );
1738
1739 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001740}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001744/*
1745 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1746 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001748 int (*f_rng)(void *, unsigned char *, size_t),
1749 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001750 int mode, size_t *olen,
1751 const unsigned char *input,
1752 unsigned char *output,
1753 size_t output_max_len)
1754{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001755 int ret;
1756 size_t ilen, pad_count = 0, i;
1757 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1761 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001762
1763 ilen = ctx->len;
1764
1765 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1769 ? mbedtls_rsa_public( ctx, input, buf )
1770 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001771
1772 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001773 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001774
1775 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001776 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001777
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001778 /*
1779 * Check and get padding len in "constant-time"
1780 */
1781 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001782
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001783 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001788 /* Get padding len, but always read till end of buffer
1789 * (minus one, for the 00 byte) */
1790 for( i = 0; i < ilen - 3; i++ )
1791 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001792 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1793 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001794 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001795
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001796 p += pad_count;
1797 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001798 }
1799 else
1800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001802
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001803 /* Get padding len, but always read till end of buffer
1804 * (minus one, for the 00 byte) */
1805 for( i = 0; i < ilen - 3; i++ )
1806 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001807 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001808 pad_count += ( pad_done == 0 );
1809 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001810
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001811 p += pad_count;
1812 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001813 }
1814
Janos Follathc69fa502016-02-12 13:30:09 +00001815 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001816
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001817 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001818 {
1819 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1820 goto cleanup;
1821 }
Paul Bakker8804f692013-02-28 18:06:26 +01001822
Paul Bakker66d5d072014-06-17 16:39:18 +02001823 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001824 {
1825 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1826 goto cleanup;
1827 }
Paul Bakker060c5682009-01-12 21:48:39 +00001828
Paul Bakker27fdf462011-06-09 13:55:13 +00001829 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001831 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001832
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001833cleanup:
1834 mbedtls_zeroize( buf, sizeof( buf ) );
1835
1836 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
1840/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001841 * Do an RSA operation, then remove the message padding
1842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001844 int (*f_rng)(void *, unsigned char *, size_t),
1845 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001846 int mode, size_t *olen,
1847 const unsigned char *input,
1848 unsigned char *output,
1849 size_t output_max_len)
1850{
1851 switch( ctx->padding )
1852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853#if defined(MBEDTLS_PKCS1_V15)
1854 case MBEDTLS_RSA_PKCS_V15:
1855 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001856 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001857#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859#if defined(MBEDTLS_PKCS1_V21)
1860 case MBEDTLS_RSA_PKCS_V21:
1861 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001862 olen, input, output,
1863 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001864#endif
1865
1866 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001868 }
1869}
1870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001872/*
1873 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001876 int (*f_rng)(void *, unsigned char *, size_t),
1877 void *p_rng,
1878 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001880 unsigned int hashlen,
1881 const unsigned char *hash,
1882 unsigned char *sig )
1883{
1884 size_t olen;
1885 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001887 unsigned int slen, hlen, offset = 0;
1888 int ret;
1889 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 const mbedtls_md_info_t *md_info;
1891 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1894 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001895
1896 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001898
1899 olen = ctx->len;
1900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001902 {
Simon Butcher02037452016-03-01 21:19:12 +00001903 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001905 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001909 }
1910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001912 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001916 slen = hlen;
1917
1918 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001920
1921 memset( sig, 0, olen );
1922
Simon Butcher02037452016-03-01 21:19:12 +00001923 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001924 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001926
Simon Butcher02037452016-03-01 21:19:12 +00001927 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001928 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001929 p += olen - hlen * 2 - 2;
1930 *p++ = 0x01;
1931 memcpy( p, salt, slen );
1932 p += slen;
1933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001935 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1936 {
1937 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001938 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001939 return( ret );
1940 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001941
Simon Butcher02037452016-03-01 21:19:12 +00001942 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 mbedtls_md_starts( &md_ctx );
1944 mbedtls_md_update( &md_ctx, p, 8 );
1945 mbedtls_md_update( &md_ctx, hash, hashlen );
1946 mbedtls_md_update( &md_ctx, salt, slen );
1947 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001948 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001949
Simon Butcher02037452016-03-01 21:19:12 +00001950 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001951 if( msb % 8 == 0 )
1952 offset = 1;
1953
Simon Butcher02037452016-03-01 21:19:12 +00001954 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001955 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001958
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001959 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001960 sig[0] &= 0xFF >> ( olen * 8 - msb );
1961
1962 p += hlen;
1963 *p++ = 0xBC;
1964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 return( ( mode == MBEDTLS_RSA_PUBLIC )
1966 ? mbedtls_rsa_public( ctx, sig, sig )
1967 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001968}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001972/*
1973 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1974 */
1975/*
1976 * Do an RSA operation to sign the message digest
1977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001979 int (*f_rng)(void *, unsigned char *, size_t),
1980 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001981 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001983 unsigned int hashlen,
1984 const unsigned char *hash,
1985 unsigned char *sig )
1986{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001987 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001988 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001989 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001990 unsigned char *sig_try = NULL, *verif = NULL;
1991 size_t i;
1992 unsigned char diff;
1993 volatile unsigned char diff_no_optimize;
1994 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1997 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001998
1999 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002000 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01002001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002005 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
2009 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002010
Paul Bakkerc70b9822013-04-07 22:00:46 +02002011 nb_pad -= 10 + oid_size;
2012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002014 }
2015
Paul Bakkerc70b9822013-04-07 22:00:46 +02002016 nb_pad -= hashlen;
2017
Paul Bakkerb3869132013-02-28 17:21:01 +01002018 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002020
2021 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01002023 memset( p, 0xFF, nb_pad );
2024 p += nb_pad;
2025 *p++ = 0;
2026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002028 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002029 memcpy( p, hash, hashlen );
2030 }
2031 else
2032 {
2033 /*
2034 * DigestInfo ::= SEQUENCE {
2035 * digestAlgorithm DigestAlgorithmIdentifier,
2036 * digest Digest }
2037 *
2038 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2039 *
2040 * Digest ::= OCTET STRING
2041 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002043 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002045 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002047 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002048 memcpy( p, oid, oid_size );
2049 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002051 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002053 *p++ = hashlen;
2054 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002055 }
2056
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002057 if( mode == MBEDTLS_RSA_PUBLIC )
2058 return( mbedtls_rsa_public( ctx, sig, sig ) );
2059
2060 /*
2061 * In order to prevent Lenstra's attack, make the signature in a
2062 * temporary buffer and check it before returning it.
2063 */
2064 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002065 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002066 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2067
Simon Butcher1285ab52016-01-01 21:42:47 +00002068 verif = mbedtls_calloc( 1, ctx->len );
2069 if( verif == NULL )
2070 {
2071 mbedtls_free( sig_try );
2072 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2073 }
2074
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002075 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2076 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2077
2078 /* Compare in constant time just in case */
2079 for( diff = 0, i = 0; i < ctx->len; i++ )
2080 diff |= verif[i] ^ sig[i];
2081 diff_no_optimize = diff;
2082
2083 if( diff_no_optimize != 0 )
2084 {
2085 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2086 goto cleanup;
2087 }
2088
2089 memcpy( sig, sig_try, ctx->len );
2090
2091cleanup:
2092 mbedtls_free( sig_try );
2093 mbedtls_free( verif );
2094
2095 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002098
2099/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 * Do an RSA operation to sign the message digest
2101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002103 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002104 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002107 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002108 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 unsigned char *sig )
2110{
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 switch( ctx->padding )
2112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#if defined(MBEDTLS_PKCS1_V15)
2114 case MBEDTLS_RSA_PKCS_V15:
2115 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002116 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002117#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119#if defined(MBEDTLS_PKCS1_V21)
2120 case MBEDTLS_RSA_PKCS_V21:
2121 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002122 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002123#endif
2124
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002128}
2129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002131/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002132 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002135 int (*f_rng)(void *, unsigned char *, size_t),
2136 void *p_rng,
2137 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002139 unsigned int hashlen,
2140 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002142 int expected_salt_len,
2143 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002144{
Paul Bakker23986e52011-04-24 08:57:21 +00002145 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002146 size_t siglen;
2147 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002149 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002150 unsigned int hlen;
2151 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 const mbedtls_md_info_t *md_info;
2153 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002154 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2157 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002158
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 siglen = ctx->len;
2160
Paul Bakker27fdf462011-06-09 13:55:13 +00002161 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2165 ? mbedtls_rsa_public( ctx, sig, buf )
2166 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002167
2168 if( ret != 0 )
2169 return( ret );
2170
2171 p = buf;
2172
Paul Bakkerb3869132013-02-28 17:21:01 +01002173 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002177 {
Simon Butcher02037452016-03-01 21:19:12 +00002178 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002180 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002184 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002187 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002191 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002192
Paul Bakkerb3869132013-02-28 17:21:01 +01002193 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002194
Simon Butcher02037452016-03-01 21:19:12 +00002195 /*
2196 * Note: EMSA-PSS verification is over the length of N - 1 bits
2197 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002198 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002199
Simon Butcher02037452016-03-01 21:19:12 +00002200 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002201 if( msb % 8 == 0 )
2202 {
2203 p++;
2204 siglen -= 1;
2205 }
2206 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002210 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2211 {
2212 mbedtls_md_free( &md_ctx );
2213 return( ret );
2214 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002215
Paul Bakkerb3869132013-02-28 17:21:01 +01002216 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002217
Paul Bakkerb3869132013-02-28 17:21:01 +01002218 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002219
Paul Bakker4de44aa2013-12-31 11:43:01 +01002220 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002221 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002222
Paul Bakkerb3869132013-02-28 17:21:01 +01002223 if( p == buf + siglen ||
2224 *p++ != 0x01 )
2225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 mbedtls_md_free( &md_ctx );
2227 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002228 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002229
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002230 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002231 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002234 slen != (size_t) expected_salt_len )
2235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 mbedtls_md_free( &md_ctx );
2237 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002238 }
2239
Simon Butcher02037452016-03-01 21:19:12 +00002240 /*
2241 * Generate H = Hash( M' )
2242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 mbedtls_md_starts( &md_ctx );
2244 mbedtls_md_update( &md_ctx, zeros, 8 );
2245 mbedtls_md_update( &md_ctx, hash, hashlen );
2246 mbedtls_md_update( &md_ctx, p, slen );
2247 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002250
Paul Bakkerb3869132013-02-28 17:21:01 +01002251 if( memcmp( p + slen, result, hlen ) == 0 )
2252 return( 0 );
2253 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002255}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002256
2257/*
2258 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002261 int (*f_rng)(void *, unsigned char *, size_t),
2262 void *p_rng,
2263 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002265 unsigned int hashlen,
2266 const unsigned char *hash,
2267 const unsigned char *sig )
2268{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2270 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002271 : md_alg;
2272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002274 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002276 sig ) );
2277
2278}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002282/*
2283 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002286 int (*f_rng)(void *, unsigned char *, size_t),
2287 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002288 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002290 unsigned int hashlen,
2291 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002292 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002293{
2294 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002295 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002296 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 mbedtls_md_type_t msg_md_alg;
2298 const mbedtls_md_info_t *md_info;
2299 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002300 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2303 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002304
2305 siglen = ctx->len;
2306
2307 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2311 ? mbedtls_rsa_public( ctx, sig, buf )
2312 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002313
2314 if( ret != 0 )
2315 return( ret );
2316
2317 p = buf;
2318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2320 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002321
2322 while( *p != 0 )
2323 {
2324 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002326 p++;
2327 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002328 p++; /* skip 00 byte */
2329
2330 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2331 if( p - buf < 11 )
2332 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002333
2334 len = siglen - ( p - buf );
2335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002337 {
2338 if( memcmp( p, hash, hashlen ) == 0 )
2339 return( 0 );
2340 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 }
2343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002345 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2347 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002348
2349 end = p + len;
2350
Simon Butcher02037452016-03-01 21:19:12 +00002351 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002352 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2353 * Insist on 2-byte length tags, to protect against variants of
2354 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002355 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002356 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2358 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2359 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002360 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002362
Gilles Peskinee7e76502017-05-04 12:48:39 +02002363 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2365 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2366 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002367 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002369
Gilles Peskinee7e76502017-05-04 12:48:39 +02002370 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2372 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002373 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002374 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002375
2376 oid.p = p;
2377 p += oid.len;
2378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2380 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002381
2382 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002384
2385 /*
2386 * assume the algorithm parameters must be NULL
2387 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002388 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2390 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002391 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002393
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002394 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002395 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2396 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002397 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002399
2400 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002402
2403 p += hashlen;
2404
2405 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002407
2408 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002411
2412/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002413 * Do an RSA operation and check the message digest
2414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002416 int (*f_rng)(void *, unsigned char *, size_t),
2417 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002418 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002420 unsigned int hashlen,
2421 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002422 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002423{
2424 switch( ctx->padding )
2425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426#if defined(MBEDTLS_PKCS1_V15)
2427 case MBEDTLS_RSA_PKCS_V15:
2428 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002429 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002430#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#if defined(MBEDTLS_PKCS1_V21)
2433 case MBEDTLS_RSA_PKCS_V21:
2434 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002435 hashlen, hash, sig );
2436#endif
2437
2438 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002440 }
2441}
2442
2443/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002444 * Copy the components of an RSA key
2445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002447{
2448 int ret;
2449
2450 dst->ver = src->ver;
2451 dst->len = src->len;
2452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2454 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2457 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2458 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002459
2460#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2462 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2463 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2465 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002466#endif
2467
2468 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2471 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002472
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002473 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002474 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002475
2476cleanup:
2477 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002479
2480 return( ret );
2481}
2482
2483/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 * Free the components of an RSA key
2485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002487{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002489 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2490 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002492
Hanno Becker33c30a02017-08-23 07:00:22 +01002493#if !defined(MBEDTLS_RSA_NO_CRT)
2494 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2495 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2496 mbedtls_mpi_free( &ctx->DP );
2497#endif /* MBEDTLS_RSA_NO_CRT */
2498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#if defined(MBEDTLS_THREADING_C)
2500 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002501#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002502}
2503
Hanno Beckerab377312017-08-23 16:24:51 +01002504#endif /* !MBEDTLS_RSA_ALT */
2505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002508#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
2510/*
2511 * Example RSA-1024 keypair, for test purposes
2512 */
2513#define KEY_LEN 128
2514
2515#define RSA_N "9292758453063D803DD603D5E777D788" \
2516 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2517 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2518 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2519 "93A89813FBF3C4F8066D2D800F7C38A8" \
2520 "1AE31942917403FF4946B0A83D3D3E05" \
2521 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2522 "5E94BB77B07507233A0BC7BAC8F90F79"
2523
2524#define RSA_E "10001"
2525
2526#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2527 "66CA472BC44D253102F8B4A9D3BFA750" \
2528 "91386C0077937FE33FA3252D28855837" \
2529 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2530 "DF79C5CE07EE72C7F123142198164234" \
2531 "CABB724CF78B8173B9F880FC86322407" \
2532 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2533 "071513A1E85B5DFA031F21ECAE91A34D"
2534
2535#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2536 "2C01CAD19EA484A87EA4377637E75500" \
2537 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2538 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2539
2540#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2541 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2542 "910E4168387E3C30AA1E00C339A79508" \
2543 "8452DD96A9A5EA5D9DCA68DA636032AF"
2544
2545#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2546 "3C94D22288ACD763FD8E5600ED4A702D" \
2547 "F84198A5F06C2E72236AE490C93F07F8" \
2548 "3CC559CD27BC2D1CA488811730BB5725"
2549
2550#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2551 "D8AAEA56749EA28623272E4F7D0592AF" \
2552 "7C1F1313CAC9471B5C523BFE592F517B" \
2553 "407A1BD76C164B93DA2D32A383E58357"
2554
2555#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2556 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2557 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2558 "A74206CEC169D74BF5A8C50D6F48EA08"
2559
2560#define PT_LEN 24
2561#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2562 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002565static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002566{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002567#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002568 size_t i;
2569
Paul Bakker545570e2010-07-18 09:00:25 +00002570 if( rng_state != NULL )
2571 rng_state = NULL;
2572
Paul Bakkera3d195c2011-11-27 21:07:34 +00002573 for( i = 0; i < len; ++i )
2574 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002575#else
2576 if( rng_state != NULL )
2577 rng_state = NULL;
2578
2579 arc4random_buf( output, len );
2580#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002581
Paul Bakkera3d195c2011-11-27 21:07:34 +00002582 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002585
Paul Bakker5121ce52009-01-03 21:22:43 +00002586/*
2587 * Checkup routine
2588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002590{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002591 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002593 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 unsigned char rsa_plaintext[PT_LEN];
2596 unsigned char rsa_decrypted[PT_LEN];
2597 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002599 unsigned char sha1sum[20];
2600#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Hanno Becker3a701162017-08-22 13:52:43 +01002602 mbedtls_mpi K;
2603
2604 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
Hanno Becker3a701162017-08-22 13:52:43 +01002607 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2608 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2609 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2610 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2611 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2612 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2613 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2614 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2615 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2616 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2617
2618 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
2620 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2624 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 {
2626 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
2629 return( 1 );
2630 }
2631
Hanno Becker3a701162017-08-22 13:52:43 +01002632 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2633 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2634
2635 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2636 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2637
2638 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2639 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2640
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
2644 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 rsa_plaintext, rsa_ciphertext ) != 0 )
2648 {
2649 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
2652 return( 1 );
2653 }
2654
2655 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002659 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002660 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 {
2662 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
2665 return( 1 );
2666 }
2667
2668 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2669 {
2670 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
2673 return( 1 );
2674 }
2675
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002676 if( verbose != 0 )
2677 mbedtls_printf( "passed\n" );
2678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002681 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 sha1sum, rsa_ciphertext ) != 0 )
2687 {
2688 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
2691 return( 1 );
2692 }
2693
2694 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 sha1sum, rsa_ciphertext ) != 0 )
2699 {
2700 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
2703 return( 1 );
2704 }
2705
2706 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002707 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002710 if( verbose != 0 )
2711 mbedtls_printf( "\n" );
2712
Paul Bakker3d8fb632014-04-17 12:42:41 +02002713cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002714 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 mbedtls_rsa_free( &rsa );
2716#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002717 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002719 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720}
2721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724#endif /* MBEDTLS_RSA_C */