blob: 073bde528d531cbc102d7c491a9ef68cb558ffe3 [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 *
Hanno Beckerd9431a72017-08-25 08:03:13 +010077 * There are two classes of helper functions:
78 * (1) Parameter-generating helpers. These are:
79 * - mbedtls_rsa_deduce_moduli
80 * - mbedtls_rsa_deduce_private
81 * - mbedtls_rsa_deduce_crt
82 * Each of these functions takes a set of core RSA parameters
83 * and generates some other, or CRT related parameters.
84 * (2) Parameter-checking helpers. These are:
85 * - mbedtls_rsa_validate_params
86 * - mbedtls_rsa_validate_crt
87 * They take a set of core or CRT related RSA parameters
88 * and check their validity.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010089 *
Hanno Beckerd9431a72017-08-25 08:03:13 +010090 * The helper functions do not use the RSA context structure
91 * and therefore do not need to be replaced when providing
92 * an alternative RSA implementation.
93 *
94 * Their main purpose is to provide common MPI operations in the context
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010095 * of RSA that can be easily shared across multiple implementations.
96 */
97
98/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010099 *
100 * Given the modulus N=PQ and a pair of public and private
101 * exponents E and D, respectively, factor N.
102 *
103 * Setting F := lcm(P-1,Q-1), the idea is as follows:
104 *
105 * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
106 * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
107 * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
108 * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
109 * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
110 * factors of N.
111 *
112 * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
113 * construction still applies since (-)^K is the identity on the set of
114 * roots of 1 in Z/NZ.
115 *
116 * The public and private key primitives (-)^E and (-)^D are mutually inverse
117 * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
118 * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
119 * Splitting L = 2^t * K with K odd, we have
120 *
121 * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
122 *
123 * so (F / 2) * K is among the numbers
124 *
125 * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
126 *
127 * where ord is the order of 2 in (DE - 1).
128 * We can therefore iterate through these numbers apply the construction
129 * of (a) and (b) above to attempt to factor N.
130 *
131 */
132int mbedtls_rsa_deduce_moduli( mbedtls_mpi *N, mbedtls_mpi *D, mbedtls_mpi *E,
133 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
134 mbedtls_mpi *P, mbedtls_mpi *Q )
135{
136 /* Implementation note:
137 *
138 * Space-efficiency is given preference over time-efficiency here:
139 * several calculations are done in place and temporarily change
140 * the values of D and E.
141 *
142 * Specifically, D is replaced the largest odd divisor of DE - 1
143 * throughout the calculations.
144 */
145
146 int ret = 0;
147
148 uint16_t attempt; /* Number of current attempt */
149 uint16_t iter; /* Number of squares computed in the current attempt */
150
151 uint16_t bitlen_half; /* Half the bitsize of the modulus N */
152 uint16_t order; /* Order of 2 in DE - 1 */
153
154 mbedtls_mpi K; /* Temporary used for two purposes:
155 * - During factorization attempts, stores a andom integer
156 * in the range of [0,..,N]
157 * - During verification, holding intermediate results.
158 */
159
160 if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
161 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
162
163 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
164 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
165 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
166 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
167 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
168 {
169 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
170 }
171
172 /*
173 * Initializations and temporary changes
174 */
175
176 mbedtls_mpi_init( &K );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100177
178 /* Replace D by DE - 1 */
179 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( D, D, E ) );
180 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( D, D, 1 ) );
181
182 if( ( order = mbedtls_mpi_lsb( D ) ) == 0 )
183 {
184 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
185 goto cleanup;
186 }
187
188 /* After this operation, D holds the largest odd divisor
189 * of DE - 1 for the original values of D and E. */
190 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( D, order ) );
191
192 /* This is used to generate a few numbers around N / 2
193 * if no PRNG is provided. */
194 if( f_rng == NULL )
195 bitlen_half = mbedtls_mpi_bitlen( N ) / 2;
196
197 /*
198 * Actual work
199 */
200
201 for( attempt = 0; attempt < 30; ++attempt )
202 {
203 /* Generate some number in [0,N], either randomly
204 * if a PRNG is given, or try numbers around N/2 */
205 if( f_rng != NULL )
206 {
207 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &K,
208 mbedtls_mpi_size( N ),
209 f_rng, p_rng ) );
210 }
211 else
212 {
213 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &K, 1 ) ) ;
214 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &K, bitlen_half ) ) ;
215 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, attempt + 1 ) );
216 }
217
218 /* Check if gcd(K,N) = 1 */
219 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
220 if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
221 continue;
222
223 /* Go through K^X + 1, K^(2X) + 1, K^(4X) + 1, ...
224 * and check whether they have nontrivial GCD with N. */
225 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, D, N,
226 Q /* temporarily use Q for storing Montgomery
227 * multiplication helper values */ ) );
228
229 for( iter = 1; iter < order; ++iter )
230 {
231 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
232 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
233
234 if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
235 mbedtls_mpi_cmp_mpi( P, N ) == -1 )
236 {
237 /*
238 * Have found a nontrivial divisor P of N.
Hanno Beckerd56d83a2017-08-25 07:29:35 +0100239 * Set Q := N / P.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100240 */
241
242 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, &K, N, P ) );
243
Hanno Beckerd56d83a2017-08-25 07:29:35 +0100244 /* Restore D */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100245
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100246 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( D, order ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100247 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( D, D, 1 ) );
248 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( D, NULL, D, E ) );
249
250 goto cleanup;
251 }
252
253 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
254 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
255 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
256 }
257 }
258
259 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
260
261cleanup:
262
263 mbedtls_mpi_free( &K );
264 return( ret );
265}
266
267/*
268 * Given P, Q and the public exponent E, deduce D.
269 * This is essentially a modular inversion.
270 */
271
272int mbedtls_rsa_deduce_private( mbedtls_mpi *P, mbedtls_mpi *Q,
273 mbedtls_mpi *D, mbedtls_mpi *E )
274{
275 int ret = 0;
276 mbedtls_mpi K;
277
278 if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
279 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
280
281 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
282 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
283 mbedtls_mpi_cmp_int( E, 0 ) == 0 )
284 {
285 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
286 }
287
288 mbedtls_mpi_init( &K );
289
290 /* Temporarily replace P and Q by P-1 and Q-1, respectively. */
291 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) );
292 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) );
293
294 /* Temporarily compute the gcd(P-1, Q-1) in D. */
295 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, P, Q ) );
296
297 /* Compute LCM(P-1, Q-1) in K */
298 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
299 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
300
301 /* Compute modular inverse of E in LCM(P-1, Q-1) */
302 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
303
304 /* Restore P and Q. */
305 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) );
306 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) );
307
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100308cleanup:
309
310 mbedtls_mpi_free( &K );
311
312 return( ret );
313}
314
315/*
Hanno Beckerd3637992017-08-25 07:55:03 +0100316 * Check that RSA CRT parameters are in accordance with core parameters.
317 */
318
319int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
320 const mbedtls_mpi *D, const mbedtls_mpi *DP,
321 const mbedtls_mpi *DQ, const mbedtls_mpi *QP )
322{
323 int ret = 0;
324
325 mbedtls_mpi K, L;
326 mbedtls_mpi_init( &K );
327 mbedtls_mpi_init( &L );
328
329 /* Check that DP - P == 0 mod P - 1 */
330 if( DP != NULL )
331 {
332 if( P == NULL )
333 {
334 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
335 goto cleanup;
336 }
337
338 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
339 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DP, D ) );
340 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
341
342 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
343 {
344 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
345 }
346 }
347
348 /* Check that DQ - Q == 0 mod Q - 1 */
349 if( DQ != NULL )
350 {
351 if( Q == NULL )
352 {
353 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
354 goto cleanup;
355 }
356
357 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
358 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
359 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
360
361 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
362 {
363 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
364 }
365 }
366
367 /* Check that QP * P - 1 == 0 mod P */
368 if( QP != NULL )
369 {
370 if( P == NULL || Q == NULL )
371 {
372 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
373 goto cleanup;
374 }
375
376 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
377 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
378 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
379 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
380 {
381 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
382 }
383 }
384
385cleanup:
386
387 /* Wrap MPI error codes by RSA check failure error code */
388 if( ret != 0 &&
389 ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
390 ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
391 {
392 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
393 }
394
395 mbedtls_mpi_free( &K );
396 mbedtls_mpi_free( &L );
397
398 return( ret );
399}
400
401/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100402 * Check that core RSA parameters are sane.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100403 */
404
Hanno Becker750e8b42017-08-25 07:54:27 +0100405int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
406 const mbedtls_mpi *Q, const mbedtls_mpi *D,
407 const mbedtls_mpi *E,
408 int (*f_rng)(void *, unsigned char *, size_t),
409 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100410{
411 int ret = 0;
Hanno Becker750e8b42017-08-25 07:54:27 +0100412 mbedtls_mpi K, L;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100413
414 mbedtls_mpi_init( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100415 mbedtls_mpi_init( &L );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100416
417 /*
418 * Step 1: If PRNG provided, check that P and Q are prime
419 */
420
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100421#if defined(MBEDTLS_GENPRIME)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100422 if( f_rng != NULL && P != NULL &&
423 ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
424 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100425 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100426 goto cleanup;
427 }
428
429 if( f_rng != NULL && Q != NULL &&
430 ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
431 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100432 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100433 goto cleanup;
434 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100435#else
436 ((void) f_rng);
437 ((void) p_rng);
438#endif /* MBEDTLS_GENPRIME */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100439
440 /*
441 * Step 2: Check that N = PQ
442 */
443
444 if( P != NULL && Q != NULL && N != NULL )
445 {
446 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
Hanno Becker750e8b42017-08-25 07:54:27 +0100447 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
448 mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100449 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100450 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100451 goto cleanup;
452 }
453 }
454
455 /*
456 * Step 3: Check that D, E are inverse modulo P-1 and Q-1
457 */
458
459 if( P != NULL && Q != NULL && D != NULL && E != NULL )
460 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100461 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
462 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
463 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
464 mbedtls_mpi_cmp_int( E, 1 ) <= 0 )
465 {
466 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
467 goto cleanup;
468 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100469
470 /* Compute DE-1 mod P-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100471 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
472 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
473 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
474 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100475 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
476 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100477 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100478 goto cleanup;
479 }
480
481 /* Compute DE-1 mod Q-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100482 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
483 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
484 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
485 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100486 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
487 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100488 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100489 goto cleanup;
490 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100491 }
492
493cleanup:
494
495 mbedtls_mpi_free( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100496 mbedtls_mpi_free( &L );
497
498 /* Wrap MPI error codes by RSA check failure error code */
499 if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
500 {
501 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
502 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100503
504 return( ret );
505}
506
507int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
508 const mbedtls_mpi *D, mbedtls_mpi *DP,
509 mbedtls_mpi *DQ, mbedtls_mpi *QP )
510{
511 int ret = 0;
512 mbedtls_mpi K;
513 mbedtls_mpi_init( &K );
514
Hanno Beckerd9431a72017-08-25 08:03:13 +0100515 /* DP = D mod P-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100516 if( DP != NULL )
517 {
518 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
519 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
520 }
521
Hanno Beckerd9431a72017-08-25 08:03:13 +0100522 /* DQ = D mod Q-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100523 if( DQ != NULL )
524 {
525 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
526 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
527 }
528
Hanno Beckerd9431a72017-08-25 08:03:13 +0100529 /* QP = Q^{-1} mod P */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100530 if( QP != NULL )
531 {
532 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
533 }
534
535cleanup:
536 mbedtls_mpi_free( &K );
537
538 return( ret );
539}
540
Hanno Becker617c1ae2017-08-23 14:11:24 +0100541
542/*
543 * Default RSA interface implementation
544 */
545
Hanno Beckerab377312017-08-23 16:24:51 +0100546#if !defined(MBEDTLS_RSA_ALT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100547
548int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
549 const mbedtls_mpi *N,
550 const mbedtls_mpi *P, const mbedtls_mpi *Q,
551 const mbedtls_mpi *D, const mbedtls_mpi *E )
552{
553 int ret;
554
555 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
556 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
557 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
558 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
559 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
560 {
561 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
562 }
563
564 if( N != NULL )
565 ctx->len = mbedtls_mpi_size( &ctx->N );
566
567 return( 0 );
568}
569
570int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
571 unsigned char *N, size_t N_len,
572 unsigned char *P, size_t P_len,
573 unsigned char *Q, size_t Q_len,
574 unsigned char *D, size_t D_len,
575 unsigned char *E, size_t E_len )
576{
577 int ret;
578
579 if( N != NULL )
580 {
581 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
582 ctx->len = mbedtls_mpi_size( &ctx->N );
583 }
584
585 if( P != NULL )
586 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
587
588 if( Q != NULL )
589 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
590
591 if( D != NULL )
592 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
593
594 if( E != NULL )
595 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
596
597cleanup:
598
599 if( ret != 0 )
600 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
601
602 return( 0 );
603}
604
605int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
606 int (*f_rng)(void *, unsigned char *, size_t),
607 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100608{
609 int ret = 0;
610
Hanno Becker617c1ae2017-08-23 14:11:24 +0100611 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
612 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
613 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
614 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
615 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100616
Hanno Becker617c1ae2017-08-23 14:11:24 +0100617 /*
618 * Check whether provided parameters are enough
619 * to deduce all others. The following incomplete
620 * parameter sets for private keys are supported:
621 *
622 * (1) P, Q missing.
623 * (2) D and potentially N missing.
624 *
625 */
626 const int complete = have_N && have_P && have_Q && have_D && have_E;
627 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
628 const int d_missing = have_P && have_Q && !have_D && have_E;
629 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100630
Hanno Becker617c1ae2017-08-23 14:11:24 +0100631 const int is_priv = complete || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100632
Hanno Becker617c1ae2017-08-23 14:11:24 +0100633 if( !is_priv && !is_pub )
634 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
635
636 /*
637 * Step 1: Deduce and verify all core parameters.
638 */
639
640 if( pq_missing )
641 {
642 /* This includes sanity checking of core parameters,
643 * so no further checks necessary. */
644 ret = mbedtls_rsa_deduce_moduli( &ctx->N, &ctx->D, &ctx->E,
645 f_rng, p_rng,
646 &ctx->P, &ctx->Q );
647 if( ret != 0 )
648 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
649
650 }
651 else if( d_missing )
652 {
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100653#if defined(MBEDTLS_GENPRIME)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100654 /* If a PRNG is provided, check if P, Q are prime. */
655 if( f_rng != NULL &&
656 ( ( ret = mbedtls_mpi_is_prime( &ctx->P, f_rng, p_rng ) ) != 0 ||
657 ( ret = mbedtls_mpi_is_prime( &ctx->Q, f_rng, p_rng ) ) != 0 ) )
658 {
659 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
660 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100661#endif /* MBEDTLS_GENPRIME */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100662
663 /* Compute N if missing. */
664 if( !have_N &&
665 ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) ) != 0 )
666 {
667 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
668 }
669
670 /* Deduce private exponent. This includes double-checking of the result,
671 * so together with the primality test above all core parameters are
672 * guaranteed to be sane if this call succeeds. */
673 if( ( ret = mbedtls_rsa_deduce_private( &ctx->P, &ctx->Q,
674 &ctx->D, &ctx->E ) ) != 0 )
675 {
676 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
677 }
678 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100679
680 /* In the remaining case of a public key, there's nothing to check for. */
681
682 /*
683 * Step 2: Deduce all additional parameters specific
684 * to our current RSA implementaiton.
685 */
686
Hanno Becker23344b52017-08-23 07:43:27 +0100687#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100688 if( is_priv )
689 {
690 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
691 &ctx->DP, &ctx->DQ, &ctx->QP );
692 if( ret != 0 )
693 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
694 }
Hanno Becker23344b52017-08-23 07:43:27 +0100695#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100696
697 /*
698 * Step 3: Double check
699 */
700
701 if( is_priv )
702 {
703 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
704 return( ret );
705 }
706 else
707 {
708 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
709 return( ret );
710 }
711
712 return( 0 );
713}
714
715/*
716 * Check if CRT parameters match RSA context.
717 * This has to be implemented even if CRT is not used,
718 * in order to be able to validate DER encoded RSA keys,
719 * which always contain CRT parameters.
720 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100721int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx,
722 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100723{
Hanno Becker23344b52017-08-23 07:43:27 +0100724 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100725
Hanno Becker23344b52017-08-23 07:43:27 +0100726 /* Check if key is private or public */
727 const int is_priv =
728 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
729 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
730 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
731 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
732 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
733
734 if( !is_priv )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100735 {
736 /* Checking optional parameters only makes sense for private keys. */
737 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
738 }
739
Hanno Becker23344b52017-08-23 07:43:27 +0100740#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100741 if( ( DP != NULL && mbedtls_mpi_cmp_mpi( DP, &ctx->DP ) != 0 ) ||
742 ( DQ != NULL && mbedtls_mpi_cmp_mpi( DQ, &ctx->DQ ) != 0 ) ||
743 ( QP != NULL && mbedtls_mpi_cmp_mpi( QP, &ctx->QP ) != 0 ) )
744 {
745 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
746 }
Hanno Becker23344b52017-08-23 07:43:27 +0100747#else /* MBEDTLS_RSA_NO_CRT */
Hanno Beckerd3637992017-08-25 07:55:03 +0100748 if( ( ret = mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
749 DP, DQ, QP ) ) != 0 )
Hanno Becker23344b52017-08-23 07:43:27 +0100750 {
Hanno Beckerd3637992017-08-25 07:55:03 +0100751 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker23344b52017-08-23 07:43:27 +0100752 }
Hanno Becker23344b52017-08-23 07:43:27 +0100753#endif
754
755 if( ret != 0 )
756 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100757
758 return( 0 );
759}
760
761int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
762 unsigned char *N, size_t N_len,
763 unsigned char *P, size_t P_len,
764 unsigned char *Q, size_t Q_len,
765 unsigned char *D, size_t D_len,
766 unsigned char *E, size_t E_len )
767{
768 int ret = 0;
769
770 /* Check if key is private or public */
771 const int is_priv =
772 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
773 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
774 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
775 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
776 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
777
778 if( !is_priv )
779 {
780 /* If we're trying to export private parameters for a public key,
781 * something must be wrong. */
782 if( P != NULL || Q != NULL || D != NULL )
783 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
784
785 }
786
787 if( N != NULL )
788 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
789
790 if( P != NULL )
791 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
792
793 if( Q != NULL )
794 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
795
796 if( D != NULL )
797 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
798
799 if( E != NULL )
800 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100801
802cleanup:
803
804 return( ret );
805}
806
Hanno Becker617c1ae2017-08-23 14:11:24 +0100807int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
808 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
809 mbedtls_mpi *D, mbedtls_mpi *E )
810{
811 int ret;
812
813 /* Check if key is private or public */
814 int is_priv =
815 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
816 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
817 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
818 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
819 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
820
821 if( !is_priv )
822 {
823 /* If we're trying to export private parameters for a public key,
824 * something must be wrong. */
825 if( P != NULL || Q != NULL || D != NULL )
826 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
827
828 }
829
830 /* Export all requested core parameters. */
831
832 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
833 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
834 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
835 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
836 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
837 {
838 return( ret );
839 }
840
841 return( 0 );
842}
843
844/*
845 * Export CRT parameters
846 * This must also be implemented if CRT is not used, for being able to
847 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
848 * can be used in this case.
849 */
850int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
851 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
852{
853 int ret;
854
855 /* Check if key is private or public */
856 int is_priv =
857 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
858 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
859 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
860 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
861 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
862
863 if( !is_priv )
864 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
865
Hanno Beckerdc95c892017-08-23 06:57:02 +0100866#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100867 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100868 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
869 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
870 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
871 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100872 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100873 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100874#else
875 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
876 DP, DQ, QP ) ) != 0 )
877 {
878 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
879 }
880#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100881
882 return( 0 );
883}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100884
885/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000886 * Initialize an RSA context
887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000890 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000891{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#if defined(MBEDTLS_THREADING_C)
897 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200898#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000899}
900
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100901/*
902 * Set padding for an existing RSA context
903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100905{
906 ctx->padding = padding;
907 ctx->hash_id = hash_id;
908}
909
Hanno Becker617c1ae2017-08-23 14:11:24 +0100910/*
911 * Get length in bytes of RSA modulus
912 */
913
914size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
915{
916 return( mbedtls_mpi_size( &ctx->N ) );
917}
918
919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000921
922/*
923 * Generate an RSA keypair
924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000926 int (*f_rng)(void *, unsigned char *, size_t),
927 void *p_rng,
928 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000929{
930 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100931 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000932
Paul Bakker21eb2802010-08-16 11:10:02 +0000933 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Janos Follathef441782016-09-21 13:18:12 +0100936 if( nbits % 2 )
937 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
938
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100939 mbedtls_mpi_init( &H );
940 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
942 /*
943 * find primes P and Q with Q < P so that:
944 * GCD( E, (P-1)*(Q-1) ) == 1
945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000947
948 do
949 {
Janos Follath10c575b2016-02-23 14:42:48 +0000950 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100951 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
Janos Follathef441782016-09-21 13:18:12 +0100953 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100954 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000957 continue;
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200960 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000961 continue;
962
Janos Follathef441782016-09-21 13:18:12 +0100963 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100964 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100965
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100966 /* Temporarily replace P,Q by P-1, Q-1 */
967 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
968 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
969 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000971 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100974 /* Restore P,Q */
975 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
976 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
977
978 ctx->len = mbedtls_mpi_size( &ctx->N );
979
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 /*
981 * D = E^-1 mod ((P-1)*(Q-1))
982 * DP = D mod (P - 1)
983 * DQ = D mod (Q - 1)
984 * QP = Q^-1 mod P
985 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100987 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
988
989#if !defined(MBEDTLS_RSA_NO_CRT)
990 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
991 &ctx->DP, &ctx->DQ, &ctx->QP ) );
992#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
Hanno Becker83aad1f2017-08-23 06:45:10 +0100994 /* Double-check */
995 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
996
Paul Bakker5121ce52009-01-03 21:22:43 +0000997cleanup:
998
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100999 mbedtls_mpi_free( &H );
1000 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
1002 if( ret != 0 )
1003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_rsa_free( ctx );
1005 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 }
1007
Paul Bakker48377d92013-08-30 12:06:24 +02001008 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009}
1010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
1013/*
1014 * Check a public RSA key
1015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001017{
Paul Bakker37940d9f2009-07-10 22:38:58 +00001018 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +00001020
Paul Bakker48377d92013-08-30 12:06:24 +02001021 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +00001022 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001025 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
1026 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001029 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
1031 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
1033 return( 0 );
1034}
1035
1036/*
1037 * Check a private RSA key
1038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001040{
1041 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001045 return( ret );
1046
Paul Bakker37940d9f2009-07-10 22:38:58 +00001047 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +00001049
Hanno Becker6345dd32017-08-23 06:59:48 +01001050 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 );
1051 mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I );
1052 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 ); mbedtls_mpi_init( &L1 );
1053 mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
1057 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
1058 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1059 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1060 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
1061 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
1064 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
1065 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +00001066
Hanno Becker6345dd32017-08-23 06:59:48 +01001067#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
1069 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
1070 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Hanno Becker6345dd32017-08-23 06:59:48 +01001071#endif
1072
Paul Bakkerb572adf2010-07-18 08:29:32 +00001073 /*
1074 * Check for a valid PKCS1v2 private key
1075 */
Hanno Becker6345dd32017-08-23 06:59:48 +01001076 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
1077#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
1079 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
1080 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
Hanno Becker6345dd32017-08-23 06:59:48 +01001081#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
Hanno Becker6345dd32017-08-23 06:59:48 +01001083 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 }
Paul Bakker48377d92013-08-30 12:06:24 +02001088
Paul Bakker5121ce52009-01-03 21:22:43 +00001089cleanup:
Hanno Becker6345dd32017-08-23 06:59:48 +01001090 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 );
1091 mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I );
1092 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 ); mbedtls_mpi_free( &L1 );
1093 mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +00001097 return( ret );
1098
Paul Bakker6c591fa2011-05-05 11:49:20 +00001099 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001101
1102 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001103}
1104
1105/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001106 * Check if contexts holding a public and private key match
1107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1111 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001114 }
1115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1117 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001120 }
1121
1122 return( 0 );
1123}
1124
1125/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 * Do an RSA public key operation
1127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001129 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 unsigned char *output )
1131{
Paul Bakker23986e52011-04-24 08:57:21 +00001132 int ret;
1133 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001138#if defined(MBEDTLS_THREADING_C)
1139 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1140 return( ret );
1141#endif
1142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001147 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1148 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 }
1150
1151 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1153 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
1155cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001157 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1158 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001159#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
1163 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
1166 return( 0 );
1167}
1168
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001169/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001170 * Generate or update blinding values, see section 10 of:
1171 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001172 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001173 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001174 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001175static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001176 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1177{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001178 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001179
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001180 if( ctx->Vf.p != NULL )
1181 {
1182 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1184 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1185 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1186 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001187
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001188 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001189 }
1190
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001191 /* Unblinding value: Vf = random number, invertible mod N */
1192 do {
1193 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1197 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1198 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001199
1200 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1202 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 +02001203
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001204
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001205cleanup:
1206 return( ret );
1207}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001208
Paul Bakker5121ce52009-01-03 21:22:43 +00001209/*
Janos Follathe81102e2017-03-22 13:38:28 +00001210 * Exponent blinding supposed to prevent side-channel attacks using multiple
1211 * traces of measurements to recover the RSA key. The more collisions are there,
1212 * the more bits of the key can be recovered. See [3].
1213 *
1214 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1215 * observations on avarage.
1216 *
1217 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1218 * to make 2^112 observations on avarage.
1219 *
1220 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1221 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1222 * Thus in this sense with 28 byte blinding the security is not reduced by
1223 * side-channel attacks like the one in [3])
1224 *
1225 * This countermeasure does not help if the key recovery is possible with a
1226 * single trace.
1227 */
1228#define RSA_EXPONENT_BLINDING 28
1229
1230/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 * Do an RSA private key operation
1232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001234 int (*f_rng)(void *, unsigned char *, size_t),
1235 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001236 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 unsigned char *output )
1238{
Paul Bakker23986e52011-04-24 08:57:21 +00001239 int ret;
1240 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001242 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001243#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001244 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001245 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001246#else
1247 mbedtls_mpi DP_blind, DQ_blind;
1248 mbedtls_mpi *DP = &ctx->DP;
1249 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001250#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001251
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001252 /* Make sure we have private key info, prevent possible misuse */
1253 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
1254 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001257 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1258
1259
1260 if( f_rng != NULL )
1261 {
Janos Follathe81102e2017-03-22 13:38:28 +00001262#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001263 mbedtls_mpi_init( &D_blind );
1264#else
1265 mbedtls_mpi_init( &DP_blind );
1266 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001267#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001268 }
Janos Follathe81102e2017-03-22 13:38:28 +00001269
Paul Bakker5121ce52009-01-03 21:22:43 +00001270
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001271#if defined(MBEDTLS_THREADING_C)
1272 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1273 return( ret );
1274#endif
1275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1277 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001279 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1280 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 }
1282
Paul Bakkerf451bac2013-08-30 15:37:02 +02001283 if( f_rng != NULL )
1284 {
1285 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001286 * Blinding
1287 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001288 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001289 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1290 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001292
Janos Follathe81102e2017-03-22 13:38:28 +00001293 /*
1294 * Exponent blinding
1295 */
1296 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1297 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1298
Janos Follathf9203b42017-03-22 15:13:15 +00001299#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001300 /*
1301 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
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( &D_blind, &P1, &Q1 ) );
1306 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1307 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1308
1309 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001310#else
1311 /*
1312 * DP_blind = ( P - 1 ) * R + DP
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( &DP_blind, &P1, &R ) );
1317 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1318 &ctx->DP ) );
1319
1320 DP = &DP_blind;
1321
1322 /*
1323 * DQ_blind = ( Q - 1 ) * R + DQ
1324 */
1325 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1326 f_rng, p_rng ) );
1327 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1328 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1329 &ctx->DQ ) );
1330
1331 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001332#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001333 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001336 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001337#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001338 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001339 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 *
1341 * T1 = input ^ dP mod P
1342 * T2 = input ^ dQ mod Q
1343 */
Janos Follathf9203b42017-03-22 15:13:15 +00001344 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1345 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
1347 /*
1348 * T = (T1 - T2) * (Q^-1 mod P) mod P
1349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1351 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1352 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
1354 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001355 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1358 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1359#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001360
Paul Bakkerf451bac2013-08-30 15:37:02 +02001361 if( f_rng != NULL )
1362 {
1363 /*
1364 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001365 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001366 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001367 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001369 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
1371 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
1374cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001376 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1377 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001378#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001381 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1382
1383 if( f_rng != NULL )
1384 {
Janos Follathe81102e2017-03-22 13:38:28 +00001385#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001386 mbedtls_mpi_free( &D_blind );
1387#else
1388 mbedtls_mpi_free( &DP_blind );
1389 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001390#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
1393 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001395
1396 return( 0 );
1397}
1398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001400/**
1401 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1402 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001403 * \param dst buffer to mask
1404 * \param dlen length of destination buffer
1405 * \param src source of the mask generation
1406 * \param slen length of the source buffer
1407 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001408 */
Paul Bakker48377d92013-08-30 12:06:24 +02001409static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001411{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001413 unsigned char counter[4];
1414 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001415 unsigned int hlen;
1416 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001419 memset( counter, 0, 4 );
1420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001422
Simon Butcher02037452016-03-01 21:19:12 +00001423 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001424 p = dst;
1425
1426 while( dlen > 0 )
1427 {
1428 use_len = hlen;
1429 if( dlen < hlen )
1430 use_len = dlen;
1431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 mbedtls_md_starts( md_ctx );
1433 mbedtls_md_update( md_ctx, src, slen );
1434 mbedtls_md_update( md_ctx, counter, 4 );
1435 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001436
1437 for( i = 0; i < use_len; ++i )
1438 *p++ ^= mask[i];
1439
1440 counter[3]++;
1441
1442 dlen -= use_len;
1443 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001444
1445 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001450/*
1451 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001454 int (*f_rng)(void *, unsigned char *, size_t),
1455 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001456 int mode,
1457 const unsigned char *label, size_t label_len,
1458 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001459 const unsigned char *input,
1460 unsigned char *output )
1461{
1462 size_t olen;
1463 int ret;
1464 unsigned char *p = output;
1465 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 const mbedtls_md_info_t *md_info;
1467 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1470 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001471
1472 if( f_rng == NULL )
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001476 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001478
1479 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001481
Simon Butcher02037452016-03-01 21:19:12 +00001482 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001483 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001485
1486 memset( output, 0, olen );
1487
1488 *p++ = 0;
1489
Simon Butcher02037452016-03-01 21:19:12 +00001490 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001491 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001493
1494 p += hlen;
1495
Simon Butcher02037452016-03-01 21:19:12 +00001496 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001498 p += hlen;
1499 p += olen - 2 * hlen - 2 - ilen;
1500 *p++ = 1;
1501 memcpy( p, input, ilen );
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001504 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1505 {
1506 mbedtls_md_free( &md_ctx );
1507 return( ret );
1508 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001509
Simon Butcher02037452016-03-01 21:19:12 +00001510 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001511 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1512 &md_ctx );
1513
Simon Butcher02037452016-03-01 21:19:12 +00001514 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001515 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1516 &md_ctx );
1517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 return( ( mode == MBEDTLS_RSA_PUBLIC )
1521 ? mbedtls_rsa_public( ctx, output, output )
1522 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001527/*
1528 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001531 int (*f_rng)(void *, unsigned char *, size_t),
1532 void *p_rng,
1533 int mode, size_t ilen,
1534 const unsigned char *input,
1535 unsigned char *output )
1536{
1537 size_t nb_pad, olen;
1538 int ret;
1539 unsigned char *p = output;
1540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1542 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001543
Janos Follath1ed9f992016-03-18 11:45:44 +00001544 // We don't check p_rng because it won't be dereferenced here
1545 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001547
1548 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001549
Simon Butcher02037452016-03-01 21:19:12 +00001550 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001551 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001553
1554 nb_pad = olen - 3 - ilen;
1555
1556 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001560
1561 while( nb_pad-- > 0 )
1562 {
1563 int rng_dl = 100;
1564
1565 do {
1566 ret = f_rng( p_rng, p, 1 );
1567 } while( *p == 0 && --rng_dl && ret == 0 );
1568
Simon Butcher02037452016-03-01 21:19:12 +00001569 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001570 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001572
1573 p++;
1574 }
1575 }
1576 else
1577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001579
1580 while( nb_pad-- > 0 )
1581 *p++ = 0xFF;
1582 }
1583
1584 *p++ = 0;
1585 memcpy( p, input, ilen );
1586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 return( ( mode == MBEDTLS_RSA_PUBLIC )
1588 ? mbedtls_rsa_public( ctx, output, output )
1589 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001590}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001592
Paul Bakker5121ce52009-01-03 21:22:43 +00001593/*
1594 * Add the message padding, then do an RSA operation
1595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001597 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001598 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001599 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001600 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001601 unsigned char *output )
1602{
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 switch( ctx->padding )
1604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#if defined(MBEDTLS_PKCS1_V15)
1606 case MBEDTLS_RSA_PKCS_V15:
1607 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001608 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001609#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611#if defined(MBEDTLS_PKCS1_V21)
1612 case MBEDTLS_RSA_PKCS_V21:
1613 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001614 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001615#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
1617 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001619 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001620}
1621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001623/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001624 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001627 int (*f_rng)(void *, unsigned char *, size_t),
1628 void *p_rng,
1629 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001630 const unsigned char *label, size_t label_len,
1631 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001632 const unsigned char *input,
1633 unsigned char *output,
1634 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001635{
Paul Bakker23986e52011-04-24 08:57:21 +00001636 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001637 size_t ilen, i, pad_len;
1638 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1640 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001641 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 const mbedtls_md_info_t *md_info;
1643 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001644
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001645 /*
1646 * Parameters sanity checks
1647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1649 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
1651 ilen = ctx->len;
1652
Paul Bakker27fdf462011-06-09 13:55:13 +00001653 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001657 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001659
Janos Follathc17cda12016-02-11 11:08:18 +00001660 hlen = mbedtls_md_get_size( md_info );
1661
1662 // checking for integer underflow
1663 if( 2 * hlen + 2 > ilen )
1664 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1665
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001666 /*
1667 * RSA operation
1668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1670 ? mbedtls_rsa_public( ctx, input, buf )
1671 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
1673 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001674 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001676 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001677 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001680 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1681 {
1682 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001683 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001684 }
1685
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001686
1687 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001689
1690 /* seed: Apply seedMask to maskedSeed */
1691 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1692 &md_ctx );
1693
1694 /* DB: Apply dbMask to maskedDB */
1695 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1696 &md_ctx );
1697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001699
1700 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001701 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001702 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001703 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001704 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001705
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001706 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001707
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001708 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001709
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001710 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001711 for( i = 0; i < hlen; i++ )
1712 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001713
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001714 /* Get zero-padding len, but always read till end of buffer
1715 * (minus one, for the 01 byte) */
1716 pad_len = 0;
1717 pad_done = 0;
1718 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1719 {
1720 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001721 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001722 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001723
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001724 p += pad_len;
1725 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001726
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001727 /*
1728 * The only information "leaked" is whether the padding was correct or not
1729 * (eg, no data is copied if it was not correct). This meets the
1730 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1731 * the different error conditions.
1732 */
1733 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001734 {
1735 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1736 goto cleanup;
1737 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001738
Paul Bakker66d5d072014-06-17 16:39:18 +02001739 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001740 {
1741 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1742 goto cleanup;
1743 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001744
1745 *olen = ilen - (p - buf);
1746 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001747 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001748
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001749cleanup:
1750 mbedtls_zeroize( buf, sizeof( buf ) );
1751 mbedtls_zeroize( lhash, sizeof( lhash ) );
1752
1753 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001754}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001758/*
1759 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001762 int (*f_rng)(void *, unsigned char *, size_t),
1763 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001764 int mode, size_t *olen,
1765 const unsigned char *input,
1766 unsigned char *output,
1767 size_t output_max_len)
1768{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001769 int ret;
1770 size_t ilen, pad_count = 0, i;
1771 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1775 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001776
1777 ilen = ctx->len;
1778
1779 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1783 ? mbedtls_rsa_public( ctx, input, buf )
1784 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001785
1786 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001787 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001788
1789 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001790 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001791
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001792 /*
1793 * Check and get padding len in "constant-time"
1794 */
1795 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001796
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001797 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001801
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001802 /* Get padding len, but always read till end of buffer
1803 * (minus one, for the 00 byte) */
1804 for( i = 0; i < ilen - 3; i++ )
1805 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001806 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1807 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001808 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001809
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001810 p += pad_count;
1811 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001812 }
1813 else
1814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001816
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001817 /* Get padding len, but always read till end of buffer
1818 * (minus one, for the 00 byte) */
1819 for( i = 0; i < ilen - 3; i++ )
1820 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001821 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001822 pad_count += ( pad_done == 0 );
1823 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001824
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001825 p += pad_count;
1826 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001827 }
1828
Janos Follathc69fa502016-02-12 13:30:09 +00001829 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001830
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001831 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001832 {
1833 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1834 goto cleanup;
1835 }
Paul Bakker8804f692013-02-28 18:06:26 +01001836
Paul Bakker66d5d072014-06-17 16:39:18 +02001837 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001838 {
1839 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1840 goto cleanup;
1841 }
Paul Bakker060c5682009-01-12 21:48:39 +00001842
Paul Bakker27fdf462011-06-09 13:55:13 +00001843 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001844 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001845 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001847cleanup:
1848 mbedtls_zeroize( buf, sizeof( buf ) );
1849
1850 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
1854/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001855 * Do an RSA operation, then remove the message padding
1856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001858 int (*f_rng)(void *, unsigned char *, size_t),
1859 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001860 int mode, size_t *olen,
1861 const unsigned char *input,
1862 unsigned char *output,
1863 size_t output_max_len)
1864{
1865 switch( ctx->padding )
1866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867#if defined(MBEDTLS_PKCS1_V15)
1868 case MBEDTLS_RSA_PKCS_V15:
1869 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001870 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001871#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873#if defined(MBEDTLS_PKCS1_V21)
1874 case MBEDTLS_RSA_PKCS_V21:
1875 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001876 olen, input, output,
1877 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001878#endif
1879
1880 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001882 }
1883}
1884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001886/*
1887 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001890 int (*f_rng)(void *, unsigned char *, size_t),
1891 void *p_rng,
1892 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001894 unsigned int hashlen,
1895 const unsigned char *hash,
1896 unsigned char *sig )
1897{
1898 size_t olen;
1899 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001901 unsigned int slen, hlen, offset = 0;
1902 int ret;
1903 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 const mbedtls_md_info_t *md_info;
1905 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1908 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001909
1910 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001912
1913 olen = ctx->len;
1914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001916 {
Simon Butcher02037452016-03-01 21:19:12 +00001917 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001919 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001923 }
1924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001926 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001930 slen = hlen;
1931
1932 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001934
1935 memset( sig, 0, olen );
1936
Simon Butcher02037452016-03-01 21:19:12 +00001937 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001938 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001940
Simon Butcher02037452016-03-01 21:19:12 +00001941 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001942 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001943 p += olen - hlen * 2 - 2;
1944 *p++ = 0x01;
1945 memcpy( p, salt, slen );
1946 p += slen;
1947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001949 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1950 {
1951 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001952 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001953 return( ret );
1954 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001955
Simon Butcher02037452016-03-01 21:19:12 +00001956 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 mbedtls_md_starts( &md_ctx );
1958 mbedtls_md_update( &md_ctx, p, 8 );
1959 mbedtls_md_update( &md_ctx, hash, hashlen );
1960 mbedtls_md_update( &md_ctx, salt, slen );
1961 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001962 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001963
Simon Butcher02037452016-03-01 21:19:12 +00001964 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001965 if( msb % 8 == 0 )
1966 offset = 1;
1967
Simon Butcher02037452016-03-01 21:19:12 +00001968 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001969 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001972
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001973 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001974 sig[0] &= 0xFF >> ( olen * 8 - msb );
1975
1976 p += hlen;
1977 *p++ = 0xBC;
1978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 return( ( mode == MBEDTLS_RSA_PUBLIC )
1980 ? mbedtls_rsa_public( ctx, sig, sig )
1981 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001982}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001986/*
1987 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1988 */
1989/*
1990 * Do an RSA operation to sign the message digest
1991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001993 int (*f_rng)(void *, unsigned char *, size_t),
1994 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001995 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001997 unsigned int hashlen,
1998 const unsigned char *hash,
1999 unsigned char *sig )
2000{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002001 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002002 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02002003 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002004 unsigned char *sig_try = NULL, *verif = NULL;
2005 size_t i;
2006 unsigned char diff;
2007 volatile unsigned char diff_no_optimize;
2008 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2011 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002012
2013 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002014 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01002015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002019 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
2023 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002024
Paul Bakkerc70b9822013-04-07 22:00:46 +02002025 nb_pad -= 10 + oid_size;
2026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002028 }
2029
Paul Bakkerc70b9822013-04-07 22:00:46 +02002030 nb_pad -= hashlen;
2031
Paul Bakkerb3869132013-02-28 17:21:01 +01002032 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002034
2035 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01002037 memset( p, 0xFF, nb_pad );
2038 p += nb_pad;
2039 *p++ = 0;
2040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002042 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002043 memcpy( p, hash, hashlen );
2044 }
2045 else
2046 {
2047 /*
2048 * DigestInfo ::= SEQUENCE {
2049 * digestAlgorithm DigestAlgorithmIdentifier,
2050 * digest Digest }
2051 *
2052 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2053 *
2054 * Digest ::= OCTET STRING
2055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002057 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002059 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002061 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002062 memcpy( p, oid, oid_size );
2063 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002065 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002067 *p++ = hashlen;
2068 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002069 }
2070
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002071 if( mode == MBEDTLS_RSA_PUBLIC )
2072 return( mbedtls_rsa_public( ctx, sig, sig ) );
2073
2074 /*
2075 * In order to prevent Lenstra's attack, make the signature in a
2076 * temporary buffer and check it before returning it.
2077 */
2078 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002079 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002080 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2081
Simon Butcher1285ab52016-01-01 21:42:47 +00002082 verif = mbedtls_calloc( 1, ctx->len );
2083 if( verif == NULL )
2084 {
2085 mbedtls_free( sig_try );
2086 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2087 }
2088
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002089 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2090 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2091
2092 /* Compare in constant time just in case */
2093 for( diff = 0, i = 0; i < ctx->len; i++ )
2094 diff |= verif[i] ^ sig[i];
2095 diff_no_optimize = diff;
2096
2097 if( diff_no_optimize != 0 )
2098 {
2099 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2100 goto cleanup;
2101 }
2102
2103 memcpy( sig, sig_try, ctx->len );
2104
2105cleanup:
2106 mbedtls_free( sig_try );
2107 mbedtls_free( verif );
2108
2109 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002112
2113/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 * Do an RSA operation to sign the message digest
2115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002117 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002118 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002121 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002122 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 unsigned char *sig )
2124{
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 switch( ctx->padding )
2126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127#if defined(MBEDTLS_PKCS1_V15)
2128 case MBEDTLS_RSA_PKCS_V15:
2129 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002130 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002131#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133#if defined(MBEDTLS_PKCS1_V21)
2134 case MBEDTLS_RSA_PKCS_V21:
2135 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002136 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002137#endif
2138
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002142}
2143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002145/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002146 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002149 int (*f_rng)(void *, unsigned char *, size_t),
2150 void *p_rng,
2151 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002153 unsigned int hashlen,
2154 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002156 int expected_salt_len,
2157 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002158{
Paul Bakker23986e52011-04-24 08:57:21 +00002159 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002160 size_t siglen;
2161 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002163 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002164 unsigned int hlen;
2165 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 const mbedtls_md_info_t *md_info;
2167 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002168 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2171 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002172
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 siglen = ctx->len;
2174
Paul Bakker27fdf462011-06-09 13:55:13 +00002175 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2179 ? mbedtls_rsa_public( ctx, sig, buf )
2180 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181
2182 if( ret != 0 )
2183 return( ret );
2184
2185 p = buf;
2186
Paul Bakkerb3869132013-02-28 17:21:01 +01002187 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 {
Simon Butcher02037452016-03-01 21:19:12 +00002192 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002194 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002198 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002201 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002205 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002206
Paul Bakkerb3869132013-02-28 17:21:01 +01002207 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002208
Simon Butcher02037452016-03-01 21:19:12 +00002209 /*
2210 * Note: EMSA-PSS verification is over the length of N - 1 bits
2211 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002212 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002213
Simon Butcher02037452016-03-01 21:19:12 +00002214 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002215 if( msb % 8 == 0 )
2216 {
2217 p++;
2218 siglen -= 1;
2219 }
2220 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002224 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2225 {
2226 mbedtls_md_free( &md_ctx );
2227 return( ret );
2228 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002229
Paul Bakkerb3869132013-02-28 17:21:01 +01002230 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002231
Paul Bakkerb3869132013-02-28 17:21:01 +01002232 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002233
Paul Bakker4de44aa2013-12-31 11:43:01 +01002234 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002235 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002236
Paul Bakkerb3869132013-02-28 17:21:01 +01002237 if( p == buf + siglen ||
2238 *p++ != 0x01 )
2239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 mbedtls_md_free( &md_ctx );
2241 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002242 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002243
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002244 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002245 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002248 slen != (size_t) expected_salt_len )
2249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 mbedtls_md_free( &md_ctx );
2251 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002252 }
2253
Simon Butcher02037452016-03-01 21:19:12 +00002254 /*
2255 * Generate H = Hash( M' )
2256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 mbedtls_md_starts( &md_ctx );
2258 mbedtls_md_update( &md_ctx, zeros, 8 );
2259 mbedtls_md_update( &md_ctx, hash, hashlen );
2260 mbedtls_md_update( &md_ctx, p, slen );
2261 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002264
Paul Bakkerb3869132013-02-28 17:21:01 +01002265 if( memcmp( p + slen, result, hlen ) == 0 )
2266 return( 0 );
2267 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002269}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002270
2271/*
2272 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002275 int (*f_rng)(void *, unsigned char *, size_t),
2276 void *p_rng,
2277 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002279 unsigned int hashlen,
2280 const unsigned char *hash,
2281 const unsigned char *sig )
2282{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2284 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002285 : md_alg;
2286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002288 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002290 sig ) );
2291
2292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002296/*
2297 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002300 int (*f_rng)(void *, unsigned char *, size_t),
2301 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002302 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002304 unsigned int hashlen,
2305 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002306 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002307{
2308 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002309 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002310 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 mbedtls_md_type_t msg_md_alg;
2312 const mbedtls_md_info_t *md_info;
2313 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002314 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2317 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002318
2319 siglen = ctx->len;
2320
2321 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2325 ? mbedtls_rsa_public( ctx, sig, buf )
2326 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002327
2328 if( ret != 0 )
2329 return( ret );
2330
2331 p = buf;
2332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2334 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002335
2336 while( *p != 0 )
2337 {
2338 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002340 p++;
2341 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002342 p++; /* skip 00 byte */
2343
2344 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2345 if( p - buf < 11 )
2346 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002347
2348 len = siglen - ( p - buf );
2349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002351 {
2352 if( memcmp( p, hash, hashlen ) == 0 )
2353 return( 0 );
2354 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002356 }
2357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002359 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2361 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002362
2363 end = p + len;
2364
Simon Butcher02037452016-03-01 21:19:12 +00002365 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002366 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2367 * Insist on 2-byte length tags, to protect against variants of
2368 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002369 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002370 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2372 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2373 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002374 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002376
Gilles Peskinee7e76502017-05-04 12:48:39 +02002377 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2379 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2380 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002381 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002383
Gilles Peskinee7e76502017-05-04 12:48:39 +02002384 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2386 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002387 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002388 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002389
2390 oid.p = p;
2391 p += oid.len;
2392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2394 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002395
2396 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002398
2399 /*
2400 * assume the algorithm parameters must be NULL
2401 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002402 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2404 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002405 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002407
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002408 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002409 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2410 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002411 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002413
2414 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002416
2417 p += hashlen;
2418
2419 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002421
2422 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002423}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
2426/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002427 * Do an RSA operation and check the message digest
2428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002430 int (*f_rng)(void *, unsigned char *, size_t),
2431 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002432 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002434 unsigned int hashlen,
2435 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002436 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002437{
2438 switch( ctx->padding )
2439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440#if defined(MBEDTLS_PKCS1_V15)
2441 case MBEDTLS_RSA_PKCS_V15:
2442 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002443 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002444#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#if defined(MBEDTLS_PKCS1_V21)
2447 case MBEDTLS_RSA_PKCS_V21:
2448 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002449 hashlen, hash, sig );
2450#endif
2451
2452 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002454 }
2455}
2456
2457/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002458 * Copy the components of an RSA key
2459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002461{
2462 int ret;
2463
2464 dst->ver = src->ver;
2465 dst->len = src->len;
2466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2468 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2471 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2472 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002473
2474#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2476 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2477 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2479 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002480#endif
2481
2482 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2485 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002486
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002487 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002488 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002489
2490cleanup:
2491 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002493
2494 return( ret );
2495}
2496
2497/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 * Free the components of an RSA key
2499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002501{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002503 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2504 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002506
Hanno Becker33c30a02017-08-23 07:00:22 +01002507#if !defined(MBEDTLS_RSA_NO_CRT)
2508 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2509 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2510 mbedtls_mpi_free( &ctx->DP );
2511#endif /* MBEDTLS_RSA_NO_CRT */
2512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#if defined(MBEDTLS_THREADING_C)
2514 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002515#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002516}
2517
Hanno Beckerab377312017-08-23 16:24:51 +01002518#endif /* !MBEDTLS_RSA_ALT */
2519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002522#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
2524/*
2525 * Example RSA-1024 keypair, for test purposes
2526 */
2527#define KEY_LEN 128
2528
2529#define RSA_N "9292758453063D803DD603D5E777D788" \
2530 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2531 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2532 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2533 "93A89813FBF3C4F8066D2D800F7C38A8" \
2534 "1AE31942917403FF4946B0A83D3D3E05" \
2535 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2536 "5E94BB77B07507233A0BC7BAC8F90F79"
2537
2538#define RSA_E "10001"
2539
2540#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2541 "66CA472BC44D253102F8B4A9D3BFA750" \
2542 "91386C0077937FE33FA3252D28855837" \
2543 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2544 "DF79C5CE07EE72C7F123142198164234" \
2545 "CABB724CF78B8173B9F880FC86322407" \
2546 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2547 "071513A1E85B5DFA031F21ECAE91A34D"
2548
2549#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2550 "2C01CAD19EA484A87EA4377637E75500" \
2551 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2552 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2553
2554#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2555 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2556 "910E4168387E3C30AA1E00C339A79508" \
2557 "8452DD96A9A5EA5D9DCA68DA636032AF"
2558
2559#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2560 "3C94D22288ACD763FD8E5600ED4A702D" \
2561 "F84198A5F06C2E72236AE490C93F07F8" \
2562 "3CC559CD27BC2D1CA488811730BB5725"
2563
2564#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2565 "D8AAEA56749EA28623272E4F7D0592AF" \
2566 "7C1F1313CAC9471B5C523BFE592F517B" \
2567 "407A1BD76C164B93DA2D32A383E58357"
2568
2569#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2570 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2571 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2572 "A74206CEC169D74BF5A8C50D6F48EA08"
2573
2574#define PT_LEN 24
2575#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2576 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002579static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002580{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002581#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002582 size_t i;
2583
Paul Bakker545570e2010-07-18 09:00:25 +00002584 if( rng_state != NULL )
2585 rng_state = NULL;
2586
Paul Bakkera3d195c2011-11-27 21:07:34 +00002587 for( i = 0; i < len; ++i )
2588 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002589#else
2590 if( rng_state != NULL )
2591 rng_state = NULL;
2592
2593 arc4random_buf( output, len );
2594#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002595
Paul Bakkera3d195c2011-11-27 21:07:34 +00002596 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002597}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002599
Paul Bakker5121ce52009-01-03 21:22:43 +00002600/*
2601 * Checkup routine
2602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002604{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002605 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002607 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 unsigned char rsa_plaintext[PT_LEN];
2610 unsigned char rsa_decrypted[PT_LEN];
2611 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002613 unsigned char sha1sum[20];
2614#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Hanno Becker3a701162017-08-22 13:52:43 +01002616 mbedtls_mpi K;
2617
2618 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
Hanno Becker3a701162017-08-22 13:52:43 +01002621 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2622 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2623 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2624 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2625 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2626 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2627 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2628 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2629 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2630 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2631
2632 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
2634 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2638 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 {
2640 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
2643 return( 1 );
2644 }
2645
Hanno Becker3a701162017-08-22 13:52:43 +01002646 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2647 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2648
2649 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2650 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2651
2652 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2653 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2654
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
2658 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 rsa_plaintext, rsa_ciphertext ) != 0 )
2662 {
2663 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
2666 return( 1 );
2667 }
2668
2669 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002673 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002674 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 {
2676 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
2679 return( 1 );
2680 }
2681
2682 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2683 {
2684 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
2687 return( 1 );
2688 }
2689
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002690 if( verbose != 0 )
2691 mbedtls_printf( "passed\n" );
2692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002695 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 sha1sum, rsa_ciphertext ) != 0 )
2701 {
2702 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
2705 return( 1 );
2706 }
2707
2708 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 sha1sum, rsa_ciphertext ) != 0 )
2713 {
2714 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
2717 return( 1 );
2718 }
2719
2720 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002721 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002724 if( verbose != 0 )
2725 mbedtls_printf( "\n" );
2726
Paul Bakker3d8fb632014-04-17 12:42:41 +02002727cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002728 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 mbedtls_rsa_free( &rsa );
2730#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002731 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002733 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734}
2735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738#endif /* MBEDTLS_RSA_C */