blob: dc1fae59cb40489f7cd5e142e00c3e1b9cb7c792 [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;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100909 mbedtls_mpi 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
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100917 mbedtls_mpi_init( &H );
918 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,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100929 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,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100932 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 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100942 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100943
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100944 /* Temporarily replace P,Q by P-1, Q-1 */
945 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
946 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
947 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000949 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100952 /* Restore P,Q */
953 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
954 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
955
956 ctx->len = mbedtls_mpi_size( &ctx->N );
957
Paul Bakker5121ce52009-01-03 21:22:43 +0000958 /*
959 * D = E^-1 mod ((P-1)*(Q-1))
960 * DP = D mod (P - 1)
961 * DQ = D mod (Q - 1)
962 * QP = Q^-1 mod P
963 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100965 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
966
967#if !defined(MBEDTLS_RSA_NO_CRT)
968 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
969 &ctx->DP, &ctx->DQ, &ctx->QP ) );
970#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
Hanno Becker83aad1f2017-08-23 06:45:10 +0100972 /* Double-check */
973 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
974
Paul Bakker5121ce52009-01-03 21:22:43 +0000975cleanup:
976
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100977 mbedtls_mpi_free( &H );
978 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
980 if( ret != 0 )
981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 mbedtls_rsa_free( ctx );
983 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 }
985
Paul Bakker48377d92013-08-30 12:06:24 +0200986 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000987}
988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000990
991/*
992 * Check a public RSA key
993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000995{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000996 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000998
Paul Bakker48377d92013-08-30 12:06:24 +0200999 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001003 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
1004 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001007 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
1009 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
1011 return( 0 );
1012}
1013
1014/*
1015 * Check a private RSA key
1016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001018{
1019 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 return( ret );
1024
Paul Bakker37940d9f2009-07-10 22:38:58 +00001025 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +00001027
Hanno Becker6345dd32017-08-23 06:59:48 +01001028 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 );
1029 mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I );
1030 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 ); mbedtls_mpi_init( &L1 );
1031 mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
1035 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
1036 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1037 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1038 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
1039 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
1042 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
1043 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +00001044
Hanno Becker6345dd32017-08-23 06:59:48 +01001045#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
1047 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
1048 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Hanno Becker6345dd32017-08-23 06:59:48 +01001049#endif
1050
Paul Bakkerb572adf2010-07-18 08:29:32 +00001051 /*
1052 * Check for a valid PKCS1v2 private key
1053 */
Hanno Becker6345dd32017-08-23 06:59:48 +01001054 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
1055#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
1057 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
1058 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
Hanno Becker6345dd32017-08-23 06:59:48 +01001059#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
Hanno Becker6345dd32017-08-23 06:59:48 +01001061 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 }
Paul Bakker48377d92013-08-30 12:06:24 +02001066
Paul Bakker5121ce52009-01-03 21:22:43 +00001067cleanup:
Hanno Becker6345dd32017-08-23 06:59:48 +01001068 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 );
1069 mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I );
1070 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 ); mbedtls_mpi_free( &L1 );
1071 mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +00001075 return( ret );
1076
Paul Bakker6c591fa2011-05-05 11:49:20 +00001077 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001079
1080 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081}
1082
1083/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001084 * Check if contexts holding a public and private key match
1085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1089 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001092 }
1093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1095 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001098 }
1099
1100 return( 0 );
1101}
1102
1103/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 * Do an RSA public key operation
1105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001107 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 unsigned char *output )
1109{
Paul Bakker23986e52011-04-24 08:57:21 +00001110 int ret;
1111 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001116#if defined(MBEDTLS_THREADING_C)
1117 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1118 return( ret );
1119#endif
1120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001125 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1126 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 }
1128
1129 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1131 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
1133cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001135 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1136 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001137#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
1141 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
1144 return( 0 );
1145}
1146
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001147/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001148 * Generate or update blinding values, see section 10 of:
1149 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001150 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001151 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001152 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001153static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001154 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1155{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001156 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001157
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001158 if( ctx->Vf.p != NULL )
1159 {
1160 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1162 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1163 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1164 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001165
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001166 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001167 }
1168
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001169 /* Unblinding value: Vf = random number, invertible mod N */
1170 do {
1171 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1175 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1176 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001177
1178 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1180 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 +02001181
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001182
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001183cleanup:
1184 return( ret );
1185}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001186
Paul Bakker5121ce52009-01-03 21:22:43 +00001187/*
Janos Follathe81102e2017-03-22 13:38:28 +00001188 * Exponent blinding supposed to prevent side-channel attacks using multiple
1189 * traces of measurements to recover the RSA key. The more collisions are there,
1190 * the more bits of the key can be recovered. See [3].
1191 *
1192 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1193 * observations on avarage.
1194 *
1195 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1196 * to make 2^112 observations on avarage.
1197 *
1198 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1199 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1200 * Thus in this sense with 28 byte blinding the security is not reduced by
1201 * side-channel attacks like the one in [3])
1202 *
1203 * This countermeasure does not help if the key recovery is possible with a
1204 * single trace.
1205 */
1206#define RSA_EXPONENT_BLINDING 28
1207
1208/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 * Do an RSA private key operation
1210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001212 int (*f_rng)(void *, unsigned char *, size_t),
1213 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001214 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 unsigned char *output )
1216{
Paul Bakker23986e52011-04-24 08:57:21 +00001217 int ret;
1218 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001220 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001221#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001222 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001223 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001224#else
1225 mbedtls_mpi DP_blind, DQ_blind;
1226 mbedtls_mpi *DP = &ctx->DP;
1227 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001228#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001230 /* Make sure we have private key info, prevent possible misuse */
1231 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
1232 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001235 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1236
1237
1238 if( f_rng != NULL )
1239 {
Janos Follathe81102e2017-03-22 13:38:28 +00001240#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001241 mbedtls_mpi_init( &D_blind );
1242#else
1243 mbedtls_mpi_init( &DP_blind );
1244 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001245#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001246 }
Janos Follathe81102e2017-03-22 13:38:28 +00001247
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001249#if defined(MBEDTLS_THREADING_C)
1250 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1251 return( ret );
1252#endif
1253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1255 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001257 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1258 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 }
1260
Paul Bakkerf451bac2013-08-30 15:37:02 +02001261 if( f_rng != NULL )
1262 {
1263 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001264 * Blinding
1265 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001266 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001267 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1268 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001270
Janos Follathe81102e2017-03-22 13:38:28 +00001271 /*
1272 * Exponent blinding
1273 */
1274 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1275 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1276
Janos Follathf9203b42017-03-22 15:13:15 +00001277#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001278 /*
1279 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1280 */
1281 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1282 f_rng, p_rng ) );
1283 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1284 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1285 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1286
1287 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001288#else
1289 /*
1290 * DP_blind = ( P - 1 ) * R + DP
1291 */
1292 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1293 f_rng, p_rng ) );
1294 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1295 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1296 &ctx->DP ) );
1297
1298 DP = &DP_blind;
1299
1300 /*
1301 * DQ_blind = ( Q - 1 ) * R + DQ
1302 */
1303 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1304 f_rng, p_rng ) );
1305 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1306 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1307 &ctx->DQ ) );
1308
1309 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001310#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001311 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001314 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001315#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001316 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001317 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 *
1319 * T1 = input ^ dP mod P
1320 * T2 = input ^ dQ mod Q
1321 */
Janos Follathf9203b42017-03-22 15:13:15 +00001322 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1323 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
1325 /*
1326 * T = (T1 - T2) * (Q^-1 mod P) mod P
1327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1329 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1330 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
1332 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001333 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1336 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1337#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001338
Paul Bakkerf451bac2013-08-30 15:37:02 +02001339 if( f_rng != NULL )
1340 {
1341 /*
1342 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001343 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001344 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001345 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001347 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
1349 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001351
1352cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001354 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1355 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001356#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001359 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1360
1361 if( f_rng != NULL )
1362 {
Janos Follathe81102e2017-03-22 13:38:28 +00001363#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001364 mbedtls_mpi_free( &D_blind );
1365#else
1366 mbedtls_mpi_free( &DP_blind );
1367 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001368#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001369 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
1371 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
1374 return( 0 );
1375}
1376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001378/**
1379 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1380 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001381 * \param dst buffer to mask
1382 * \param dlen length of destination buffer
1383 * \param src source of the mask generation
1384 * \param slen length of the source buffer
1385 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001386 */
Paul Bakker48377d92013-08-30 12:06:24 +02001387static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001391 unsigned char counter[4];
1392 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001393 unsigned int hlen;
1394 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001397 memset( counter, 0, 4 );
1398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001400
Simon Butcher02037452016-03-01 21:19:12 +00001401 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001402 p = dst;
1403
1404 while( dlen > 0 )
1405 {
1406 use_len = hlen;
1407 if( dlen < hlen )
1408 use_len = dlen;
1409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 mbedtls_md_starts( md_ctx );
1411 mbedtls_md_update( md_ctx, src, slen );
1412 mbedtls_md_update( md_ctx, counter, 4 );
1413 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001414
1415 for( i = 0; i < use_len; ++i )
1416 *p++ ^= mask[i];
1417
1418 counter[3]++;
1419
1420 dlen -= use_len;
1421 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001422
1423 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001424}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001428/*
1429 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001432 int (*f_rng)(void *, unsigned char *, size_t),
1433 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001434 int mode,
1435 const unsigned char *label, size_t label_len,
1436 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001437 const unsigned char *input,
1438 unsigned char *output )
1439{
1440 size_t olen;
1441 int ret;
1442 unsigned char *p = output;
1443 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 const mbedtls_md_info_t *md_info;
1445 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1448 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001449
1450 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001454 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001456
1457 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001459
Simon Butcher02037452016-03-01 21:19:12 +00001460 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001461 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001463
1464 memset( output, 0, olen );
1465
1466 *p++ = 0;
1467
Simon Butcher02037452016-03-01 21:19:12 +00001468 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001469 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001471
1472 p += hlen;
1473
Simon Butcher02037452016-03-01 21:19:12 +00001474 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001476 p += hlen;
1477 p += olen - 2 * hlen - 2 - ilen;
1478 *p++ = 1;
1479 memcpy( p, input, ilen );
1480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001482 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1483 {
1484 mbedtls_md_free( &md_ctx );
1485 return( ret );
1486 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001487
Simon Butcher02037452016-03-01 21:19:12 +00001488 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001489 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1490 &md_ctx );
1491
Simon Butcher02037452016-03-01 21:19:12 +00001492 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001493 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1494 &md_ctx );
1495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 return( ( mode == MBEDTLS_RSA_PUBLIC )
1499 ? mbedtls_rsa_public( ctx, output, output )
1500 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001505/*
1506 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001509 int (*f_rng)(void *, unsigned char *, size_t),
1510 void *p_rng,
1511 int mode, size_t ilen,
1512 const unsigned char *input,
1513 unsigned char *output )
1514{
1515 size_t nb_pad, olen;
1516 int ret;
1517 unsigned char *p = output;
1518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1520 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001521
Janos Follath1ed9f992016-03-18 11:45:44 +00001522 // We don't check p_rng because it won't be dereferenced here
1523 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001525
1526 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001527
Simon Butcher02037452016-03-01 21:19:12 +00001528 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001529 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001531
1532 nb_pad = olen - 3 - ilen;
1533
1534 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001538
1539 while( nb_pad-- > 0 )
1540 {
1541 int rng_dl = 100;
1542
1543 do {
1544 ret = f_rng( p_rng, p, 1 );
1545 } while( *p == 0 && --rng_dl && ret == 0 );
1546
Simon Butcher02037452016-03-01 21:19:12 +00001547 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001548 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001550
1551 p++;
1552 }
1553 }
1554 else
1555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001557
1558 while( nb_pad-- > 0 )
1559 *p++ = 0xFF;
1560 }
1561
1562 *p++ = 0;
1563 memcpy( p, input, ilen );
1564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 return( ( mode == MBEDTLS_RSA_PUBLIC )
1566 ? mbedtls_rsa_public( ctx, output, output )
1567 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001568}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001570
Paul Bakker5121ce52009-01-03 21:22:43 +00001571/*
1572 * Add the message padding, then do an RSA operation
1573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001575 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001576 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001577 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001578 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001579 unsigned char *output )
1580{
Paul Bakker5121ce52009-01-03 21:22:43 +00001581 switch( ctx->padding )
1582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_PKCS1_V15)
1584 case MBEDTLS_RSA_PKCS_V15:
1585 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001586 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001587#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_PKCS1_V21)
1590 case MBEDTLS_RSA_PKCS_V21:
1591 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001592 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001593#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
1595 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001598}
1599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001601/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001602 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001605 int (*f_rng)(void *, unsigned char *, size_t),
1606 void *p_rng,
1607 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001608 const unsigned char *label, size_t label_len,
1609 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001610 const unsigned char *input,
1611 unsigned char *output,
1612 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001613{
Paul Bakker23986e52011-04-24 08:57:21 +00001614 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001615 size_t ilen, i, pad_len;
1616 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1618 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001619 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 const mbedtls_md_info_t *md_info;
1621 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001622
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001623 /*
1624 * Parameters sanity checks
1625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1627 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
1629 ilen = ctx->len;
1630
Paul Bakker27fdf462011-06-09 13:55:13 +00001631 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001635 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001637
Janos Follathc17cda12016-02-11 11:08:18 +00001638 hlen = mbedtls_md_get_size( md_info );
1639
1640 // checking for integer underflow
1641 if( 2 * hlen + 2 > ilen )
1642 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1643
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001644 /*
1645 * RSA operation
1646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1648 ? mbedtls_rsa_public( ctx, input, buf )
1649 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
1651 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001652 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001654 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001655 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001658 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1659 {
1660 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001661 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001662 }
1663
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001664
1665 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001667
1668 /* seed: Apply seedMask to maskedSeed */
1669 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1670 &md_ctx );
1671
1672 /* DB: Apply dbMask to maskedDB */
1673 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1674 &md_ctx );
1675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001677
1678 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001679 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001680 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001681 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001682 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001684 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001685
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001686 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001687
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001688 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001689 for( i = 0; i < hlen; i++ )
1690 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001691
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001692 /* Get zero-padding len, but always read till end of buffer
1693 * (minus one, for the 01 byte) */
1694 pad_len = 0;
1695 pad_done = 0;
1696 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1697 {
1698 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001699 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001700 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001701
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001702 p += pad_len;
1703 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001704
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001705 /*
1706 * The only information "leaked" is whether the padding was correct or not
1707 * (eg, no data is copied if it was not correct). This meets the
1708 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1709 * the different error conditions.
1710 */
1711 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001712 {
1713 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1714 goto cleanup;
1715 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001716
Paul Bakker66d5d072014-06-17 16:39:18 +02001717 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001718 {
1719 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1720 goto cleanup;
1721 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001722
1723 *olen = ilen - (p - buf);
1724 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001725 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001726
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001727cleanup:
1728 mbedtls_zeroize( buf, sizeof( buf ) );
1729 mbedtls_zeroize( lhash, sizeof( lhash ) );
1730
1731 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001736/*
1737 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001740 int (*f_rng)(void *, unsigned char *, size_t),
1741 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001742 int mode, size_t *olen,
1743 const unsigned char *input,
1744 unsigned char *output,
1745 size_t output_max_len)
1746{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001747 int ret;
1748 size_t ilen, pad_count = 0, i;
1749 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1753 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001754
1755 ilen = ctx->len;
1756
1757 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1761 ? mbedtls_rsa_public( ctx, input, buf )
1762 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001763
1764 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001765 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001766
1767 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001768 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001769
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001770 /*
1771 * Check and get padding len in "constant-time"
1772 */
1773 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001774
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001775 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001780 /* Get padding len, but always read till end of buffer
1781 * (minus one, for the 00 byte) */
1782 for( i = 0; i < ilen - 3; i++ )
1783 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001784 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1785 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001786 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001787
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001788 p += pad_count;
1789 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001790 }
1791 else
1792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001794
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001795 /* Get padding len, but always read till end of buffer
1796 * (minus one, for the 00 byte) */
1797 for( i = 0; i < ilen - 3; i++ )
1798 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001799 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001800 pad_count += ( pad_done == 0 );
1801 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001802
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001803 p += pad_count;
1804 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 }
1806
Janos Follathc69fa502016-02-12 13:30:09 +00001807 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001808
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001809 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001810 {
1811 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1812 goto cleanup;
1813 }
Paul Bakker8804f692013-02-28 18:06:26 +01001814
Paul Bakker66d5d072014-06-17 16:39:18 +02001815 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001816 {
1817 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1818 goto cleanup;
1819 }
Paul Bakker060c5682009-01-12 21:48:39 +00001820
Paul Bakker27fdf462011-06-09 13:55:13 +00001821 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001822 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001823 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001824
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001825cleanup:
1826 mbedtls_zeroize( buf, sizeof( buf ) );
1827
1828 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001831
1832/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001833 * Do an RSA operation, then remove the message padding
1834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001836 int (*f_rng)(void *, unsigned char *, size_t),
1837 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001838 int mode, size_t *olen,
1839 const unsigned char *input,
1840 unsigned char *output,
1841 size_t output_max_len)
1842{
1843 switch( ctx->padding )
1844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845#if defined(MBEDTLS_PKCS1_V15)
1846 case MBEDTLS_RSA_PKCS_V15:
1847 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001848 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001849#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#if defined(MBEDTLS_PKCS1_V21)
1852 case MBEDTLS_RSA_PKCS_V21:
1853 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001854 olen, input, output,
1855 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001856#endif
1857
1858 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001860 }
1861}
1862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001864/*
1865 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001868 int (*f_rng)(void *, unsigned char *, size_t),
1869 void *p_rng,
1870 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001872 unsigned int hashlen,
1873 const unsigned char *hash,
1874 unsigned char *sig )
1875{
1876 size_t olen;
1877 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001879 unsigned int slen, hlen, offset = 0;
1880 int ret;
1881 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 const mbedtls_md_info_t *md_info;
1883 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1886 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001887
1888 if( f_rng == 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
1891 olen = ctx->len;
1892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001894 {
Simon Butcher02037452016-03-01 21:19:12 +00001895 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001897 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001901 }
1902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001904 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001908 slen = hlen;
1909
1910 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001912
1913 memset( sig, 0, olen );
1914
Simon Butcher02037452016-03-01 21:19:12 +00001915 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001916 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001918
Simon Butcher02037452016-03-01 21:19:12 +00001919 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001920 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001921 p += olen - hlen * 2 - 2;
1922 *p++ = 0x01;
1923 memcpy( p, salt, slen );
1924 p += slen;
1925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001927 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1928 {
1929 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001930 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001931 return( ret );
1932 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001933
Simon Butcher02037452016-03-01 21:19:12 +00001934 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 mbedtls_md_starts( &md_ctx );
1936 mbedtls_md_update( &md_ctx, p, 8 );
1937 mbedtls_md_update( &md_ctx, hash, hashlen );
1938 mbedtls_md_update( &md_ctx, salt, slen );
1939 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001940 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001941
Simon Butcher02037452016-03-01 21:19:12 +00001942 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001943 if( msb % 8 == 0 )
1944 offset = 1;
1945
Simon Butcher02037452016-03-01 21:19:12 +00001946 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001947 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001950
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001951 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001952 sig[0] &= 0xFF >> ( olen * 8 - msb );
1953
1954 p += hlen;
1955 *p++ = 0xBC;
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 return( ( mode == MBEDTLS_RSA_PUBLIC )
1958 ? mbedtls_rsa_public( ctx, sig, sig )
1959 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001960}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001964/*
1965 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1966 */
1967/*
1968 * Do an RSA operation to sign the message digest
1969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001971 int (*f_rng)(void *, unsigned char *, size_t),
1972 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001973 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001975 unsigned int hashlen,
1976 const unsigned char *hash,
1977 unsigned char *sig )
1978{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001979 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001980 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001981 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001982 unsigned char *sig_try = NULL, *verif = NULL;
1983 size_t i;
1984 unsigned char diff;
1985 volatile unsigned char diff_no_optimize;
1986 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1989 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001990
1991 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001992 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001997 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
2001 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002002
Paul Bakkerc70b9822013-04-07 22:00:46 +02002003 nb_pad -= 10 + oid_size;
2004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002006 }
2007
Paul Bakkerc70b9822013-04-07 22:00:46 +02002008 nb_pad -= hashlen;
2009
Paul Bakkerb3869132013-02-28 17:21:01 +01002010 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002012
2013 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01002015 memset( p, 0xFF, nb_pad );
2016 p += nb_pad;
2017 *p++ = 0;
2018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002020 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002021 memcpy( p, hash, hashlen );
2022 }
2023 else
2024 {
2025 /*
2026 * DigestInfo ::= SEQUENCE {
2027 * digestAlgorithm DigestAlgorithmIdentifier,
2028 * digest Digest }
2029 *
2030 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2031 *
2032 * Digest ::= OCTET STRING
2033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002035 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002037 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002039 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002040 memcpy( p, oid, oid_size );
2041 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002043 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002045 *p++ = hashlen;
2046 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002047 }
2048
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002049 if( mode == MBEDTLS_RSA_PUBLIC )
2050 return( mbedtls_rsa_public( ctx, sig, sig ) );
2051
2052 /*
2053 * In order to prevent Lenstra's attack, make the signature in a
2054 * temporary buffer and check it before returning it.
2055 */
2056 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002057 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002058 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2059
Simon Butcher1285ab52016-01-01 21:42:47 +00002060 verif = mbedtls_calloc( 1, ctx->len );
2061 if( verif == NULL )
2062 {
2063 mbedtls_free( sig_try );
2064 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2065 }
2066
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002067 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2068 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2069
2070 /* Compare in constant time just in case */
2071 for( diff = 0, i = 0; i < ctx->len; i++ )
2072 diff |= verif[i] ^ sig[i];
2073 diff_no_optimize = diff;
2074
2075 if( diff_no_optimize != 0 )
2076 {
2077 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2078 goto cleanup;
2079 }
2080
2081 memcpy( sig, sig_try, ctx->len );
2082
2083cleanup:
2084 mbedtls_free( sig_try );
2085 mbedtls_free( verif );
2086
2087 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002088}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002090
2091/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 * Do an RSA operation to sign the message digest
2093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002095 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002096 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002099 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002100 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 unsigned char *sig )
2102{
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 switch( ctx->padding )
2104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105#if defined(MBEDTLS_PKCS1_V15)
2106 case MBEDTLS_RSA_PKCS_V15:
2107 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002108 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002109#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111#if defined(MBEDTLS_PKCS1_V21)
2112 case MBEDTLS_RSA_PKCS_V21:
2113 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002114 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002115#endif
2116
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002120}
2121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002123/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002124 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002127 int (*f_rng)(void *, unsigned char *, size_t),
2128 void *p_rng,
2129 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002131 unsigned int hashlen,
2132 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002134 int expected_salt_len,
2135 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002136{
Paul Bakker23986e52011-04-24 08:57:21 +00002137 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002138 size_t siglen;
2139 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002141 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002142 unsigned int hlen;
2143 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 const mbedtls_md_info_t *md_info;
2145 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002146 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2149 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002150
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 siglen = ctx->len;
2152
Paul Bakker27fdf462011-06-09 13:55:13 +00002153 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2157 ? mbedtls_rsa_public( ctx, sig, buf )
2158 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002159
2160 if( ret != 0 )
2161 return( ret );
2162
2163 p = buf;
2164
Paul Bakkerb3869132013-02-28 17:21:01 +01002165 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 {
Simon Butcher02037452016-03-01 21:19:12 +00002170 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002172 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002176 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002179 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002183 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002184
Paul Bakkerb3869132013-02-28 17:21:01 +01002185 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002186
Simon Butcher02037452016-03-01 21:19:12 +00002187 /*
2188 * Note: EMSA-PSS verification is over the length of N - 1 bits
2189 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002190 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002191
Simon Butcher02037452016-03-01 21:19:12 +00002192 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002193 if( msb % 8 == 0 )
2194 {
2195 p++;
2196 siglen -= 1;
2197 }
2198 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002202 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2203 {
2204 mbedtls_md_free( &md_ctx );
2205 return( ret );
2206 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002207
Paul Bakkerb3869132013-02-28 17:21:01 +01002208 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002209
Paul Bakkerb3869132013-02-28 17:21:01 +01002210 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002211
Paul Bakker4de44aa2013-12-31 11:43:01 +01002212 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002213 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002214
Paul Bakkerb3869132013-02-28 17:21:01 +01002215 if( p == buf + siglen ||
2216 *p++ != 0x01 )
2217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 mbedtls_md_free( &md_ctx );
2219 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002220 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002221
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002222 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002223 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002226 slen != (size_t) expected_salt_len )
2227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 mbedtls_md_free( &md_ctx );
2229 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002230 }
2231
Simon Butcher02037452016-03-01 21:19:12 +00002232 /*
2233 * Generate H = Hash( M' )
2234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 mbedtls_md_starts( &md_ctx );
2236 mbedtls_md_update( &md_ctx, zeros, 8 );
2237 mbedtls_md_update( &md_ctx, hash, hashlen );
2238 mbedtls_md_update( &md_ctx, p, slen );
2239 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002242
Paul Bakkerb3869132013-02-28 17:21:01 +01002243 if( memcmp( p + slen, result, hlen ) == 0 )
2244 return( 0 );
2245 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002247}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002248
2249/*
2250 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002253 int (*f_rng)(void *, unsigned char *, size_t),
2254 void *p_rng,
2255 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002257 unsigned int hashlen,
2258 const unsigned char *hash,
2259 const unsigned char *sig )
2260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2262 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002263 : md_alg;
2264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002266 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002268 sig ) );
2269
2270}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002274/*
2275 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002278 int (*f_rng)(void *, unsigned char *, size_t),
2279 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002280 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002282 unsigned int hashlen,
2283 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002284 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002285{
2286 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002287 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002288 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 mbedtls_md_type_t msg_md_alg;
2290 const mbedtls_md_info_t *md_info;
2291 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002292 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2295 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002296
2297 siglen = ctx->len;
2298
2299 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2303 ? mbedtls_rsa_public( ctx, sig, buf )
2304 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002305
2306 if( ret != 0 )
2307 return( ret );
2308
2309 p = buf;
2310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2312 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002313
2314 while( *p != 0 )
2315 {
2316 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002318 p++;
2319 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002320 p++; /* skip 00 byte */
2321
2322 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2323 if( p - buf < 11 )
2324 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002325
2326 len = siglen - ( p - buf );
2327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002329 {
2330 if( memcmp( p, hash, hashlen ) == 0 )
2331 return( 0 );
2332 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 }
2335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002337 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2339 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002340
2341 end = p + len;
2342
Simon Butcher02037452016-03-01 21:19:12 +00002343 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002344 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2345 * Insist on 2-byte length tags, to protect against variants of
2346 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002347 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002348 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2350 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2351 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002352 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002354
Gilles Peskinee7e76502017-05-04 12:48:39 +02002355 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2357 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2358 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002359 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002361
Gilles Peskinee7e76502017-05-04 12:48:39 +02002362 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2364 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002365 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002366 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002367
2368 oid.p = p;
2369 p += oid.len;
2370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2372 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002373
2374 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002376
2377 /*
2378 * assume the algorithm parameters must be NULL
2379 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002380 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2382 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002383 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002385
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002386 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002387 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2388 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002389 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002391
2392 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002394
2395 p += hashlen;
2396
2397 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002399
2400 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
2404/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002405 * Do an RSA operation and check the message digest
2406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002408 int (*f_rng)(void *, unsigned char *, size_t),
2409 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002410 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002412 unsigned int hashlen,
2413 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002414 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002415{
2416 switch( ctx->padding )
2417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418#if defined(MBEDTLS_PKCS1_V15)
2419 case MBEDTLS_RSA_PKCS_V15:
2420 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002421 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002422#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424#if defined(MBEDTLS_PKCS1_V21)
2425 case MBEDTLS_RSA_PKCS_V21:
2426 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002427 hashlen, hash, sig );
2428#endif
2429
2430 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002432 }
2433}
2434
2435/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002436 * Copy the components of an RSA key
2437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002439{
2440 int ret;
2441
2442 dst->ver = src->ver;
2443 dst->len = src->len;
2444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2446 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2449 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2450 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002451
2452#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2454 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2455 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2457 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002458#endif
2459
2460 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2463 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002464
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002465 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002466 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002467
2468cleanup:
2469 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002471
2472 return( ret );
2473}
2474
2475/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 * Free the components of an RSA key
2477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002479{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002481 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2482 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002484
Hanno Becker33c30a02017-08-23 07:00:22 +01002485#if !defined(MBEDTLS_RSA_NO_CRT)
2486 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2487 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2488 mbedtls_mpi_free( &ctx->DP );
2489#endif /* MBEDTLS_RSA_NO_CRT */
2490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#if defined(MBEDTLS_THREADING_C)
2492 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002493#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002494}
2495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002498#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
2500/*
2501 * Example RSA-1024 keypair, for test purposes
2502 */
2503#define KEY_LEN 128
2504
2505#define RSA_N "9292758453063D803DD603D5E777D788" \
2506 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2507 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2508 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2509 "93A89813FBF3C4F8066D2D800F7C38A8" \
2510 "1AE31942917403FF4946B0A83D3D3E05" \
2511 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2512 "5E94BB77B07507233A0BC7BAC8F90F79"
2513
2514#define RSA_E "10001"
2515
2516#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2517 "66CA472BC44D253102F8B4A9D3BFA750" \
2518 "91386C0077937FE33FA3252D28855837" \
2519 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2520 "DF79C5CE07EE72C7F123142198164234" \
2521 "CABB724CF78B8173B9F880FC86322407" \
2522 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2523 "071513A1E85B5DFA031F21ECAE91A34D"
2524
2525#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2526 "2C01CAD19EA484A87EA4377637E75500" \
2527 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2528 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2529
2530#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2531 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2532 "910E4168387E3C30AA1E00C339A79508" \
2533 "8452DD96A9A5EA5D9DCA68DA636032AF"
2534
2535#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2536 "3C94D22288ACD763FD8E5600ED4A702D" \
2537 "F84198A5F06C2E72236AE490C93F07F8" \
2538 "3CC559CD27BC2D1CA488811730BB5725"
2539
2540#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2541 "D8AAEA56749EA28623272E4F7D0592AF" \
2542 "7C1F1313CAC9471B5C523BFE592F517B" \
2543 "407A1BD76C164B93DA2D32A383E58357"
2544
2545#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2546 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2547 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2548 "A74206CEC169D74BF5A8C50D6F48EA08"
2549
2550#define PT_LEN 24
2551#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2552 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002555static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002556{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002557#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002558 size_t i;
2559
Paul Bakker545570e2010-07-18 09:00:25 +00002560 if( rng_state != NULL )
2561 rng_state = NULL;
2562
Paul Bakkera3d195c2011-11-27 21:07:34 +00002563 for( i = 0; i < len; ++i )
2564 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002565#else
2566 if( rng_state != NULL )
2567 rng_state = NULL;
2568
2569 arc4random_buf( output, len );
2570#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002571
Paul Bakkera3d195c2011-11-27 21:07:34 +00002572 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002575
Paul Bakker5121ce52009-01-03 21:22:43 +00002576/*
2577 * Checkup routine
2578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002580{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002581 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002583 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 unsigned char rsa_plaintext[PT_LEN];
2586 unsigned char rsa_decrypted[PT_LEN];
2587 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002589 unsigned char sha1sum[20];
2590#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Hanno Becker3a701162017-08-22 13:52:43 +01002592 mbedtls_mpi K;
2593
2594 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
Hanno Becker3a701162017-08-22 13:52:43 +01002597 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2598 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2599 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2600 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2601 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2602 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2603 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2604 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2605 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2606 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2607
2608 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
2610 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2614 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 {
2616 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
2619 return( 1 );
2620 }
2621
Hanno Becker3a701162017-08-22 13:52:43 +01002622 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2623 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2624
2625 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2626 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2627
2628 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2629 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2630
Paul Bakker5121ce52009-01-03 21:22:43 +00002631 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
2634 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 rsa_plaintext, rsa_ciphertext ) != 0 )
2638 {
2639 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
2642 return( 1 );
2643 }
2644
2645 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002649 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002650 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 {
2652 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
2655 return( 1 );
2656 }
2657
2658 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2659 {
2660 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
2663 return( 1 );
2664 }
2665
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002666 if( verbose != 0 )
2667 mbedtls_printf( "passed\n" );
2668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002671 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 sha1sum, rsa_ciphertext ) != 0 )
2677 {
2678 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
2681 return( 1 );
2682 }
2683
2684 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 sha1sum, rsa_ciphertext ) != 0 )
2689 {
2690 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
2693 return( 1 );
2694 }
2695
2696 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002697 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002700 if( verbose != 0 )
2701 mbedtls_printf( "\n" );
2702
Paul Bakker3d8fb632014-04-17 12:42:41 +02002703cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002704 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 mbedtls_rsa_free( &rsa );
2706#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002707 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002709 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710}
2711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#endif /* MBEDTLS_RSA_C */