blob: 78814bbdc65608b67006b3a6698d32024cf391f8 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
1029 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
1030 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
1031 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
1034 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
1035 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1036 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1037 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
1038 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
1041 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
1042 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +00001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
1045 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
1046 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +00001047 /*
1048 * Check for a valid PKCS1v2 private key
1049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
1051 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
1052 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
1053 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
1054 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
1055 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
1056 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 }
Paul Bakker48377d92013-08-30 12:06:24 +02001060
Paul Bakker5121ce52009-01-03 21:22:43 +00001061cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
1063 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
1064 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
1065 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +00001068 return( ret );
1069
Paul Bakker6c591fa2011-05-05 11:49:20 +00001070 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001072
1073 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074}
1075
1076/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001077 * Check if contexts holding a public and private key match
1078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1082 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001085 }
1086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1088 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001091 }
1092
1093 return( 0 );
1094}
1095
1096/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001097 * Do an RSA public key operation
1098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001100 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 unsigned char *output )
1102{
Paul Bakker23986e52011-04-24 08:57:21 +00001103 int ret;
1104 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001109#if defined(MBEDTLS_THREADING_C)
1110 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1111 return( ret );
1112#endif
1113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001118 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1119 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 }
1121
1122 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1124 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
1126cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001128 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1129 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001130#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
1134 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
1137 return( 0 );
1138}
1139
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001140/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001141 * Generate or update blinding values, see section 10 of:
1142 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001143 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001144 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001145 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001146static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001147 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1148{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001149 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001150
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001151 if( ctx->Vf.p != NULL )
1152 {
1153 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1155 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1156 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1157 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001158
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001159 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001160 }
1161
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001162 /* Unblinding value: Vf = random number, invertible mod N */
1163 do {
1164 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1168 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1169 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001170
1171 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1173 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 +02001174
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001175
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001176cleanup:
1177 return( ret );
1178}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001179
Paul Bakker5121ce52009-01-03 21:22:43 +00001180/*
Janos Follathe81102e2017-03-22 13:38:28 +00001181 * Exponent blinding supposed to prevent side-channel attacks using multiple
1182 * traces of measurements to recover the RSA key. The more collisions are there,
1183 * the more bits of the key can be recovered. See [3].
1184 *
1185 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1186 * observations on avarage.
1187 *
1188 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1189 * to make 2^112 observations on avarage.
1190 *
1191 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1192 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1193 * Thus in this sense with 28 byte blinding the security is not reduced by
1194 * side-channel attacks like the one in [3])
1195 *
1196 * This countermeasure does not help if the key recovery is possible with a
1197 * single trace.
1198 */
1199#define RSA_EXPONENT_BLINDING 28
1200
1201/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001202 * Do an RSA private key operation
1203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001205 int (*f_rng)(void *, unsigned char *, size_t),
1206 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001207 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 unsigned char *output )
1209{
Paul Bakker23986e52011-04-24 08:57:21 +00001210 int ret;
1211 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001213 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001214#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001215 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001216 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001217#else
1218 mbedtls_mpi DP_blind, DQ_blind;
1219 mbedtls_mpi *DP = &ctx->DP;
1220 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001221#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001222
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001223 /* Make sure we have private key info, prevent possible misuse */
1224 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
1225 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001228 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1229
1230
1231 if( f_rng != NULL )
1232 {
Janos Follathe81102e2017-03-22 13:38:28 +00001233#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001234 mbedtls_mpi_init( &D_blind );
1235#else
1236 mbedtls_mpi_init( &DP_blind );
1237 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001238#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001239 }
Janos Follathe81102e2017-03-22 13:38:28 +00001240
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001242#if defined(MBEDTLS_THREADING_C)
1243 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1244 return( ret );
1245#endif
1246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1248 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001250 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1251 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 }
1253
Paul Bakkerf451bac2013-08-30 15:37:02 +02001254 if( f_rng != NULL )
1255 {
1256 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001257 * Blinding
1258 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001259 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001260 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1261 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001263
Janos Follathe81102e2017-03-22 13:38:28 +00001264 /*
1265 * Exponent blinding
1266 */
1267 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1268 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1269
Janos Follathf9203b42017-03-22 15:13:15 +00001270#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001271 /*
1272 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1273 */
1274 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1275 f_rng, p_rng ) );
1276 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1277 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1278 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1279
1280 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001281#else
1282 /*
1283 * DP_blind = ( P - 1 ) * R + DP
1284 */
1285 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1286 f_rng, p_rng ) );
1287 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1288 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1289 &ctx->DP ) );
1290
1291 DP = &DP_blind;
1292
1293 /*
1294 * DQ_blind = ( Q - 1 ) * R + DQ
1295 */
1296 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1297 f_rng, p_rng ) );
1298 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1299 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1300 &ctx->DQ ) );
1301
1302 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001303#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001304 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001307 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001308#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001309 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001310 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 *
1312 * T1 = input ^ dP mod P
1313 * T2 = input ^ dQ mod Q
1314 */
Janos Follathf9203b42017-03-22 15:13:15 +00001315 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1316 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
1318 /*
1319 * T = (T1 - T2) * (Q^-1 mod P) mod P
1320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1322 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1323 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
1325 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001326 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1329 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1330#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001331
Paul Bakkerf451bac2013-08-30 15:37:02 +02001332 if( f_rng != NULL )
1333 {
1334 /*
1335 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001336 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001337 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001338 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001340 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
1342 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
1345cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001347 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1348 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001349#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001352 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1353
1354 if( f_rng != NULL )
1355 {
Janos Follathe81102e2017-03-22 13:38:28 +00001356#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001357 mbedtls_mpi_free( &D_blind );
1358#else
1359 mbedtls_mpi_free( &DP_blind );
1360 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001361#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001362 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
1364 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
1367 return( 0 );
1368}
1369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001371/**
1372 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1373 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001374 * \param dst buffer to mask
1375 * \param dlen length of destination buffer
1376 * \param src source of the mask generation
1377 * \param slen length of the source buffer
1378 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001379 */
Paul Bakker48377d92013-08-30 12:06:24 +02001380static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001382{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001384 unsigned char counter[4];
1385 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001386 unsigned int hlen;
1387 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001390 memset( counter, 0, 4 );
1391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001393
Simon Butcher02037452016-03-01 21:19:12 +00001394 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001395 p = dst;
1396
1397 while( dlen > 0 )
1398 {
1399 use_len = hlen;
1400 if( dlen < hlen )
1401 use_len = dlen;
1402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 mbedtls_md_starts( md_ctx );
1404 mbedtls_md_update( md_ctx, src, slen );
1405 mbedtls_md_update( md_ctx, counter, 4 );
1406 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001407
1408 for( i = 0; i < use_len; ++i )
1409 *p++ ^= mask[i];
1410
1411 counter[3]++;
1412
1413 dlen -= use_len;
1414 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001415
1416 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001417}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001421/*
1422 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001425 int (*f_rng)(void *, unsigned char *, size_t),
1426 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001427 int mode,
1428 const unsigned char *label, size_t label_len,
1429 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001430 const unsigned char *input,
1431 unsigned char *output )
1432{
1433 size_t olen;
1434 int ret;
1435 unsigned char *p = output;
1436 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 const mbedtls_md_info_t *md_info;
1438 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1441 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001442
1443 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001447 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001449
1450 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001452
Simon Butcher02037452016-03-01 21:19:12 +00001453 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001454 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
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 memset( output, 0, olen );
1458
1459 *p++ = 0;
1460
Simon Butcher02037452016-03-01 21:19:12 +00001461 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001462 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001464
1465 p += hlen;
1466
Simon Butcher02037452016-03-01 21:19:12 +00001467 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001469 p += hlen;
1470 p += olen - 2 * hlen - 2 - ilen;
1471 *p++ = 1;
1472 memcpy( p, input, ilen );
1473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001475 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1476 {
1477 mbedtls_md_free( &md_ctx );
1478 return( ret );
1479 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001480
Simon Butcher02037452016-03-01 21:19:12 +00001481 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001482 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1483 &md_ctx );
1484
Simon Butcher02037452016-03-01 21:19:12 +00001485 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001486 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1487 &md_ctx );
1488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 return( ( mode == MBEDTLS_RSA_PUBLIC )
1492 ? mbedtls_rsa_public( ctx, output, output )
1493 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001494}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001498/*
1499 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001502 int (*f_rng)(void *, unsigned char *, size_t),
1503 void *p_rng,
1504 int mode, size_t ilen,
1505 const unsigned char *input,
1506 unsigned char *output )
1507{
1508 size_t nb_pad, olen;
1509 int ret;
1510 unsigned char *p = output;
1511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1513 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001514
Janos Follath1ed9f992016-03-18 11:45:44 +00001515 // We don't check p_rng because it won't be dereferenced here
1516 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001518
1519 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001520
Simon Butcher02037452016-03-01 21:19:12 +00001521 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001522 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001524
1525 nb_pad = olen - 3 - ilen;
1526
1527 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001531
1532 while( nb_pad-- > 0 )
1533 {
1534 int rng_dl = 100;
1535
1536 do {
1537 ret = f_rng( p_rng, p, 1 );
1538 } while( *p == 0 && --rng_dl && ret == 0 );
1539
Simon Butcher02037452016-03-01 21:19:12 +00001540 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001541 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001543
1544 p++;
1545 }
1546 }
1547 else
1548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001550
1551 while( nb_pad-- > 0 )
1552 *p++ = 0xFF;
1553 }
1554
1555 *p++ = 0;
1556 memcpy( p, input, ilen );
1557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 return( ( mode == MBEDTLS_RSA_PUBLIC )
1559 ? mbedtls_rsa_public( ctx, output, output )
1560 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001563
Paul Bakker5121ce52009-01-03 21:22:43 +00001564/*
1565 * Add the message padding, then do an RSA operation
1566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001568 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001569 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001570 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001571 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001572 unsigned char *output )
1573{
Paul Bakker5121ce52009-01-03 21:22:43 +00001574 switch( ctx->padding )
1575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#if defined(MBEDTLS_PKCS1_V15)
1577 case MBEDTLS_RSA_PKCS_V15:
1578 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001579 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001580#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_PKCS1_V21)
1583 case MBEDTLS_RSA_PKCS_V21:
1584 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001585 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001586#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001587
1588 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001591}
1592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001594/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001595 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001598 int (*f_rng)(void *, unsigned char *, size_t),
1599 void *p_rng,
1600 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001601 const unsigned char *label, size_t label_len,
1602 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001603 const unsigned char *input,
1604 unsigned char *output,
1605 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001606{
Paul Bakker23986e52011-04-24 08:57:21 +00001607 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001608 size_t ilen, i, pad_len;
1609 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1611 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001612 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 const mbedtls_md_info_t *md_info;
1614 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001615
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001616 /*
1617 * Parameters sanity checks
1618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1620 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001621
1622 ilen = ctx->len;
1623
Paul Bakker27fdf462011-06-09 13:55:13 +00001624 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001628 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001630
Janos Follathc17cda12016-02-11 11:08:18 +00001631 hlen = mbedtls_md_get_size( md_info );
1632
1633 // checking for integer underflow
1634 if( 2 * hlen + 2 > ilen )
1635 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1636
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001637 /*
1638 * RSA operation
1639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1641 ? mbedtls_rsa_public( ctx, input, buf )
1642 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001643
1644 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001645 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001646
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001647 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001648 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001651 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1652 {
1653 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001654 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001655 }
1656
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001657
1658 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001660
1661 /* seed: Apply seedMask to maskedSeed */
1662 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1663 &md_ctx );
1664
1665 /* DB: Apply dbMask to maskedDB */
1666 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1667 &md_ctx );
1668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001670
1671 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001672 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001673 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001674 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001675 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001677 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001678
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001679 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001680
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001681 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001682 for( i = 0; i < hlen; i++ )
1683 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001684
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001685 /* Get zero-padding len, but always read till end of buffer
1686 * (minus one, for the 01 byte) */
1687 pad_len = 0;
1688 pad_done = 0;
1689 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1690 {
1691 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001692 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001693 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001694
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001695 p += pad_len;
1696 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001697
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001698 /*
1699 * The only information "leaked" is whether the padding was correct or not
1700 * (eg, no data is copied if it was not correct). This meets the
1701 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1702 * the different error conditions.
1703 */
1704 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001705 {
1706 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1707 goto cleanup;
1708 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001709
Paul Bakker66d5d072014-06-17 16:39:18 +02001710 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001711 {
1712 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1713 goto cleanup;
1714 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001715
1716 *olen = ilen - (p - buf);
1717 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001718 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001719
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001720cleanup:
1721 mbedtls_zeroize( buf, sizeof( buf ) );
1722 mbedtls_zeroize( lhash, sizeof( lhash ) );
1723
1724 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001729/*
1730 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001733 int (*f_rng)(void *, unsigned char *, size_t),
1734 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001735 int mode, size_t *olen,
1736 const unsigned char *input,
1737 unsigned char *output,
1738 size_t output_max_len)
1739{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001740 int ret;
1741 size_t ilen, pad_count = 0, i;
1742 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1746 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001747
1748 ilen = ctx->len;
1749
1750 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1754 ? mbedtls_rsa_public( ctx, input, buf )
1755 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001756
1757 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001758 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001759
1760 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001761 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001762
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001763 /*
1764 * Check and get padding len in "constant-time"
1765 */
1766 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001767
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001768 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001773 /* Get padding len, but always read till end of buffer
1774 * (minus one, for the 00 byte) */
1775 for( i = 0; i < ilen - 3; i++ )
1776 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001777 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1778 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001779 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001780
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001781 p += pad_count;
1782 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001783 }
1784 else
1785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001787
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001788 /* Get padding len, but always read till end of buffer
1789 * (minus one, for the 00 byte) */
1790 for( i = 0; i < ilen - 3; i++ )
1791 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001792 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001793 pad_count += ( pad_done == 0 );
1794 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001795
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001796 p += pad_count;
1797 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001798 }
1799
Janos Follathc69fa502016-02-12 13:30:09 +00001800 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001801
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001802 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001803 {
1804 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1805 goto cleanup;
1806 }
Paul Bakker8804f692013-02-28 18:06:26 +01001807
Paul Bakker66d5d072014-06-17 16:39:18 +02001808 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001809 {
1810 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1811 goto cleanup;
1812 }
Paul Bakker060c5682009-01-12 21:48:39 +00001813
Paul Bakker27fdf462011-06-09 13:55:13 +00001814 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001815 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001816 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001817
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001818cleanup:
1819 mbedtls_zeroize( buf, sizeof( buf ) );
1820
1821 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001824
1825/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001826 * Do an RSA operation, then remove the message padding
1827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001829 int (*f_rng)(void *, unsigned char *, size_t),
1830 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001831 int mode, size_t *olen,
1832 const unsigned char *input,
1833 unsigned char *output,
1834 size_t output_max_len)
1835{
1836 switch( ctx->padding )
1837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838#if defined(MBEDTLS_PKCS1_V15)
1839 case MBEDTLS_RSA_PKCS_V15:
1840 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001841 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001842#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844#if defined(MBEDTLS_PKCS1_V21)
1845 case MBEDTLS_RSA_PKCS_V21:
1846 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001847 olen, input, output,
1848 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001849#endif
1850
1851 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001853 }
1854}
1855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001857/*
1858 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001861 int (*f_rng)(void *, unsigned char *, size_t),
1862 void *p_rng,
1863 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001865 unsigned int hashlen,
1866 const unsigned char *hash,
1867 unsigned char *sig )
1868{
1869 size_t olen;
1870 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001872 unsigned int slen, hlen, offset = 0;
1873 int ret;
1874 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 const mbedtls_md_info_t *md_info;
1876 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1879 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001880
1881 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001883
1884 olen = ctx->len;
1885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001887 {
Simon Butcher02037452016-03-01 21:19:12 +00001888 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001890 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001894 }
1895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001897 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001901 slen = hlen;
1902
1903 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001905
1906 memset( sig, 0, olen );
1907
Simon Butcher02037452016-03-01 21:19:12 +00001908 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001909 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001911
Simon Butcher02037452016-03-01 21:19:12 +00001912 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001913 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001914 p += olen - hlen * 2 - 2;
1915 *p++ = 0x01;
1916 memcpy( p, salt, slen );
1917 p += slen;
1918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001920 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1921 {
1922 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001923 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001924 return( ret );
1925 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001926
Simon Butcher02037452016-03-01 21:19:12 +00001927 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 mbedtls_md_starts( &md_ctx );
1929 mbedtls_md_update( &md_ctx, p, 8 );
1930 mbedtls_md_update( &md_ctx, hash, hashlen );
1931 mbedtls_md_update( &md_ctx, salt, slen );
1932 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001933 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001934
Simon Butcher02037452016-03-01 21:19:12 +00001935 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001936 if( msb % 8 == 0 )
1937 offset = 1;
1938
Simon Butcher02037452016-03-01 21:19:12 +00001939 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001940 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001943
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001944 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001945 sig[0] &= 0xFF >> ( olen * 8 - msb );
1946
1947 p += hlen;
1948 *p++ = 0xBC;
1949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 return( ( mode == MBEDTLS_RSA_PUBLIC )
1951 ? mbedtls_rsa_public( ctx, sig, sig )
1952 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001953}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001957/*
1958 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1959 */
1960/*
1961 * Do an RSA operation to sign the message digest
1962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001964 int (*f_rng)(void *, unsigned char *, size_t),
1965 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001966 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001968 unsigned int hashlen,
1969 const unsigned char *hash,
1970 unsigned char *sig )
1971{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001972 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001973 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001974 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001975 unsigned char *sig_try = NULL, *verif = NULL;
1976 size_t i;
1977 unsigned char diff;
1978 volatile unsigned char diff_no_optimize;
1979 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1982 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001983
1984 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001985 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001990 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1994 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001995
Paul Bakkerc70b9822013-04-07 22:00:46 +02001996 nb_pad -= 10 + oid_size;
1997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001999 }
2000
Paul Bakkerc70b9822013-04-07 22:00:46 +02002001 nb_pad -= hashlen;
2002
Paul Bakkerb3869132013-02-28 17:21:01 +01002003 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002005
2006 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01002008 memset( p, 0xFF, nb_pad );
2009 p += nb_pad;
2010 *p++ = 0;
2011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002013 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002014 memcpy( p, hash, hashlen );
2015 }
2016 else
2017 {
2018 /*
2019 * DigestInfo ::= SEQUENCE {
2020 * digestAlgorithm DigestAlgorithmIdentifier,
2021 * digest Digest }
2022 *
2023 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2024 *
2025 * Digest ::= OCTET STRING
2026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002028 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002030 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002032 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002033 memcpy( p, oid, oid_size );
2034 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002036 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002038 *p++ = hashlen;
2039 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002040 }
2041
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002042 if( mode == MBEDTLS_RSA_PUBLIC )
2043 return( mbedtls_rsa_public( ctx, sig, sig ) );
2044
2045 /*
2046 * In order to prevent Lenstra's attack, make the signature in a
2047 * temporary buffer and check it before returning it.
2048 */
2049 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002050 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002051 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2052
Simon Butcher1285ab52016-01-01 21:42:47 +00002053 verif = mbedtls_calloc( 1, ctx->len );
2054 if( verif == NULL )
2055 {
2056 mbedtls_free( sig_try );
2057 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2058 }
2059
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002060 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2061 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2062
2063 /* Compare in constant time just in case */
2064 for( diff = 0, i = 0; i < ctx->len; i++ )
2065 diff |= verif[i] ^ sig[i];
2066 diff_no_optimize = diff;
2067
2068 if( diff_no_optimize != 0 )
2069 {
2070 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2071 goto cleanup;
2072 }
2073
2074 memcpy( sig, sig_try, ctx->len );
2075
2076cleanup:
2077 mbedtls_free( sig_try );
2078 mbedtls_free( verif );
2079
2080 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002083
2084/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 * Do an RSA operation to sign the message digest
2086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002088 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002089 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002092 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002093 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 unsigned char *sig )
2095{
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 switch( ctx->padding )
2097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#if defined(MBEDTLS_PKCS1_V15)
2099 case MBEDTLS_RSA_PKCS_V15:
2100 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002101 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002102#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104#if defined(MBEDTLS_PKCS1_V21)
2105 case MBEDTLS_RSA_PKCS_V21:
2106 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002107 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002108#endif
2109
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002113}
2114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002116/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002117 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002120 int (*f_rng)(void *, unsigned char *, size_t),
2121 void *p_rng,
2122 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002124 unsigned int hashlen,
2125 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002127 int expected_salt_len,
2128 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002129{
Paul Bakker23986e52011-04-24 08:57:21 +00002130 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002131 size_t siglen;
2132 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002134 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002135 unsigned int hlen;
2136 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 const mbedtls_md_info_t *md_info;
2138 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002139 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2142 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002143
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 siglen = ctx->len;
2145
Paul Bakker27fdf462011-06-09 13:55:13 +00002146 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2150 ? mbedtls_rsa_public( ctx, sig, buf )
2151 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
2153 if( ret != 0 )
2154 return( ret );
2155
2156 p = buf;
2157
Paul Bakkerb3869132013-02-28 17:21:01 +01002158 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 {
Simon Butcher02037452016-03-01 21:19:12 +00002163 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002165 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002169 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002172 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002176 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002177
Paul Bakkerb3869132013-02-28 17:21:01 +01002178 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002179
Simon Butcher02037452016-03-01 21:19:12 +00002180 /*
2181 * Note: EMSA-PSS verification is over the length of N - 1 bits
2182 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002183 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002184
Simon Butcher02037452016-03-01 21:19:12 +00002185 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002186 if( msb % 8 == 0 )
2187 {
2188 p++;
2189 siglen -= 1;
2190 }
2191 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002195 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2196 {
2197 mbedtls_md_free( &md_ctx );
2198 return( ret );
2199 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002200
Paul Bakkerb3869132013-02-28 17:21:01 +01002201 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002202
Paul Bakkerb3869132013-02-28 17:21:01 +01002203 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002204
Paul Bakker4de44aa2013-12-31 11:43:01 +01002205 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002206 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002207
Paul Bakkerb3869132013-02-28 17:21:01 +01002208 if( p == buf + siglen ||
2209 *p++ != 0x01 )
2210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 mbedtls_md_free( &md_ctx );
2212 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002213 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002214
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002215 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002216 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002219 slen != (size_t) expected_salt_len )
2220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 mbedtls_md_free( &md_ctx );
2222 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002223 }
2224
Simon Butcher02037452016-03-01 21:19:12 +00002225 /*
2226 * Generate H = Hash( M' )
2227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 mbedtls_md_starts( &md_ctx );
2229 mbedtls_md_update( &md_ctx, zeros, 8 );
2230 mbedtls_md_update( &md_ctx, hash, hashlen );
2231 mbedtls_md_update( &md_ctx, p, slen );
2232 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002235
Paul Bakkerb3869132013-02-28 17:21:01 +01002236 if( memcmp( p + slen, result, hlen ) == 0 )
2237 return( 0 );
2238 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002240}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002241
2242/*
2243 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002246 int (*f_rng)(void *, unsigned char *, size_t),
2247 void *p_rng,
2248 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002250 unsigned int hashlen,
2251 const unsigned char *hash,
2252 const unsigned char *sig )
2253{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2255 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002256 : md_alg;
2257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002259 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002261 sig ) );
2262
2263}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002267/*
2268 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002271 int (*f_rng)(void *, unsigned char *, size_t),
2272 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002273 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002275 unsigned int hashlen,
2276 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002277 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002278{
2279 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002280 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002281 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 mbedtls_md_type_t msg_md_alg;
2283 const mbedtls_md_info_t *md_info;
2284 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002285 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2288 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002289
2290 siglen = ctx->len;
2291
2292 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2296 ? mbedtls_rsa_public( ctx, sig, buf )
2297 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002298
2299 if( ret != 0 )
2300 return( ret );
2301
2302 p = buf;
2303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2305 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002306
2307 while( *p != 0 )
2308 {
2309 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002311 p++;
2312 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002313 p++; /* skip 00 byte */
2314
2315 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2316 if( p - buf < 11 )
2317 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002318
2319 len = siglen - ( p - buf );
2320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002322 {
2323 if( memcmp( p, hash, hashlen ) == 0 )
2324 return( 0 );
2325 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 }
2328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002330 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2332 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002333
2334 end = p + len;
2335
Simon Butcher02037452016-03-01 21:19:12 +00002336 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002337 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2338 * Insist on 2-byte length tags, to protect against variants of
2339 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002340 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002341 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2343 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2344 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002345 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002347
Gilles Peskinee7e76502017-05-04 12:48:39 +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 Peskinee7e76502017-05-04 12:48:39 +02002352 if( p != p0 + 2 || asn1_len + 6 + hashlen != 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, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2357 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002358 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002359 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002360
2361 oid.p = p;
2362 p += oid.len;
2363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2365 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002366
2367 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002369
2370 /*
2371 * assume the algorithm parameters must be NULL
2372 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002373 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2375 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002376 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002378
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002379 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002380 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2381 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002382 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002384
2385 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002387
2388 p += hashlen;
2389
2390 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002392
2393 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
2397/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002398 * Do an RSA operation and check the message digest
2399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002401 int (*f_rng)(void *, unsigned char *, size_t),
2402 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002403 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002405 unsigned int hashlen,
2406 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002407 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002408{
2409 switch( ctx->padding )
2410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411#if defined(MBEDTLS_PKCS1_V15)
2412 case MBEDTLS_RSA_PKCS_V15:
2413 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002414 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002415#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#if defined(MBEDTLS_PKCS1_V21)
2418 case MBEDTLS_RSA_PKCS_V21:
2419 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002420 hashlen, hash, sig );
2421#endif
2422
2423 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002425 }
2426}
2427
2428/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002429 * Copy the components of an RSA key
2430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002432{
2433 int ret;
2434
2435 dst->ver = src->ver;
2436 dst->len = src->len;
2437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2439 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2442 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2443 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002444
2445#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2447 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2448 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2450 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002451#endif
2452
2453 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2456 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002457
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002458 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002459 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002460
2461cleanup:
2462 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002464
2465 return( ret );
2466}
2467
2468/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 * Free the components of an RSA key
2470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002472{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002474 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2475 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002477
Hanno Becker33c30a02017-08-23 07:00:22 +01002478#if !defined(MBEDTLS_RSA_NO_CRT)
2479 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2480 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2481 mbedtls_mpi_free( &ctx->DP );
2482#endif /* MBEDTLS_RSA_NO_CRT */
2483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_THREADING_C)
2485 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002486#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002487}
2488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002490
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002491#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
2493/*
2494 * Example RSA-1024 keypair, for test purposes
2495 */
2496#define KEY_LEN 128
2497
2498#define RSA_N "9292758453063D803DD603D5E777D788" \
2499 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2500 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2501 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2502 "93A89813FBF3C4F8066D2D800F7C38A8" \
2503 "1AE31942917403FF4946B0A83D3D3E05" \
2504 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2505 "5E94BB77B07507233A0BC7BAC8F90F79"
2506
2507#define RSA_E "10001"
2508
2509#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2510 "66CA472BC44D253102F8B4A9D3BFA750" \
2511 "91386C0077937FE33FA3252D28855837" \
2512 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2513 "DF79C5CE07EE72C7F123142198164234" \
2514 "CABB724CF78B8173B9F880FC86322407" \
2515 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2516 "071513A1E85B5DFA031F21ECAE91A34D"
2517
2518#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2519 "2C01CAD19EA484A87EA4377637E75500" \
2520 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2521 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2522
2523#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2524 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2525 "910E4168387E3C30AA1E00C339A79508" \
2526 "8452DD96A9A5EA5D9DCA68DA636032AF"
2527
2528#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2529 "3C94D22288ACD763FD8E5600ED4A702D" \
2530 "F84198A5F06C2E72236AE490C93F07F8" \
2531 "3CC559CD27BC2D1CA488811730BB5725"
2532
2533#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2534 "D8AAEA56749EA28623272E4F7D0592AF" \
2535 "7C1F1313CAC9471B5C523BFE592F517B" \
2536 "407A1BD76C164B93DA2D32A383E58357"
2537
2538#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2539 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2540 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2541 "A74206CEC169D74BF5A8C50D6F48EA08"
2542
2543#define PT_LEN 24
2544#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2545 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002548static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002549{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002550#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002551 size_t i;
2552
Paul Bakker545570e2010-07-18 09:00:25 +00002553 if( rng_state != NULL )
2554 rng_state = NULL;
2555
Paul Bakkera3d195c2011-11-27 21:07:34 +00002556 for( i = 0; i < len; ++i )
2557 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002558#else
2559 if( rng_state != NULL )
2560 rng_state = NULL;
2561
2562 arc4random_buf( output, len );
2563#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002564
Paul Bakkera3d195c2011-11-27 21:07:34 +00002565 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002568
Paul Bakker5121ce52009-01-03 21:22:43 +00002569/*
2570 * Checkup routine
2571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002573{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002574 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002576 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 unsigned char rsa_plaintext[PT_LEN];
2579 unsigned char rsa_decrypted[PT_LEN];
2580 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002582 unsigned char sha1sum[20];
2583#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Hanno Becker3a701162017-08-22 13:52:43 +01002585 mbedtls_mpi K;
2586
2587 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
Hanno Becker3a701162017-08-22 13:52:43 +01002590 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2591 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2592 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2593 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2594 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2595 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2596 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2597 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2598 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2599 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2600
2601 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
2603 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2607 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 {
2609 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
2612 return( 1 );
2613 }
2614
Hanno Becker3a701162017-08-22 13:52:43 +01002615 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2616 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2617
2618 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2619 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2620
2621 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2622 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2623
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
2627 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 rsa_plaintext, rsa_ciphertext ) != 0 )
2631 {
2632 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
2635 return( 1 );
2636 }
2637
2638 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002642 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002643 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 {
2645 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
2648 return( 1 );
2649 }
2650
2651 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2652 {
2653 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
2656 return( 1 );
2657 }
2658
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002659 if( verbose != 0 )
2660 mbedtls_printf( "passed\n" );
2661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002664 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 sha1sum, rsa_ciphertext ) != 0 )
2670 {
2671 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
2674 return( 1 );
2675 }
2676
2677 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 sha1sum, rsa_ciphertext ) != 0 )
2682 {
2683 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
2686 return( 1 );
2687 }
2688
2689 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002690 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002693 if( verbose != 0 )
2694 mbedtls_printf( "\n" );
2695
Paul Bakker3d8fb632014-04-17 12:42:41 +02002696cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002697 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 mbedtls_rsa_free( &rsa );
2699#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002700 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002702 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703}
2704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707#endif /* MBEDTLS_RSA_C */