blob: cc8f5722b992638b39965fbf76246e1b524f844e [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
615 if( is_priv )
616 {
617 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
618 &ctx->DP, &ctx->DQ, &ctx->QP );
619 if( ret != 0 )
620 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
621 }
622
623 /*
624 * Step 3: Double check
625 */
626
627 if( is_priv )
628 {
629 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
630 return( ret );
631 }
632 else
633 {
634 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
635 return( ret );
636 }
637
638 return( 0 );
639}
640
641/*
642 * Check if CRT parameters match RSA context.
643 * This has to be implemented even if CRT is not used,
644 * in order to be able to validate DER encoded RSA keys,
645 * which always contain CRT parameters.
646 */
647int mbedtls_rsa_check_crt( mbedtls_rsa_context *ctx, mbedtls_mpi *DP,
648 mbedtls_mpi *DQ, mbedtls_mpi *QP )
649{
650 /* Check if key is private or public */
651 const int opt_present =
652 mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 &&
653 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 &&
654 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0;
655
656 if( !opt_present )
657 {
658 /* Checking optional parameters only makes sense for private keys. */
659 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
660 }
661
662 /* Alternative implementations not having DP, DQ, QP as part of
663 * the RSA context structure could perform the following checks instead:
664 * (1) Check that DP - P == 0 mod P - 1
665 * (2) Check that DQ - Q == 0 mod Q - 1
666 * (3) Check that QP * P - 1 == 0 mod P
667 */
668
669 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 }
675
676 return( 0 );
677}
678
679int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
680 unsigned char *N, size_t N_len,
681 unsigned char *P, size_t P_len,
682 unsigned char *Q, size_t Q_len,
683 unsigned char *D, size_t D_len,
684 unsigned char *E, size_t E_len )
685{
686 int ret = 0;
687
688 /* Check if key is private or public */
689 const int is_priv =
690 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
691 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
692 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
693 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
694 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
695
696 if( !is_priv )
697 {
698 /* If we're trying to export private parameters for a public key,
699 * something must be wrong. */
700 if( P != NULL || Q != NULL || D != NULL )
701 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
702
703 }
704
705 if( N != NULL )
706 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
707
708 if( P != NULL )
709 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
710
711 if( Q != NULL )
712 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
713
714 if( D != NULL )
715 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
716
717 if( E != NULL )
718 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100719
720cleanup:
721
722 return( ret );
723}
724
Hanno Becker617c1ae2017-08-23 14:11:24 +0100725int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
726 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
727 mbedtls_mpi *D, mbedtls_mpi *E )
728{
729 int ret;
730
731 /* Check if key is private or public */
732 int is_priv =
733 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
734 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
735 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
736 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
737 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
738
739 if( !is_priv )
740 {
741 /* If we're trying to export private parameters for a public key,
742 * something must be wrong. */
743 if( P != NULL || Q != NULL || D != NULL )
744 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
745
746 }
747
748 /* Export all requested core parameters. */
749
750 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
751 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
752 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
753 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
754 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
755 {
756 return( ret );
757 }
758
759 return( 0 );
760}
761
762/*
763 * Export CRT parameters
764 * This must also be implemented if CRT is not used, for being able to
765 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
766 * can be used in this case.
767 */
768int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
769 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
770{
771 int ret;
772
773 /* Check if key is private or public */
774 int is_priv =
775 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
776 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
777 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
778 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
779 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
780
781 if( !is_priv )
782 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
783
784 /* Export all requested blinding parameters. */
785
786 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
787 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
788 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
789 {
790 return( ret );
791 }
792
793 return( 0 );
794}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100795
796/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000797 * Initialize an RSA context
798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000800 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000801 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000802{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_THREADING_C)
808 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200809#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000810}
811
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100812/*
813 * Set padding for an existing RSA context
814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100816{
817 ctx->padding = padding;
818 ctx->hash_id = hash_id;
819}
820
Hanno Becker617c1ae2017-08-23 14:11:24 +0100821/*
822 * Get length in bytes of RSA modulus
823 */
824
825size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
826{
827 return( mbedtls_mpi_size( &ctx->N ) );
828}
829
830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000832
833/*
834 * Generate an RSA keypair
835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000837 int (*f_rng)(void *, unsigned char *, size_t),
838 void *p_rng,
839 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000840{
841 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Paul Bakker21eb2802010-08-16 11:10:02 +0000844 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Janos Follathef441782016-09-21 13:18:12 +0100847 if( nbits % 2 )
848 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
849
850 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath10c575b2016-02-23 14:42:48 +0000851 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000852
853 /*
854 * find primes P and Q with Q < P so that:
855 * GCD( E, (P-1)*(Q-1) ) == 1
856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
859 do
860 {
Janos Follath10c575b2016-02-23 14:42:48 +0000861 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000862 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000863
Janos Follathef441782016-09-21 13:18:12 +0100864 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000865 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000868 continue;
869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200871 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 continue;
873
Janos Follathef441782016-09-21 13:18:12 +0100874 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
875 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
878 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
879 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
880 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000881 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
884 /*
885 * D = E^-1 mod ((P-1)*(Q-1))
886 * DP = D mod (P - 1)
887 * DQ = D mod (Q - 1)
888 * QP = Q^-1 mod P
889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
891 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
892 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
893 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000894
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200895 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000896
Hanno Becker83aad1f2017-08-23 06:45:10 +0100897 /* Double-check */
898 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
899
Paul Bakker5121ce52009-01-03 21:22:43 +0000900cleanup:
901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000903
904 if( ret != 0 )
905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_rsa_free( ctx );
907 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908 }
909
Paul Bakker48377d92013-08-30 12:06:24 +0200910 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000911}
912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
915/*
916 * Check a public RSA key
917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000919{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000920 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000922
Paul Bakker48377d92013-08-30 12:06:24 +0200923 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000924 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200927 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
928 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200931 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
933 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
935 return( 0 );
936}
937
938/*
939 * Check a private RSA key
940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000942{
943 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 return( ret );
948
Paul Bakker37940d9f2009-07-10 22:38:58 +0000949 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
953 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
954 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
955 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
958 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
959 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
960 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
961 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
962 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
965 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
966 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
969 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
970 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000971 /*
972 * Check for a valid PKCS1v2 private key
973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
975 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
976 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
977 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
978 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
979 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
980 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 }
Paul Bakker48377d92013-08-30 12:06:24 +0200984
Paul Bakker5121ce52009-01-03 21:22:43 +0000985cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
987 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
988 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
989 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000992 return( ret );
993
Paul Bakker6c591fa2011-05-05 11:49:20 +0000994 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000996
997 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000998}
999
1000/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001001 * Check if contexts holding a public and private key match
1002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001004{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1006 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001009 }
1010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1012 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001015 }
1016
1017 return( 0 );
1018}
1019
1020/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001021 * Do an RSA public key operation
1022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001024 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001025 unsigned char *output )
1026{
Paul Bakker23986e52011-04-24 08:57:21 +00001027 int ret;
1028 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001033#if defined(MBEDTLS_THREADING_C)
1034 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1035 return( ret );
1036#endif
1037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001042 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1043 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 }
1045
1046 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1048 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
1050cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001052 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1053 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
1058 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
1061 return( 0 );
1062}
1063
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001064/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001065 * Generate or update blinding values, see section 10 of:
1066 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001067 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001068 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001069 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001070static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001071 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1072{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001073 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001074
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001075 if( ctx->Vf.p != NULL )
1076 {
1077 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1079 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1080 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1081 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001082
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001083 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001084 }
1085
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001086 /* Unblinding value: Vf = random number, invertible mod N */
1087 do {
1088 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1092 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1093 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001094
1095 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1097 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 +02001098
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001099
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001100cleanup:
1101 return( ret );
1102}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001103
Paul Bakker5121ce52009-01-03 21:22:43 +00001104/*
Janos Follathe81102e2017-03-22 13:38:28 +00001105 * Exponent blinding supposed to prevent side-channel attacks using multiple
1106 * traces of measurements to recover the RSA key. The more collisions are there,
1107 * the more bits of the key can be recovered. See [3].
1108 *
1109 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1110 * observations on avarage.
1111 *
1112 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1113 * to make 2^112 observations on avarage.
1114 *
1115 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1116 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1117 * Thus in this sense with 28 byte blinding the security is not reduced by
1118 * side-channel attacks like the one in [3])
1119 *
1120 * This countermeasure does not help if the key recovery is possible with a
1121 * single trace.
1122 */
1123#define RSA_EXPONENT_BLINDING 28
1124
1125/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 * Do an RSA private key operation
1127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001129 int (*f_rng)(void *, unsigned char *, size_t),
1130 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001131 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 unsigned char *output )
1133{
Paul Bakker23986e52011-04-24 08:57:21 +00001134 int ret;
1135 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001137 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001138#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001139 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001140 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001141#else
1142 mbedtls_mpi DP_blind, DQ_blind;
1143 mbedtls_mpi *DP = &ctx->DP;
1144 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001145#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001147 /* Make sure we have private key info, prevent possible misuse */
1148 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
1149 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001152 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1153
1154
1155 if( f_rng != NULL )
1156 {
Janos Follathe81102e2017-03-22 13:38:28 +00001157#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001158 mbedtls_mpi_init( &D_blind );
1159#else
1160 mbedtls_mpi_init( &DP_blind );
1161 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001162#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001163 }
Janos Follathe81102e2017-03-22 13:38:28 +00001164
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001166#if defined(MBEDTLS_THREADING_C)
1167 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1168 return( ret );
1169#endif
1170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1172 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001173 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001174 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1175 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 }
1177
Paul Bakkerf451bac2013-08-30 15:37:02 +02001178 if( f_rng != NULL )
1179 {
1180 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001181 * Blinding
1182 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001183 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001184 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1185 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001187
Janos Follathe81102e2017-03-22 13:38:28 +00001188 /*
1189 * Exponent blinding
1190 */
1191 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1192 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1193
Janos Follathf9203b42017-03-22 15:13:15 +00001194#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001195 /*
1196 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1197 */
1198 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1199 f_rng, p_rng ) );
1200 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1201 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1202 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1203
1204 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001205#else
1206 /*
1207 * DP_blind = ( P - 1 ) * R + DP
1208 */
1209 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1210 f_rng, p_rng ) );
1211 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1212 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1213 &ctx->DP ) );
1214
1215 DP = &DP_blind;
1216
1217 /*
1218 * DQ_blind = ( Q - 1 ) * R + DQ
1219 */
1220 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1221 f_rng, p_rng ) );
1222 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1223 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1224 &ctx->DQ ) );
1225
1226 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001227#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001228 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001231 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001232#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001233 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001234 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 *
1236 * T1 = input ^ dP mod P
1237 * T2 = input ^ dQ mod Q
1238 */
Janos Follathf9203b42017-03-22 15:13:15 +00001239 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1240 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
1242 /*
1243 * T = (T1 - T2) * (Q^-1 mod P) mod P
1244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1246 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1247 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
1249 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001250 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1253 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1254#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001255
Paul Bakkerf451bac2013-08-30 15:37:02 +02001256 if( f_rng != NULL )
1257 {
1258 /*
1259 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001260 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001261 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001262 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001264 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
1266 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001268
1269cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001271 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1272 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001273#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001276 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1277
1278 if( f_rng != NULL )
1279 {
Janos Follathe81102e2017-03-22 13:38:28 +00001280#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001281 mbedtls_mpi_free( &D_blind );
1282#else
1283 mbedtls_mpi_free( &DP_blind );
1284 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001285#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
1288 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001290
1291 return( 0 );
1292}
1293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001295/**
1296 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1297 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001298 * \param dst buffer to mask
1299 * \param dlen length of destination buffer
1300 * \param src source of the mask generation
1301 * \param slen length of the source buffer
1302 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001303 */
Paul Bakker48377d92013-08-30 12:06:24 +02001304static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001306{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001308 unsigned char counter[4];
1309 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001310 unsigned int hlen;
1311 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001314 memset( counter, 0, 4 );
1315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001317
Simon Butcher02037452016-03-01 21:19:12 +00001318 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001319 p = dst;
1320
1321 while( dlen > 0 )
1322 {
1323 use_len = hlen;
1324 if( dlen < hlen )
1325 use_len = dlen;
1326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 mbedtls_md_starts( md_ctx );
1328 mbedtls_md_update( md_ctx, src, slen );
1329 mbedtls_md_update( md_ctx, counter, 4 );
1330 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001331
1332 for( i = 0; i < use_len; ++i )
1333 *p++ ^= mask[i];
1334
1335 counter[3]++;
1336
1337 dlen -= use_len;
1338 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001339
1340 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001341}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001345/*
1346 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001349 int (*f_rng)(void *, unsigned char *, size_t),
1350 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001351 int mode,
1352 const unsigned char *label, size_t label_len,
1353 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001354 const unsigned char *input,
1355 unsigned char *output )
1356{
1357 size_t olen;
1358 int ret;
1359 unsigned char *p = output;
1360 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 const mbedtls_md_info_t *md_info;
1362 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1365 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001366
1367 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001371 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001373
1374 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001376
Simon Butcher02037452016-03-01 21:19:12 +00001377 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001378 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001380
1381 memset( output, 0, olen );
1382
1383 *p++ = 0;
1384
Simon Butcher02037452016-03-01 21:19:12 +00001385 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001386 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001388
1389 p += hlen;
1390
Simon Butcher02037452016-03-01 21:19:12 +00001391 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001393 p += hlen;
1394 p += olen - 2 * hlen - 2 - ilen;
1395 *p++ = 1;
1396 memcpy( p, input, ilen );
1397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001399 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1400 {
1401 mbedtls_md_free( &md_ctx );
1402 return( ret );
1403 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001404
Simon Butcher02037452016-03-01 21:19:12 +00001405 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001406 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1407 &md_ctx );
1408
Simon Butcher02037452016-03-01 21:19:12 +00001409 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001410 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1411 &md_ctx );
1412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 return( ( mode == MBEDTLS_RSA_PUBLIC )
1416 ? mbedtls_rsa_public( ctx, output, output )
1417 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001422/*
1423 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001426 int (*f_rng)(void *, unsigned char *, size_t),
1427 void *p_rng,
1428 int mode, size_t ilen,
1429 const unsigned char *input,
1430 unsigned char *output )
1431{
1432 size_t nb_pad, olen;
1433 int ret;
1434 unsigned char *p = output;
1435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1437 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001438
Janos Follath1ed9f992016-03-18 11:45:44 +00001439 // We don't check p_rng because it won't be dereferenced here
1440 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001442
1443 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001444
Simon Butcher02037452016-03-01 21:19:12 +00001445 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001446 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001448
1449 nb_pad = olen - 3 - ilen;
1450
1451 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001455
1456 while( nb_pad-- > 0 )
1457 {
1458 int rng_dl = 100;
1459
1460 do {
1461 ret = f_rng( p_rng, p, 1 );
1462 } while( *p == 0 && --rng_dl && ret == 0 );
1463
Simon Butcher02037452016-03-01 21:19:12 +00001464 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001465 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001467
1468 p++;
1469 }
1470 }
1471 else
1472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001474
1475 while( nb_pad-- > 0 )
1476 *p++ = 0xFF;
1477 }
1478
1479 *p++ = 0;
1480 memcpy( p, input, ilen );
1481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 return( ( mode == MBEDTLS_RSA_PUBLIC )
1483 ? mbedtls_rsa_public( ctx, output, output )
1484 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001487
Paul Bakker5121ce52009-01-03 21:22:43 +00001488/*
1489 * Add the message padding, then do an RSA operation
1490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001492 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001493 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001494 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001495 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 unsigned char *output )
1497{
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 switch( ctx->padding )
1499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_PKCS1_V15)
1501 case MBEDTLS_RSA_PKCS_V15:
1502 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001503 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001504#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_PKCS1_V21)
1507 case MBEDTLS_RSA_PKCS_V21:
1508 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001509 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001510#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001511
1512 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001515}
1516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001518/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001519 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001522 int (*f_rng)(void *, unsigned char *, size_t),
1523 void *p_rng,
1524 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001525 const unsigned char *label, size_t label_len,
1526 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001527 const unsigned char *input,
1528 unsigned char *output,
1529 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001530{
Paul Bakker23986e52011-04-24 08:57:21 +00001531 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001532 size_t ilen, i, pad_len;
1533 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1535 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001536 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 const mbedtls_md_info_t *md_info;
1538 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001539
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001540 /*
1541 * Parameters sanity checks
1542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1544 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001545
1546 ilen = ctx->len;
1547
Paul Bakker27fdf462011-06-09 13:55:13 +00001548 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001552 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001554
Janos Follathc17cda12016-02-11 11:08:18 +00001555 hlen = mbedtls_md_get_size( md_info );
1556
1557 // checking for integer underflow
1558 if( 2 * hlen + 2 > ilen )
1559 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1560
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001561 /*
1562 * RSA operation
1563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1565 ? mbedtls_rsa_public( ctx, input, buf )
1566 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001567
1568 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001569 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001570
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001571 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001572 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001575 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1576 {
1577 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001578 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001579 }
1580
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001581
1582 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001584
1585 /* seed: Apply seedMask to maskedSeed */
1586 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1587 &md_ctx );
1588
1589 /* DB: Apply dbMask to maskedDB */
1590 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1591 &md_ctx );
1592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001594
1595 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001596 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001597 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001599 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001600
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001601 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001602
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001603 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001604
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001605 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001606 for( i = 0; i < hlen; i++ )
1607 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001608
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001609 /* Get zero-padding len, but always read till end of buffer
1610 * (minus one, for the 01 byte) */
1611 pad_len = 0;
1612 pad_done = 0;
1613 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1614 {
1615 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001616 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001617 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001618
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001619 p += pad_len;
1620 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001621
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001622 /*
1623 * The only information "leaked" is whether the padding was correct or not
1624 * (eg, no data is copied if it was not correct). This meets the
1625 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1626 * the different error conditions.
1627 */
1628 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001629 {
1630 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1631 goto cleanup;
1632 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001633
Paul Bakker66d5d072014-06-17 16:39:18 +02001634 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001635 {
1636 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1637 goto cleanup;
1638 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001639
1640 *olen = ilen - (p - buf);
1641 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001642 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001643
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001644cleanup:
1645 mbedtls_zeroize( buf, sizeof( buf ) );
1646 mbedtls_zeroize( lhash, sizeof( lhash ) );
1647
1648 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001653/*
1654 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001657 int (*f_rng)(void *, unsigned char *, size_t),
1658 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001659 int mode, size_t *olen,
1660 const unsigned char *input,
1661 unsigned char *output,
1662 size_t output_max_len)
1663{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001664 int ret;
1665 size_t ilen, pad_count = 0, i;
1666 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1670 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001671
1672 ilen = ctx->len;
1673
1674 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1678 ? mbedtls_rsa_public( ctx, input, buf )
1679 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001680
1681 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001682 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001683
1684 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001685 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001686
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001687 /*
1688 * Check and get padding len in "constant-time"
1689 */
1690 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001691
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001692 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001696
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001697 /* Get padding len, but always read till end of buffer
1698 * (minus one, for the 00 byte) */
1699 for( i = 0; i < ilen - 3; i++ )
1700 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001701 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1702 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001703 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001704
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001705 p += pad_count;
1706 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001707 }
1708 else
1709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001711
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001712 /* Get padding len, but always read till end of buffer
1713 * (minus one, for the 00 byte) */
1714 for( i = 0; i < ilen - 3; i++ )
1715 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001716 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001717 pad_count += ( pad_done == 0 );
1718 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001719
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001720 p += pad_count;
1721 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001722 }
1723
Janos Follathc69fa502016-02-12 13:30:09 +00001724 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001725
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001726 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001727 {
1728 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1729 goto cleanup;
1730 }
Paul Bakker8804f692013-02-28 18:06:26 +01001731
Paul Bakker66d5d072014-06-17 16:39:18 +02001732 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001733 {
1734 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1735 goto cleanup;
1736 }
Paul Bakker060c5682009-01-12 21:48:39 +00001737
Paul Bakker27fdf462011-06-09 13:55:13 +00001738 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001739 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001740 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001742cleanup:
1743 mbedtls_zeroize( buf, sizeof( buf ) );
1744
1745 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001746}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
1749/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001750 * Do an RSA operation, then remove the message padding
1751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001753 int (*f_rng)(void *, unsigned char *, size_t),
1754 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001755 int mode, size_t *olen,
1756 const unsigned char *input,
1757 unsigned char *output,
1758 size_t output_max_len)
1759{
1760 switch( ctx->padding )
1761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762#if defined(MBEDTLS_PKCS1_V15)
1763 case MBEDTLS_RSA_PKCS_V15:
1764 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001765 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001766#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768#if defined(MBEDTLS_PKCS1_V21)
1769 case MBEDTLS_RSA_PKCS_V21:
1770 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001771 olen, input, output,
1772 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001773#endif
1774
1775 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001777 }
1778}
1779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001781/*
1782 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001785 int (*f_rng)(void *, unsigned char *, size_t),
1786 void *p_rng,
1787 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001789 unsigned int hashlen,
1790 const unsigned char *hash,
1791 unsigned char *sig )
1792{
1793 size_t olen;
1794 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001796 unsigned int slen, hlen, offset = 0;
1797 int ret;
1798 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 const mbedtls_md_info_t *md_info;
1800 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1803 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001804
1805 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001807
1808 olen = ctx->len;
1809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001811 {
Simon Butcher02037452016-03-01 21:19:12 +00001812 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001814 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001818 }
1819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001821 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001825 slen = hlen;
1826
1827 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001829
1830 memset( sig, 0, olen );
1831
Simon Butcher02037452016-03-01 21:19:12 +00001832 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001833 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001835
Simon Butcher02037452016-03-01 21:19:12 +00001836 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001837 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001838 p += olen - hlen * 2 - 2;
1839 *p++ = 0x01;
1840 memcpy( p, salt, slen );
1841 p += slen;
1842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001844 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1845 {
1846 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001847 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001848 return( ret );
1849 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001850
Simon Butcher02037452016-03-01 21:19:12 +00001851 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 mbedtls_md_starts( &md_ctx );
1853 mbedtls_md_update( &md_ctx, p, 8 );
1854 mbedtls_md_update( &md_ctx, hash, hashlen );
1855 mbedtls_md_update( &md_ctx, salt, slen );
1856 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001857 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001858
Simon Butcher02037452016-03-01 21:19:12 +00001859 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001860 if( msb % 8 == 0 )
1861 offset = 1;
1862
Simon Butcher02037452016-03-01 21:19:12 +00001863 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001864 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001867
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001868 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001869 sig[0] &= 0xFF >> ( olen * 8 - msb );
1870
1871 p += hlen;
1872 *p++ = 0xBC;
1873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 return( ( mode == MBEDTLS_RSA_PUBLIC )
1875 ? mbedtls_rsa_public( ctx, sig, sig )
1876 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001877}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001881/*
1882 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1883 */
1884/*
1885 * Do an RSA operation to sign the message digest
1886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001888 int (*f_rng)(void *, unsigned char *, size_t),
1889 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001890 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001892 unsigned int hashlen,
1893 const unsigned char *hash,
1894 unsigned char *sig )
1895{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001896 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001897 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001898 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001899 unsigned char *sig_try = NULL, *verif = NULL;
1900 size_t i;
1901 unsigned char diff;
1902 volatile unsigned char diff_no_optimize;
1903 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1906 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001907
1908 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001909 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001914 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1918 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001919
Paul Bakkerc70b9822013-04-07 22:00:46 +02001920 nb_pad -= 10 + oid_size;
1921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001923 }
1924
Paul Bakkerc70b9822013-04-07 22:00:46 +02001925 nb_pad -= hashlen;
1926
Paul Bakkerb3869132013-02-28 17:21:01 +01001927 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001929
1930 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001932 memset( p, 0xFF, nb_pad );
1933 p += nb_pad;
1934 *p++ = 0;
1935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001937 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001938 memcpy( p, hash, hashlen );
1939 }
1940 else
1941 {
1942 /*
1943 * DigestInfo ::= SEQUENCE {
1944 * digestAlgorithm DigestAlgorithmIdentifier,
1945 * digest Digest }
1946 *
1947 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1948 *
1949 * Digest ::= OCTET STRING
1950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001952 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001954 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001956 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001957 memcpy( p, oid, oid_size );
1958 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001960 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001962 *p++ = hashlen;
1963 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001964 }
1965
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001966 if( mode == MBEDTLS_RSA_PUBLIC )
1967 return( mbedtls_rsa_public( ctx, sig, sig ) );
1968
1969 /*
1970 * In order to prevent Lenstra's attack, make the signature in a
1971 * temporary buffer and check it before returning it.
1972 */
1973 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001974 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001975 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1976
Simon Butcher1285ab52016-01-01 21:42:47 +00001977 verif = mbedtls_calloc( 1, ctx->len );
1978 if( verif == NULL )
1979 {
1980 mbedtls_free( sig_try );
1981 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1982 }
1983
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001984 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1985 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1986
1987 /* Compare in constant time just in case */
1988 for( diff = 0, i = 0; i < ctx->len; i++ )
1989 diff |= verif[i] ^ sig[i];
1990 diff_no_optimize = diff;
1991
1992 if( diff_no_optimize != 0 )
1993 {
1994 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1995 goto cleanup;
1996 }
1997
1998 memcpy( sig, sig_try, ctx->len );
1999
2000cleanup:
2001 mbedtls_free( sig_try );
2002 mbedtls_free( verif );
2003
2004 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002005}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002007
2008/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002009 * Do an RSA operation to sign the message digest
2010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002012 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002013 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002016 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002017 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 unsigned char *sig )
2019{
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 switch( ctx->padding )
2021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022#if defined(MBEDTLS_PKCS1_V15)
2023 case MBEDTLS_RSA_PKCS_V15:
2024 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002025 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028#if defined(MBEDTLS_PKCS1_V21)
2029 case MBEDTLS_RSA_PKCS_V21:
2030 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002031 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002032#endif
2033
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002037}
2038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002040/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002041 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002044 int (*f_rng)(void *, unsigned char *, size_t),
2045 void *p_rng,
2046 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002048 unsigned int hashlen,
2049 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002051 int expected_salt_len,
2052 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002053{
Paul Bakker23986e52011-04-24 08:57:21 +00002054 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002055 size_t siglen;
2056 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002058 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002059 unsigned int hlen;
2060 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 const mbedtls_md_info_t *md_info;
2062 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002063 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2066 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002067
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 siglen = ctx->len;
2069
Paul Bakker27fdf462011-06-09 13:55:13 +00002070 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2074 ? mbedtls_rsa_public( ctx, sig, buf )
2075 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002076
2077 if( ret != 0 )
2078 return( ret );
2079
2080 p = buf;
2081
Paul Bakkerb3869132013-02-28 17:21:01 +01002082 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 {
Simon Butcher02037452016-03-01 21:19:12 +00002087 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002089 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002093 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002096 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002100 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002101
Paul Bakkerb3869132013-02-28 17:21:01 +01002102 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002103
Simon Butcher02037452016-03-01 21:19:12 +00002104 /*
2105 * Note: EMSA-PSS verification is over the length of N - 1 bits
2106 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002107 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002108
Simon Butcher02037452016-03-01 21:19:12 +00002109 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002110 if( msb % 8 == 0 )
2111 {
2112 p++;
2113 siglen -= 1;
2114 }
2115 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002119 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2120 {
2121 mbedtls_md_free( &md_ctx );
2122 return( ret );
2123 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002124
Paul Bakkerb3869132013-02-28 17:21:01 +01002125 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002126
Paul Bakkerb3869132013-02-28 17:21:01 +01002127 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002128
Paul Bakker4de44aa2013-12-31 11:43:01 +01002129 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002130 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002131
Paul Bakkerb3869132013-02-28 17:21:01 +01002132 if( p == buf + siglen ||
2133 *p++ != 0x01 )
2134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 mbedtls_md_free( &md_ctx );
2136 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002137 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002138
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002139 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002140 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002143 slen != (size_t) expected_salt_len )
2144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 mbedtls_md_free( &md_ctx );
2146 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002147 }
2148
Simon Butcher02037452016-03-01 21:19:12 +00002149 /*
2150 * Generate H = Hash( M' )
2151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 mbedtls_md_starts( &md_ctx );
2153 mbedtls_md_update( &md_ctx, zeros, 8 );
2154 mbedtls_md_update( &md_ctx, hash, hashlen );
2155 mbedtls_md_update( &md_ctx, p, slen );
2156 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002159
Paul Bakkerb3869132013-02-28 17:21:01 +01002160 if( memcmp( p + slen, result, hlen ) == 0 )
2161 return( 0 );
2162 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002164}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002165
2166/*
2167 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002170 int (*f_rng)(void *, unsigned char *, size_t),
2171 void *p_rng,
2172 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002174 unsigned int hashlen,
2175 const unsigned char *hash,
2176 const unsigned char *sig )
2177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2179 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002180 : md_alg;
2181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002183 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002185 sig ) );
2186
2187}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002191/*
2192 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002195 int (*f_rng)(void *, unsigned char *, size_t),
2196 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002197 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002199 unsigned int hashlen,
2200 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002201 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002202{
2203 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002204 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002205 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 mbedtls_md_type_t msg_md_alg;
2207 const mbedtls_md_info_t *md_info;
2208 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002209 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2212 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002213
2214 siglen = ctx->len;
2215
2216 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2220 ? mbedtls_rsa_public( ctx, sig, buf )
2221 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002222
2223 if( ret != 0 )
2224 return( ret );
2225
2226 p = buf;
2227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2229 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002230
2231 while( *p != 0 )
2232 {
2233 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002235 p++;
2236 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002237 p++; /* skip 00 byte */
2238
2239 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2240 if( p - buf < 11 )
2241 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002242
2243 len = siglen - ( p - buf );
2244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002246 {
2247 if( memcmp( p, hash, hashlen ) == 0 )
2248 return( 0 );
2249 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 }
2252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002254 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2256 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002257
2258 end = p + len;
2259
Simon Butcher02037452016-03-01 21:19:12 +00002260 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002261 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2262 * Insist on 2-byte length tags, to protect against variants of
2263 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002264 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002265 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2267 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2268 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002269 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002271
Gilles Peskinee7e76502017-05-04 12:48:39 +02002272 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2274 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2275 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002276 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002278
Gilles Peskinee7e76502017-05-04 12:48:39 +02002279 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2281 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002282 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002283 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002284
2285 oid.p = p;
2286 p += oid.len;
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2289 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002290
2291 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002293
2294 /*
2295 * assume the algorithm parameters must be NULL
2296 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002297 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2299 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002300 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002302
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002303 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002304 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2305 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002306 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002308
2309 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002311
2312 p += hashlen;
2313
2314 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002316
2317 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002318}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002320
2321/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002322 * Do an RSA operation and check the message digest
2323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002325 int (*f_rng)(void *, unsigned char *, size_t),
2326 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002327 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002329 unsigned int hashlen,
2330 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002331 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002332{
2333 switch( ctx->padding )
2334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335#if defined(MBEDTLS_PKCS1_V15)
2336 case MBEDTLS_RSA_PKCS_V15:
2337 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002338 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002339#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341#if defined(MBEDTLS_PKCS1_V21)
2342 case MBEDTLS_RSA_PKCS_V21:
2343 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002344 hashlen, hash, sig );
2345#endif
2346
2347 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002349 }
2350}
2351
2352/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002353 * Copy the components of an RSA key
2354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002356{
2357 int ret;
2358
2359 dst->ver = src->ver;
2360 dst->len = src->len;
2361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2363 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2366 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2367 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
2368 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2369 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2370 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
2373 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2374 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2377 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002378
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002379 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002380 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002381
2382cleanup:
2383 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002385
2386 return( ret );
2387}
2388
2389/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 * Free the components of an RSA key
2391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
2395 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
2396 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
2397 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
2398 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400#if defined(MBEDTLS_THREADING_C)
2401 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002402#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002403}
2404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002407#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002408
2409/*
2410 * Example RSA-1024 keypair, for test purposes
2411 */
2412#define KEY_LEN 128
2413
2414#define RSA_N "9292758453063D803DD603D5E777D788" \
2415 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2416 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2417 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2418 "93A89813FBF3C4F8066D2D800F7C38A8" \
2419 "1AE31942917403FF4946B0A83D3D3E05" \
2420 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2421 "5E94BB77B07507233A0BC7BAC8F90F79"
2422
2423#define RSA_E "10001"
2424
2425#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2426 "66CA472BC44D253102F8B4A9D3BFA750" \
2427 "91386C0077937FE33FA3252D28855837" \
2428 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2429 "DF79C5CE07EE72C7F123142198164234" \
2430 "CABB724CF78B8173B9F880FC86322407" \
2431 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2432 "071513A1E85B5DFA031F21ECAE91A34D"
2433
2434#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2435 "2C01CAD19EA484A87EA4377637E75500" \
2436 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2437 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2438
2439#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2440 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2441 "910E4168387E3C30AA1E00C339A79508" \
2442 "8452DD96A9A5EA5D9DCA68DA636032AF"
2443
2444#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2445 "3C94D22288ACD763FD8E5600ED4A702D" \
2446 "F84198A5F06C2E72236AE490C93F07F8" \
2447 "3CC559CD27BC2D1CA488811730BB5725"
2448
2449#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2450 "D8AAEA56749EA28623272E4F7D0592AF" \
2451 "7C1F1313CAC9471B5C523BFE592F517B" \
2452 "407A1BD76C164B93DA2D32A383E58357"
2453
2454#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2455 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2456 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2457 "A74206CEC169D74BF5A8C50D6F48EA08"
2458
2459#define PT_LEN 24
2460#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2461 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002464static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002465{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002466#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002467 size_t i;
2468
Paul Bakker545570e2010-07-18 09:00:25 +00002469 if( rng_state != NULL )
2470 rng_state = NULL;
2471
Paul Bakkera3d195c2011-11-27 21:07:34 +00002472 for( i = 0; i < len; ++i )
2473 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002474#else
2475 if( rng_state != NULL )
2476 rng_state = NULL;
2477
2478 arc4random_buf( output, len );
2479#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002480
Paul Bakkera3d195c2011-11-27 21:07:34 +00002481 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002484
Paul Bakker5121ce52009-01-03 21:22:43 +00002485/*
2486 * Checkup routine
2487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002489{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002490 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002492 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 unsigned char rsa_plaintext[PT_LEN];
2495 unsigned char rsa_decrypted[PT_LEN];
2496 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002498 unsigned char sha1sum[20];
2499#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Hanno Becker3a701162017-08-22 13:52:43 +01002501 mbedtls_mpi K;
2502
2503 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
Hanno Becker3a701162017-08-22 13:52:43 +01002506 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2507 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2508 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2509 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2510 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2511 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2512 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2513 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2514 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2515 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2516
2517 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
2519 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2523 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 {
2525 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
2528 return( 1 );
2529 }
2530
Hanno Becker3a701162017-08-22 13:52:43 +01002531 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2532 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2533
2534 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2535 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2536
2537 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2538 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2539
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
2543 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 rsa_plaintext, rsa_ciphertext ) != 0 )
2547 {
2548 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
2551 return( 1 );
2552 }
2553
2554 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002558 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002559 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 {
2561 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
2564 return( 1 );
2565 }
2566
2567 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2568 {
2569 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
2572 return( 1 );
2573 }
2574
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002575 if( verbose != 0 )
2576 mbedtls_printf( "passed\n" );
2577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002580 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 sha1sum, rsa_ciphertext ) != 0 )
2586 {
2587 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
2590 return( 1 );
2591 }
2592
2593 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 sha1sum, rsa_ciphertext ) != 0 )
2598 {
2599 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
2602 return( 1 );
2603 }
2604
2605 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002606 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002608
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002609 if( verbose != 0 )
2610 mbedtls_printf( "\n" );
2611
Paul Bakker3d8fb632014-04-17 12:42:41 +02002612cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002613 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 mbedtls_rsa_free( &rsa );
2615#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002616 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002618 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619}
2620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623#endif /* MBEDTLS_RSA_C */