blob: 78db24031e87ece3271a403a9cb8774567bc83b6 [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
897cleanup:
898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
901 if( ret != 0 )
902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_rsa_free( ctx );
904 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 }
906
Paul Bakker48377d92013-08-30 12:06:24 +0200907 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908}
909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
912/*
913 * Check a public RSA key
914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000916{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000917 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000919
Paul Bakker48377d92013-08-30 12:06:24 +0200920 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000921 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000923
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200924 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
925 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200928 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
930 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
932 return( 0 );
933}
934
935/*
936 * Check a private RSA key
937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000939{
940 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000944 return( ret );
945
Paul Bakker37940d9f2009-07-10 22:38:58 +0000946 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
950 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
951 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
952 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
955 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
956 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
957 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
958 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
959 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
962 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
963 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
966 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
967 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000968 /*
969 * Check for a valid PKCS1v2 private key
970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
972 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
973 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
974 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
975 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
976 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
977 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 }
Paul Bakker48377d92013-08-30 12:06:24 +0200981
Paul Bakker5121ce52009-01-03 21:22:43 +0000982cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
984 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
985 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
986 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000989 return( ret );
990
Paul Bakker6c591fa2011-05-05 11:49:20 +0000991 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000993
994 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995}
996
997/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100998 * Check if contexts holding a public and private key match
999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001001{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1003 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001006 }
1007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1009 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001012 }
1013
1014 return( 0 );
1015}
1016
1017/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 * Do an RSA public key operation
1019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001021 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001022 unsigned char *output )
1023{
Paul Bakker23986e52011-04-24 08:57:21 +00001024 int ret;
1025 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001030#if defined(MBEDTLS_THREADING_C)
1031 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1032 return( ret );
1033#endif
1034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001039 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1040 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 }
1042
1043 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1045 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
1047cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001049 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1050 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
1055 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
1058 return( 0 );
1059}
1060
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001061/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001062 * Generate or update blinding values, see section 10 of:
1063 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001064 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001065 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001066 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001067static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001068 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1069{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001070 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001071
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001072 if( ctx->Vf.p != NULL )
1073 {
1074 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1076 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1077 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1078 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001079
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001080 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001081 }
1082
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001083 /* Unblinding value: Vf = random number, invertible mod N */
1084 do {
1085 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1089 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1090 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001091
1092 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1094 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 +02001095
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001096
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001097cleanup:
1098 return( ret );
1099}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001100
Paul Bakker5121ce52009-01-03 21:22:43 +00001101/*
Janos Follathe81102e2017-03-22 13:38:28 +00001102 * Exponent blinding supposed to prevent side-channel attacks using multiple
1103 * traces of measurements to recover the RSA key. The more collisions are there,
1104 * the more bits of the key can be recovered. See [3].
1105 *
1106 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1107 * observations on avarage.
1108 *
1109 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1110 * to make 2^112 observations on avarage.
1111 *
1112 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1113 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1114 * Thus in this sense with 28 byte blinding the security is not reduced by
1115 * side-channel attacks like the one in [3])
1116 *
1117 * This countermeasure does not help if the key recovery is possible with a
1118 * single trace.
1119 */
1120#define RSA_EXPONENT_BLINDING 28
1121
1122/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 * Do an RSA private key operation
1124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001126 int (*f_rng)(void *, unsigned char *, size_t),
1127 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001128 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 unsigned char *output )
1130{
Paul Bakker23986e52011-04-24 08:57:21 +00001131 int ret;
1132 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001134 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001135#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001136 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001137 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001138#else
1139 mbedtls_mpi DP_blind, DQ_blind;
1140 mbedtls_mpi *DP = &ctx->DP;
1141 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001142#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001144 /* Make sure we have private key info, prevent possible misuse */
1145 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
1146 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001149 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1150
1151
1152 if( f_rng != NULL )
1153 {
Janos Follathe81102e2017-03-22 13:38:28 +00001154#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001155 mbedtls_mpi_init( &D_blind );
1156#else
1157 mbedtls_mpi_init( &DP_blind );
1158 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001159#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001160 }
Janos Follathe81102e2017-03-22 13:38:28 +00001161
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001163#if defined(MBEDTLS_THREADING_C)
1164 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1165 return( ret );
1166#endif
1167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1169 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001171 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1172 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001173 }
1174
Paul Bakkerf451bac2013-08-30 15:37:02 +02001175 if( f_rng != NULL )
1176 {
1177 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001178 * Blinding
1179 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001180 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001181 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1182 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001184
Janos Follathe81102e2017-03-22 13:38:28 +00001185 /*
1186 * Exponent blinding
1187 */
1188 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1189 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1190
Janos Follathf9203b42017-03-22 15:13:15 +00001191#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001192 /*
1193 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1194 */
1195 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1196 f_rng, p_rng ) );
1197 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1198 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1199 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1200
1201 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001202#else
1203 /*
1204 * DP_blind = ( P - 1 ) * R + DP
1205 */
1206 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1207 f_rng, p_rng ) );
1208 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1209 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1210 &ctx->DP ) );
1211
1212 DP = &DP_blind;
1213
1214 /*
1215 * DQ_blind = ( Q - 1 ) * R + DQ
1216 */
1217 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1218 f_rng, p_rng ) );
1219 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1220 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1221 &ctx->DQ ) );
1222
1223 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001224#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001225 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001228 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001229#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001230 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001231 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 *
1233 * T1 = input ^ dP mod P
1234 * T2 = input ^ dQ mod Q
1235 */
Janos Follathf9203b42017-03-22 15:13:15 +00001236 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1237 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
1239 /*
1240 * T = (T1 - T2) * (Q^-1 mod P) mod P
1241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1243 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1244 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
1246 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001247 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1250 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1251#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001252
Paul Bakkerf451bac2013-08-30 15:37:02 +02001253 if( f_rng != NULL )
1254 {
1255 /*
1256 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001257 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001258 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001259 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001261 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
1263 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
1266cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001268 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1269 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001270#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001273 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1274
1275 if( f_rng != NULL )
1276 {
Janos Follathe81102e2017-03-22 13:38:28 +00001277#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001278 mbedtls_mpi_free( &D_blind );
1279#else
1280 mbedtls_mpi_free( &DP_blind );
1281 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001282#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001283 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
1285 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
1288 return( 0 );
1289}
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001292/**
1293 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1294 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001295 * \param dst buffer to mask
1296 * \param dlen length of destination buffer
1297 * \param src source of the mask generation
1298 * \param slen length of the source buffer
1299 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001300 */
Paul Bakker48377d92013-08-30 12:06:24 +02001301static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001303{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001305 unsigned char counter[4];
1306 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001307 unsigned int hlen;
1308 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001311 memset( counter, 0, 4 );
1312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001314
Simon Butcher02037452016-03-01 21:19:12 +00001315 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001316 p = dst;
1317
1318 while( dlen > 0 )
1319 {
1320 use_len = hlen;
1321 if( dlen < hlen )
1322 use_len = dlen;
1323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 mbedtls_md_starts( md_ctx );
1325 mbedtls_md_update( md_ctx, src, slen );
1326 mbedtls_md_update( md_ctx, counter, 4 );
1327 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001328
1329 for( i = 0; i < use_len; ++i )
1330 *p++ ^= mask[i];
1331
1332 counter[3]++;
1333
1334 dlen -= use_len;
1335 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001336
1337 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001338}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001342/*
1343 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001346 int (*f_rng)(void *, unsigned char *, size_t),
1347 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001348 int mode,
1349 const unsigned char *label, size_t label_len,
1350 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001351 const unsigned char *input,
1352 unsigned char *output )
1353{
1354 size_t olen;
1355 int ret;
1356 unsigned char *p = output;
1357 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 const mbedtls_md_info_t *md_info;
1359 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1362 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001363
1364 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001368 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001370
1371 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001373
Simon Butcher02037452016-03-01 21:19:12 +00001374 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001375 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001377
1378 memset( output, 0, olen );
1379
1380 *p++ = 0;
1381
Simon Butcher02037452016-03-01 21:19:12 +00001382 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001383 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001385
1386 p += hlen;
1387
Simon Butcher02037452016-03-01 21:19:12 +00001388 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001390 p += hlen;
1391 p += olen - 2 * hlen - 2 - ilen;
1392 *p++ = 1;
1393 memcpy( p, input, ilen );
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001396 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1397 {
1398 mbedtls_md_free( &md_ctx );
1399 return( ret );
1400 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001401
Simon Butcher02037452016-03-01 21:19:12 +00001402 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001403 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1404 &md_ctx );
1405
Simon Butcher02037452016-03-01 21:19:12 +00001406 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001407 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1408 &md_ctx );
1409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 return( ( mode == MBEDTLS_RSA_PUBLIC )
1413 ? mbedtls_rsa_public( ctx, output, output )
1414 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001419/*
1420 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001423 int (*f_rng)(void *, unsigned char *, size_t),
1424 void *p_rng,
1425 int mode, size_t ilen,
1426 const unsigned char *input,
1427 unsigned char *output )
1428{
1429 size_t nb_pad, olen;
1430 int ret;
1431 unsigned char *p = output;
1432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1434 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001435
Janos Follath1ed9f992016-03-18 11:45:44 +00001436 // We don't check p_rng because it won't be dereferenced here
1437 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001439
1440 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001441
Simon Butcher02037452016-03-01 21:19:12 +00001442 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001443 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
1446 nb_pad = olen - 3 - ilen;
1447
1448 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001452
1453 while( nb_pad-- > 0 )
1454 {
1455 int rng_dl = 100;
1456
1457 do {
1458 ret = f_rng( p_rng, p, 1 );
1459 } while( *p == 0 && --rng_dl && ret == 0 );
1460
Simon Butcher02037452016-03-01 21:19:12 +00001461 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001462 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001464
1465 p++;
1466 }
1467 }
1468 else
1469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001471
1472 while( nb_pad-- > 0 )
1473 *p++ = 0xFF;
1474 }
1475
1476 *p++ = 0;
1477 memcpy( p, input, ilen );
1478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 return( ( mode == MBEDTLS_RSA_PUBLIC )
1480 ? mbedtls_rsa_public( ctx, output, output )
1481 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001484
Paul Bakker5121ce52009-01-03 21:22:43 +00001485/*
1486 * Add the message padding, then do an RSA operation
1487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001489 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001490 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001491 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001492 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001493 unsigned char *output )
1494{
Paul Bakker5121ce52009-01-03 21:22:43 +00001495 switch( ctx->padding )
1496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#if defined(MBEDTLS_PKCS1_V15)
1498 case MBEDTLS_RSA_PKCS_V15:
1499 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001500 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001501#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#if defined(MBEDTLS_PKCS1_V21)
1504 case MBEDTLS_RSA_PKCS_V21:
1505 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001506 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001507#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
1509 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001512}
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001515/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001516 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001519 int (*f_rng)(void *, unsigned char *, size_t),
1520 void *p_rng,
1521 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001522 const unsigned char *label, size_t label_len,
1523 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001524 const unsigned char *input,
1525 unsigned char *output,
1526 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001527{
Paul Bakker23986e52011-04-24 08:57:21 +00001528 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001529 size_t ilen, i, pad_len;
1530 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1532 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001533 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 const mbedtls_md_info_t *md_info;
1535 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001536
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001537 /*
1538 * Parameters sanity checks
1539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1541 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001542
1543 ilen = ctx->len;
1544
Paul Bakker27fdf462011-06-09 13:55:13 +00001545 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001549 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001551
Janos Follathc17cda12016-02-11 11:08:18 +00001552 hlen = mbedtls_md_get_size( md_info );
1553
1554 // checking for integer underflow
1555 if( 2 * hlen + 2 > ilen )
1556 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1557
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001558 /*
1559 * RSA operation
1560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1562 ? mbedtls_rsa_public( ctx, input, buf )
1563 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001564
1565 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001566 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001567
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001568 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001569 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001572 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1573 {
1574 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001575 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001576 }
1577
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001578
1579 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001581
1582 /* seed: Apply seedMask to maskedSeed */
1583 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1584 &md_ctx );
1585
1586 /* DB: Apply dbMask to maskedDB */
1587 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1588 &md_ctx );
1589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001591
1592 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001593 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001594 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001596 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001597
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001598 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001599
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001600 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001601
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001602 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001603 for( i = 0; i < hlen; i++ )
1604 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001605
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001606 /* Get zero-padding len, but always read till end of buffer
1607 * (minus one, for the 01 byte) */
1608 pad_len = 0;
1609 pad_done = 0;
1610 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1611 {
1612 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001613 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001614 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001615
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001616 p += pad_len;
1617 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001618
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001619 /*
1620 * The only information "leaked" is whether the padding was correct or not
1621 * (eg, no data is copied if it was not correct). This meets the
1622 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1623 * the different error conditions.
1624 */
1625 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001626 {
1627 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1628 goto cleanup;
1629 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001630
Paul Bakker66d5d072014-06-17 16:39:18 +02001631 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001632 {
1633 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1634 goto cleanup;
1635 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001636
1637 *olen = ilen - (p - buf);
1638 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001639 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001640
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001641cleanup:
1642 mbedtls_zeroize( buf, sizeof( buf ) );
1643 mbedtls_zeroize( lhash, sizeof( lhash ) );
1644
1645 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001646}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001650/*
1651 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001654 int (*f_rng)(void *, unsigned char *, size_t),
1655 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001656 int mode, size_t *olen,
1657 const unsigned char *input,
1658 unsigned char *output,
1659 size_t output_max_len)
1660{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001661 int ret;
1662 size_t ilen, pad_count = 0, i;
1663 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1667 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001668
1669 ilen = ctx->len;
1670
1671 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1675 ? mbedtls_rsa_public( ctx, input, buf )
1676 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001677
1678 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001679 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001680
1681 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001682 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001683
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001684 /*
1685 * Check and get padding len in "constant-time"
1686 */
1687 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001688
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001689 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001693
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001694 /* Get padding len, but always read till end of buffer
1695 * (minus one, for the 00 byte) */
1696 for( i = 0; i < ilen - 3; i++ )
1697 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001698 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1699 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001700 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001701
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001702 p += pad_count;
1703 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001704 }
1705 else
1706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001708
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001709 /* Get padding len, but always read till end of buffer
1710 * (minus one, for the 00 byte) */
1711 for( i = 0; i < ilen - 3; i++ )
1712 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001713 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001714 pad_count += ( pad_done == 0 );
1715 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001716
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001717 p += pad_count;
1718 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001719 }
1720
Janos Follathc69fa502016-02-12 13:30:09 +00001721 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001722
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001723 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001724 {
1725 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1726 goto cleanup;
1727 }
Paul Bakker8804f692013-02-28 18:06:26 +01001728
Paul Bakker66d5d072014-06-17 16:39:18 +02001729 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001730 {
1731 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1732 goto cleanup;
1733 }
Paul Bakker060c5682009-01-12 21:48:39 +00001734
Paul Bakker27fdf462011-06-09 13:55:13 +00001735 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001736 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001737 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001739cleanup:
1740 mbedtls_zeroize( buf, sizeof( buf ) );
1741
1742 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001745
1746/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001747 * Do an RSA operation, then remove the message padding
1748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001750 int (*f_rng)(void *, unsigned char *, size_t),
1751 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001752 int mode, size_t *olen,
1753 const unsigned char *input,
1754 unsigned char *output,
1755 size_t output_max_len)
1756{
1757 switch( ctx->padding )
1758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#if defined(MBEDTLS_PKCS1_V15)
1760 case MBEDTLS_RSA_PKCS_V15:
1761 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001762 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001763#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765#if defined(MBEDTLS_PKCS1_V21)
1766 case MBEDTLS_RSA_PKCS_V21:
1767 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001768 olen, input, output,
1769 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001770#endif
1771
1772 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001774 }
1775}
1776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001778/*
1779 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001782 int (*f_rng)(void *, unsigned char *, size_t),
1783 void *p_rng,
1784 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001786 unsigned int hashlen,
1787 const unsigned char *hash,
1788 unsigned char *sig )
1789{
1790 size_t olen;
1791 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001793 unsigned int slen, hlen, offset = 0;
1794 int ret;
1795 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 const mbedtls_md_info_t *md_info;
1797 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1800 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001801
1802 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001804
1805 olen = ctx->len;
1806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001808 {
Simon Butcher02037452016-03-01 21:19:12 +00001809 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001811 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001815 }
1816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001818 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001822 slen = hlen;
1823
1824 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001826
1827 memset( sig, 0, olen );
1828
Simon Butcher02037452016-03-01 21:19:12 +00001829 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001830 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001832
Simon Butcher02037452016-03-01 21:19:12 +00001833 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001834 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001835 p += olen - hlen * 2 - 2;
1836 *p++ = 0x01;
1837 memcpy( p, salt, slen );
1838 p += slen;
1839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001841 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1842 {
1843 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001844 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001845 return( ret );
1846 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001847
Simon Butcher02037452016-03-01 21:19:12 +00001848 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 mbedtls_md_starts( &md_ctx );
1850 mbedtls_md_update( &md_ctx, p, 8 );
1851 mbedtls_md_update( &md_ctx, hash, hashlen );
1852 mbedtls_md_update( &md_ctx, salt, slen );
1853 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001854 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001855
Simon Butcher02037452016-03-01 21:19:12 +00001856 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001857 if( msb % 8 == 0 )
1858 offset = 1;
1859
Simon Butcher02037452016-03-01 21:19:12 +00001860 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001861 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001864
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001865 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001866 sig[0] &= 0xFF >> ( olen * 8 - msb );
1867
1868 p += hlen;
1869 *p++ = 0xBC;
1870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 return( ( mode == MBEDTLS_RSA_PUBLIC )
1872 ? mbedtls_rsa_public( ctx, sig, sig )
1873 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001874}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001878/*
1879 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1880 */
1881/*
1882 * Do an RSA operation to sign the message digest
1883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001885 int (*f_rng)(void *, unsigned char *, size_t),
1886 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001887 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001889 unsigned int hashlen,
1890 const unsigned char *hash,
1891 unsigned char *sig )
1892{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001893 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001894 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001895 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001896 unsigned char *sig_try = NULL, *verif = NULL;
1897 size_t i;
1898 unsigned char diff;
1899 volatile unsigned char diff_no_optimize;
1900 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1903 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001904
1905 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001906 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001911 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1915 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001916
Paul Bakkerc70b9822013-04-07 22:00:46 +02001917 nb_pad -= 10 + oid_size;
1918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001920 }
1921
Paul Bakkerc70b9822013-04-07 22:00:46 +02001922 nb_pad -= hashlen;
1923
Paul Bakkerb3869132013-02-28 17:21:01 +01001924 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001926
1927 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001929 memset( p, 0xFF, nb_pad );
1930 p += nb_pad;
1931 *p++ = 0;
1932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001934 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001935 memcpy( p, hash, hashlen );
1936 }
1937 else
1938 {
1939 /*
1940 * DigestInfo ::= SEQUENCE {
1941 * digestAlgorithm DigestAlgorithmIdentifier,
1942 * digest Digest }
1943 *
1944 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1945 *
1946 * Digest ::= OCTET STRING
1947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001949 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001951 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001953 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001954 memcpy( p, oid, oid_size );
1955 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001957 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001959 *p++ = hashlen;
1960 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001961 }
1962
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001963 if( mode == MBEDTLS_RSA_PUBLIC )
1964 return( mbedtls_rsa_public( ctx, sig, sig ) );
1965
1966 /*
1967 * In order to prevent Lenstra's attack, make the signature in a
1968 * temporary buffer and check it before returning it.
1969 */
1970 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001971 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001972 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1973
Simon Butcher1285ab52016-01-01 21:42:47 +00001974 verif = mbedtls_calloc( 1, ctx->len );
1975 if( verif == NULL )
1976 {
1977 mbedtls_free( sig_try );
1978 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1979 }
1980
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001981 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1982 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1983
1984 /* Compare in constant time just in case */
1985 for( diff = 0, i = 0; i < ctx->len; i++ )
1986 diff |= verif[i] ^ sig[i];
1987 diff_no_optimize = diff;
1988
1989 if( diff_no_optimize != 0 )
1990 {
1991 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1992 goto cleanup;
1993 }
1994
1995 memcpy( sig, sig_try, ctx->len );
1996
1997cleanup:
1998 mbedtls_free( sig_try );
1999 mbedtls_free( verif );
2000
2001 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002002}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002004
2005/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 * Do an RSA operation to sign the message digest
2007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002009 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002010 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002013 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002014 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002015 unsigned char *sig )
2016{
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 switch( ctx->padding )
2018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019#if defined(MBEDTLS_PKCS1_V15)
2020 case MBEDTLS_RSA_PKCS_V15:
2021 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002022 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002023#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025#if defined(MBEDTLS_PKCS1_V21)
2026 case MBEDTLS_RSA_PKCS_V21:
2027 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002028 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002029#endif
2030
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002033 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002034}
2035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002037/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002038 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002041 int (*f_rng)(void *, unsigned char *, size_t),
2042 void *p_rng,
2043 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002045 unsigned int hashlen,
2046 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002048 int expected_salt_len,
2049 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002050{
Paul Bakker23986e52011-04-24 08:57:21 +00002051 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002052 size_t siglen;
2053 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002055 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002056 unsigned int hlen;
2057 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 const mbedtls_md_info_t *md_info;
2059 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002060 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2063 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002064
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 siglen = ctx->len;
2066
Paul Bakker27fdf462011-06-09 13:55:13 +00002067 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2071 ? mbedtls_rsa_public( ctx, sig, buf )
2072 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002073
2074 if( ret != 0 )
2075 return( ret );
2076
2077 p = buf;
2078
Paul Bakkerb3869132013-02-28 17:21:01 +01002079 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 {
Simon Butcher02037452016-03-01 21:19:12 +00002084 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002086 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002090 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002093 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002097 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002098
Paul Bakkerb3869132013-02-28 17:21:01 +01002099 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002100
Simon Butcher02037452016-03-01 21:19:12 +00002101 /*
2102 * Note: EMSA-PSS verification is over the length of N - 1 bits
2103 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002104 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002105
Simon Butcher02037452016-03-01 21:19:12 +00002106 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002107 if( msb % 8 == 0 )
2108 {
2109 p++;
2110 siglen -= 1;
2111 }
2112 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002116 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2117 {
2118 mbedtls_md_free( &md_ctx );
2119 return( ret );
2120 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002121
Paul Bakkerb3869132013-02-28 17:21:01 +01002122 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002123
Paul Bakkerb3869132013-02-28 17:21:01 +01002124 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002125
Paul Bakker4de44aa2013-12-31 11:43:01 +01002126 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002127 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002128
Paul Bakkerb3869132013-02-28 17:21:01 +01002129 if( p == buf + siglen ||
2130 *p++ != 0x01 )
2131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 mbedtls_md_free( &md_ctx );
2133 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002134 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002135
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002136 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002137 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002140 slen != (size_t) expected_salt_len )
2141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 mbedtls_md_free( &md_ctx );
2143 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002144 }
2145
Simon Butcher02037452016-03-01 21:19:12 +00002146 /*
2147 * Generate H = Hash( M' )
2148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 mbedtls_md_starts( &md_ctx );
2150 mbedtls_md_update( &md_ctx, zeros, 8 );
2151 mbedtls_md_update( &md_ctx, hash, hashlen );
2152 mbedtls_md_update( &md_ctx, p, slen );
2153 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002156
Paul Bakkerb3869132013-02-28 17:21:01 +01002157 if( memcmp( p + slen, result, hlen ) == 0 )
2158 return( 0 );
2159 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002161}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002162
2163/*
2164 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002167 int (*f_rng)(void *, unsigned char *, size_t),
2168 void *p_rng,
2169 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002171 unsigned int hashlen,
2172 const unsigned char *hash,
2173 const unsigned char *sig )
2174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2176 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002177 : md_alg;
2178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002180 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002182 sig ) );
2183
2184}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002188/*
2189 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002192 int (*f_rng)(void *, unsigned char *, size_t),
2193 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002194 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002196 unsigned int hashlen,
2197 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002198 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002199{
2200 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002201 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002202 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 mbedtls_md_type_t msg_md_alg;
2204 const mbedtls_md_info_t *md_info;
2205 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002206 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002210
2211 siglen = ctx->len;
2212
2213 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2217 ? mbedtls_rsa_public( ctx, sig, buf )
2218 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002219
2220 if( ret != 0 )
2221 return( ret );
2222
2223 p = buf;
2224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2226 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002227
2228 while( *p != 0 )
2229 {
2230 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002232 p++;
2233 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002234 p++; /* skip 00 byte */
2235
2236 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2237 if( p - buf < 11 )
2238 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002239
2240 len = siglen - ( p - buf );
2241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002243 {
2244 if( memcmp( p, hash, hashlen ) == 0 )
2245 return( 0 );
2246 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 }
2249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002251 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2253 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002254
2255 end = p + len;
2256
Simon Butcher02037452016-03-01 21:19:12 +00002257 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002258 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2259 * Insist on 2-byte length tags, to protect against variants of
2260 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002261 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002262 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2264 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2265 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002266 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002268
Gilles Peskinee7e76502017-05-04 12:48:39 +02002269 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2271 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2272 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002273 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002275
Gilles Peskinee7e76502017-05-04 12:48:39 +02002276 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2278 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002279 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002280 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002281
2282 oid.p = p;
2283 p += oid.len;
2284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2286 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002287
2288 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002290
2291 /*
2292 * assume the algorithm parameters must be NULL
2293 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002294 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2296 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002297 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002299
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002300 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002301 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2302 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002303 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002305
2306 if( memcmp( p, hash, hashlen ) != 0 )
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 p += hashlen;
2310
2311 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002313
2314 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002315}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002317
2318/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002319 * Do an RSA operation and check the message digest
2320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002322 int (*f_rng)(void *, unsigned char *, size_t),
2323 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002324 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002326 unsigned int hashlen,
2327 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002328 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002329{
2330 switch( ctx->padding )
2331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332#if defined(MBEDTLS_PKCS1_V15)
2333 case MBEDTLS_RSA_PKCS_V15:
2334 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002335 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002336#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338#if defined(MBEDTLS_PKCS1_V21)
2339 case MBEDTLS_RSA_PKCS_V21:
2340 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002341 hashlen, hash, sig );
2342#endif
2343
2344 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002346 }
2347}
2348
2349/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002350 * Copy the components of an RSA key
2351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002353{
2354 int ret;
2355
2356 dst->ver = src->ver;
2357 dst->len = src->len;
2358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2360 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2363 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2364 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
2365 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2366 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2367 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
2370 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2371 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2374 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002375
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002376 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002377 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002378
2379cleanup:
2380 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002382
2383 return( ret );
2384}
2385
2386/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 * Free the components of an RSA key
2388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002390{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
2392 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
2393 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
2394 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
2395 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#if defined(MBEDTLS_THREADING_C)
2398 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002399#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002400}
2401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002404#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
2406/*
2407 * Example RSA-1024 keypair, for test purposes
2408 */
2409#define KEY_LEN 128
2410
2411#define RSA_N "9292758453063D803DD603D5E777D788" \
2412 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2413 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2414 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2415 "93A89813FBF3C4F8066D2D800F7C38A8" \
2416 "1AE31942917403FF4946B0A83D3D3E05" \
2417 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2418 "5E94BB77B07507233A0BC7BAC8F90F79"
2419
2420#define RSA_E "10001"
2421
2422#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2423 "66CA472BC44D253102F8B4A9D3BFA750" \
2424 "91386C0077937FE33FA3252D28855837" \
2425 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2426 "DF79C5CE07EE72C7F123142198164234" \
2427 "CABB724CF78B8173B9F880FC86322407" \
2428 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2429 "071513A1E85B5DFA031F21ECAE91A34D"
2430
2431#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2432 "2C01CAD19EA484A87EA4377637E75500" \
2433 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2434 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2435
2436#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2437 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2438 "910E4168387E3C30AA1E00C339A79508" \
2439 "8452DD96A9A5EA5D9DCA68DA636032AF"
2440
2441#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2442 "3C94D22288ACD763FD8E5600ED4A702D" \
2443 "F84198A5F06C2E72236AE490C93F07F8" \
2444 "3CC559CD27BC2D1CA488811730BB5725"
2445
2446#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2447 "D8AAEA56749EA28623272E4F7D0592AF" \
2448 "7C1F1313CAC9471B5C523BFE592F517B" \
2449 "407A1BD76C164B93DA2D32A383E58357"
2450
2451#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2452 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2453 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2454 "A74206CEC169D74BF5A8C50D6F48EA08"
2455
2456#define PT_LEN 24
2457#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2458 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002461static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002462{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002463#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002464 size_t i;
2465
Paul Bakker545570e2010-07-18 09:00:25 +00002466 if( rng_state != NULL )
2467 rng_state = NULL;
2468
Paul Bakkera3d195c2011-11-27 21:07:34 +00002469 for( i = 0; i < len; ++i )
2470 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002471#else
2472 if( rng_state != NULL )
2473 rng_state = NULL;
2474
2475 arc4random_buf( output, len );
2476#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002477
Paul Bakkera3d195c2011-11-27 21:07:34 +00002478 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002481
Paul Bakker5121ce52009-01-03 21:22:43 +00002482/*
2483 * Checkup routine
2484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002487 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002489 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 unsigned char rsa_plaintext[PT_LEN];
2492 unsigned char rsa_decrypted[PT_LEN];
2493 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002495 unsigned char sha1sum[20];
2496#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Hanno Becker3a701162017-08-22 13:52:43 +01002498 mbedtls_mpi K;
2499
2500 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Hanno Becker3a701162017-08-22 13:52:43 +01002503 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2504 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2505 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2506 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2507 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2508 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2509 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2510 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2511 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2512 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2513
2514 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
2516 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2520 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 {
2522 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
2525 return( 1 );
2526 }
2527
Hanno Becker3a701162017-08-22 13:52:43 +01002528 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2529 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2530
2531 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2532 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2533
2534 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2535 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2536
Paul Bakker5121ce52009-01-03 21:22:43 +00002537 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
2540 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 rsa_plaintext, rsa_ciphertext ) != 0 )
2544 {
2545 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
2548 return( 1 );
2549 }
2550
2551 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002555 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002556 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 {
2558 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
2561 return( 1 );
2562 }
2563
2564 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2565 {
2566 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
2569 return( 1 );
2570 }
2571
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002572 if( verbose != 0 )
2573 mbedtls_printf( "passed\n" );
2574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002577 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 sha1sum, rsa_ciphertext ) != 0 )
2583 {
2584 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
2587 return( 1 );
2588 }
2589
2590 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 sha1sum, rsa_ciphertext ) != 0 )
2595 {
2596 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598
2599 return( 1 );
2600 }
2601
2602 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002603 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002606 if( verbose != 0 )
2607 mbedtls_printf( "\n" );
2608
Paul Bakker3d8fb632014-04-17 12:42:41 +02002609cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002610 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 mbedtls_rsa_free( &rsa );
2612#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002613 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002615 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616}
2617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#endif /* MBEDTLS_RSA_C */