blob: bd72aee8e3adc5d7efd992a2b82a4eb71483480b [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/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010091 *
92 * Given the modulus N=PQ and a pair of public and private
93 * exponents E and D, respectively, factor N.
94 *
95 * Setting F := lcm(P-1,Q-1), the idea is as follows:
96 *
97 * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
98 * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
99 * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
100 * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
101 * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
102 * factors of N.
103 *
104 * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
105 * construction still applies since (-)^K is the identity on the set of
106 * roots of 1 in Z/NZ.
107 *
108 * The public and private key primitives (-)^E and (-)^D are mutually inverse
109 * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
110 * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
111 * Splitting L = 2^t * K with K odd, we have
112 *
113 * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
114 *
115 * so (F / 2) * K is among the numbers
116 *
117 * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
118 *
119 * where ord is the order of 2 in (DE - 1).
120 * We can therefore iterate through these numbers apply the construction
121 * of (a) and (b) above to attempt to factor N.
122 *
123 */
124int mbedtls_rsa_deduce_moduli( mbedtls_mpi *N, mbedtls_mpi *D, mbedtls_mpi *E,
125 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
126 mbedtls_mpi *P, mbedtls_mpi *Q )
127{
128 /* Implementation note:
129 *
130 * Space-efficiency is given preference over time-efficiency here:
131 * several calculations are done in place and temporarily change
132 * the values of D and E.
133 *
134 * Specifically, D is replaced the largest odd divisor of DE - 1
135 * throughout the calculations.
136 */
137
138 int ret = 0;
139
140 uint16_t attempt; /* Number of current attempt */
141 uint16_t iter; /* Number of squares computed in the current attempt */
142
143 uint16_t bitlen_half; /* Half the bitsize of the modulus N */
144 uint16_t order; /* Order of 2 in DE - 1 */
145
146 mbedtls_mpi K; /* Temporary used for two purposes:
147 * - During factorization attempts, stores a andom integer
148 * in the range of [0,..,N]
149 * - During verification, holding intermediate results.
150 */
151
152 if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
153 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
154
155 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
156 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
157 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
158 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
159 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
160 {
161 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
162 }
163
164 /*
165 * Initializations and temporary changes
166 */
167
168 mbedtls_mpi_init( &K );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100169
170 /* Replace D by DE - 1 */
171 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( D, D, E ) );
172 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( D, D, 1 ) );
173
174 if( ( order = mbedtls_mpi_lsb( D ) ) == 0 )
175 {
176 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
177 goto cleanup;
178 }
179
180 /* After this operation, D holds the largest odd divisor
181 * of DE - 1 for the original values of D and E. */
182 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( D, order ) );
183
184 /* This is used to generate a few numbers around N / 2
185 * if no PRNG is provided. */
186 if( f_rng == NULL )
187 bitlen_half = mbedtls_mpi_bitlen( N ) / 2;
188
189 /*
190 * Actual work
191 */
192
193 for( attempt = 0; attempt < 30; ++attempt )
194 {
195 /* Generate some number in [0,N], either randomly
196 * if a PRNG is given, or try numbers around N/2 */
197 if( f_rng != NULL )
198 {
199 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &K,
200 mbedtls_mpi_size( N ),
201 f_rng, p_rng ) );
202 }
203 else
204 {
205 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &K, 1 ) ) ;
206 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &K, bitlen_half ) ) ;
207 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, attempt + 1 ) );
208 }
209
210 /* Check if gcd(K,N) = 1 */
211 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
212 if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
213 continue;
214
215 /* Go through K^X + 1, K^(2X) + 1, K^(4X) + 1, ...
216 * and check whether they have nontrivial GCD with N. */
217 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, D, N,
218 Q /* temporarily use Q for storing Montgomery
219 * multiplication helper values */ ) );
220
221 for( iter = 1; iter < order; ++iter )
222 {
223 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
225
226 if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
227 mbedtls_mpi_cmp_mpi( P, N ) == -1 )
228 {
229 /*
230 * Have found a nontrivial divisor P of N.
Hanno Beckerd56d83a2017-08-25 07:29:35 +0100231 * Set Q := N / P.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100232 */
233
234 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, &K, N, P ) );
235
Hanno Beckerd56d83a2017-08-25 07:29:35 +0100236 /* Restore D */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100237
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100238 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( D, order ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100239 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( D, D, 1 ) );
240 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( D, NULL, D, E ) );
241
242 goto cleanup;
243 }
244
245 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
246 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
247 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
248 }
249 }
250
251 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
252
253cleanup:
254
255 mbedtls_mpi_free( &K );
256 return( ret );
257}
258
259/*
260 * Given P, Q and the public exponent E, deduce D.
261 * This is essentially a modular inversion.
262 */
263
264int mbedtls_rsa_deduce_private( mbedtls_mpi *P, mbedtls_mpi *Q,
265 mbedtls_mpi *D, mbedtls_mpi *E )
266{
267 int ret = 0;
268 mbedtls_mpi K;
269
270 if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
271 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
272
273 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
274 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
275 mbedtls_mpi_cmp_int( E, 0 ) == 0 )
276 {
277 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
278 }
279
280 mbedtls_mpi_init( &K );
281
282 /* Temporarily replace P and Q by P-1 and Q-1, respectively. */
283 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) );
284 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) );
285
286 /* Temporarily compute the gcd(P-1, Q-1) in D. */
287 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, P, Q ) );
288
289 /* Compute LCM(P-1, Q-1) in K */
290 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
291 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
292
293 /* Compute modular inverse of E in LCM(P-1, Q-1) */
294 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
295
296 /* Restore P and Q. */
297 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) );
298 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) );
299
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100300cleanup:
301
302 mbedtls_mpi_free( &K );
303
304 return( ret );
305}
306
307/*
Hanno Beckerd3637992017-08-25 07:55:03 +0100308 * Check that RSA CRT parameters are in accordance with core parameters.
309 */
310
311int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
312 const mbedtls_mpi *D, const mbedtls_mpi *DP,
313 const mbedtls_mpi *DQ, const mbedtls_mpi *QP )
314{
315 int ret = 0;
316
317 mbedtls_mpi K, L;
318 mbedtls_mpi_init( &K );
319 mbedtls_mpi_init( &L );
320
321 /* Check that DP - P == 0 mod P - 1 */
322 if( DP != NULL )
323 {
324 if( P == NULL )
325 {
326 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
327 goto cleanup;
328 }
329
330 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
331 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DP, D ) );
332 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
333
334 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
335 {
336 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
337 }
338 }
339
340 /* Check that DQ - Q == 0 mod Q - 1 */
341 if( DQ != NULL )
342 {
343 if( Q == NULL )
344 {
345 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
346 goto cleanup;
347 }
348
349 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
350 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
351 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
352
353 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
354 {
355 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
356 }
357 }
358
359 /* Check that QP * P - 1 == 0 mod P */
360 if( QP != NULL )
361 {
362 if( P == NULL || Q == NULL )
363 {
364 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
365 goto cleanup;
366 }
367
368 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
369 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
370 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
371 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
372 {
373 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
374 }
375 }
376
377cleanup:
378
379 /* Wrap MPI error codes by RSA check failure error code */
380 if( ret != 0 &&
381 ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
382 ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
383 {
384 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
385 }
386
387 mbedtls_mpi_free( &K );
388 mbedtls_mpi_free( &L );
389
390 return( ret );
391}
392
393/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100394 * Check that core RSA parameters are sane.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100395 */
396
Hanno Becker750e8b42017-08-25 07:54:27 +0100397int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
398 const mbedtls_mpi *Q, const mbedtls_mpi *D,
399 const mbedtls_mpi *E,
400 int (*f_rng)(void *, unsigned char *, size_t),
401 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100402{
403 int ret = 0;
Hanno Becker750e8b42017-08-25 07:54:27 +0100404 mbedtls_mpi K, L;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100405
406 mbedtls_mpi_init( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100407 mbedtls_mpi_init( &L );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100408
409 /*
410 * Step 1: If PRNG provided, check that P and Q are prime
411 */
412
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100413#if defined(MBEDTLS_GENPRIME)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100414 if( f_rng != NULL && P != NULL &&
415 ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
416 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100417 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100418 goto cleanup;
419 }
420
421 if( f_rng != NULL && Q != NULL &&
422 ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
423 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100424 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100425 goto cleanup;
426 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100427#else
428 ((void) f_rng);
429 ((void) p_rng);
430#endif /* MBEDTLS_GENPRIME */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100431
432 /*
433 * Step 2: Check that N = PQ
434 */
435
436 if( P != NULL && Q != NULL && N != NULL )
437 {
438 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
Hanno Becker750e8b42017-08-25 07:54:27 +0100439 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
440 mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100441 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100442 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100443 goto cleanup;
444 }
445 }
446
447 /*
448 * Step 3: Check that D, E are inverse modulo P-1 and Q-1
449 */
450
451 if( P != NULL && Q != NULL && D != NULL && E != NULL )
452 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100453 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
454 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
455 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
456 mbedtls_mpi_cmp_int( E, 1 ) <= 0 )
457 {
458 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
459 goto cleanup;
460 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100461
462 /* Compute DE-1 mod P-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100463 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
464 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
465 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
466 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100467 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
468 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100469 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100470 goto cleanup;
471 }
472
473 /* Compute DE-1 mod Q-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100474 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
475 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
476 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
477 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100478 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
479 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100480 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100481 goto cleanup;
482 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100483 }
484
485cleanup:
486
487 mbedtls_mpi_free( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100488 mbedtls_mpi_free( &L );
489
490 /* Wrap MPI error codes by RSA check failure error code */
491 if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
492 {
493 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
494 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100495
496 return( ret );
497}
498
499int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
500 const mbedtls_mpi *D, mbedtls_mpi *DP,
501 mbedtls_mpi *DQ, mbedtls_mpi *QP )
502{
503 int ret = 0;
504 mbedtls_mpi K;
505 mbedtls_mpi_init( &K );
506
507 if( DP != NULL )
508 {
509 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
510 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
511 }
512
513 if( DQ != NULL )
514 {
515 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
516 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
517 }
518
519 if( QP != NULL )
520 {
521 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
522 }
523
524cleanup:
525 mbedtls_mpi_free( &K );
526
527 return( ret );
528}
529
Hanno Becker617c1ae2017-08-23 14:11:24 +0100530
531/*
532 * Default RSA interface implementation
533 */
534
Hanno Beckerab377312017-08-23 16:24:51 +0100535#if !defined(MBEDTLS_RSA_ALT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100536
537int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
538 const mbedtls_mpi *N,
539 const mbedtls_mpi *P, const mbedtls_mpi *Q,
540 const mbedtls_mpi *D, const mbedtls_mpi *E )
541{
542 int ret;
543
544 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
545 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
546 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
547 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
548 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
549 {
550 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
551 }
552
553 if( N != NULL )
554 ctx->len = mbedtls_mpi_size( &ctx->N );
555
556 return( 0 );
557}
558
559int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
560 unsigned char *N, size_t N_len,
561 unsigned char *P, size_t P_len,
562 unsigned char *Q, size_t Q_len,
563 unsigned char *D, size_t D_len,
564 unsigned char *E, size_t E_len )
565{
566 int ret;
567
568 if( N != NULL )
569 {
570 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
571 ctx->len = mbedtls_mpi_size( &ctx->N );
572 }
573
574 if( P != NULL )
575 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
576
577 if( Q != NULL )
578 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
579
580 if( D != NULL )
581 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
582
583 if( E != NULL )
584 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
585
586cleanup:
587
588 if( ret != 0 )
589 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
590
591 return( 0 );
592}
593
594int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
595 int (*f_rng)(void *, unsigned char *, size_t),
596 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100597{
598 int ret = 0;
599
Hanno Becker617c1ae2017-08-23 14:11:24 +0100600 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
601 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
602 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
603 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
604 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100605
Hanno Becker617c1ae2017-08-23 14:11:24 +0100606 /*
607 * Check whether provided parameters are enough
608 * to deduce all others. The following incomplete
609 * parameter sets for private keys are supported:
610 *
611 * (1) P, Q missing.
612 * (2) D and potentially N missing.
613 *
614 */
615 const int complete = have_N && have_P && have_Q && have_D && have_E;
616 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
617 const int d_missing = have_P && have_Q && !have_D && have_E;
618 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100619
Hanno Becker617c1ae2017-08-23 14:11:24 +0100620 const int is_priv = complete || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100621
Hanno Becker617c1ae2017-08-23 14:11:24 +0100622 if( !is_priv && !is_pub )
623 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
624
625 /*
626 * Step 1: Deduce and verify all core parameters.
627 */
628
629 if( pq_missing )
630 {
631 /* This includes sanity checking of core parameters,
632 * so no further checks necessary. */
633 ret = mbedtls_rsa_deduce_moduli( &ctx->N, &ctx->D, &ctx->E,
634 f_rng, p_rng,
635 &ctx->P, &ctx->Q );
636 if( ret != 0 )
637 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
638
639 }
640 else if( d_missing )
641 {
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100642#if defined(MBEDTLS_GENPRIME)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100643 /* If a PRNG is provided, check if P, Q are prime. */
644 if( f_rng != NULL &&
645 ( ( ret = mbedtls_mpi_is_prime( &ctx->P, f_rng, p_rng ) ) != 0 ||
646 ( ret = mbedtls_mpi_is_prime( &ctx->Q, f_rng, p_rng ) ) != 0 ) )
647 {
648 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
649 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100650#endif /* MBEDTLS_GENPRIME */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100651
652 /* Compute N if missing. */
653 if( !have_N &&
654 ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) ) != 0 )
655 {
656 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
657 }
658
659 /* Deduce private exponent. This includes double-checking of the result,
660 * so together with the primality test above all core parameters are
661 * guaranteed to be sane if this call succeeds. */
662 if( ( ret = mbedtls_rsa_deduce_private( &ctx->P, &ctx->Q,
663 &ctx->D, &ctx->E ) ) != 0 )
664 {
665 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
666 }
667 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100668
669 /* In the remaining case of a public key, there's nothing to check for. */
670
671 /*
672 * Step 2: Deduce all additional parameters specific
673 * to our current RSA implementaiton.
674 */
675
Hanno Becker23344b52017-08-23 07:43:27 +0100676#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100677 if( is_priv )
678 {
679 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
680 &ctx->DP, &ctx->DQ, &ctx->QP );
681 if( ret != 0 )
682 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
683 }
Hanno Becker23344b52017-08-23 07:43:27 +0100684#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100685
686 /*
687 * Step 3: Double check
688 */
689
690 if( is_priv )
691 {
692 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
693 return( ret );
694 }
695 else
696 {
697 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
698 return( ret );
699 }
700
701 return( 0 );
702}
703
704/*
705 * Check if CRT parameters match RSA context.
706 * This has to be implemented even if CRT is not used,
707 * in order to be able to validate DER encoded RSA keys,
708 * which always contain CRT parameters.
709 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100710int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx,
711 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100712{
Hanno Becker23344b52017-08-23 07:43:27 +0100713 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100714
Hanno Becker23344b52017-08-23 07:43:27 +0100715 /* Check if key is private or public */
716 const int is_priv =
717 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
718 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
719 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
720 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
721 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
722
723 if( !is_priv )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100724 {
725 /* Checking optional parameters only makes sense for private keys. */
726 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
727 }
728
Hanno Becker23344b52017-08-23 07:43:27 +0100729#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100730 if( ( DP != NULL && mbedtls_mpi_cmp_mpi( DP, &ctx->DP ) != 0 ) ||
731 ( DQ != NULL && mbedtls_mpi_cmp_mpi( DQ, &ctx->DQ ) != 0 ) ||
732 ( QP != NULL && mbedtls_mpi_cmp_mpi( QP, &ctx->QP ) != 0 ) )
733 {
734 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
735 }
Hanno Becker23344b52017-08-23 07:43:27 +0100736#else /* MBEDTLS_RSA_NO_CRT */
Hanno Beckerd3637992017-08-25 07:55:03 +0100737 if( ( ret = mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
738 DP, DQ, QP ) ) != 0 )
Hanno Becker23344b52017-08-23 07:43:27 +0100739 {
Hanno Beckerd3637992017-08-25 07:55:03 +0100740 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker23344b52017-08-23 07:43:27 +0100741 }
Hanno Becker23344b52017-08-23 07:43:27 +0100742#endif
743
744 if( ret != 0 )
745 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100746
747 return( 0 );
748}
749
750int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
751 unsigned char *N, size_t N_len,
752 unsigned char *P, size_t P_len,
753 unsigned char *Q, size_t Q_len,
754 unsigned char *D, size_t D_len,
755 unsigned char *E, size_t E_len )
756{
757 int ret = 0;
758
759 /* Check if key is private or public */
760 const int is_priv =
761 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
762 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
763 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
764 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
765 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
766
767 if( !is_priv )
768 {
769 /* If we're trying to export private parameters for a public key,
770 * something must be wrong. */
771 if( P != NULL || Q != NULL || D != NULL )
772 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
773
774 }
775
776 if( N != NULL )
777 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
778
779 if( P != NULL )
780 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
781
782 if( Q != NULL )
783 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
784
785 if( D != NULL )
786 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
787
788 if( E != NULL )
789 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100790
791cleanup:
792
793 return( ret );
794}
795
Hanno Becker617c1ae2017-08-23 14:11:24 +0100796int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
797 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
798 mbedtls_mpi *D, mbedtls_mpi *E )
799{
800 int ret;
801
802 /* Check if key is private or public */
803 int is_priv =
804 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
805 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
806 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
807 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
808 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
809
810 if( !is_priv )
811 {
812 /* If we're trying to export private parameters for a public key,
813 * something must be wrong. */
814 if( P != NULL || Q != NULL || D != NULL )
815 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
816
817 }
818
819 /* Export all requested core parameters. */
820
821 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
822 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
823 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
824 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
825 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
826 {
827 return( ret );
828 }
829
830 return( 0 );
831}
832
833/*
834 * Export CRT parameters
835 * This must also be implemented if CRT is not used, for being able to
836 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
837 * can be used in this case.
838 */
839int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
840 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
841{
842 int ret;
843
844 /* Check if key is private or public */
845 int is_priv =
846 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
847 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
848 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
849 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
850 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
851
852 if( !is_priv )
853 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
854
Hanno Beckerdc95c892017-08-23 06:57:02 +0100855#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100856 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100857 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
858 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
859 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
860 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100861 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100862 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100863#else
864 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
865 DP, DQ, QP ) ) != 0 )
866 {
867 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
868 }
869#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100870
871 return( 0 );
872}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100873
874/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 * Initialize an RSA context
876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000878 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000879 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000880{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885#if defined(MBEDTLS_THREADING_C)
886 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200887#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000888}
889
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100890/*
891 * Set padding for an existing RSA context
892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100894{
895 ctx->padding = padding;
896 ctx->hash_id = hash_id;
897}
898
Hanno Becker617c1ae2017-08-23 14:11:24 +0100899/*
900 * Get length in bytes of RSA modulus
901 */
902
903size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
904{
905 return( mbedtls_mpi_size( &ctx->N ) );
906}
907
908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
911/*
912 * Generate an RSA keypair
913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000915 int (*f_rng)(void *, unsigned char *, size_t),
916 void *p_rng,
917 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000918{
919 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100920 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000921
Paul Bakker21eb2802010-08-16 11:10:02 +0000922 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000924
Janos Follathef441782016-09-21 13:18:12 +0100925 if( nbits % 2 )
926 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
927
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100928 mbedtls_mpi_init( &H );
929 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
931 /*
932 * find primes P and Q with Q < P so that:
933 * GCD( E, (P-1)*(Q-1) ) == 1
934 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
937 do
938 {
Janos Follath10c575b2016-02-23 14:42:48 +0000939 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100940 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Janos Follathef441782016-09-21 13:18:12 +0100942 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100943 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000946 continue;
947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200949 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 continue;
951
Janos Follathef441782016-09-21 13:18:12 +0100952 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100953 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100954
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100955 /* Temporarily replace P,Q by P-1, Q-1 */
956 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
957 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
958 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000962
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100963 /* Restore P,Q */
964 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
965 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
966
967 ctx->len = mbedtls_mpi_size( &ctx->N );
968
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 /*
970 * D = E^-1 mod ((P-1)*(Q-1))
971 * DP = D mod (P - 1)
972 * DQ = D mod (Q - 1)
973 * QP = Q^-1 mod P
974 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100976 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
977
978#if !defined(MBEDTLS_RSA_NO_CRT)
979 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
980 &ctx->DP, &ctx->DQ, &ctx->QP ) );
981#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
Hanno Becker83aad1f2017-08-23 06:45:10 +0100983 /* Double-check */
984 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
985
Paul Bakker5121ce52009-01-03 21:22:43 +0000986cleanup:
987
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100988 mbedtls_mpi_free( &H );
989 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000990
991 if( ret != 0 )
992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_rsa_free( ctx );
994 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 }
996
Paul Bakker48377d92013-08-30 12:06:24 +0200997 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000998}
999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
1002/*
1003 * Check a public RSA key
1004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001006{
Paul Bakker37940d9f2009-07-10 22:38:58 +00001007 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +00001009
Paul Bakker48377d92013-08-30 12:06:24 +02001010 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001014 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
1015 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001018 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
1020 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
1022 return( 0 );
1023}
1024
1025/*
1026 * Check a private RSA key
1027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001029{
1030 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 return( ret );
1035
Paul Bakker37940d9f2009-07-10 22:38:58 +00001036 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +00001038
Hanno Becker6345dd32017-08-23 06:59:48 +01001039 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 );
1040 mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I );
1041 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 ); mbedtls_mpi_init( &L1 );
1042 mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
1046 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
1047 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1048 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1049 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
1050 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
1053 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
1054 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +00001055
Hanno Becker6345dd32017-08-23 06:59:48 +01001056#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
1058 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
1059 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Hanno Becker6345dd32017-08-23 06:59:48 +01001060#endif
1061
Paul Bakkerb572adf2010-07-18 08:29:32 +00001062 /*
1063 * Check for a valid PKCS1v2 private key
1064 */
Hanno Becker6345dd32017-08-23 06:59:48 +01001065 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
1066#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
1068 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
1069 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
Hanno Becker6345dd32017-08-23 06:59:48 +01001070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
Hanno Becker6345dd32017-08-23 06:59:48 +01001072 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 }
Paul Bakker48377d92013-08-30 12:06:24 +02001077
Paul Bakker5121ce52009-01-03 21:22:43 +00001078cleanup:
Hanno Becker6345dd32017-08-23 06:59:48 +01001079 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 );
1080 mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I );
1081 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 ); mbedtls_mpi_free( &L1 );
1082 mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +00001086 return( ret );
1087
Paul Bakker6c591fa2011-05-05 11:49:20 +00001088 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001090
1091 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001092}
1093
1094/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001095 * Check if contexts holding a public and private key match
1096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1100 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001103 }
1104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1106 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001109 }
1110
1111 return( 0 );
1112}
1113
1114/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 * Do an RSA public key operation
1116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001118 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 unsigned char *output )
1120{
Paul Bakker23986e52011-04-24 08:57:21 +00001121 int ret;
1122 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001126
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001127#if defined(MBEDTLS_THREADING_C)
1128 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1129 return( ret );
1130#endif
1131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001136 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1137 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001138 }
1139
1140 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1142 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
1144cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001146 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1147 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001148#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
1152 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
1155 return( 0 );
1156}
1157
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001158/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001159 * Generate or update blinding values, see section 10 of:
1160 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001161 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001162 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001163 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001164static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001165 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1166{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001167 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001168
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001169 if( ctx->Vf.p != NULL )
1170 {
1171 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1173 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1174 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1175 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001176
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001177 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001178 }
1179
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001180 /* Unblinding value: Vf = random number, invertible mod N */
1181 do {
1182 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1186 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1187 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001188
1189 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1191 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 +02001192
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001193
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001194cleanup:
1195 return( ret );
1196}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001197
Paul Bakker5121ce52009-01-03 21:22:43 +00001198/*
Janos Follathe81102e2017-03-22 13:38:28 +00001199 * Exponent blinding supposed to prevent side-channel attacks using multiple
1200 * traces of measurements to recover the RSA key. The more collisions are there,
1201 * the more bits of the key can be recovered. See [3].
1202 *
1203 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1204 * observations on avarage.
1205 *
1206 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1207 * to make 2^112 observations on avarage.
1208 *
1209 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1210 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1211 * Thus in this sense with 28 byte blinding the security is not reduced by
1212 * side-channel attacks like the one in [3])
1213 *
1214 * This countermeasure does not help if the key recovery is possible with a
1215 * single trace.
1216 */
1217#define RSA_EXPONENT_BLINDING 28
1218
1219/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 * Do an RSA private key operation
1221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001223 int (*f_rng)(void *, unsigned char *, size_t),
1224 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001225 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 unsigned char *output )
1227{
Paul Bakker23986e52011-04-24 08:57:21 +00001228 int ret;
1229 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001231 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001232#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001233 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001234 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001235#else
1236 mbedtls_mpi DP_blind, DQ_blind;
1237 mbedtls_mpi *DP = &ctx->DP;
1238 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001239#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001241 /* Make sure we have private key info, prevent possible misuse */
1242 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
1243 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001246 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1247
1248
1249 if( f_rng != NULL )
1250 {
Janos Follathe81102e2017-03-22 13:38:28 +00001251#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001252 mbedtls_mpi_init( &D_blind );
1253#else
1254 mbedtls_mpi_init( &DP_blind );
1255 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001256#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001257 }
Janos Follathe81102e2017-03-22 13:38:28 +00001258
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001260#if defined(MBEDTLS_THREADING_C)
1261 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1262 return( ret );
1263#endif
1264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1266 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001268 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1269 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 }
1271
Paul Bakkerf451bac2013-08-30 15:37:02 +02001272 if( f_rng != NULL )
1273 {
1274 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001275 * Blinding
1276 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001277 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001278 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1279 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001281
Janos Follathe81102e2017-03-22 13:38:28 +00001282 /*
1283 * Exponent blinding
1284 */
1285 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1286 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1287
Janos Follathf9203b42017-03-22 15:13:15 +00001288#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001289 /*
1290 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1291 */
1292 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1293 f_rng, p_rng ) );
1294 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1295 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1296 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1297
1298 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001299#else
1300 /*
1301 * DP_blind = ( P - 1 ) * R + DP
1302 */
1303 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1304 f_rng, p_rng ) );
1305 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1306 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1307 &ctx->DP ) );
1308
1309 DP = &DP_blind;
1310
1311 /*
1312 * DQ_blind = ( Q - 1 ) * R + DQ
1313 */
1314 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1315 f_rng, p_rng ) );
1316 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1317 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1318 &ctx->DQ ) );
1319
1320 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001321#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001322 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001325 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001326#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001327 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001328 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 *
1330 * T1 = input ^ dP mod P
1331 * T2 = input ^ dQ mod Q
1332 */
Janos Follathf9203b42017-03-22 15:13:15 +00001333 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1334 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
1336 /*
1337 * T = (T1 - T2) * (Q^-1 mod P) mod P
1338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1340 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1341 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
1343 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001344 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1347 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1348#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001349
Paul Bakkerf451bac2013-08-30 15:37:02 +02001350 if( f_rng != NULL )
1351 {
1352 /*
1353 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001354 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001355 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001356 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001358 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
1360 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001362
1363cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001365 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1366 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001367#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001370 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1371
1372 if( f_rng != NULL )
1373 {
Janos Follathe81102e2017-03-22 13:38:28 +00001374#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001375 mbedtls_mpi_free( &D_blind );
1376#else
1377 mbedtls_mpi_free( &DP_blind );
1378 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001379#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
1382 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
1385 return( 0 );
1386}
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001389/**
1390 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1391 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001392 * \param dst buffer to mask
1393 * \param dlen length of destination buffer
1394 * \param src source of the mask generation
1395 * \param slen length of the source buffer
1396 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001397 */
Paul Bakker48377d92013-08-30 12:06:24 +02001398static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001400{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001402 unsigned char counter[4];
1403 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001404 unsigned int hlen;
1405 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001408 memset( counter, 0, 4 );
1409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001411
Simon Butcher02037452016-03-01 21:19:12 +00001412 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001413 p = dst;
1414
1415 while( dlen > 0 )
1416 {
1417 use_len = hlen;
1418 if( dlen < hlen )
1419 use_len = dlen;
1420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 mbedtls_md_starts( md_ctx );
1422 mbedtls_md_update( md_ctx, src, slen );
1423 mbedtls_md_update( md_ctx, counter, 4 );
1424 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001425
1426 for( i = 0; i < use_len; ++i )
1427 *p++ ^= mask[i];
1428
1429 counter[3]++;
1430
1431 dlen -= use_len;
1432 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001433
1434 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001439/*
1440 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001443 int (*f_rng)(void *, unsigned char *, size_t),
1444 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001445 int mode,
1446 const unsigned char *label, size_t label_len,
1447 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001448 const unsigned char *input,
1449 unsigned char *output )
1450{
1451 size_t olen;
1452 int ret;
1453 unsigned char *p = output;
1454 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 const mbedtls_md_info_t *md_info;
1456 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1459 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001460
1461 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001465 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001467
1468 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001470
Simon Butcher02037452016-03-01 21:19:12 +00001471 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001472 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001474
1475 memset( output, 0, olen );
1476
1477 *p++ = 0;
1478
Simon Butcher02037452016-03-01 21:19:12 +00001479 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001480 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001482
1483 p += hlen;
1484
Simon Butcher02037452016-03-01 21:19:12 +00001485 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001487 p += hlen;
1488 p += olen - 2 * hlen - 2 - ilen;
1489 *p++ = 1;
1490 memcpy( p, input, ilen );
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001493 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1494 {
1495 mbedtls_md_free( &md_ctx );
1496 return( ret );
1497 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001498
Simon Butcher02037452016-03-01 21:19:12 +00001499 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001500 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1501 &md_ctx );
1502
Simon Butcher02037452016-03-01 21:19:12 +00001503 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001504 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1505 &md_ctx );
1506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 return( ( mode == MBEDTLS_RSA_PUBLIC )
1510 ? mbedtls_rsa_public( ctx, output, output )
1511 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001516/*
1517 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001520 int (*f_rng)(void *, unsigned char *, size_t),
1521 void *p_rng,
1522 int mode, size_t ilen,
1523 const unsigned char *input,
1524 unsigned char *output )
1525{
1526 size_t nb_pad, olen;
1527 int ret;
1528 unsigned char *p = output;
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1531 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001532
Janos Follath1ed9f992016-03-18 11:45:44 +00001533 // We don't check p_rng because it won't be dereferenced here
1534 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001536
1537 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001538
Simon Butcher02037452016-03-01 21:19:12 +00001539 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001540 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001542
1543 nb_pad = olen - 3 - ilen;
1544
1545 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001549
1550 while( nb_pad-- > 0 )
1551 {
1552 int rng_dl = 100;
1553
1554 do {
1555 ret = f_rng( p_rng, p, 1 );
1556 } while( *p == 0 && --rng_dl && ret == 0 );
1557
Simon Butcher02037452016-03-01 21:19:12 +00001558 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001559 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001561
1562 p++;
1563 }
1564 }
1565 else
1566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001568
1569 while( nb_pad-- > 0 )
1570 *p++ = 0xFF;
1571 }
1572
1573 *p++ = 0;
1574 memcpy( p, input, ilen );
1575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 return( ( mode == MBEDTLS_RSA_PUBLIC )
1577 ? mbedtls_rsa_public( ctx, output, output )
1578 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001579}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001581
Paul Bakker5121ce52009-01-03 21:22:43 +00001582/*
1583 * Add the message padding, then do an RSA operation
1584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001586 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001587 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001588 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001589 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 unsigned char *output )
1591{
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 switch( ctx->padding )
1593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#if defined(MBEDTLS_PKCS1_V15)
1595 case MBEDTLS_RSA_PKCS_V15:
1596 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001597 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001598#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#if defined(MBEDTLS_PKCS1_V21)
1601 case MBEDTLS_RSA_PKCS_V21:
1602 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001603 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
1606 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001609}
1610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001612/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001613 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001616 int (*f_rng)(void *, unsigned char *, size_t),
1617 void *p_rng,
1618 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001619 const unsigned char *label, size_t label_len,
1620 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001621 const unsigned char *input,
1622 unsigned char *output,
1623 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001624{
Paul Bakker23986e52011-04-24 08:57:21 +00001625 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001626 size_t ilen, i, pad_len;
1627 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1629 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001630 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 const mbedtls_md_info_t *md_info;
1632 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001633
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001634 /*
1635 * Parameters sanity checks
1636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1638 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001639
1640 ilen = ctx->len;
1641
Paul Bakker27fdf462011-06-09 13:55:13 +00001642 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001646 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001648
Janos Follathc17cda12016-02-11 11:08:18 +00001649 hlen = mbedtls_md_get_size( md_info );
1650
1651 // checking for integer underflow
1652 if( 2 * hlen + 2 > ilen )
1653 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1654
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001655 /*
1656 * RSA operation
1657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1659 ? mbedtls_rsa_public( ctx, input, buf )
1660 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001661
1662 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001663 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001665 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001666 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001669 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1670 {
1671 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001672 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001673 }
1674
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001675
1676 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001678
1679 /* seed: Apply seedMask to maskedSeed */
1680 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1681 &md_ctx );
1682
1683 /* DB: Apply dbMask to maskedDB */
1684 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1685 &md_ctx );
1686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001688
1689 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001690 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001691 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001692 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001693 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001695 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001696
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001697 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001698
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001699 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001700 for( i = 0; i < hlen; i++ )
1701 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001702
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001703 /* Get zero-padding len, but always read till end of buffer
1704 * (minus one, for the 01 byte) */
1705 pad_len = 0;
1706 pad_done = 0;
1707 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1708 {
1709 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001710 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001711 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001712
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001713 p += pad_len;
1714 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001715
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001716 /*
1717 * The only information "leaked" is whether the padding was correct or not
1718 * (eg, no data is copied if it was not correct). This meets the
1719 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1720 * the different error conditions.
1721 */
1722 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001723 {
1724 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1725 goto cleanup;
1726 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001727
Paul Bakker66d5d072014-06-17 16:39:18 +02001728 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001729 {
1730 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1731 goto cleanup;
1732 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001733
1734 *olen = ilen - (p - buf);
1735 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001736 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001737
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001738cleanup:
1739 mbedtls_zeroize( buf, sizeof( buf ) );
1740 mbedtls_zeroize( lhash, sizeof( lhash ) );
1741
1742 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001747/*
1748 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001751 int (*f_rng)(void *, unsigned char *, size_t),
1752 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001753 int mode, size_t *olen,
1754 const unsigned char *input,
1755 unsigned char *output,
1756 size_t output_max_len)
1757{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001758 int ret;
1759 size_t ilen, pad_count = 0, i;
1760 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1764 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001765
1766 ilen = ctx->len;
1767
1768 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1772 ? mbedtls_rsa_public( ctx, input, buf )
1773 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001774
1775 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001776 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001777
1778 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001779 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001780
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001781 /*
1782 * Check and get padding len in "constant-time"
1783 */
1784 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001785
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001786 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001790
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001791 /* Get padding len, but always read till end of buffer
1792 * (minus one, for the 00 byte) */
1793 for( i = 0; i < ilen - 3; i++ )
1794 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001795 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1796 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001797 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001798
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001799 p += pad_count;
1800 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001801 }
1802 else
1803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001805
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001806 /* Get padding len, but always read till end of buffer
1807 * (minus one, for the 00 byte) */
1808 for( i = 0; i < ilen - 3; i++ )
1809 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001810 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001811 pad_count += ( pad_done == 0 );
1812 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001813
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001814 p += pad_count;
1815 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 }
1817
Janos Follathc69fa502016-02-12 13:30:09 +00001818 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001819
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001820 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001821 {
1822 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1823 goto cleanup;
1824 }
Paul Bakker8804f692013-02-28 18:06:26 +01001825
Paul Bakker66d5d072014-06-17 16:39:18 +02001826 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001827 {
1828 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1829 goto cleanup;
1830 }
Paul Bakker060c5682009-01-12 21:48:39 +00001831
Paul Bakker27fdf462011-06-09 13:55:13 +00001832 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001834 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001835
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001836cleanup:
1837 mbedtls_zeroize( buf, sizeof( buf ) );
1838
1839 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
1843/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001844 * Do an RSA operation, then remove the message padding
1845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001847 int (*f_rng)(void *, unsigned char *, size_t),
1848 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001849 int mode, size_t *olen,
1850 const unsigned char *input,
1851 unsigned char *output,
1852 size_t output_max_len)
1853{
1854 switch( ctx->padding )
1855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856#if defined(MBEDTLS_PKCS1_V15)
1857 case MBEDTLS_RSA_PKCS_V15:
1858 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001859 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001860#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862#if defined(MBEDTLS_PKCS1_V21)
1863 case MBEDTLS_RSA_PKCS_V21:
1864 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001865 olen, input, output,
1866 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001867#endif
1868
1869 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001871 }
1872}
1873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001875/*
1876 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001879 int (*f_rng)(void *, unsigned char *, size_t),
1880 void *p_rng,
1881 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001883 unsigned int hashlen,
1884 const unsigned char *hash,
1885 unsigned char *sig )
1886{
1887 size_t olen;
1888 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001890 unsigned int slen, hlen, offset = 0;
1891 int ret;
1892 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 const mbedtls_md_info_t *md_info;
1894 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1897 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001898
1899 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001901
1902 olen = ctx->len;
1903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001905 {
Simon Butcher02037452016-03-01 21:19:12 +00001906 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001908 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001912 }
1913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001915 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001919 slen = hlen;
1920
1921 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001923
1924 memset( sig, 0, olen );
1925
Simon Butcher02037452016-03-01 21:19:12 +00001926 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001927 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001929
Simon Butcher02037452016-03-01 21:19:12 +00001930 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001931 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001932 p += olen - hlen * 2 - 2;
1933 *p++ = 0x01;
1934 memcpy( p, salt, slen );
1935 p += slen;
1936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001938 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1939 {
1940 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001941 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001942 return( ret );
1943 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001944
Simon Butcher02037452016-03-01 21:19:12 +00001945 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 mbedtls_md_starts( &md_ctx );
1947 mbedtls_md_update( &md_ctx, p, 8 );
1948 mbedtls_md_update( &md_ctx, hash, hashlen );
1949 mbedtls_md_update( &md_ctx, salt, slen );
1950 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001951 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001952
Simon Butcher02037452016-03-01 21:19:12 +00001953 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001954 if( msb % 8 == 0 )
1955 offset = 1;
1956
Simon Butcher02037452016-03-01 21:19:12 +00001957 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001958 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001961
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001962 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001963 sig[0] &= 0xFF >> ( olen * 8 - msb );
1964
1965 p += hlen;
1966 *p++ = 0xBC;
1967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 return( ( mode == MBEDTLS_RSA_PUBLIC )
1969 ? mbedtls_rsa_public( ctx, sig, sig )
1970 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001975/*
1976 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1977 */
1978/*
1979 * Do an RSA operation to sign the message digest
1980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001982 int (*f_rng)(void *, unsigned char *, size_t),
1983 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001984 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001986 unsigned int hashlen,
1987 const unsigned char *hash,
1988 unsigned char *sig )
1989{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001990 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001991 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001992 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001993 unsigned char *sig_try = NULL, *verif = NULL;
1994 size_t i;
1995 unsigned char diff;
1996 volatile unsigned char diff_no_optimize;
1997 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2000 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002001
2002 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002003 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01002004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002008 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
2012 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002013
Paul Bakkerc70b9822013-04-07 22:00:46 +02002014 nb_pad -= 10 + oid_size;
2015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002017 }
2018
Paul Bakkerc70b9822013-04-07 22:00:46 +02002019 nb_pad -= hashlen;
2020
Paul Bakkerb3869132013-02-28 17:21:01 +01002021 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002023
2024 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01002026 memset( p, 0xFF, nb_pad );
2027 p += nb_pad;
2028 *p++ = 0;
2029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002031 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002032 memcpy( p, hash, hashlen );
2033 }
2034 else
2035 {
2036 /*
2037 * DigestInfo ::= SEQUENCE {
2038 * digestAlgorithm DigestAlgorithmIdentifier,
2039 * digest Digest }
2040 *
2041 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2042 *
2043 * Digest ::= OCTET STRING
2044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002046 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002048 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002050 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002051 memcpy( p, oid, oid_size );
2052 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002054 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002056 *p++ = hashlen;
2057 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002058 }
2059
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002060 if( mode == MBEDTLS_RSA_PUBLIC )
2061 return( mbedtls_rsa_public( ctx, sig, sig ) );
2062
2063 /*
2064 * In order to prevent Lenstra's attack, make the signature in a
2065 * temporary buffer and check it before returning it.
2066 */
2067 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002068 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002069 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2070
Simon Butcher1285ab52016-01-01 21:42:47 +00002071 verif = mbedtls_calloc( 1, ctx->len );
2072 if( verif == NULL )
2073 {
2074 mbedtls_free( sig_try );
2075 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2076 }
2077
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002078 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2079 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2080
2081 /* Compare in constant time just in case */
2082 for( diff = 0, i = 0; i < ctx->len; i++ )
2083 diff |= verif[i] ^ sig[i];
2084 diff_no_optimize = diff;
2085
2086 if( diff_no_optimize != 0 )
2087 {
2088 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2089 goto cleanup;
2090 }
2091
2092 memcpy( sig, sig_try, ctx->len );
2093
2094cleanup:
2095 mbedtls_free( sig_try );
2096 mbedtls_free( verif );
2097
2098 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002101
2102/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 * Do an RSA operation to sign the message digest
2104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002106 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002107 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002110 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002111 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 unsigned char *sig )
2113{
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 switch( ctx->padding )
2115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#if defined(MBEDTLS_PKCS1_V15)
2117 case MBEDTLS_RSA_PKCS_V15:
2118 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002119 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002120#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#if defined(MBEDTLS_PKCS1_V21)
2123 case MBEDTLS_RSA_PKCS_V21:
2124 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002125 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002126#endif
2127
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002131}
2132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002134/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002135 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002138 int (*f_rng)(void *, unsigned char *, size_t),
2139 void *p_rng,
2140 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002142 unsigned int hashlen,
2143 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002145 int expected_salt_len,
2146 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002147{
Paul Bakker23986e52011-04-24 08:57:21 +00002148 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002149 size_t siglen;
2150 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002152 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002153 unsigned int hlen;
2154 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 const mbedtls_md_info_t *md_info;
2156 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002157 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2160 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002161
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 siglen = ctx->len;
2163
Paul Bakker27fdf462011-06-09 13:55:13 +00002164 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2168 ? mbedtls_rsa_public( ctx, sig, buf )
2169 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
2171 if( ret != 0 )
2172 return( ret );
2173
2174 p = buf;
2175
Paul Bakkerb3869132013-02-28 17:21:01 +01002176 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 {
Simon Butcher02037452016-03-01 21:19:12 +00002181 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002183 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002187 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002190 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002194 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002195
Paul Bakkerb3869132013-02-28 17:21:01 +01002196 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002197
Simon Butcher02037452016-03-01 21:19:12 +00002198 /*
2199 * Note: EMSA-PSS verification is over the length of N - 1 bits
2200 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002201 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002202
Simon Butcher02037452016-03-01 21:19:12 +00002203 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002204 if( msb % 8 == 0 )
2205 {
2206 p++;
2207 siglen -= 1;
2208 }
2209 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002213 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2214 {
2215 mbedtls_md_free( &md_ctx );
2216 return( ret );
2217 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002218
Paul Bakkerb3869132013-02-28 17:21:01 +01002219 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002220
Paul Bakkerb3869132013-02-28 17:21:01 +01002221 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002222
Paul Bakker4de44aa2013-12-31 11:43:01 +01002223 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002224 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002225
Paul Bakkerb3869132013-02-28 17:21:01 +01002226 if( p == buf + siglen ||
2227 *p++ != 0x01 )
2228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 mbedtls_md_free( &md_ctx );
2230 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002231 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002232
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002233 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002234 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002237 slen != (size_t) expected_salt_len )
2238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 mbedtls_md_free( &md_ctx );
2240 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002241 }
2242
Simon Butcher02037452016-03-01 21:19:12 +00002243 /*
2244 * Generate H = Hash( M' )
2245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 mbedtls_md_starts( &md_ctx );
2247 mbedtls_md_update( &md_ctx, zeros, 8 );
2248 mbedtls_md_update( &md_ctx, hash, hashlen );
2249 mbedtls_md_update( &md_ctx, p, slen );
2250 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002253
Paul Bakkerb3869132013-02-28 17:21:01 +01002254 if( memcmp( p + slen, result, hlen ) == 0 )
2255 return( 0 );
2256 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002258}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002259
2260/*
2261 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002264 int (*f_rng)(void *, unsigned char *, size_t),
2265 void *p_rng,
2266 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002268 unsigned int hashlen,
2269 const unsigned char *hash,
2270 const unsigned char *sig )
2271{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2273 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002274 : md_alg;
2275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002277 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002279 sig ) );
2280
2281}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002285/*
2286 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002289 int (*f_rng)(void *, unsigned char *, size_t),
2290 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002291 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002293 unsigned int hashlen,
2294 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002295 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002296{
2297 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002298 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002299 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 mbedtls_md_type_t msg_md_alg;
2301 const mbedtls_md_info_t *md_info;
2302 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002303 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2306 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002307
2308 siglen = ctx->len;
2309
2310 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2314 ? mbedtls_rsa_public( ctx, sig, buf )
2315 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002316
2317 if( ret != 0 )
2318 return( ret );
2319
2320 p = buf;
2321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2323 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002324
2325 while( *p != 0 )
2326 {
2327 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002329 p++;
2330 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002331 p++; /* skip 00 byte */
2332
2333 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2334 if( p - buf < 11 )
2335 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002336
2337 len = siglen - ( p - buf );
2338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002340 {
2341 if( memcmp( p, hash, hashlen ) == 0 )
2342 return( 0 );
2343 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 }
2346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002348 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2350 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002351
2352 end = p + len;
2353
Simon Butcher02037452016-03-01 21:19:12 +00002354 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002355 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2356 * Insist on 2-byte length tags, to protect against variants of
2357 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002358 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002359 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2361 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2362 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002363 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002365
Gilles Peskinee7e76502017-05-04 12:48:39 +02002366 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2368 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2369 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002370 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002372
Gilles Peskinee7e76502017-05-04 12:48:39 +02002373 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2375 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002376 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002377 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002378
2379 oid.p = p;
2380 p += oid.len;
2381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2383 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002384
2385 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002387
2388 /*
2389 * assume the algorithm parameters must be NULL
2390 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002391 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2393 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002394 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002396
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002397 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002398 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2399 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002400 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002402
2403 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002405
2406 p += hashlen;
2407
2408 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002410
2411 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
2415/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002416 * Do an RSA operation and check the message digest
2417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002419 int (*f_rng)(void *, unsigned char *, size_t),
2420 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002421 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002423 unsigned int hashlen,
2424 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002425 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002426{
2427 switch( ctx->padding )
2428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429#if defined(MBEDTLS_PKCS1_V15)
2430 case MBEDTLS_RSA_PKCS_V15:
2431 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002432 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002433#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435#if defined(MBEDTLS_PKCS1_V21)
2436 case MBEDTLS_RSA_PKCS_V21:
2437 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002438 hashlen, hash, sig );
2439#endif
2440
2441 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002443 }
2444}
2445
2446/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002447 * Copy the components of an RSA key
2448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002450{
2451 int ret;
2452
2453 dst->ver = src->ver;
2454 dst->len = src->len;
2455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2457 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2460 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2461 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002462
2463#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2465 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2466 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2468 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002469#endif
2470
2471 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2474 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002475
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002476 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002477 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002478
2479cleanup:
2480 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002482
2483 return( ret );
2484}
2485
2486/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 * Free the components of an RSA key
2488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002490{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002492 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2493 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002495
Hanno Becker33c30a02017-08-23 07:00:22 +01002496#if !defined(MBEDTLS_RSA_NO_CRT)
2497 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2498 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2499 mbedtls_mpi_free( &ctx->DP );
2500#endif /* MBEDTLS_RSA_NO_CRT */
2501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502#if defined(MBEDTLS_THREADING_C)
2503 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002504#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002505}
2506
Hanno Beckerab377312017-08-23 16:24:51 +01002507#endif /* !MBEDTLS_RSA_ALT */
2508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002511#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
2513/*
2514 * Example RSA-1024 keypair, for test purposes
2515 */
2516#define KEY_LEN 128
2517
2518#define RSA_N "9292758453063D803DD603D5E777D788" \
2519 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2520 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2521 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2522 "93A89813FBF3C4F8066D2D800F7C38A8" \
2523 "1AE31942917403FF4946B0A83D3D3E05" \
2524 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2525 "5E94BB77B07507233A0BC7BAC8F90F79"
2526
2527#define RSA_E "10001"
2528
2529#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2530 "66CA472BC44D253102F8B4A9D3BFA750" \
2531 "91386C0077937FE33FA3252D28855837" \
2532 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2533 "DF79C5CE07EE72C7F123142198164234" \
2534 "CABB724CF78B8173B9F880FC86322407" \
2535 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2536 "071513A1E85B5DFA031F21ECAE91A34D"
2537
2538#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2539 "2C01CAD19EA484A87EA4377637E75500" \
2540 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2541 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2542
2543#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2544 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2545 "910E4168387E3C30AA1E00C339A79508" \
2546 "8452DD96A9A5EA5D9DCA68DA636032AF"
2547
2548#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2549 "3C94D22288ACD763FD8E5600ED4A702D" \
2550 "F84198A5F06C2E72236AE490C93F07F8" \
2551 "3CC559CD27BC2D1CA488811730BB5725"
2552
2553#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2554 "D8AAEA56749EA28623272E4F7D0592AF" \
2555 "7C1F1313CAC9471B5C523BFE592F517B" \
2556 "407A1BD76C164B93DA2D32A383E58357"
2557
2558#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2559 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2560 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2561 "A74206CEC169D74BF5A8C50D6F48EA08"
2562
2563#define PT_LEN 24
2564#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2565 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002568static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002569{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002570#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002571 size_t i;
2572
Paul Bakker545570e2010-07-18 09:00:25 +00002573 if( rng_state != NULL )
2574 rng_state = NULL;
2575
Paul Bakkera3d195c2011-11-27 21:07:34 +00002576 for( i = 0; i < len; ++i )
2577 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002578#else
2579 if( rng_state != NULL )
2580 rng_state = NULL;
2581
2582 arc4random_buf( output, len );
2583#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002584
Paul Bakkera3d195c2011-11-27 21:07:34 +00002585 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002586}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002588
Paul Bakker5121ce52009-01-03 21:22:43 +00002589/*
2590 * Checkup routine
2591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002593{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002594 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002596 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 unsigned char rsa_plaintext[PT_LEN];
2599 unsigned char rsa_decrypted[PT_LEN];
2600 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002602 unsigned char sha1sum[20];
2603#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Hanno Becker3a701162017-08-22 13:52:43 +01002605 mbedtls_mpi K;
2606
2607 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
Hanno Becker3a701162017-08-22 13:52:43 +01002610 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2611 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2612 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2613 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2614 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2615 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2616 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2617 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2618 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2619 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2620
2621 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
2623 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2627 mbedtls_rsa_check_privkey( &rsa ) != 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
Hanno Becker3a701162017-08-22 13:52:43 +01002635 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2636 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2637
2638 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2639 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2640
2641 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2642 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2643
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
2647 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 rsa_plaintext, rsa_ciphertext ) != 0 )
2651 {
2652 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
2655 return( 1 );
2656 }
2657
2658 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002662 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002663 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 {
2665 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
2668 return( 1 );
2669 }
2670
2671 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2672 {
2673 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
2676 return( 1 );
2677 }
2678
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002679 if( verbose != 0 )
2680 mbedtls_printf( "passed\n" );
2681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002684 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 sha1sum, rsa_ciphertext ) != 0 )
2690 {
2691 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
2694 return( 1 );
2695 }
2696
2697 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 sha1sum, rsa_ciphertext ) != 0 )
2702 {
2703 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
2706 return( 1 );
2707 }
2708
2709 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002710 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002713 if( verbose != 0 )
2714 mbedtls_printf( "\n" );
2715
Paul Bakker3d8fb632014-04-17 12:42:41 +02002716cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002717 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 mbedtls_rsa_free( &rsa );
2719#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002720 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002722 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002723}
2724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727#endif /* MBEDTLS_RSA_C */