blob: a1a9debb309aa4a68315245cdaedac57b5987c4f [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
364 if( f_rng != NULL && P != NULL &&
365 ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
366 {
367 goto cleanup;
368 }
369
370 if( f_rng != NULL && Q != NULL &&
371 ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
372 {
373 goto cleanup;
374 }
375
376 /*
377 * Step 2: Check that N = PQ
378 */
379
380 if( P != NULL && Q != NULL && N != NULL )
381 {
382 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
383 if( mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
384 {
385 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
386 goto cleanup;
387 }
388 }
389
390 /*
391 * Step 3: Check that D, E are inverse modulo P-1 and Q-1
392 */
393
394 if( P != NULL && Q != NULL && D != NULL && E != NULL )
395 {
396 /* Temporarily replace P, Q by P-1, Q-1. */
397 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) );
398 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) );
399
400 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
401 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
402
403 /* Compute DE-1 mod P-1 */
404 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
405 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
406 {
407 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
408 goto cleanup;
409 }
410
411 /* Compute DE-1 mod Q-1 */
412 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, Q ) );
413 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
414 {
415 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
416 goto cleanup;
417 }
418
419 /* Restore P, Q. */
420 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) );
421 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) );
422 }
423
424cleanup:
425
426 mbedtls_mpi_free( &K );
427
428 return( ret );
429}
430
431int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
432 const mbedtls_mpi *D, mbedtls_mpi *DP,
433 mbedtls_mpi *DQ, mbedtls_mpi *QP )
434{
435 int ret = 0;
436 mbedtls_mpi K;
437 mbedtls_mpi_init( &K );
438
439 if( DP != NULL )
440 {
441 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
442 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
443 }
444
445 if( DQ != NULL )
446 {
447 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
448 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
449 }
450
451 if( QP != NULL )
452 {
453 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
454 }
455
456cleanup:
457 mbedtls_mpi_free( &K );
458
459 return( ret );
460}
461
Hanno Becker617c1ae2017-08-23 14:11:24 +0100462
463/*
464 * Default RSA interface implementation
465 */
466
467
468int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
469 const mbedtls_mpi *N,
470 const mbedtls_mpi *P, const mbedtls_mpi *Q,
471 const mbedtls_mpi *D, const mbedtls_mpi *E )
472{
473 int ret;
474
475 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
476 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
477 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
478 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
479 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
480 {
481 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
482 }
483
484 if( N != NULL )
485 ctx->len = mbedtls_mpi_size( &ctx->N );
486
487 return( 0 );
488}
489
490int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
491 unsigned char *N, size_t N_len,
492 unsigned char *P, size_t P_len,
493 unsigned char *Q, size_t Q_len,
494 unsigned char *D, size_t D_len,
495 unsigned char *E, size_t E_len )
496{
497 int ret;
498
499 if( N != NULL )
500 {
501 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
502 ctx->len = mbedtls_mpi_size( &ctx->N );
503 }
504
505 if( P != NULL )
506 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
507
508 if( Q != NULL )
509 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
510
511 if( D != NULL )
512 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
513
514 if( E != NULL )
515 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
516
517cleanup:
518
519 if( ret != 0 )
520 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
521
522 return( 0 );
523}
524
525int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
526 int (*f_rng)(void *, unsigned char *, size_t),
527 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100528{
529 int ret = 0;
530
Hanno Becker617c1ae2017-08-23 14:11:24 +0100531 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
532 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
533 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
534 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
535 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100536
Hanno Becker617c1ae2017-08-23 14:11:24 +0100537 /*
538 * Check whether provided parameters are enough
539 * to deduce all others. The following incomplete
540 * parameter sets for private keys are supported:
541 *
542 * (1) P, Q missing.
543 * (2) D and potentially N missing.
544 *
545 */
546 const int complete = have_N && have_P && have_Q && have_D && have_E;
547 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
548 const int d_missing = have_P && have_Q && !have_D && have_E;
549 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100550
Hanno Becker617c1ae2017-08-23 14:11:24 +0100551 const int is_priv = complete || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100552
Hanno Becker617c1ae2017-08-23 14:11:24 +0100553 if( !is_priv && !is_pub )
554 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
555
556 /*
557 * Step 1: Deduce and verify all core parameters.
558 */
559
560 if( pq_missing )
561 {
562 /* This includes sanity checking of core parameters,
563 * so no further checks necessary. */
564 ret = mbedtls_rsa_deduce_moduli( &ctx->N, &ctx->D, &ctx->E,
565 f_rng, p_rng,
566 &ctx->P, &ctx->Q );
567 if( ret != 0 )
568 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
569
570 }
571 else if( d_missing )
572 {
573 /* If a PRNG is provided, check if P, Q are prime. */
574 if( f_rng != NULL &&
575 ( ( ret = mbedtls_mpi_is_prime( &ctx->P, f_rng, p_rng ) ) != 0 ||
576 ( ret = mbedtls_mpi_is_prime( &ctx->Q, f_rng, p_rng ) ) != 0 ) )
577 {
578 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
579 }
580
581 /* Compute N if missing. */
582 if( !have_N &&
583 ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) ) != 0 )
584 {
585 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
586 }
587
588 /* Deduce private exponent. This includes double-checking of the result,
589 * so together with the primality test above all core parameters are
590 * guaranteed to be sane if this call succeeds. */
591 if( ( ret = mbedtls_rsa_deduce_private( &ctx->P, &ctx->Q,
592 &ctx->D, &ctx->E ) ) != 0 )
593 {
594 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
595 }
596 }
597 else if( complete )
598 {
599 /* Check complete set of imported core parameters. */
600 if( ( ret = mbedtls_rsa_check_params( &ctx->N, &ctx->P, &ctx->Q,
601 &ctx->D, &ctx->E,
602 f_rng, p_rng ) ) != 0 )
603 {
604 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
605 }
606 }
607
608 /* In the remaining case of a public key, there's nothing to check for. */
609
610 /*
611 * Step 2: Deduce all additional parameters specific
612 * to our current RSA implementaiton.
613 */
614
Hanno Becker23344b52017-08-23 07:43:27 +0100615#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100616 if( is_priv )
617 {
618 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
619 &ctx->DP, &ctx->DQ, &ctx->QP );
620 if( ret != 0 )
621 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
622 }
Hanno Becker23344b52017-08-23 07:43:27 +0100623#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100624
625 /*
626 * Step 3: Double check
627 */
628
629 if( is_priv )
630 {
631 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
632 return( ret );
633 }
634 else
635 {
636 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
637 return( ret );
638 }
639
640 return( 0 );
641}
642
643/*
644 * Check if CRT parameters match RSA context.
645 * This has to be implemented even if CRT is not used,
646 * in order to be able to validate DER encoded RSA keys,
647 * which always contain CRT parameters.
648 */
649int mbedtls_rsa_check_crt( mbedtls_rsa_context *ctx, mbedtls_mpi *DP,
650 mbedtls_mpi *DQ, mbedtls_mpi *QP )
651{
Hanno Becker23344b52017-08-23 07:43:27 +0100652 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100653
Hanno Becker23344b52017-08-23 07:43:27 +0100654 /* Check if key is private or public */
655 const int is_priv =
656 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
657 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
658 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
659 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
660 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
661
662 if( !is_priv )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100663 {
664 /* Checking optional parameters only makes sense for private keys. */
665 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
666 }
667
Hanno Becker23344b52017-08-23 07:43:27 +0100668#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100669 if( ( DP != NULL && mbedtls_mpi_cmp_mpi( DP, &ctx->DP ) != 0 ) ||
670 ( DQ != NULL && mbedtls_mpi_cmp_mpi( DQ, &ctx->DQ ) != 0 ) ||
671 ( QP != NULL && mbedtls_mpi_cmp_mpi( QP, &ctx->QP ) != 0 ) )
672 {
673 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
674 }
Hanno Becker23344b52017-08-23 07:43:27 +0100675#else /* MBEDTLS_RSA_NO_CRT */
676
677 /*
678 * Check that DP, DQ and QP are in accordance with core parameters.
679 * (1) Check that DP - P == 0 mod P - 1
680 * (2) Check that DQ - Q == 0 mod Q - 1
681 * (3) Check that QP * P - 1 == 0 mod P
682
683 * Alternative implementation also not using DP, DQ and QP
684 * should be able to reuse this codepath.
685 */
686
687 /* Check (1) */
688 if( DP != NULL )
689 {
690 /* Temporarily replace P by P-1 and compute DP - D mod P-1 */
691 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
692 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( DP, DP, &ctx->D ) );
693 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, DP, &ctx->P ) );
694 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
695
696 if( mbedtls_mpi_cmp_int( DP, 0 ) != 0 )
697 {
698 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
699 }
700 }
701
702 /* Check (1) */
703 if( DQ != NULL )
704 {
705 /* Temporarily replace Q by Q-1 and compute DQ - D mod Q-1 */
706 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
707 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( DQ, DQ, &ctx->D ) );
708 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, DQ, &ctx->Q ) );
709 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
710
711 if( mbedtls_mpi_cmp_int( DQ, 0 ) != 0 )
712 {
713 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
714 }
715 }
716
717 /* Check (3) */
718 if( QP != NULL )
719 {
720 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( QP, QP, &ctx->Q ) );
721 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( QP, QP, 1 ) );
722 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( QP, QP, &ctx->P ) );
723 if( mbedtls_mpi_cmp_int( QP, 0 ) != 0 )
724 {
725 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
726 }
727 }
728
729cleanup:
730
731#endif
732
733 if( ret != 0 )
734 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100735
736 return( 0 );
737}
738
739int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
740 unsigned char *N, size_t N_len,
741 unsigned char *P, size_t P_len,
742 unsigned char *Q, size_t Q_len,
743 unsigned char *D, size_t D_len,
744 unsigned char *E, size_t E_len )
745{
746 int ret = 0;
747
748 /* Check if key is private or public */
749 const int is_priv =
750 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
751 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
752 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
753 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
754 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
755
756 if( !is_priv )
757 {
758 /* If we're trying to export private parameters for a public key,
759 * something must be wrong. */
760 if( P != NULL || Q != NULL || D != NULL )
761 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
762
763 }
764
765 if( N != NULL )
766 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
767
768 if( P != NULL )
769 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
770
771 if( Q != NULL )
772 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
773
774 if( D != NULL )
775 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
776
777 if( E != NULL )
778 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100779
780cleanup:
781
782 return( ret );
783}
784
Hanno Becker617c1ae2017-08-23 14:11:24 +0100785int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
786 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
787 mbedtls_mpi *D, mbedtls_mpi *E )
788{
789 int ret;
790
791 /* Check if key is private or public */
792 int is_priv =
793 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
794 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
795 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
796 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
797 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
798
799 if( !is_priv )
800 {
801 /* If we're trying to export private parameters for a public key,
802 * something must be wrong. */
803 if( P != NULL || Q != NULL || D != NULL )
804 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
805
806 }
807
808 /* Export all requested core parameters. */
809
810 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
811 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
812 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
813 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
814 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
815 {
816 return( ret );
817 }
818
819 return( 0 );
820}
821
822/*
823 * Export CRT parameters
824 * This must also be implemented if CRT is not used, for being able to
825 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
826 * can be used in this case.
827 */
828int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
829 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
830{
831 int ret;
832
833 /* Check if key is private or public */
834 int is_priv =
835 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
836 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
837 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
838 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
839 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
840
841 if( !is_priv )
842 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
843
Hanno Beckerdc95c892017-08-23 06:57:02 +0100844#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100845 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100846 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
847 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
848 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
849 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100850 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100851 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100852#else
853 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
854 DP, DQ, QP ) ) != 0 )
855 {
856 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
857 }
858#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100859
860 return( 0 );
861}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100862
863/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 * Initialize an RSA context
865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000868 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000869{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_THREADING_C)
875 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200876#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000877}
878
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100879/*
880 * Set padding for an existing RSA context
881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100883{
884 ctx->padding = padding;
885 ctx->hash_id = hash_id;
886}
887
Hanno Becker617c1ae2017-08-23 14:11:24 +0100888/*
889 * Get length in bytes of RSA modulus
890 */
891
892size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
893{
894 return( mbedtls_mpi_size( &ctx->N ) );
895}
896
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
900/*
901 * Generate an RSA keypair
902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000904 int (*f_rng)(void *, unsigned char *, size_t),
905 void *p_rng,
906 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000907{
908 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
Paul Bakker21eb2802010-08-16 11:10:02 +0000911 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Janos Follathef441782016-09-21 13:18:12 +0100914 if( nbits % 2 )
915 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
916
917 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath10c575b2016-02-23 14:42:48 +0000918 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
920 /*
921 * find primes P and Q with Q < P so that:
922 * GCD( E, (P-1)*(Q-1) ) == 1
923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000925
926 do
927 {
Janos Follath10c575b2016-02-23 14:42:48 +0000928 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000929 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
Janos Follathef441782016-09-21 13:18:12 +0100931 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000932 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 continue;
936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200938 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 continue;
940
Janos Follathef441782016-09-21 13:18:12 +0100941 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
942 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
945 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
946 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
947 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000948 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000950
951 /*
952 * D = E^-1 mod ((P-1)*(Q-1))
953 * DP = D mod (P - 1)
954 * DQ = D mod (Q - 1)
955 * QP = Q^-1 mod P
956 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
958 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
959 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
960 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200962 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
Hanno Becker83aad1f2017-08-23 06:45:10 +0100964 /* Double-check */
965 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
966
Paul Bakker5121ce52009-01-03 21:22:43 +0000967cleanup:
968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000970
971 if( ret != 0 )
972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_rsa_free( ctx );
974 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 }
976
Paul Bakker48377d92013-08-30 12:06:24 +0200977 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000978}
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
982/*
983 * Check a public RSA key
984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000986{
Paul Bakker37940d92009-07-10 22:38:58 +0000987 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000989
Paul Bakker48377d92013-08-30 12:06:24 +0200990 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200994 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
995 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200998 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
1000 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
1002 return( 0 );
1003}
1004
1005/*
1006 * Check a private RSA key
1007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001009{
1010 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 return( ret );
1015
Paul Bakker37940d92009-07-10 22:38:58 +00001016 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +00001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
1020 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
1021 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
1022 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +00001023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
1025 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
1026 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1027 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1028 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
1029 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
1032 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
1033 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +00001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
1036 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
1037 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +00001038 /*
1039 * Check for a valid PKCS1v2 private key
1040 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
1042 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
1043 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
1044 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
1045 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
1046 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
1047 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 }
Paul Bakker48377d92013-08-30 12:06:24 +02001051
Paul Bakker5121ce52009-01-03 21:22:43 +00001052cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
1054 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
1055 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
1056 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +00001059 return( ret );
1060
Paul Bakker6c591fa2011-05-05 11:49:20 +00001061 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001063
1064 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001065}
1066
1067/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001068 * Check if contexts holding a public and private key match
1069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1073 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001076 }
1077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1079 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001082 }
1083
1084 return( 0 );
1085}
1086
1087/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 * Do an RSA public key operation
1089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001091 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 unsigned char *output )
1093{
Paul Bakker23986e52011-04-24 08:57:21 +00001094 int ret;
1095 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001099
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001100#if defined(MBEDTLS_THREADING_C)
1101 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1102 return( ret );
1103#endif
1104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001109 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1110 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 }
1112
1113 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1115 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001116
1117cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001119 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1120 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
1125 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001127
1128 return( 0 );
1129}
1130
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001131/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001132 * Generate or update blinding values, see section 10 of:
1133 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001134 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001135 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001136 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001137static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001138 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1139{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001140 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001141
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001142 if( ctx->Vf.p != NULL )
1143 {
1144 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1146 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1147 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1148 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001149
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001150 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001151 }
1152
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001153 /* Unblinding value: Vf = random number, invertible mod N */
1154 do {
1155 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1159 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1160 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001161
1162 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1164 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 +02001165
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001166
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001167cleanup:
1168 return( ret );
1169}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001170
Paul Bakker5121ce52009-01-03 21:22:43 +00001171/*
Janos Follathe81102e2017-03-22 13:38:28 +00001172 * Exponent blinding supposed to prevent side-channel attacks using multiple
1173 * traces of measurements to recover the RSA key. The more collisions are there,
1174 * the more bits of the key can be recovered. See [3].
1175 *
1176 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1177 * observations on avarage.
1178 *
1179 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1180 * to make 2^112 observations on avarage.
1181 *
1182 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1183 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1184 * Thus in this sense with 28 byte blinding the security is not reduced by
1185 * side-channel attacks like the one in [3])
1186 *
1187 * This countermeasure does not help if the key recovery is possible with a
1188 * single trace.
1189 */
1190#define RSA_EXPONENT_BLINDING 28
1191
1192/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 * Do an RSA private key operation
1194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001196 int (*f_rng)(void *, unsigned char *, size_t),
1197 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001198 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 unsigned char *output )
1200{
Paul Bakker23986e52011-04-24 08:57:21 +00001201 int ret;
1202 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001204 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001205#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001206 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001207 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001208#else
1209 mbedtls_mpi DP_blind, DQ_blind;
1210 mbedtls_mpi *DP = &ctx->DP;
1211 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001212#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001213
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001214 /* Make sure we have private key info, prevent possible misuse */
1215 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
1216 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001219 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1220
1221
1222 if( f_rng != NULL )
1223 {
Janos Follathe81102e2017-03-22 13:38:28 +00001224#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001225 mbedtls_mpi_init( &D_blind );
1226#else
1227 mbedtls_mpi_init( &DP_blind );
1228 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001229#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001230 }
Janos Follathe81102e2017-03-22 13:38:28 +00001231
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001233#if defined(MBEDTLS_THREADING_C)
1234 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1235 return( ret );
1236#endif
1237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1239 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001241 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1242 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 }
1244
Paul Bakkerf451bac2013-08-30 15:37:02 +02001245 if( f_rng != NULL )
1246 {
1247 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001248 * Blinding
1249 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001250 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001251 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1252 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001254
Janos Follathe81102e2017-03-22 13:38:28 +00001255 /*
1256 * Exponent blinding
1257 */
1258 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1259 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1260
Janos Follathf9203b42017-03-22 15:13:15 +00001261#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001262 /*
1263 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1264 */
1265 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1266 f_rng, p_rng ) );
1267 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1268 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1269 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1270
1271 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001272#else
1273 /*
1274 * DP_blind = ( P - 1 ) * R + DP
1275 */
1276 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1277 f_rng, p_rng ) );
1278 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1279 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1280 &ctx->DP ) );
1281
1282 DP = &DP_blind;
1283
1284 /*
1285 * DQ_blind = ( Q - 1 ) * R + DQ
1286 */
1287 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1288 f_rng, p_rng ) );
1289 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1290 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1291 &ctx->DQ ) );
1292
1293 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001294#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001295 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001298 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001299#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001300 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001301 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 *
1303 * T1 = input ^ dP mod P
1304 * T2 = input ^ dQ mod Q
1305 */
Janos Follathf9203b42017-03-22 15:13:15 +00001306 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1307 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
1309 /*
1310 * T = (T1 - T2) * (Q^-1 mod P) mod P
1311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1313 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1314 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315
1316 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001317 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1320 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1321#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001322
Paul Bakkerf451bac2013-08-30 15:37:02 +02001323 if( f_rng != NULL )
1324 {
1325 /*
1326 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001327 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001328 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001329 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
1333 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
1336cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001338 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1339 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001340#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001343 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1344
1345 if( f_rng != NULL )
1346 {
Janos Follathe81102e2017-03-22 13:38:28 +00001347#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001348 mbedtls_mpi_free( &D_blind );
1349#else
1350 mbedtls_mpi_free( &DP_blind );
1351 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001352#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001353 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001354
1355 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
1358 return( 0 );
1359}
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001362/**
1363 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1364 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001365 * \param dst buffer to mask
1366 * \param dlen length of destination buffer
1367 * \param src source of the mask generation
1368 * \param slen length of the source buffer
1369 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001370 */
Paul Bakker48377d92013-08-30 12:06:24 +02001371static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001375 unsigned char counter[4];
1376 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001377 unsigned int hlen;
1378 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001381 memset( counter, 0, 4 );
1382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001384
Simon Butcher02037452016-03-01 21:19:12 +00001385 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001386 p = dst;
1387
1388 while( dlen > 0 )
1389 {
1390 use_len = hlen;
1391 if( dlen < hlen )
1392 use_len = dlen;
1393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 mbedtls_md_starts( md_ctx );
1395 mbedtls_md_update( md_ctx, src, slen );
1396 mbedtls_md_update( md_ctx, counter, 4 );
1397 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001398
1399 for( i = 0; i < use_len; ++i )
1400 *p++ ^= mask[i];
1401
1402 counter[3]++;
1403
1404 dlen -= use_len;
1405 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001406
1407 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001412/*
1413 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001416 int (*f_rng)(void *, unsigned char *, size_t),
1417 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001418 int mode,
1419 const unsigned char *label, size_t label_len,
1420 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001421 const unsigned char *input,
1422 unsigned char *output )
1423{
1424 size_t olen;
1425 int ret;
1426 unsigned char *p = output;
1427 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 const mbedtls_md_info_t *md_info;
1429 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1432 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001433
1434 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001438 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001440
1441 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001443
Simon Butcher02037452016-03-01 21:19:12 +00001444 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001445 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001447
1448 memset( output, 0, olen );
1449
1450 *p++ = 0;
1451
Simon Butcher02037452016-03-01 21:19:12 +00001452 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001453 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001455
1456 p += hlen;
1457
Simon Butcher02037452016-03-01 21:19:12 +00001458 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001460 p += hlen;
1461 p += olen - 2 * hlen - 2 - ilen;
1462 *p++ = 1;
1463 memcpy( p, input, ilen );
1464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001466 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1467 {
1468 mbedtls_md_free( &md_ctx );
1469 return( ret );
1470 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001471
Simon Butcher02037452016-03-01 21:19:12 +00001472 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001473 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1474 &md_ctx );
1475
Simon Butcher02037452016-03-01 21:19:12 +00001476 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001477 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1478 &md_ctx );
1479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 return( ( mode == MBEDTLS_RSA_PUBLIC )
1483 ? mbedtls_rsa_public( ctx, output, output )
1484 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001489/*
1490 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001493 int (*f_rng)(void *, unsigned char *, size_t),
1494 void *p_rng,
1495 int mode, size_t ilen,
1496 const unsigned char *input,
1497 unsigned char *output )
1498{
1499 size_t nb_pad, olen;
1500 int ret;
1501 unsigned char *p = output;
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1504 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001505
Janos Follath1ed9f992016-03-18 11:45:44 +00001506 // We don't check p_rng because it won't be dereferenced here
1507 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001509
1510 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001511
Simon Butcher02037452016-03-01 21:19:12 +00001512 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001513 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001515
1516 nb_pad = olen - 3 - ilen;
1517
1518 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001522
1523 while( nb_pad-- > 0 )
1524 {
1525 int rng_dl = 100;
1526
1527 do {
1528 ret = f_rng( p_rng, p, 1 );
1529 } while( *p == 0 && --rng_dl && ret == 0 );
1530
Simon Butcher02037452016-03-01 21:19:12 +00001531 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001532 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001534
1535 p++;
1536 }
1537 }
1538 else
1539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001541
1542 while( nb_pad-- > 0 )
1543 *p++ = 0xFF;
1544 }
1545
1546 *p++ = 0;
1547 memcpy( p, input, ilen );
1548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 return( ( mode == MBEDTLS_RSA_PUBLIC )
1550 ? mbedtls_rsa_public( ctx, output, output )
1551 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001554
Paul Bakker5121ce52009-01-03 21:22:43 +00001555/*
1556 * Add the message padding, then do an RSA operation
1557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001559 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001560 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001561 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001562 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 unsigned char *output )
1564{
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 switch( ctx->padding )
1566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_PKCS1_V15)
1568 case MBEDTLS_RSA_PKCS_V15:
1569 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001570 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001571#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_PKCS1_V21)
1574 case MBEDTLS_RSA_PKCS_V21:
1575 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001576 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001577#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001578
1579 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001582}
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001585/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001586 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001589 int (*f_rng)(void *, unsigned char *, size_t),
1590 void *p_rng,
1591 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001592 const unsigned char *label, size_t label_len,
1593 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001594 const unsigned char *input,
1595 unsigned char *output,
1596 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001597{
Paul Bakker23986e52011-04-24 08:57:21 +00001598 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001599 size_t ilen, i, pad_len;
1600 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1602 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001603 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 const mbedtls_md_info_t *md_info;
1605 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001606
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001607 /*
1608 * Parameters sanity checks
1609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1611 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001612
1613 ilen = ctx->len;
1614
Paul Bakker27fdf462011-06-09 13:55:13 +00001615 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001619 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001621
Janos Follathc17cda12016-02-11 11:08:18 +00001622 hlen = mbedtls_md_get_size( md_info );
1623
1624 // checking for integer underflow
1625 if( 2 * hlen + 2 > ilen )
1626 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1627
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001628 /*
1629 * RSA operation
1630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1632 ? mbedtls_rsa_public( ctx, input, buf )
1633 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001634
1635 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001636 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001637
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001638 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001639 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001642 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1643 {
1644 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001645 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001646 }
1647
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001648
1649 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001651
1652 /* seed: Apply seedMask to maskedSeed */
1653 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1654 &md_ctx );
1655
1656 /* DB: Apply dbMask to maskedDB */
1657 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1658 &md_ctx );
1659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001661
1662 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001663 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001664 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001665 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001666 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001668 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001669
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001670 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001671
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001672 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001673 for( i = 0; i < hlen; i++ )
1674 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001675
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001676 /* Get zero-padding len, but always read till end of buffer
1677 * (minus one, for the 01 byte) */
1678 pad_len = 0;
1679 pad_done = 0;
1680 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1681 {
1682 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001683 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001684 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001685
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001686 p += pad_len;
1687 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001688
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001689 /*
1690 * The only information "leaked" is whether the padding was correct or not
1691 * (eg, no data is copied if it was not correct). This meets the
1692 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1693 * the different error conditions.
1694 */
1695 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001696 {
1697 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1698 goto cleanup;
1699 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001700
Paul Bakker66d5d072014-06-17 16:39:18 +02001701 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001702 {
1703 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1704 goto cleanup;
1705 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001706
1707 *olen = ilen - (p - buf);
1708 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001709 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001710
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001711cleanup:
1712 mbedtls_zeroize( buf, sizeof( buf ) );
1713 mbedtls_zeroize( lhash, sizeof( lhash ) );
1714
1715 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001716}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001720/*
1721 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001724 int (*f_rng)(void *, unsigned char *, size_t),
1725 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001726 int mode, size_t *olen,
1727 const unsigned char *input,
1728 unsigned char *output,
1729 size_t output_max_len)
1730{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001731 int ret;
1732 size_t ilen, pad_count = 0, i;
1733 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1737 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001738
1739 ilen = ctx->len;
1740
1741 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1745 ? mbedtls_rsa_public( ctx, input, buf )
1746 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001747
1748 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001749 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001750
1751 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001752 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001753
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001754 /*
1755 * Check and get padding len in "constant-time"
1756 */
1757 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001758
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001759 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001763
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001764 /* Get padding len, but always read till end of buffer
1765 * (minus one, for the 00 byte) */
1766 for( i = 0; i < ilen - 3; i++ )
1767 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001768 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1769 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001770 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001771
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001772 p += pad_count;
1773 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001774 }
1775 else
1776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001778
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001779 /* Get padding len, but always read till end of buffer
1780 * (minus one, for the 00 byte) */
1781 for( i = 0; i < ilen - 3; i++ )
1782 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001783 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001784 pad_count += ( pad_done == 0 );
1785 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001786
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001787 p += pad_count;
1788 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 }
1790
Janos Follathc69fa502016-02-12 13:30:09 +00001791 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001792
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001793 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001794 {
1795 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1796 goto cleanup;
1797 }
Paul Bakker8804f692013-02-28 18:06:26 +01001798
Paul Bakker66d5d072014-06-17 16:39:18 +02001799 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001800 {
1801 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1802 goto cleanup;
1803 }
Paul Bakker060c5682009-01-12 21:48:39 +00001804
Paul Bakker27fdf462011-06-09 13:55:13 +00001805 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001806 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001807 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001808
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001809cleanup:
1810 mbedtls_zeroize( buf, sizeof( buf ) );
1811
1812 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001813}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001815
1816/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001817 * Do an RSA operation, then remove the message padding
1818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001820 int (*f_rng)(void *, unsigned char *, size_t),
1821 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001822 int mode, size_t *olen,
1823 const unsigned char *input,
1824 unsigned char *output,
1825 size_t output_max_len)
1826{
1827 switch( ctx->padding )
1828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#if defined(MBEDTLS_PKCS1_V15)
1830 case MBEDTLS_RSA_PKCS_V15:
1831 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001832 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001833#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835#if defined(MBEDTLS_PKCS1_V21)
1836 case MBEDTLS_RSA_PKCS_V21:
1837 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001838 olen, input, output,
1839 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001840#endif
1841
1842 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001844 }
1845}
1846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001848/*
1849 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001852 int (*f_rng)(void *, unsigned char *, size_t),
1853 void *p_rng,
1854 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001856 unsigned int hashlen,
1857 const unsigned char *hash,
1858 unsigned char *sig )
1859{
1860 size_t olen;
1861 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001863 unsigned int slen, hlen, offset = 0;
1864 int ret;
1865 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 const mbedtls_md_info_t *md_info;
1867 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1870 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001871
1872 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001874
1875 olen = ctx->len;
1876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001878 {
Simon Butcher02037452016-03-01 21:19:12 +00001879 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001881 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001885 }
1886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001888 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001892 slen = hlen;
1893
1894 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001896
1897 memset( sig, 0, olen );
1898
Simon Butcher02037452016-03-01 21:19:12 +00001899 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001900 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001902
Simon Butcher02037452016-03-01 21:19:12 +00001903 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001904 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001905 p += olen - hlen * 2 - 2;
1906 *p++ = 0x01;
1907 memcpy( p, salt, slen );
1908 p += slen;
1909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001911 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1912 {
1913 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001914 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001915 return( ret );
1916 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001917
Simon Butcher02037452016-03-01 21:19:12 +00001918 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 mbedtls_md_starts( &md_ctx );
1920 mbedtls_md_update( &md_ctx, p, 8 );
1921 mbedtls_md_update( &md_ctx, hash, hashlen );
1922 mbedtls_md_update( &md_ctx, salt, slen );
1923 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001924 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001925
Simon Butcher02037452016-03-01 21:19:12 +00001926 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001927 if( msb % 8 == 0 )
1928 offset = 1;
1929
Simon Butcher02037452016-03-01 21:19:12 +00001930 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001931 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001934
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001935 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001936 sig[0] &= 0xFF >> ( olen * 8 - msb );
1937
1938 p += hlen;
1939 *p++ = 0xBC;
1940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 return( ( mode == MBEDTLS_RSA_PUBLIC )
1942 ? mbedtls_rsa_public( ctx, sig, sig )
1943 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001944}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001948/*
1949 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1950 */
1951/*
1952 * Do an RSA operation to sign the message digest
1953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001955 int (*f_rng)(void *, unsigned char *, size_t),
1956 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001957 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001959 unsigned int hashlen,
1960 const unsigned char *hash,
1961 unsigned char *sig )
1962{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001963 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001964 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001965 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001966 unsigned char *sig_try = NULL, *verif = NULL;
1967 size_t i;
1968 unsigned char diff;
1969 volatile unsigned char diff_no_optimize;
1970 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1973 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001974
1975 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001976 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001981 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1985 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001986
Paul Bakkerc70b9822013-04-07 22:00:46 +02001987 nb_pad -= 10 + oid_size;
1988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001990 }
1991
Paul Bakkerc70b9822013-04-07 22:00:46 +02001992 nb_pad -= hashlen;
1993
Paul Bakkerb3869132013-02-28 17:21:01 +01001994 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001996
1997 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001999 memset( p, 0xFF, nb_pad );
2000 p += nb_pad;
2001 *p++ = 0;
2002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002004 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002005 memcpy( p, hash, hashlen );
2006 }
2007 else
2008 {
2009 /*
2010 * DigestInfo ::= SEQUENCE {
2011 * digestAlgorithm DigestAlgorithmIdentifier,
2012 * digest Digest }
2013 *
2014 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2015 *
2016 * Digest ::= OCTET STRING
2017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002019 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002021 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002023 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002024 memcpy( p, oid, oid_size );
2025 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002027 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002029 *p++ = hashlen;
2030 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002031 }
2032
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002033 if( mode == MBEDTLS_RSA_PUBLIC )
2034 return( mbedtls_rsa_public( ctx, sig, sig ) );
2035
2036 /*
2037 * In order to prevent Lenstra's attack, make the signature in a
2038 * temporary buffer and check it before returning it.
2039 */
2040 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002041 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002042 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2043
Simon Butcher1285ab52016-01-01 21:42:47 +00002044 verif = mbedtls_calloc( 1, ctx->len );
2045 if( verif == NULL )
2046 {
2047 mbedtls_free( sig_try );
2048 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2049 }
2050
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002051 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2052 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2053
2054 /* Compare in constant time just in case */
2055 for( diff = 0, i = 0; i < ctx->len; i++ )
2056 diff |= verif[i] ^ sig[i];
2057 diff_no_optimize = diff;
2058
2059 if( diff_no_optimize != 0 )
2060 {
2061 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2062 goto cleanup;
2063 }
2064
2065 memcpy( sig, sig_try, ctx->len );
2066
2067cleanup:
2068 mbedtls_free( sig_try );
2069 mbedtls_free( verif );
2070
2071 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002072}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002074
2075/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 * Do an RSA operation to sign the message digest
2077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002079 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002080 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002083 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002084 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 unsigned char *sig )
2086{
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 switch( ctx->padding )
2088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#if defined(MBEDTLS_PKCS1_V15)
2090 case MBEDTLS_RSA_PKCS_V15:
2091 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002092 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002093#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095#if defined(MBEDTLS_PKCS1_V21)
2096 case MBEDTLS_RSA_PKCS_V21:
2097 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002098 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002099#endif
2100
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002104}
2105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002107/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002108 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002111 int (*f_rng)(void *, unsigned char *, size_t),
2112 void *p_rng,
2113 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002115 unsigned int hashlen,
2116 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002118 int expected_salt_len,
2119 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002120{
Paul Bakker23986e52011-04-24 08:57:21 +00002121 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002122 size_t siglen;
2123 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002125 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002126 unsigned int hlen;
2127 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 const mbedtls_md_info_t *md_info;
2129 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002130 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2133 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002134
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 siglen = ctx->len;
2136
Paul Bakker27fdf462011-06-09 13:55:13 +00002137 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2141 ? mbedtls_rsa_public( ctx, sig, buf )
2142 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002143
2144 if( ret != 0 )
2145 return( ret );
2146
2147 p = buf;
2148
Paul Bakkerb3869132013-02-28 17:21:01 +01002149 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 {
Simon Butcher02037452016-03-01 21:19:12 +00002154 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002156 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002160 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002163 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002167 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002168
Paul Bakkerb3869132013-02-28 17:21:01 +01002169 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002170
Simon Butcher02037452016-03-01 21:19:12 +00002171 /*
2172 * Note: EMSA-PSS verification is over the length of N - 1 bits
2173 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002174 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002175
Simon Butcher02037452016-03-01 21:19:12 +00002176 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002177 if( msb % 8 == 0 )
2178 {
2179 p++;
2180 siglen -= 1;
2181 }
2182 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002186 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2187 {
2188 mbedtls_md_free( &md_ctx );
2189 return( ret );
2190 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002191
Paul Bakkerb3869132013-02-28 17:21:01 +01002192 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002193
Paul Bakkerb3869132013-02-28 17:21:01 +01002194 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002195
Paul Bakker4de44aa2013-12-31 11:43:01 +01002196 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002197 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002198
Paul Bakkerb3869132013-02-28 17:21:01 +01002199 if( p == buf + siglen ||
2200 *p++ != 0x01 )
2201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 mbedtls_md_free( &md_ctx );
2203 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002204 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002205
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002206 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002207 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002210 slen != (size_t) expected_salt_len )
2211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 mbedtls_md_free( &md_ctx );
2213 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002214 }
2215
Simon Butcher02037452016-03-01 21:19:12 +00002216 /*
2217 * Generate H = Hash( M' )
2218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 mbedtls_md_starts( &md_ctx );
2220 mbedtls_md_update( &md_ctx, zeros, 8 );
2221 mbedtls_md_update( &md_ctx, hash, hashlen );
2222 mbedtls_md_update( &md_ctx, p, slen );
2223 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002226
Paul Bakkerb3869132013-02-28 17:21:01 +01002227 if( memcmp( p + slen, result, hlen ) == 0 )
2228 return( 0 );
2229 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002231}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002232
2233/*
2234 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002237 int (*f_rng)(void *, unsigned char *, size_t),
2238 void *p_rng,
2239 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002241 unsigned int hashlen,
2242 const unsigned char *hash,
2243 const unsigned char *sig )
2244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2246 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002247 : md_alg;
2248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002250 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002252 sig ) );
2253
2254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002258/*
2259 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002262 int (*f_rng)(void *, unsigned char *, size_t),
2263 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002264 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002266 unsigned int hashlen,
2267 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002268 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002269{
2270 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002271 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002272 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 mbedtls_md_type_t msg_md_alg;
2274 const mbedtls_md_info_t *md_info;
2275 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002276 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2279 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002280
2281 siglen = ctx->len;
2282
2283 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2287 ? mbedtls_rsa_public( ctx, sig, buf )
2288 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002289
2290 if( ret != 0 )
2291 return( ret );
2292
2293 p = buf;
2294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2296 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002297
2298 while( *p != 0 )
2299 {
2300 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002302 p++;
2303 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002304 p++; /* skip 00 byte */
2305
2306 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2307 if( p - buf < 11 )
2308 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002309
2310 len = siglen - ( p - buf );
2311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002313 {
2314 if( memcmp( p, hash, hashlen ) == 0 )
2315 return( 0 );
2316 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002318 }
2319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002321 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2323 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002324
2325 end = p + len;
2326
Simon Butcher02037452016-03-01 21:19:12 +00002327 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002328 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2329 * Insist on 2-byte length tags, to protect against variants of
2330 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002331 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002332 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2334 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2335 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002336 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002338
Gilles Peskinee7e76502017-05-04 12:48:39 +02002339 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2341 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2342 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002343 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002345
Gilles Peskinee7e76502017-05-04 12:48:39 +02002346 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2348 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002349 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002350 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002351
2352 oid.p = p;
2353 p += oid.len;
2354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2356 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002357
2358 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002360
2361 /*
2362 * assume the algorithm parameters must be NULL
2363 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002364 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2366 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002367 if( p != p0 + 2 )
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 Peskine0e17eb02017-05-03 18:32:21 +02002370 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002371 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2372 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002373 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002375
2376 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002378
2379 p += hashlen;
2380
2381 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002383
2384 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
2388/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002389 * Do an RSA operation and check the message digest
2390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002392 int (*f_rng)(void *, unsigned char *, size_t),
2393 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002394 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002396 unsigned int hashlen,
2397 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002398 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002399{
2400 switch( ctx->padding )
2401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#if defined(MBEDTLS_PKCS1_V15)
2403 case MBEDTLS_RSA_PKCS_V15:
2404 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002405 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002406#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408#if defined(MBEDTLS_PKCS1_V21)
2409 case MBEDTLS_RSA_PKCS_V21:
2410 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002411 hashlen, hash, sig );
2412#endif
2413
2414 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002416 }
2417}
2418
2419/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002420 * Copy the components of an RSA key
2421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002423{
2424 int ret;
2425
2426 dst->ver = src->ver;
2427 dst->len = src->len;
2428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2430 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2433 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2434 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002435
2436#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2438 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2439 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2441 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002442#endif
2443
2444 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2447 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002448
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002449 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002450 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002451
2452cleanup:
2453 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002455
2456 return( ret );
2457}
2458
2459/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 * Free the components of an RSA key
2461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002465 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2466 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002468
Hanno Becker33c30a02017-08-23 07:00:22 +01002469#if !defined(MBEDTLS_RSA_NO_CRT)
2470 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2471 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2472 mbedtls_mpi_free( &ctx->DP );
2473#endif /* MBEDTLS_RSA_NO_CRT */
2474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475#if defined(MBEDTLS_THREADING_C)
2476 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002477#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002478}
2479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002482#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
2484/*
2485 * Example RSA-1024 keypair, for test purposes
2486 */
2487#define KEY_LEN 128
2488
2489#define RSA_N "9292758453063D803DD603D5E777D788" \
2490 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2491 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2492 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2493 "93A89813FBF3C4F8066D2D800F7C38A8" \
2494 "1AE31942917403FF4946B0A83D3D3E05" \
2495 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2496 "5E94BB77B07507233A0BC7BAC8F90F79"
2497
2498#define RSA_E "10001"
2499
2500#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2501 "66CA472BC44D253102F8B4A9D3BFA750" \
2502 "91386C0077937FE33FA3252D28855837" \
2503 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2504 "DF79C5CE07EE72C7F123142198164234" \
2505 "CABB724CF78B8173B9F880FC86322407" \
2506 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2507 "071513A1E85B5DFA031F21ECAE91A34D"
2508
2509#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2510 "2C01CAD19EA484A87EA4377637E75500" \
2511 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2512 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2513
2514#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2515 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2516 "910E4168387E3C30AA1E00C339A79508" \
2517 "8452DD96A9A5EA5D9DCA68DA636032AF"
2518
2519#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2520 "3C94D22288ACD763FD8E5600ED4A702D" \
2521 "F84198A5F06C2E72236AE490C93F07F8" \
2522 "3CC559CD27BC2D1CA488811730BB5725"
2523
2524#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2525 "D8AAEA56749EA28623272E4F7D0592AF" \
2526 "7C1F1313CAC9471B5C523BFE592F517B" \
2527 "407A1BD76C164B93DA2D32A383E58357"
2528
2529#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2530 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2531 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2532 "A74206CEC169D74BF5A8C50D6F48EA08"
2533
2534#define PT_LEN 24
2535#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2536 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002539static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002540{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002541#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002542 size_t i;
2543
Paul Bakker545570e2010-07-18 09:00:25 +00002544 if( rng_state != NULL )
2545 rng_state = NULL;
2546
Paul Bakkera3d195c2011-11-27 21:07:34 +00002547 for( i = 0; i < len; ++i )
2548 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002549#else
2550 if( rng_state != NULL )
2551 rng_state = NULL;
2552
2553 arc4random_buf( output, len );
2554#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002555
Paul Bakkera3d195c2011-11-27 21:07:34 +00002556 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002559
Paul Bakker5121ce52009-01-03 21:22:43 +00002560/*
2561 * Checkup routine
2562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002564{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002565 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002567 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 unsigned char rsa_plaintext[PT_LEN];
2570 unsigned char rsa_decrypted[PT_LEN];
2571 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002573 unsigned char sha1sum[20];
2574#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Hanno Becker3a701162017-08-22 13:52:43 +01002576 mbedtls_mpi K;
2577
2578 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
Hanno Becker3a701162017-08-22 13:52:43 +01002581 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2582 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2583 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2584 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2585 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2586 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2587 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2588 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2589 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2590 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2591
2592 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
2594 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2598 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 {
2600 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
2603 return( 1 );
2604 }
2605
Hanno Becker3a701162017-08-22 13:52:43 +01002606 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2607 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2608
2609 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2610 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2611
2612 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2613 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2614
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002617
2618 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 rsa_plaintext, rsa_ciphertext ) != 0 )
2622 {
2623 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
2626 return( 1 );
2627 }
2628
2629 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002633 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002634 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 {
2636 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
2639 return( 1 );
2640 }
2641
2642 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2643 {
2644 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
2647 return( 1 );
2648 }
2649
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002650 if( verbose != 0 )
2651 mbedtls_printf( "passed\n" );
2652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002655 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 sha1sum, rsa_ciphertext ) != 0 )
2661 {
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( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 sha1sum, rsa_ciphertext ) != 0 )
2673 {
2674 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
2677 return( 1 );
2678 }
2679
2680 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002681 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002684 if( verbose != 0 )
2685 mbedtls_printf( "\n" );
2686
Paul Bakker3d8fb632014-04-17 12:42:41 +02002687cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002688 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 mbedtls_rsa_free( &rsa );
2690#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002691 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002693 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694}
2695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698#endif /* MBEDTLS_RSA_C */