blob: 9a111b75c58513be6e0688204dedc265590547f6 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000022 * The following sources were referenced in the design of this implementation
23 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000024 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000025 * [1] A method for obtaining digital signatures and public-key cryptosystems
26 * R Rivest, A Shamir, and L Adleman
27 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
28 *
29 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
30 * Menezes, van Oorschot and Vanstone
31 *
Janos Follathe81102e2017-03-22 13:38:28 +000032 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
33 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
34 * Stefan Mangard
35 * https://arxiv.org/abs/1702.08719v2
36 *
Paul Bakker5121ce52009-01-03 21:22:43 +000037 */
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020041#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/rsa.h"
48#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#else
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020065#define mbedtls_calloc calloc
66#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#endif
68
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010069/* Implementation that should never be optimized out by the compiler */
70static void mbedtls_zeroize( void *v, size_t n ) {
71 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
72}
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010075 * Context-independent RSA helper functions.
76 *
77 * The following three functions
78 * - mbedtls_rsa_deduce_moduli
79 * - mbedtls_rsa_deduce_private
80 * - mbedtls_rsa_check_params
81 * are helper functions operating on the core RSA parameters
82 * (represented as MPI's). They do not use the RSA context structure
83 * and therefore need not be replaced when providing an alternative
84 * RSA implementation.
85 *
86 * Their purpose is to provide common MPI operations in the context
87 * of RSA that can be easily shared across multiple implementations.
88 */
89
90/*
91 * mbedtls_rsa_deduce_moduli
92 *
93 * Given the modulus N=PQ and a pair of public and private
94 * exponents E and D, respectively, factor N.
95 *
96 * Setting F := lcm(P-1,Q-1), the idea is as follows:
97 *
98 * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
99 * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
100 * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
101 * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
102 * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
103 * factors of N.
104 *
105 * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
106 * construction still applies since (-)^K is the identity on the set of
107 * roots of 1 in Z/NZ.
108 *
109 * The public and private key primitives (-)^E and (-)^D are mutually inverse
110 * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
111 * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
112 * Splitting L = 2^t * K with K odd, we have
113 *
114 * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
115 *
116 * so (F / 2) * K is among the numbers
117 *
118 * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
119 *
120 * where ord is the order of 2 in (DE - 1).
121 * We can therefore iterate through these numbers apply the construction
122 * of (a) and (b) above to attempt to factor N.
123 *
124 */
125int mbedtls_rsa_deduce_moduli( mbedtls_mpi *N, mbedtls_mpi *D, mbedtls_mpi *E,
126 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
127 mbedtls_mpi *P, mbedtls_mpi *Q )
128{
129 /* Implementation note:
130 *
131 * Space-efficiency is given preference over time-efficiency here:
132 * several calculations are done in place and temporarily change
133 * the values of D and E.
134 *
135 * Specifically, D is replaced the largest odd divisor of DE - 1
136 * throughout the calculations.
137 */
138
139 int ret = 0;
140
141 uint16_t attempt; /* Number of current attempt */
142 uint16_t iter; /* Number of squares computed in the current attempt */
143
144 uint16_t bitlen_half; /* Half the bitsize of the modulus N */
145 uint16_t order; /* Order of 2 in DE - 1 */
146
147 mbedtls_mpi K; /* Temporary used for two purposes:
148 * - During factorization attempts, stores a andom integer
149 * in the range of [0,..,N]
150 * - During verification, holding intermediate results.
151 */
152
153 if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
154 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
155
156 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
157 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
158 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
159 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
160 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
161 {
162 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
163 }
164
165 /*
166 * Initializations and temporary changes
167 */
168
169 mbedtls_mpi_init( &K );
170 mbedtls_mpi_init( P );
171 mbedtls_mpi_init( Q );
172
173 /* Replace D by DE - 1 */
174 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( D, D, E ) );
175 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( D, D, 1 ) );
176
177 if( ( order = mbedtls_mpi_lsb( D ) ) == 0 )
178 {
179 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
180 goto cleanup;
181 }
182
183 /* After this operation, D holds the largest odd divisor
184 * of DE - 1 for the original values of D and E. */
185 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( D, order ) );
186
187 /* This is used to generate a few numbers around N / 2
188 * if no PRNG is provided. */
189 if( f_rng == NULL )
190 bitlen_half = mbedtls_mpi_bitlen( N ) / 2;
191
192 /*
193 * Actual work
194 */
195
196 for( attempt = 0; attempt < 30; ++attempt )
197 {
198 /* Generate some number in [0,N], either randomly
199 * if a PRNG is given, or try numbers around N/2 */
200 if( f_rng != NULL )
201 {
202 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &K,
203 mbedtls_mpi_size( N ),
204 f_rng, p_rng ) );
205 }
206 else
207 {
208 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &K, 1 ) ) ;
209 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &K, bitlen_half ) ) ;
210 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, attempt + 1 ) );
211 }
212
213 /* Check if gcd(K,N) = 1 */
214 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
215 if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
216 continue;
217
218 /* Go through K^X + 1, K^(2X) + 1, K^(4X) + 1, ...
219 * and check whether they have nontrivial GCD with N. */
220 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, D, N,
221 Q /* temporarily use Q for storing Montgomery
222 * multiplication helper values */ ) );
223
224 for( iter = 1; iter < order; ++iter )
225 {
226 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
227 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
228
229 if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
230 mbedtls_mpi_cmp_mpi( P, N ) == -1 )
231 {
232 /*
233 * Have found a nontrivial divisor P of N.
234 * Set Q := N / P and verify D, E.
235 */
236
237 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, &K, N, P ) );
238
239 /*
240 * Verify that DE - 1 is indeed a multiple of
241 * lcm(P-1, Q-1), i.e. that it's a multiple of both
242 * P-1 and Q-1.
243 */
244
245 /* Restore DE - 1 and temporarily replace P, Q by P-1, Q-1. */
246 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( D, order ) );
247 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) );
248 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) );
249
250 /* Compute DE-1 mod P-1 */
251 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, D, P ) );
252 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
253 {
254 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
255 goto cleanup;
256 }
257
258 /* Compute DE-1 mod Q-1 */
259 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, D, Q ) );
260 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
261 {
262 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
263 goto cleanup;
264 }
265
266 /*
267 * All good, restore P, Q and D and return.
268 */
269
270 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) );
271 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) );
272 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( D, D, 1 ) );
273 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( D, NULL, D, E ) );
274
275 goto cleanup;
276 }
277
278 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
279 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
280 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
281 }
282 }
283
284 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
285
286cleanup:
287
288 mbedtls_mpi_free( &K );
289 return( ret );
290}
291
292/*
293 * Given P, Q and the public exponent E, deduce D.
294 * This is essentially a modular inversion.
295 */
296
297int mbedtls_rsa_deduce_private( mbedtls_mpi *P, mbedtls_mpi *Q,
298 mbedtls_mpi *D, mbedtls_mpi *E )
299{
300 int ret = 0;
301 mbedtls_mpi K;
302
303 if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
304 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
305
306 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
307 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
308 mbedtls_mpi_cmp_int( E, 0 ) == 0 )
309 {
310 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
311 }
312
313 mbedtls_mpi_init( &K );
314
315 /* Temporarily replace P and Q by P-1 and Q-1, respectively. */
316 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) );
317 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) );
318
319 /* Temporarily compute the gcd(P-1, Q-1) in D. */
320 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, P, Q ) );
321
322 /* Compute LCM(P-1, Q-1) in K */
323 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
324 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
325
326 /* Compute modular inverse of E in LCM(P-1, Q-1) */
327 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
328
329 /* Restore P and Q. */
330 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) );
331 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) );
332
333 /* Double-check result */
334 MBEDTLS_MPI_CHK( mbedtls_rsa_check_params( NULL, P, Q, D, E, NULL, NULL ) );
335
336cleanup:
337
338 mbedtls_mpi_free( &K );
339
340 return( ret );
341}
342
343/*
344 * Check that core RSA parameters are sane.
345 *
346 * Note that the inputs are not declared const and may be
347 * altered on an unsuccessful run.
348 */
349
350int mbedtls_rsa_check_params( mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
351 mbedtls_mpi *D, mbedtls_mpi *E,
352 int (*f_rng)(void *, unsigned char *, size_t),
353 void *p_rng )
354{
355 int ret = 0;
356 mbedtls_mpi K;
357
358 mbedtls_mpi_init( &K );
359
360 /*
361 * Step 1: If PRNG provided, check that P and Q are prime
362 */
363
364 if( f_rng != NULL && P != NULL &&
365 ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
366 {
367 goto cleanup;
368 }
369
370 if( f_rng != NULL && Q != NULL &&
371 ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
372 {
373 goto cleanup;
374 }
375
376 /*
377 * Step 2: Check that N = PQ
378 */
379
380 if( P != NULL && Q != NULL && N != NULL )
381 {
382 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
383 if( mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
384 {
385 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
386 goto cleanup;
387 }
388 }
389
390 /*
391 * Step 3: Check that D, E are inverse modulo P-1 and Q-1
392 */
393
394 if( P != NULL && Q != NULL && D != NULL && E != NULL )
395 {
396 /* Temporarily replace P, Q by P-1, Q-1. */
397 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) );
398 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) );
399
400 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
401 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
402
403 /* Compute DE-1 mod P-1 */
404 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
405 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
406 {
407 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
408 goto cleanup;
409 }
410
411 /* Compute DE-1 mod Q-1 */
412 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, Q ) );
413 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
414 {
415 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
416 goto cleanup;
417 }
418
419 /* Restore P, Q. */
420 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) );
421 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) );
422 }
423
424cleanup:
425
426 mbedtls_mpi_free( &K );
427
428 return( ret );
429}
430
431int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
432 const mbedtls_mpi *D, mbedtls_mpi *DP,
433 mbedtls_mpi *DQ, mbedtls_mpi *QP )
434{
435 int ret = 0;
436 mbedtls_mpi K;
437 mbedtls_mpi_init( &K );
438
439 if( DP != NULL )
440 {
441 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
442 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
443 }
444
445 if( DQ != NULL )
446 {
447 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
448 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
449 }
450
451 if( QP != NULL )
452 {
453 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
454 }
455
456cleanup:
457 mbedtls_mpi_free( &K );
458
459 return( ret );
460}
461
Hanno Becker617c1ae2017-08-23 14:11:24 +0100462
463/*
464 * Default RSA interface implementation
465 */
466
467
468int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
469 const mbedtls_mpi *N,
470 const mbedtls_mpi *P, const mbedtls_mpi *Q,
471 const mbedtls_mpi *D, const mbedtls_mpi *E )
472{
473 int ret;
474
475 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
476 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
477 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
478 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
479 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
480 {
481 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
482 }
483
484 if( N != NULL )
485 ctx->len = mbedtls_mpi_size( &ctx->N );
486
487 return( 0 );
488}
489
490int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
491 unsigned char *N, size_t N_len,
492 unsigned char *P, size_t P_len,
493 unsigned char *Q, size_t Q_len,
494 unsigned char *D, size_t D_len,
495 unsigned char *E, size_t E_len )
496{
497 int ret;
498
499 if( N != NULL )
500 {
501 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
502 ctx->len = mbedtls_mpi_size( &ctx->N );
503 }
504
505 if( P != NULL )
506 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
507
508 if( Q != NULL )
509 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
510
511 if( D != NULL )
512 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
513
514 if( E != NULL )
515 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
516
517cleanup:
518
519 if( ret != 0 )
520 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
521
522 return( 0 );
523}
524
525int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
526 int (*f_rng)(void *, unsigned char *, size_t),
527 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100528{
529 int ret = 0;
530
Hanno Becker617c1ae2017-08-23 14:11:24 +0100531 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
532 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
533 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
534 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
535 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100536
Hanno Becker617c1ae2017-08-23 14:11:24 +0100537 /*
538 * Check whether provided parameters are enough
539 * to deduce all others. The following incomplete
540 * parameter sets for private keys are supported:
541 *
542 * (1) P, Q missing.
543 * (2) D and potentially N missing.
544 *
545 */
546 const int complete = have_N && have_P && have_Q && have_D && have_E;
547 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
548 const int d_missing = have_P && have_Q && !have_D && have_E;
549 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100550
Hanno Becker617c1ae2017-08-23 14:11:24 +0100551 const int is_priv = complete || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100552
Hanno Becker617c1ae2017-08-23 14:11:24 +0100553 if( !is_priv && !is_pub )
554 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
555
556 /*
557 * Step 1: Deduce and verify all core parameters.
558 */
559
560 if( pq_missing )
561 {
562 /* This includes sanity checking of core parameters,
563 * so no further checks necessary. */
564 ret = mbedtls_rsa_deduce_moduli( &ctx->N, &ctx->D, &ctx->E,
565 f_rng, p_rng,
566 &ctx->P, &ctx->Q );
567 if( ret != 0 )
568 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
569
570 }
571 else if( d_missing )
572 {
573 /* If a PRNG is provided, check if P, Q are prime. */
574 if( f_rng != NULL &&
575 ( ( ret = mbedtls_mpi_is_prime( &ctx->P, f_rng, p_rng ) ) != 0 ||
576 ( ret = mbedtls_mpi_is_prime( &ctx->Q, f_rng, p_rng ) ) != 0 ) )
577 {
578 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
579 }
580
581 /* Compute N if missing. */
582 if( !have_N &&
583 ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) ) != 0 )
584 {
585 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
586 }
587
588 /* Deduce private exponent. This includes double-checking of the result,
589 * so together with the primality test above all core parameters are
590 * guaranteed to be sane if this call succeeds. */
591 if( ( ret = mbedtls_rsa_deduce_private( &ctx->P, &ctx->Q,
592 &ctx->D, &ctx->E ) ) != 0 )
593 {
594 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
595 }
596 }
597 else if( complete )
598 {
599 /* Check complete set of imported core parameters. */
600 if( ( ret = mbedtls_rsa_check_params( &ctx->N, &ctx->P, &ctx->Q,
601 &ctx->D, &ctx->E,
602 f_rng, p_rng ) ) != 0 )
603 {
604 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
605 }
606 }
607
608 /* In the remaining case of a public key, there's nothing to check for. */
609
610 /*
611 * Step 2: Deduce all additional parameters specific
612 * to our current RSA implementaiton.
613 */
614
Hanno Becker23344b52017-08-23 07:43:27 +0100615#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100616 if( is_priv )
617 {
618 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
619 &ctx->DP, &ctx->DQ, &ctx->QP );
620 if( ret != 0 )
621 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
622 }
Hanno Becker23344b52017-08-23 07:43:27 +0100623#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100624
625 /*
626 * Step 3: Double check
627 */
628
629 if( is_priv )
630 {
631 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
632 return( ret );
633 }
634 else
635 {
636 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
637 return( ret );
638 }
639
640 return( 0 );
641}
642
643/*
644 * Check if CRT parameters match RSA context.
645 * This has to be implemented even if CRT is not used,
646 * in order to be able to validate DER encoded RSA keys,
647 * which always contain CRT parameters.
648 */
649int mbedtls_rsa_check_crt( mbedtls_rsa_context *ctx, mbedtls_mpi *DP,
650 mbedtls_mpi *DQ, mbedtls_mpi *QP )
651{
Hanno Becker23344b52017-08-23 07:43:27 +0100652 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100653
Hanno Becker23344b52017-08-23 07:43:27 +0100654 /* Check if key is private or public */
655 const int is_priv =
656 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
657 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
658 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
659 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
660 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
661
662 if( !is_priv )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100663 {
664 /* Checking optional parameters only makes sense for private keys. */
665 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
666 }
667
Hanno Becker23344b52017-08-23 07:43:27 +0100668#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100669 if( ( DP != NULL && mbedtls_mpi_cmp_mpi( DP, &ctx->DP ) != 0 ) ||
670 ( DQ != NULL && mbedtls_mpi_cmp_mpi( DQ, &ctx->DQ ) != 0 ) ||
671 ( QP != NULL && mbedtls_mpi_cmp_mpi( QP, &ctx->QP ) != 0 ) )
672 {
673 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
674 }
Hanno Becker23344b52017-08-23 07:43:27 +0100675#else /* MBEDTLS_RSA_NO_CRT */
676
677 /*
678 * Check that DP, DQ and QP are in accordance with core parameters.
679 * (1) Check that DP - P == 0 mod P - 1
680 * (2) Check that DQ - Q == 0 mod Q - 1
681 * (3) Check that QP * P - 1 == 0 mod P
682
683 * Alternative implementation also not using DP, DQ and QP
684 * should be able to reuse this codepath.
685 */
686
687 /* Check (1) */
688 if( DP != NULL )
689 {
690 /* Temporarily replace P by P-1 and compute DP - D mod P-1 */
691 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
692 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( DP, DP, &ctx->D ) );
693 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, DP, &ctx->P ) );
694 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
695
696 if( mbedtls_mpi_cmp_int( DP, 0 ) != 0 )
697 {
698 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
699 }
700 }
701
702 /* Check (1) */
703 if( DQ != NULL )
704 {
705 /* Temporarily replace Q by Q-1 and compute DQ - D mod Q-1 */
706 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
707 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( DQ, DQ, &ctx->D ) );
708 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, DQ, &ctx->Q ) );
709 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
710
711 if( mbedtls_mpi_cmp_int( DQ, 0 ) != 0 )
712 {
713 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
714 }
715 }
716
717 /* Check (3) */
718 if( QP != NULL )
719 {
720 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( QP, QP, &ctx->Q ) );
721 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( QP, QP, 1 ) );
722 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( QP, QP, &ctx->P ) );
723 if( mbedtls_mpi_cmp_int( QP, 0 ) != 0 )
724 {
725 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
726 }
727 }
728
729cleanup:
730
731#endif
732
733 if( ret != 0 )
734 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100735
736 return( 0 );
737}
738
739int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
740 unsigned char *N, size_t N_len,
741 unsigned char *P, size_t P_len,
742 unsigned char *Q, size_t Q_len,
743 unsigned char *D, size_t D_len,
744 unsigned char *E, size_t E_len )
745{
746 int ret = 0;
747
748 /* Check if key is private or public */
749 const int is_priv =
750 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
751 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
752 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
753 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
754 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
755
756 if( !is_priv )
757 {
758 /* If we're trying to export private parameters for a public key,
759 * something must be wrong. */
760 if( P != NULL || Q != NULL || D != NULL )
761 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
762
763 }
764
765 if( N != NULL )
766 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
767
768 if( P != NULL )
769 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
770
771 if( Q != NULL )
772 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
773
774 if( D != NULL )
775 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
776
777 if( E != NULL )
778 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100779
780cleanup:
781
782 return( ret );
783}
784
Hanno Becker617c1ae2017-08-23 14:11:24 +0100785int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
786 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
787 mbedtls_mpi *D, mbedtls_mpi *E )
788{
789 int ret;
790
791 /* Check if key is private or public */
792 int is_priv =
793 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
794 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
795 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
796 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
797 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
798
799 if( !is_priv )
800 {
801 /* If we're trying to export private parameters for a public key,
802 * something must be wrong. */
803 if( P != NULL || Q != NULL || D != NULL )
804 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
805
806 }
807
808 /* Export all requested core parameters. */
809
810 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
811 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
812 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
813 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
814 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
815 {
816 return( ret );
817 }
818
819 return( 0 );
820}
821
822/*
823 * Export CRT parameters
824 * This must also be implemented if CRT is not used, for being able to
825 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
826 * can be used in this case.
827 */
828int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
829 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
830{
831 int ret;
832
833 /* Check if key is private or public */
834 int is_priv =
835 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
836 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
837 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
838 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
839 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
840
841 if( !is_priv )
842 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
843
844 /* Export all requested blinding parameters. */
845
846 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
847 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
848 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
849 {
850 return( ret );
851 }
852
853 return( 0 );
854}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100855
856/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 * Initialize an RSA context
858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000861 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000862{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_THREADING_C)
868 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200869#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000870}
871
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100872/*
873 * Set padding for an existing RSA context
874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100876{
877 ctx->padding = padding;
878 ctx->hash_id = hash_id;
879}
880
Hanno Becker617c1ae2017-08-23 14:11:24 +0100881/*
882 * Get length in bytes of RSA modulus
883 */
884
885size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
886{
887 return( mbedtls_mpi_size( &ctx->N ) );
888}
889
890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
893/*
894 * Generate an RSA keypair
895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000897 int (*f_rng)(void *, unsigned char *, size_t),
898 void *p_rng,
899 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000900{
901 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000903
Paul Bakker21eb2802010-08-16 11:10:02 +0000904 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000906
Janos Follathef441782016-09-21 13:18:12 +0100907 if( nbits % 2 )
908 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
909
910 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath10c575b2016-02-23 14:42:48 +0000911 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
913 /*
914 * find primes P and Q with Q < P so that:
915 * GCD( E, (P-1)*(Q-1) ) == 1
916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
919 do
920 {
Janos Follath10c575b2016-02-23 14:42:48 +0000921 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000922 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000923
Janos Follathef441782016-09-21 13:18:12 +0100924 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000925 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 continue;
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200931 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000932 continue;
933
Janos Follathef441782016-09-21 13:18:12 +0100934 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
935 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
938 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
939 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
940 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000943
944 /*
945 * D = E^-1 mod ((P-1)*(Q-1))
946 * DP = D mod (P - 1)
947 * DQ = D mod (Q - 1)
948 * QP = Q^-1 mod P
949 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
951 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
952 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
953 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200955 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
Hanno Becker83aad1f2017-08-23 06:45:10 +0100957 /* Double-check */
958 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
959
Paul Bakker5121ce52009-01-03 21:22:43 +0000960cleanup:
961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
964 if( ret != 0 )
965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_rsa_free( ctx );
967 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 }
969
Paul Bakker48377d92013-08-30 12:06:24 +0200970 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000971}
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000974
975/*
976 * Check a public RSA key
977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000979{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000980 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000982
Paul Bakker48377d92013-08-30 12:06:24 +0200983 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200987 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
988 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000990
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200991 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
993 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
995 return( 0 );
996}
997
998/*
999 * Check a private RSA key
1000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001002{
1003 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +00001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001007 return( ret );
1008
Paul Bakker37940d9f2009-07-10 22:38:58 +00001009 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +00001011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
1013 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
1014 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
1015 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
1018 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
1019 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1020 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1021 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
1022 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
1025 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
1026 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +00001027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
1029 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
1030 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +00001031 /*
1032 * Check for a valid PKCS1v2 private key
1033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
1035 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
1036 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
1037 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
1038 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
1039 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
1040 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 }
Paul Bakker48377d92013-08-30 12:06:24 +02001044
Paul Bakker5121ce52009-01-03 21:22:43 +00001045cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
1047 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
1048 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
1049 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +00001052 return( ret );
1053
Paul Bakker6c591fa2011-05-05 11:49:20 +00001054 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001056
1057 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001058}
1059
1060/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001061 * Check if contexts holding a public and private key match
1062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1066 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001069 }
1070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1072 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001075 }
1076
1077 return( 0 );
1078}
1079
1080/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 * Do an RSA public key operation
1082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001084 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 unsigned char *output )
1086{
Paul Bakker23986e52011-04-24 08:57:21 +00001087 int ret;
1088 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001092
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001093#if defined(MBEDTLS_THREADING_C)
1094 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1095 return( ret );
1096#endif
1097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001102 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1103 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 }
1105
1106 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1108 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
1110cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001112 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1113 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001114#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
1118 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
1121 return( 0 );
1122}
1123
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001124/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001125 * Generate or update blinding values, see section 10 of:
1126 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001127 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001128 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001129 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001130static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001131 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1132{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001133 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001134
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001135 if( ctx->Vf.p != NULL )
1136 {
1137 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1139 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1140 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1141 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001142
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001143 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001144 }
1145
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001146 /* Unblinding value: Vf = random number, invertible mod N */
1147 do {
1148 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1152 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1153 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001154
1155 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1157 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 +02001158
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001159
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001160cleanup:
1161 return( ret );
1162}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001163
Paul Bakker5121ce52009-01-03 21:22:43 +00001164/*
Janos Follathe81102e2017-03-22 13:38:28 +00001165 * Exponent blinding supposed to prevent side-channel attacks using multiple
1166 * traces of measurements to recover the RSA key. The more collisions are there,
1167 * the more bits of the key can be recovered. See [3].
1168 *
1169 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1170 * observations on avarage.
1171 *
1172 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1173 * to make 2^112 observations on avarage.
1174 *
1175 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1176 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1177 * Thus in this sense with 28 byte blinding the security is not reduced by
1178 * side-channel attacks like the one in [3])
1179 *
1180 * This countermeasure does not help if the key recovery is possible with a
1181 * single trace.
1182 */
1183#define RSA_EXPONENT_BLINDING 28
1184
1185/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 * Do an RSA private key operation
1187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001189 int (*f_rng)(void *, unsigned char *, size_t),
1190 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001191 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 unsigned char *output )
1193{
Paul Bakker23986e52011-04-24 08:57:21 +00001194 int ret;
1195 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001197 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001198#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001199 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001200 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001201#else
1202 mbedtls_mpi DP_blind, DQ_blind;
1203 mbedtls_mpi *DP = &ctx->DP;
1204 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001205#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001206
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001207 /* Make sure we have private key info, prevent possible misuse */
1208 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
1209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001212 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1213
1214
1215 if( f_rng != NULL )
1216 {
Janos Follathe81102e2017-03-22 13:38:28 +00001217#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001218 mbedtls_mpi_init( &D_blind );
1219#else
1220 mbedtls_mpi_init( &DP_blind );
1221 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001222#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001223 }
Janos Follathe81102e2017-03-22 13:38:28 +00001224
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001226#if defined(MBEDTLS_THREADING_C)
1227 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1228 return( ret );
1229#endif
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1232 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001234 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1235 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 }
1237
Paul Bakkerf451bac2013-08-30 15:37:02 +02001238 if( f_rng != NULL )
1239 {
1240 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001241 * Blinding
1242 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001243 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001244 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1245 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001247
Janos Follathe81102e2017-03-22 13:38:28 +00001248 /*
1249 * Exponent blinding
1250 */
1251 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1252 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1253
Janos Follathf9203b42017-03-22 15:13:15 +00001254#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001255 /*
1256 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1257 */
1258 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1259 f_rng, p_rng ) );
1260 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1261 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1262 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1263
1264 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001265#else
1266 /*
1267 * DP_blind = ( P - 1 ) * R + DP
1268 */
1269 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1270 f_rng, p_rng ) );
1271 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1272 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1273 &ctx->DP ) );
1274
1275 DP = &DP_blind;
1276
1277 /*
1278 * DQ_blind = ( Q - 1 ) * R + DQ
1279 */
1280 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1281 f_rng, p_rng ) );
1282 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1283 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1284 &ctx->DQ ) );
1285
1286 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001287#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001288 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001291 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001292#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001293 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001294 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 *
1296 * T1 = input ^ dP mod P
1297 * T2 = input ^ dQ mod Q
1298 */
Janos Follathf9203b42017-03-22 15:13:15 +00001299 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1300 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001301
1302 /*
1303 * T = (T1 - T2) * (Q^-1 mod P) mod P
1304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1306 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1307 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
1309 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001310 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1313 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1314#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001315
Paul Bakkerf451bac2013-08-30 15:37:02 +02001316 if( f_rng != NULL )
1317 {
1318 /*
1319 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001320 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001321 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001322 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001324 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
1326 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
1329cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001331 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1332 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001333#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001336 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1337
1338 if( f_rng != NULL )
1339 {
Janos Follathe81102e2017-03-22 13:38:28 +00001340#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001341 mbedtls_mpi_free( &D_blind );
1342#else
1343 mbedtls_mpi_free( &DP_blind );
1344 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001345#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001346 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001347
1348 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
1351 return( 0 );
1352}
1353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001355/**
1356 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1357 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001358 * \param dst buffer to mask
1359 * \param dlen length of destination buffer
1360 * \param src source of the mask generation
1361 * \param slen length of the source buffer
1362 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001363 */
Paul Bakker48377d92013-08-30 12:06:24 +02001364static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001366{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001368 unsigned char counter[4];
1369 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001370 unsigned int hlen;
1371 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001374 memset( counter, 0, 4 );
1375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001377
Simon Butcher02037452016-03-01 21:19:12 +00001378 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001379 p = dst;
1380
1381 while( dlen > 0 )
1382 {
1383 use_len = hlen;
1384 if( dlen < hlen )
1385 use_len = dlen;
1386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 mbedtls_md_starts( md_ctx );
1388 mbedtls_md_update( md_ctx, src, slen );
1389 mbedtls_md_update( md_ctx, counter, 4 );
1390 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001391
1392 for( i = 0; i < use_len; ++i )
1393 *p++ ^= mask[i];
1394
1395 counter[3]++;
1396
1397 dlen -= use_len;
1398 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001399
1400 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001405/*
1406 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001409 int (*f_rng)(void *, unsigned char *, size_t),
1410 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001411 int mode,
1412 const unsigned char *label, size_t label_len,
1413 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001414 const unsigned char *input,
1415 unsigned char *output )
1416{
1417 size_t olen;
1418 int ret;
1419 unsigned char *p = output;
1420 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 const mbedtls_md_info_t *md_info;
1422 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1425 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001426
1427 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001431 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001433
1434 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001436
Simon Butcher02037452016-03-01 21:19:12 +00001437 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001438 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001440
1441 memset( output, 0, olen );
1442
1443 *p++ = 0;
1444
Simon Butcher02037452016-03-01 21:19:12 +00001445 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001446 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001448
1449 p += hlen;
1450
Simon Butcher02037452016-03-01 21:19:12 +00001451 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001453 p += hlen;
1454 p += olen - 2 * hlen - 2 - ilen;
1455 *p++ = 1;
1456 memcpy( p, input, ilen );
1457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001459 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1460 {
1461 mbedtls_md_free( &md_ctx );
1462 return( ret );
1463 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001464
Simon Butcher02037452016-03-01 21:19:12 +00001465 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001466 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1467 &md_ctx );
1468
Simon Butcher02037452016-03-01 21:19:12 +00001469 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001470 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1471 &md_ctx );
1472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 return( ( mode == MBEDTLS_RSA_PUBLIC )
1476 ? mbedtls_rsa_public( ctx, output, output )
1477 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001478}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001482/*
1483 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001486 int (*f_rng)(void *, unsigned char *, size_t),
1487 void *p_rng,
1488 int mode, size_t ilen,
1489 const unsigned char *input,
1490 unsigned char *output )
1491{
1492 size_t nb_pad, olen;
1493 int ret;
1494 unsigned char *p = output;
1495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1497 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001498
Janos Follath1ed9f992016-03-18 11:45:44 +00001499 // We don't check p_rng because it won't be dereferenced here
1500 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001502
1503 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001504
Simon Butcher02037452016-03-01 21:19:12 +00001505 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001506 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001508
1509 nb_pad = olen - 3 - ilen;
1510
1511 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001515
1516 while( nb_pad-- > 0 )
1517 {
1518 int rng_dl = 100;
1519
1520 do {
1521 ret = f_rng( p_rng, p, 1 );
1522 } while( *p == 0 && --rng_dl && ret == 0 );
1523
Simon Butcher02037452016-03-01 21:19:12 +00001524 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001525 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001527
1528 p++;
1529 }
1530 }
1531 else
1532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001534
1535 while( nb_pad-- > 0 )
1536 *p++ = 0xFF;
1537 }
1538
1539 *p++ = 0;
1540 memcpy( p, input, ilen );
1541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 return( ( mode == MBEDTLS_RSA_PUBLIC )
1543 ? mbedtls_rsa_public( ctx, output, output )
1544 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001547
Paul Bakker5121ce52009-01-03 21:22:43 +00001548/*
1549 * Add the message padding, then do an RSA operation
1550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001552 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001553 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001554 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001555 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001556 unsigned char *output )
1557{
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 switch( ctx->padding )
1559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560#if defined(MBEDTLS_PKCS1_V15)
1561 case MBEDTLS_RSA_PKCS_V15:
1562 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001563 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001564#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_PKCS1_V21)
1567 case MBEDTLS_RSA_PKCS_V21:
1568 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001569 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001570#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
1572 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001575}
1576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001578/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001579 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001582 int (*f_rng)(void *, unsigned char *, size_t),
1583 void *p_rng,
1584 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001585 const unsigned char *label, size_t label_len,
1586 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001587 const unsigned char *input,
1588 unsigned char *output,
1589 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001590{
Paul Bakker23986e52011-04-24 08:57:21 +00001591 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001592 size_t ilen, i, pad_len;
1593 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1595 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001596 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 const mbedtls_md_info_t *md_info;
1598 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001599
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001600 /*
1601 * Parameters sanity checks
1602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1604 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
1606 ilen = ctx->len;
1607
Paul Bakker27fdf462011-06-09 13:55:13 +00001608 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001612 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001614
Janos Follathc17cda12016-02-11 11:08:18 +00001615 hlen = mbedtls_md_get_size( md_info );
1616
1617 // checking for integer underflow
1618 if( 2 * hlen + 2 > ilen )
1619 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1620
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001621 /*
1622 * RSA operation
1623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1625 ? mbedtls_rsa_public( ctx, input, buf )
1626 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
1628 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001629 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001631 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001632 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001635 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1636 {
1637 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001638 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001639 }
1640
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001641
1642 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001644
1645 /* seed: Apply seedMask to maskedSeed */
1646 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1647 &md_ctx );
1648
1649 /* DB: Apply dbMask to maskedDB */
1650 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1651 &md_ctx );
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001654
1655 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001656 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001657 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001658 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001659 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001661 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001662
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001663 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001664
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001665 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001666 for( i = 0; i < hlen; i++ )
1667 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001668
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001669 /* Get zero-padding len, but always read till end of buffer
1670 * (minus one, for the 01 byte) */
1671 pad_len = 0;
1672 pad_done = 0;
1673 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1674 {
1675 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001676 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001677 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001678
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001679 p += pad_len;
1680 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001681
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001682 /*
1683 * The only information "leaked" is whether the padding was correct or not
1684 * (eg, no data is copied if it was not correct). This meets the
1685 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1686 * the different error conditions.
1687 */
1688 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001689 {
1690 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1691 goto cleanup;
1692 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001693
Paul Bakker66d5d072014-06-17 16:39:18 +02001694 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001695 {
1696 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1697 goto cleanup;
1698 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001699
1700 *olen = ilen - (p - buf);
1701 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001702 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001703
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001704cleanup:
1705 mbedtls_zeroize( buf, sizeof( buf ) );
1706 mbedtls_zeroize( lhash, sizeof( lhash ) );
1707
1708 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001713/*
1714 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001717 int (*f_rng)(void *, unsigned char *, size_t),
1718 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001719 int mode, size_t *olen,
1720 const unsigned char *input,
1721 unsigned char *output,
1722 size_t output_max_len)
1723{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001724 int ret;
1725 size_t ilen, pad_count = 0, i;
1726 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1730 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001731
1732 ilen = ctx->len;
1733
1734 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1738 ? mbedtls_rsa_public( ctx, input, buf )
1739 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001740
1741 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001742 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001743
1744 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001745 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001746
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001747 /*
1748 * Check and get padding len in "constant-time"
1749 */
1750 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001751
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001752 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001756
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001757 /* Get padding len, but always read till end of buffer
1758 * (minus one, for the 00 byte) */
1759 for( i = 0; i < ilen - 3; i++ )
1760 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001761 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1762 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001763 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001764
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001765 p += pad_count;
1766 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001767 }
1768 else
1769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001771
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001772 /* Get padding len, but always read till end of buffer
1773 * (minus one, for the 00 byte) */
1774 for( i = 0; i < ilen - 3; i++ )
1775 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001776 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001777 pad_count += ( pad_done == 0 );
1778 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001779
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001780 p += pad_count;
1781 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 }
1783
Janos Follathc69fa502016-02-12 13:30:09 +00001784 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001785
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001786 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001787 {
1788 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1789 goto cleanup;
1790 }
Paul Bakker8804f692013-02-28 18:06:26 +01001791
Paul Bakker66d5d072014-06-17 16:39:18 +02001792 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001793 {
1794 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1795 goto cleanup;
1796 }
Paul Bakker060c5682009-01-12 21:48:39 +00001797
Paul Bakker27fdf462011-06-09 13:55:13 +00001798 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001800 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001801
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001802cleanup:
1803 mbedtls_zeroize( buf, sizeof( buf ) );
1804
1805 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001806}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001808
1809/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001810 * Do an RSA operation, then remove the message padding
1811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001813 int (*f_rng)(void *, unsigned char *, size_t),
1814 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001815 int mode, size_t *olen,
1816 const unsigned char *input,
1817 unsigned char *output,
1818 size_t output_max_len)
1819{
1820 switch( ctx->padding )
1821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#if defined(MBEDTLS_PKCS1_V15)
1823 case MBEDTLS_RSA_PKCS_V15:
1824 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001825 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001826#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#if defined(MBEDTLS_PKCS1_V21)
1829 case MBEDTLS_RSA_PKCS_V21:
1830 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001831 olen, input, output,
1832 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001833#endif
1834
1835 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001837 }
1838}
1839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001841/*
1842 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001845 int (*f_rng)(void *, unsigned char *, size_t),
1846 void *p_rng,
1847 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001849 unsigned int hashlen,
1850 const unsigned char *hash,
1851 unsigned char *sig )
1852{
1853 size_t olen;
1854 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001856 unsigned int slen, hlen, offset = 0;
1857 int ret;
1858 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 const mbedtls_md_info_t *md_info;
1860 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1863 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001864
1865 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001867
1868 olen = ctx->len;
1869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001871 {
Simon Butcher02037452016-03-01 21:19:12 +00001872 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001874 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001878 }
1879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001881 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001885 slen = hlen;
1886
1887 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001889
1890 memset( sig, 0, olen );
1891
Simon Butcher02037452016-03-01 21:19:12 +00001892 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001893 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001895
Simon Butcher02037452016-03-01 21:19:12 +00001896 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001897 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001898 p += olen - hlen * 2 - 2;
1899 *p++ = 0x01;
1900 memcpy( p, salt, slen );
1901 p += slen;
1902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001904 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1905 {
1906 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001907 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001908 return( ret );
1909 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001910
Simon Butcher02037452016-03-01 21:19:12 +00001911 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 mbedtls_md_starts( &md_ctx );
1913 mbedtls_md_update( &md_ctx, p, 8 );
1914 mbedtls_md_update( &md_ctx, hash, hashlen );
1915 mbedtls_md_update( &md_ctx, salt, slen );
1916 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001917 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001918
Simon Butcher02037452016-03-01 21:19:12 +00001919 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001920 if( msb % 8 == 0 )
1921 offset = 1;
1922
Simon Butcher02037452016-03-01 21:19:12 +00001923 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001924 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001927
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001928 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001929 sig[0] &= 0xFF >> ( olen * 8 - msb );
1930
1931 p += hlen;
1932 *p++ = 0xBC;
1933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 return( ( mode == MBEDTLS_RSA_PUBLIC )
1935 ? mbedtls_rsa_public( ctx, sig, sig )
1936 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001937}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001941/*
1942 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1943 */
1944/*
1945 * Do an RSA operation to sign the message digest
1946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001948 int (*f_rng)(void *, unsigned char *, size_t),
1949 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001950 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001952 unsigned int hashlen,
1953 const unsigned char *hash,
1954 unsigned char *sig )
1955{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001956 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001957 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001958 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001959 unsigned char *sig_try = NULL, *verif = NULL;
1960 size_t i;
1961 unsigned char diff;
1962 volatile unsigned char diff_no_optimize;
1963 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1966 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001967
1968 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001969 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001974 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1978 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001979
Paul Bakkerc70b9822013-04-07 22:00:46 +02001980 nb_pad -= 10 + oid_size;
1981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001983 }
1984
Paul Bakkerc70b9822013-04-07 22:00:46 +02001985 nb_pad -= hashlen;
1986
Paul Bakkerb3869132013-02-28 17:21:01 +01001987 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001989
1990 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001992 memset( p, 0xFF, nb_pad );
1993 p += nb_pad;
1994 *p++ = 0;
1995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001997 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001998 memcpy( p, hash, hashlen );
1999 }
2000 else
2001 {
2002 /*
2003 * DigestInfo ::= SEQUENCE {
2004 * digestAlgorithm DigestAlgorithmIdentifier,
2005 * digest Digest }
2006 *
2007 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2008 *
2009 * Digest ::= OCTET STRING
2010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002012 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002014 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002016 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002017 memcpy( p, oid, oid_size );
2018 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002020 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002022 *p++ = hashlen;
2023 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002024 }
2025
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002026 if( mode == MBEDTLS_RSA_PUBLIC )
2027 return( mbedtls_rsa_public( ctx, sig, sig ) );
2028
2029 /*
2030 * In order to prevent Lenstra's attack, make the signature in a
2031 * temporary buffer and check it before returning it.
2032 */
2033 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002034 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002035 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2036
Simon Butcher1285ab52016-01-01 21:42:47 +00002037 verif = mbedtls_calloc( 1, ctx->len );
2038 if( verif == NULL )
2039 {
2040 mbedtls_free( sig_try );
2041 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2042 }
2043
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002044 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2045 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2046
2047 /* Compare in constant time just in case */
2048 for( diff = 0, i = 0; i < ctx->len; i++ )
2049 diff |= verif[i] ^ sig[i];
2050 diff_no_optimize = diff;
2051
2052 if( diff_no_optimize != 0 )
2053 {
2054 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2055 goto cleanup;
2056 }
2057
2058 memcpy( sig, sig_try, ctx->len );
2059
2060cleanup:
2061 mbedtls_free( sig_try );
2062 mbedtls_free( verif );
2063
2064 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002067
2068/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 * Do an RSA operation to sign the message digest
2070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002072 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002073 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002076 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002077 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 unsigned char *sig )
2079{
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 switch( ctx->padding )
2081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#if defined(MBEDTLS_PKCS1_V15)
2083 case MBEDTLS_RSA_PKCS_V15:
2084 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002085 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002086#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#if defined(MBEDTLS_PKCS1_V21)
2089 case MBEDTLS_RSA_PKCS_V21:
2090 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002091 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002092#endif
2093
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002097}
2098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002100/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002101 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002104 int (*f_rng)(void *, unsigned char *, size_t),
2105 void *p_rng,
2106 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002108 unsigned int hashlen,
2109 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002111 int expected_salt_len,
2112 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002113{
Paul Bakker23986e52011-04-24 08:57:21 +00002114 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002115 size_t siglen;
2116 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002118 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002119 unsigned int hlen;
2120 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 const mbedtls_md_info_t *md_info;
2122 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002123 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2126 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002127
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 siglen = ctx->len;
2129
Paul Bakker27fdf462011-06-09 13:55:13 +00002130 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2134 ? mbedtls_rsa_public( ctx, sig, buf )
2135 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136
2137 if( ret != 0 )
2138 return( ret );
2139
2140 p = buf;
2141
Paul Bakkerb3869132013-02-28 17:21:01 +01002142 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 {
Simon Butcher02037452016-03-01 21:19:12 +00002147 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002149 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002153 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002156 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002160 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002161
Paul Bakkerb3869132013-02-28 17:21:01 +01002162 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002163
Simon Butcher02037452016-03-01 21:19:12 +00002164 /*
2165 * Note: EMSA-PSS verification is over the length of N - 1 bits
2166 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002167 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002168
Simon Butcher02037452016-03-01 21:19:12 +00002169 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002170 if( msb % 8 == 0 )
2171 {
2172 p++;
2173 siglen -= 1;
2174 }
2175 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002179 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2180 {
2181 mbedtls_md_free( &md_ctx );
2182 return( ret );
2183 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002184
Paul Bakkerb3869132013-02-28 17:21:01 +01002185 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002186
Paul Bakkerb3869132013-02-28 17:21:01 +01002187 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002188
Paul Bakker4de44aa2013-12-31 11:43:01 +01002189 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002190 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002191
Paul Bakkerb3869132013-02-28 17:21:01 +01002192 if( p == buf + siglen ||
2193 *p++ != 0x01 )
2194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 mbedtls_md_free( &md_ctx );
2196 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002197 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002198
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002199 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002200 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002203 slen != (size_t) expected_salt_len )
2204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 mbedtls_md_free( &md_ctx );
2206 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002207 }
2208
Simon Butcher02037452016-03-01 21:19:12 +00002209 /*
2210 * Generate H = Hash( M' )
2211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 mbedtls_md_starts( &md_ctx );
2213 mbedtls_md_update( &md_ctx, zeros, 8 );
2214 mbedtls_md_update( &md_ctx, hash, hashlen );
2215 mbedtls_md_update( &md_ctx, p, slen );
2216 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002219
Paul Bakkerb3869132013-02-28 17:21:01 +01002220 if( memcmp( p + slen, result, hlen ) == 0 )
2221 return( 0 );
2222 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002224}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002225
2226/*
2227 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002230 int (*f_rng)(void *, unsigned char *, size_t),
2231 void *p_rng,
2232 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002234 unsigned int hashlen,
2235 const unsigned char *hash,
2236 const unsigned char *sig )
2237{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2239 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002240 : md_alg;
2241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002243 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002245 sig ) );
2246
2247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002251/*
2252 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002255 int (*f_rng)(void *, unsigned char *, size_t),
2256 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002257 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002259 unsigned int hashlen,
2260 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002261 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002262{
2263 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002264 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002265 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 mbedtls_md_type_t msg_md_alg;
2267 const mbedtls_md_info_t *md_info;
2268 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002269 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2272 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002273
2274 siglen = ctx->len;
2275
2276 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2280 ? mbedtls_rsa_public( ctx, sig, buf )
2281 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002282
2283 if( ret != 0 )
2284 return( ret );
2285
2286 p = buf;
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2289 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002290
2291 while( *p != 0 )
2292 {
2293 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002295 p++;
2296 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002297 p++; /* skip 00 byte */
2298
2299 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2300 if( p - buf < 11 )
2301 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002302
2303 len = siglen - ( p - buf );
2304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002306 {
2307 if( memcmp( p, hash, hashlen ) == 0 )
2308 return( 0 );
2309 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002311 }
2312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002314 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2316 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002317
2318 end = p + len;
2319
Simon Butcher02037452016-03-01 21:19:12 +00002320 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002321 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2322 * Insist on 2-byte length tags, to protect against variants of
2323 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002324 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002325 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2327 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2328 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002329 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002331
Gilles Peskinee7e76502017-05-04 12:48:39 +02002332 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2334 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2335 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002336 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002338
Gilles Peskinee7e76502017-05-04 12:48:39 +02002339 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2341 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002342 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002343 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002344
2345 oid.p = p;
2346 p += oid.len;
2347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2349 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002350
2351 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002353
2354 /*
2355 * assume the algorithm parameters must be NULL
2356 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002357 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2359 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002360 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002362
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002363 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002364 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2365 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002366 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002368
2369 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002371
2372 p += hashlen;
2373
2374 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002376
2377 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
2381/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002382 * Do an RSA operation and check the message digest
2383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002385 int (*f_rng)(void *, unsigned char *, size_t),
2386 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002387 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002389 unsigned int hashlen,
2390 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002391 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002392{
2393 switch( ctx->padding )
2394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395#if defined(MBEDTLS_PKCS1_V15)
2396 case MBEDTLS_RSA_PKCS_V15:
2397 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002398 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002399#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401#if defined(MBEDTLS_PKCS1_V21)
2402 case MBEDTLS_RSA_PKCS_V21:
2403 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002404 hashlen, hash, sig );
2405#endif
2406
2407 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002409 }
2410}
2411
2412/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002413 * Copy the components of an RSA key
2414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002416{
2417 int ret;
2418
2419 dst->ver = src->ver;
2420 dst->len = src->len;
2421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2423 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2426 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2427 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002428
2429#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2431 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2432 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2434 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002435#endif
2436
2437 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2440 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002441
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002442 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002443 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002444
2445cleanup:
2446 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002448
2449 return( ret );
2450}
2451
2452/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 * Free the components of an RSA key
2454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002456{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002458 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2459 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002461
Hanno Becker33c30a02017-08-23 07:00:22 +01002462#if !defined(MBEDTLS_RSA_NO_CRT)
2463 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2464 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2465 mbedtls_mpi_free( &ctx->DP );
2466#endif /* MBEDTLS_RSA_NO_CRT */
2467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468#if defined(MBEDTLS_THREADING_C)
2469 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002470#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002471}
2472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002475#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
2477/*
2478 * Example RSA-1024 keypair, for test purposes
2479 */
2480#define KEY_LEN 128
2481
2482#define RSA_N "9292758453063D803DD603D5E777D788" \
2483 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2484 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2485 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2486 "93A89813FBF3C4F8066D2D800F7C38A8" \
2487 "1AE31942917403FF4946B0A83D3D3E05" \
2488 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2489 "5E94BB77B07507233A0BC7BAC8F90F79"
2490
2491#define RSA_E "10001"
2492
2493#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2494 "66CA472BC44D253102F8B4A9D3BFA750" \
2495 "91386C0077937FE33FA3252D28855837" \
2496 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2497 "DF79C5CE07EE72C7F123142198164234" \
2498 "CABB724CF78B8173B9F880FC86322407" \
2499 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2500 "071513A1E85B5DFA031F21ECAE91A34D"
2501
2502#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2503 "2C01CAD19EA484A87EA4377637E75500" \
2504 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2505 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2506
2507#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2508 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2509 "910E4168387E3C30AA1E00C339A79508" \
2510 "8452DD96A9A5EA5D9DCA68DA636032AF"
2511
2512#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2513 "3C94D22288ACD763FD8E5600ED4A702D" \
2514 "F84198A5F06C2E72236AE490C93F07F8" \
2515 "3CC559CD27BC2D1CA488811730BB5725"
2516
2517#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2518 "D8AAEA56749EA28623272E4F7D0592AF" \
2519 "7C1F1313CAC9471B5C523BFE592F517B" \
2520 "407A1BD76C164B93DA2D32A383E58357"
2521
2522#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2523 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2524 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2525 "A74206CEC169D74BF5A8C50D6F48EA08"
2526
2527#define PT_LEN 24
2528#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2529 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002532static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002533{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002534#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002535 size_t i;
2536
Paul Bakker545570e2010-07-18 09:00:25 +00002537 if( rng_state != NULL )
2538 rng_state = NULL;
2539
Paul Bakkera3d195c2011-11-27 21:07:34 +00002540 for( i = 0; i < len; ++i )
2541 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002542#else
2543 if( rng_state != NULL )
2544 rng_state = NULL;
2545
2546 arc4random_buf( output, len );
2547#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002548
Paul Bakkera3d195c2011-11-27 21:07:34 +00002549 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002552
Paul Bakker5121ce52009-01-03 21:22:43 +00002553/*
2554 * Checkup routine
2555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002557{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002558 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002560 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 unsigned char rsa_plaintext[PT_LEN];
2563 unsigned char rsa_decrypted[PT_LEN];
2564 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002566 unsigned char sha1sum[20];
2567#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
Hanno Becker3a701162017-08-22 13:52:43 +01002569 mbedtls_mpi K;
2570
2571 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Hanno Becker3a701162017-08-22 13:52:43 +01002574 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2575 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2576 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2577 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2578 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2579 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2580 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2581 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2582 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2583 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2584
2585 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
2587 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2591 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 {
2593 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
2596 return( 1 );
2597 }
2598
Hanno Becker3a701162017-08-22 13:52:43 +01002599 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2600 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2601
2602 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2603 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2604
2605 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2606 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2607
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
2611 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 rsa_plaintext, rsa_ciphertext ) != 0 )
2615 {
2616 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
2619 return( 1 );
2620 }
2621
2622 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002626 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002627 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 {
2629 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
2632 return( 1 );
2633 }
2634
2635 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2636 {
2637 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
2640 return( 1 );
2641 }
2642
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002643 if( verbose != 0 )
2644 mbedtls_printf( "passed\n" );
2645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002648 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 sha1sum, rsa_ciphertext ) != 0 )
2654 {
2655 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
2658 return( 1 );
2659 }
2660
2661 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 sha1sum, rsa_ciphertext ) != 0 )
2666 {
2667 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
2670 return( 1 );
2671 }
2672
2673 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002674 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002677 if( verbose != 0 )
2678 mbedtls_printf( "\n" );
2679
Paul Bakker3d8fb632014-04-17 12:42:41 +02002680cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002681 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 mbedtls_rsa_free( &rsa );
2683#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002684 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002686 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002687}
2688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#endif /* MBEDTLS_RSA_C */