blob: e01397ec9211633db1800d580aaed6e9e0187ce3 [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 */
Hanno Beckerba5b7552017-10-02 09:55:49 +0100132int mbedtls_rsa_deduce_moduli( mbedtls_mpi const *N,
133 mbedtls_mpi const *D, mbedtls_mpi const *E,
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100134 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
135 mbedtls_mpi *P, mbedtls_mpi *Q )
136{
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100137 int ret = 0;
138
139 uint16_t attempt; /* Number of current attempt */
140 uint16_t iter; /* Number of squares computed in the current attempt */
141
142 uint16_t bitlen_half; /* Half the bitsize of the modulus N */
143 uint16_t order; /* Order of 2 in DE - 1 */
144
Hanno Beckerba5b7552017-10-02 09:55:49 +0100145 mbedtls_mpi T; /* Holds largest odd divisor of DE - 1 */
146 mbedtls_mpi K; /* During factorization attempts, stores a random integer
147 * in the range of [0,..,N] */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100148
149 if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
150 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
151
152 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
153 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
154 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
155 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
156 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
157 {
158 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
159 }
160
161 /*
162 * Initializations and temporary changes
163 */
164
165 mbedtls_mpi_init( &K );
Hanno Beckerba5b7552017-10-02 09:55:49 +0100166 mbedtls_mpi_init( &T );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100167
Hanno Beckerba5b7552017-10-02 09:55:49 +0100168 /* T := DE - 1 */
169 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, D, E ) );
170 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &T, &T, 1 ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100171
Hanno Beckerba5b7552017-10-02 09:55:49 +0100172 if( ( order = mbedtls_mpi_lsb( &T ) ) == 0 )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100173 {
174 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
175 goto cleanup;
176 }
177
Hanno Beckerba5b7552017-10-02 09:55:49 +0100178 /* After this operation, T holds the largest odd divisor of DE - 1. */
179 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &T, order ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100180
181 /* This is used to generate a few numbers around N / 2
182 * if no PRNG is provided. */
183 if( f_rng == NULL )
184 bitlen_half = mbedtls_mpi_bitlen( N ) / 2;
185
186 /*
187 * Actual work
188 */
189
190 for( attempt = 0; attempt < 30; ++attempt )
191 {
192 /* Generate some number in [0,N], either randomly
193 * if a PRNG is given, or try numbers around N/2 */
194 if( f_rng != NULL )
195 {
196 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &K,
197 mbedtls_mpi_size( N ),
198 f_rng, p_rng ) );
199 }
200 else
201 {
202 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &K, 1 ) ) ;
203 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &K, bitlen_half ) ) ;
204 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, attempt + 1 ) );
205 }
206
207 /* Check if gcd(K,N) = 1 */
208 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
209 if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
210 continue;
211
Hanno Beckerba5b7552017-10-02 09:55:49 +0100212 /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100213 * and check whether they have nontrivial GCD with N. */
Hanno Beckerba5b7552017-10-02 09:55:49 +0100214 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, &T, N,
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100215 Q /* temporarily use Q for storing Montgomery
216 * multiplication helper values */ ) );
217
218 for( iter = 1; iter < order; ++iter )
219 {
220 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
221 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
222
223 if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
224 mbedtls_mpi_cmp_mpi( P, N ) == -1 )
225 {
226 /*
227 * Have found a nontrivial divisor P of N.
Hanno Beckerd56d83a2017-08-25 07:29:35 +0100228 * Set Q := N / P.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100229 */
230
Hanno Beckerba5b7552017-10-02 09:55:49 +0100231 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, NULL, N, P ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100232 goto cleanup;
233 }
234
235 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
236 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
237 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
238 }
239 }
240
241 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
242
243cleanup:
244
245 mbedtls_mpi_free( &K );
Hanno Beckerba5b7552017-10-02 09:55:49 +0100246 mbedtls_mpi_free( &T );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100247 return( ret );
248}
249
250/*
251 * Given P, Q and the public exponent E, deduce D.
252 * This is essentially a modular inversion.
253 */
254
255int mbedtls_rsa_deduce_private( mbedtls_mpi *P, mbedtls_mpi *Q,
256 mbedtls_mpi *D, mbedtls_mpi *E )
257{
258 int ret = 0;
259 mbedtls_mpi K;
260
261 if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
262 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
263
264 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
265 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
266 mbedtls_mpi_cmp_int( E, 0 ) == 0 )
267 {
268 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
269 }
270
271 mbedtls_mpi_init( &K );
272
273 /* Temporarily replace P and Q by P-1 and Q-1, respectively. */
274 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) );
275 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) );
276
277 /* Temporarily compute the gcd(P-1, Q-1) in D. */
278 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, P, Q ) );
279
280 /* Compute LCM(P-1, Q-1) in K */
281 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
282 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
283
284 /* Compute modular inverse of E in LCM(P-1, Q-1) */
285 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
286
287 /* Restore P and Q. */
288 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) );
289 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) );
290
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100291cleanup:
292
293 mbedtls_mpi_free( &K );
294
295 return( ret );
296}
297
298/*
Hanno Beckerd3637992017-08-25 07:55:03 +0100299 * Check that RSA CRT parameters are in accordance with core parameters.
300 */
301
302int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
303 const mbedtls_mpi *D, const mbedtls_mpi *DP,
304 const mbedtls_mpi *DQ, const mbedtls_mpi *QP )
305{
306 int ret = 0;
307
308 mbedtls_mpi K, L;
309 mbedtls_mpi_init( &K );
310 mbedtls_mpi_init( &L );
311
312 /* Check that DP - P == 0 mod P - 1 */
313 if( DP != NULL )
314 {
315 if( P == NULL )
316 {
317 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
318 goto cleanup;
319 }
320
321 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
322 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DP, D ) );
323 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
324
325 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
326 {
327 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
328 }
329 }
330
331 /* Check that DQ - Q == 0 mod Q - 1 */
332 if( DQ != NULL )
333 {
334 if( Q == NULL )
335 {
336 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
337 goto cleanup;
338 }
339
340 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
341 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
342 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
343
344 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
345 {
346 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
347 }
348 }
349
350 /* Check that QP * P - 1 == 0 mod P */
351 if( QP != NULL )
352 {
353 if( P == NULL || Q == NULL )
354 {
355 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
356 goto cleanup;
357 }
358
359 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
360 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
361 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
362 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
363 {
364 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
365 }
366 }
367
368cleanup:
369
370 /* Wrap MPI error codes by RSA check failure error code */
371 if( ret != 0 &&
372 ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
373 ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
374 {
375 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
376 }
377
378 mbedtls_mpi_free( &K );
379 mbedtls_mpi_free( &L );
380
381 return( ret );
382}
383
384/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100385 * Check that core RSA parameters are sane.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100386 */
387
Hanno Becker750e8b42017-08-25 07:54:27 +0100388int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
389 const mbedtls_mpi *Q, const mbedtls_mpi *D,
390 const mbedtls_mpi *E,
391 int (*f_rng)(void *, unsigned char *, size_t),
392 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100393{
394 int ret = 0;
Hanno Becker750e8b42017-08-25 07:54:27 +0100395 mbedtls_mpi K, L;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100396
397 mbedtls_mpi_init( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100398 mbedtls_mpi_init( &L );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100399
400 /*
401 * Step 1: If PRNG provided, check that P and Q are prime
402 */
403
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100404#if defined(MBEDTLS_GENPRIME)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100405 if( f_rng != NULL && P != NULL &&
406 ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
407 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100408 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100409 goto cleanup;
410 }
411
412 if( f_rng != NULL && Q != NULL &&
413 ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
414 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100415 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100416 goto cleanup;
417 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100418#else
419 ((void) f_rng);
420 ((void) p_rng);
421#endif /* MBEDTLS_GENPRIME */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100422
423 /*
424 * Step 2: Check that N = PQ
425 */
426
427 if( P != NULL && Q != NULL && N != NULL )
428 {
429 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
Hanno Becker750e8b42017-08-25 07:54:27 +0100430 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
431 mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100432 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100433 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100434 goto cleanup;
435 }
436 }
437
438 /*
439 * Step 3: Check that D, E are inverse modulo P-1 and Q-1
440 */
441
442 if( P != NULL && Q != NULL && D != NULL && E != NULL )
443 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100444 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
445 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
446 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
447 mbedtls_mpi_cmp_int( E, 1 ) <= 0 )
448 {
449 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
450 goto cleanup;
451 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100452
453 /* Compute DE-1 mod P-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100454 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
455 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
456 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
457 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100458 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
459 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100460 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100461 goto cleanup;
462 }
463
464 /* Compute DE-1 mod Q-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100465 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
466 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
467 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
468 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100469 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
470 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100471 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100472 goto cleanup;
473 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100474 }
475
476cleanup:
477
478 mbedtls_mpi_free( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100479 mbedtls_mpi_free( &L );
480
481 /* Wrap MPI error codes by RSA check failure error code */
482 if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
483 {
484 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
485 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100486
487 return( ret );
488}
489
490int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
491 const mbedtls_mpi *D, mbedtls_mpi *DP,
492 mbedtls_mpi *DQ, mbedtls_mpi *QP )
493{
494 int ret = 0;
495 mbedtls_mpi K;
496 mbedtls_mpi_init( &K );
497
Hanno Beckerd9431a72017-08-25 08:03:13 +0100498 /* DP = D mod P-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100499 if( DP != NULL )
500 {
501 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
502 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
503 }
504
Hanno Beckerd9431a72017-08-25 08:03:13 +0100505 /* DQ = D mod Q-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100506 if( DQ != NULL )
507 {
508 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
509 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
510 }
511
Hanno Beckerd9431a72017-08-25 08:03:13 +0100512 /* QP = Q^{-1} mod P */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100513 if( QP != NULL )
514 {
515 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
516 }
517
518cleanup:
519 mbedtls_mpi_free( &K );
520
521 return( ret );
522}
523
Hanno Becker617c1ae2017-08-23 14:11:24 +0100524
525/*
526 * Default RSA interface implementation
527 */
528
Hanno Beckerab377312017-08-23 16:24:51 +0100529#if !defined(MBEDTLS_RSA_ALT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100530
531int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
532 const mbedtls_mpi *N,
533 const mbedtls_mpi *P, const mbedtls_mpi *Q,
534 const mbedtls_mpi *D, const mbedtls_mpi *E )
535{
536 int ret;
537
538 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
539 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
540 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
541 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
542 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
543 {
544 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
545 }
546
547 if( N != NULL )
548 ctx->len = mbedtls_mpi_size( &ctx->N );
549
550 return( 0 );
551}
552
553int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
554 unsigned char *N, size_t N_len,
555 unsigned char *P, size_t P_len,
556 unsigned char *Q, size_t Q_len,
557 unsigned char *D, size_t D_len,
558 unsigned char *E, size_t E_len )
559{
560 int ret;
561
562 if( N != NULL )
563 {
564 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
565 ctx->len = mbedtls_mpi_size( &ctx->N );
566 }
567
568 if( P != NULL )
569 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
570
571 if( Q != NULL )
572 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
573
574 if( D != NULL )
575 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
576
577 if( E != NULL )
578 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
579
580cleanup:
581
582 if( ret != 0 )
583 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
584
585 return( 0 );
586}
587
588int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
589 int (*f_rng)(void *, unsigned char *, size_t),
590 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100591{
592 int ret = 0;
593
Hanno Becker617c1ae2017-08-23 14:11:24 +0100594 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
595 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
596 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
597 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
598 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100599
Hanno Becker617c1ae2017-08-23 14:11:24 +0100600 /*
601 * Check whether provided parameters are enough
602 * to deduce all others. The following incomplete
603 * parameter sets for private keys are supported:
604 *
605 * (1) P, Q missing.
606 * (2) D and potentially N missing.
607 *
608 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100609
Hanno Becker2cca6f32017-09-29 11:46:40 +0100610 const int n_missing = have_P && have_Q && have_D && have_E;
611 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
612 const int d_missing = have_P && have_Q && !have_D && have_E;
613 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
614
615 /* These three alternatives are mutually exclusive */
616 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100617
Hanno Becker617c1ae2017-08-23 14:11:24 +0100618 if( !is_priv && !is_pub )
619 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
620
621 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100622 * Step 1: Deduce N if P, Q are provided.
623 */
624
625 if( !have_N && have_P && have_Q )
626 {
627 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
628 &ctx->Q ) ) != 0 )
629 {
630 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
631 }
632
633 ctx->len = mbedtls_mpi_size( &ctx->N );
634 }
635
636 /*
637 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100638 */
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
Hanno Becker617c1ae2017-08-23 14:11:24 +0100663 /* Deduce private exponent. This includes double-checking of the result,
664 * so together with the primality test above all core parameters are
665 * guaranteed to be sane if this call succeeds. */
666 if( ( ret = mbedtls_rsa_deduce_private( &ctx->P, &ctx->Q,
667 &ctx->D, &ctx->E ) ) != 0 )
668 {
669 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
670 }
671 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100672
673 /* In the remaining case of a public key, there's nothing to check for. */
674
675 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100676 * Step 3: Deduce all additional parameters specific
Hanno Becker617c1ae2017-08-23 14:11:24 +0100677 * to our current RSA implementaiton.
678 */
679
Hanno Becker23344b52017-08-23 07:43:27 +0100680#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100681 if( is_priv )
682 {
683 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
684 &ctx->DP, &ctx->DQ, &ctx->QP );
685 if( ret != 0 )
686 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
687 }
Hanno Becker23344b52017-08-23 07:43:27 +0100688#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100689
690 /*
691 * Step 3: Double check
692 */
693
694 if( is_priv )
695 {
696 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
697 return( ret );
698 }
699 else
700 {
701 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
702 return( ret );
703 }
704
705 return( 0 );
706}
707
708/*
709 * Check if CRT parameters match RSA context.
710 * This has to be implemented even if CRT is not used,
711 * in order to be able to validate DER encoded RSA keys,
712 * which always contain CRT parameters.
713 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100714int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx,
715 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100716{
Hanno Becker23344b52017-08-23 07:43:27 +0100717 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100718
Hanno Becker23344b52017-08-23 07:43:27 +0100719 /* Check if key is private or public */
720 const int is_priv =
721 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
722 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
723 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
724 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
725 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
726
727 if( !is_priv )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100728 {
729 /* Checking optional parameters only makes sense for private keys. */
730 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
731 }
732
Hanno Becker23344b52017-08-23 07:43:27 +0100733#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100734 if( ( DP != NULL && mbedtls_mpi_cmp_mpi( DP, &ctx->DP ) != 0 ) ||
735 ( DQ != NULL && mbedtls_mpi_cmp_mpi( DQ, &ctx->DQ ) != 0 ) ||
736 ( QP != NULL && mbedtls_mpi_cmp_mpi( QP, &ctx->QP ) != 0 ) )
737 {
738 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
739 }
Hanno Becker23344b52017-08-23 07:43:27 +0100740#else /* MBEDTLS_RSA_NO_CRT */
Hanno Beckerd3637992017-08-25 07:55:03 +0100741 if( ( ret = mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
742 DP, DQ, QP ) ) != 0 )
Hanno Becker23344b52017-08-23 07:43:27 +0100743 {
Hanno Beckerd3637992017-08-25 07:55:03 +0100744 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker23344b52017-08-23 07:43:27 +0100745 }
Hanno Becker23344b52017-08-23 07:43:27 +0100746#endif
747
748 if( ret != 0 )
749 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100750
751 return( 0 );
752}
753
754int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
755 unsigned char *N, size_t N_len,
756 unsigned char *P, size_t P_len,
757 unsigned char *Q, size_t Q_len,
758 unsigned char *D, size_t D_len,
759 unsigned char *E, size_t E_len )
760{
761 int ret = 0;
762
763 /* Check if key is private or public */
764 const int is_priv =
765 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
766 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
767 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
768 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
769 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
770
771 if( !is_priv )
772 {
773 /* If we're trying to export private parameters for a public key,
774 * something must be wrong. */
775 if( P != NULL || Q != NULL || D != NULL )
776 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
777
778 }
779
780 if( N != NULL )
781 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
782
783 if( P != NULL )
784 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
785
786 if( Q != NULL )
787 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
788
789 if( D != NULL )
790 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
791
792 if( E != NULL )
793 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100794
795cleanup:
796
797 return( ret );
798}
799
Hanno Becker617c1ae2017-08-23 14:11:24 +0100800int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
801 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
802 mbedtls_mpi *D, mbedtls_mpi *E )
803{
804 int ret;
805
806 /* Check if key is private or public */
807 int is_priv =
808 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
809 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
810 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
811 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
812 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
813
814 if( !is_priv )
815 {
816 /* If we're trying to export private parameters for a public key,
817 * something must be wrong. */
818 if( P != NULL || Q != NULL || D != NULL )
819 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
820
821 }
822
823 /* Export all requested core parameters. */
824
825 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
826 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
827 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
828 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
829 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
830 {
831 return( ret );
832 }
833
834 return( 0 );
835}
836
837/*
838 * Export CRT parameters
839 * This must also be implemented if CRT is not used, for being able to
840 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
841 * can be used in this case.
842 */
843int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
844 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
845{
846 int ret;
847
848 /* Check if key is private or public */
849 int is_priv =
850 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
851 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
852 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
853 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
854 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
855
856 if( !is_priv )
857 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
858
Hanno Beckerdc95c892017-08-23 06:57:02 +0100859#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100860 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100861 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
862 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
863 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
864 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100865 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100866 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100867#else
868 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
869 DP, DQ, QP ) ) != 0 )
870 {
871 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
872 }
873#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100874
875 return( 0 );
876}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100877
878/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000879 * Initialize an RSA context
880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000882 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000883 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000884{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_THREADING_C)
890 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200891#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000892}
893
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100894/*
895 * Set padding for an existing RSA context
896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100898{
899 ctx->padding = padding;
900 ctx->hash_id = hash_id;
901}
902
Hanno Becker617c1ae2017-08-23 14:11:24 +0100903/*
904 * Get length in bytes of RSA modulus
905 */
906
907size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
908{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100909 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100910}
911
912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
915/*
916 * Generate an RSA keypair
917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000919 int (*f_rng)(void *, unsigned char *, size_t),
920 void *p_rng,
921 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000922{
923 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100924 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000925
Paul Bakker21eb2802010-08-16 11:10:02 +0000926 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Janos Follathef441782016-09-21 13:18:12 +0100929 if( nbits % 2 )
930 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
931
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100932 mbedtls_mpi_init( &H );
933 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
935 /*
936 * find primes P and Q with Q < P so that:
937 * GCD( E, (P-1)*(Q-1) ) == 1
938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940
941 do
942 {
Janos Follath10c575b2016-02-23 14:42:48 +0000943 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100944 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Janos Follathef441782016-09-21 13:18:12 +0100946 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100947 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 continue;
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200953 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 continue;
955
Janos Follathef441782016-09-21 13:18:12 +0100956 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100957 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100958
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100959 /* Temporarily replace P,Q by P-1, Q-1 */
960 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
961 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
962 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000966
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100967 /* Restore P,Q */
968 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
969 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
970
971 ctx->len = mbedtls_mpi_size( &ctx->N );
972
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 /*
974 * D = E^-1 mod ((P-1)*(Q-1))
975 * DP = D mod (P - 1)
976 * DQ = D mod (Q - 1)
977 * QP = Q^-1 mod P
978 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100980 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
981
982#if !defined(MBEDTLS_RSA_NO_CRT)
983 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
984 &ctx->DP, &ctx->DQ, &ctx->QP ) );
985#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Hanno Becker83aad1f2017-08-23 06:45:10 +0100987 /* Double-check */
988 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
989
Paul Bakker5121ce52009-01-03 21:22:43 +0000990cleanup:
991
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100992 mbedtls_mpi_free( &H );
993 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
995 if( ret != 0 )
996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_rsa_free( ctx );
998 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 }
1000
Paul Bakker48377d92013-08-30 12:06:24 +02001001 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001002}
1003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001005
1006/*
1007 * Check a public RSA key
1008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001010{
Paul Bakker37940d92009-07-10 22:38:58 +00001011 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +00001013
Hanno Beckerba1ba112017-09-29 11:48:23 +01001014 if( ctx->len != mbedtls_mpi_size( &ctx->N ) )
1015 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1016
Paul Bakker48377d92013-08-30 12:06:24 +02001017 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001021 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
1022 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
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->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
1027 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
1029 return( 0 );
1030}
1031
1032/*
1033 * Check a private RSA key
1034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001036{
Hanno Beckerb269a852017-08-25 08:03:21 +01001037 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
1038 mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
1039 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 {
Hanno Beckerb269a852017-08-25 08:03:21 +01001041 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 }
Hanno Beckerb269a852017-08-25 08:03:21 +01001043#if !defined(MBEDTLS_RSA_NO_CRT)
1044 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
1045 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
1046 {
1047 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1048 }
1049#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001050
1051 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001052}
1053
1054/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001055 * Check if contexts holding a public and private key match
1056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001058{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1060 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001063 }
1064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1066 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001069 }
1070
1071 return( 0 );
1072}
1073
1074/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 * Do an RSA public key operation
1076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001078 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 unsigned char *output )
1080{
Paul Bakker23986e52011-04-24 08:57:21 +00001081 int ret;
1082 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001087#if defined(MBEDTLS_THREADING_C)
1088 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1089 return( ret );
1090#endif
1091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001096 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1097 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 }
1099
1100 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1102 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
1104cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001106 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1107 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001108#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
1112 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
1115 return( 0 );
1116}
1117
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001118/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001119 * Generate or update blinding values, see section 10 of:
1120 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001121 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001122 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001123 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001124static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001125 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1126{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001127 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001128
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001129 if( ctx->Vf.p != NULL )
1130 {
1131 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1133 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1134 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1135 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001136
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001137 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001138 }
1139
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001140 /* Unblinding value: Vf = random number, invertible mod N */
1141 do {
1142 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1146 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1147 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001148
1149 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1151 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 +02001152
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001153
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001154cleanup:
1155 return( ret );
1156}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001157
Paul Bakker5121ce52009-01-03 21:22:43 +00001158/*
Janos Follathe81102e2017-03-22 13:38:28 +00001159 * Exponent blinding supposed to prevent side-channel attacks using multiple
1160 * traces of measurements to recover the RSA key. The more collisions are there,
1161 * the more bits of the key can be recovered. See [3].
1162 *
1163 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1164 * observations on avarage.
1165 *
1166 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1167 * to make 2^112 observations on avarage.
1168 *
1169 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1170 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1171 * Thus in this sense with 28 byte blinding the security is not reduced by
1172 * side-channel attacks like the one in [3])
1173 *
1174 * This countermeasure does not help if the key recovery is possible with a
1175 * single trace.
1176 */
1177#define RSA_EXPONENT_BLINDING 28
1178
1179/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 * Do an RSA private key operation
1181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001183 int (*f_rng)(void *, unsigned char *, size_t),
1184 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001185 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 unsigned char *output )
1187{
Paul Bakker23986e52011-04-24 08:57:21 +00001188 int ret;
1189 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001191 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001192#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001193 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001194 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001195#else
1196 mbedtls_mpi DP_blind, DQ_blind;
1197 mbedtls_mpi *DP = &ctx->DP;
1198 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001199#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
Hanno Becker45037ce2017-08-25 11:03:34 +01001201 /* Sanity-check that all relevant fields are at least set,
1202 * but don't perform a full keycheck. */
1203 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
1204 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
1205 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
1206 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
1207 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
1208 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker45037ce2017-08-25 11:03:34 +01001210 }
1211#if !defined(MBEDTLS_RSA_NO_CRT)
1212 if( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
1213 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
1214 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
1215 {
1216 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1217 }
1218#endif /* MBEDTLS_RSA_NO_CRT */
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001221 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1222
Janos Follathf9203b42017-03-22 15:13:15 +00001223 if( f_rng != NULL )
1224 {
Janos Follathe81102e2017-03-22 13:38:28 +00001225#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001226 mbedtls_mpi_init( &D_blind );
1227#else
1228 mbedtls_mpi_init( &DP_blind );
1229 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001230#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001231 }
Janos Follathe81102e2017-03-22 13:38:28 +00001232
Paul Bakker5121ce52009-01-03 21:22:43 +00001233
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001234#if defined(MBEDTLS_THREADING_C)
1235 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1236 return( ret );
1237#endif
1238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1240 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001242 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1243 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 }
1245
Paul Bakkerf451bac2013-08-30 15:37:02 +02001246 if( f_rng != NULL )
1247 {
1248 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001249 * Blinding
1250 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001251 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001252 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1253 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001255
Janos Follathe81102e2017-03-22 13:38:28 +00001256 /*
1257 * Exponent blinding
1258 */
1259 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1260 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1261
Janos Follathf9203b42017-03-22 15:13:15 +00001262#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001263 /*
1264 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1265 */
1266 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1267 f_rng, p_rng ) );
1268 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1269 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1270 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1271
1272 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001273#else
1274 /*
1275 * DP_blind = ( P - 1 ) * R + DP
1276 */
1277 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1278 f_rng, p_rng ) );
1279 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1280 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1281 &ctx->DP ) );
1282
1283 DP = &DP_blind;
1284
1285 /*
1286 * DQ_blind = ( Q - 1 ) * R + DQ
1287 */
1288 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1289 f_rng, p_rng ) );
1290 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1291 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1292 &ctx->DQ ) );
1293
1294 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001295#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001296 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001299 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001300#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001301 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001302 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 *
1304 * T1 = input ^ dP mod P
1305 * T2 = input ^ dQ mod Q
1306 */
Janos Follathf9203b42017-03-22 15:13:15 +00001307 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1308 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
1310 /*
1311 * T = (T1 - T2) * (Q^-1 mod P) mod P
1312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1314 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1315 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
1317 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001318 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1321 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1322#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001323
Paul Bakkerf451bac2013-08-30 15:37:02 +02001324 if( f_rng != NULL )
1325 {
1326 /*
1327 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001328 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001329 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001330 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001332 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
1334 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
1337cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001339 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1340 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001341#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001344 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1345
1346 if( f_rng != NULL )
1347 {
Janos Follathe81102e2017-03-22 13:38:28 +00001348#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001349 mbedtls_mpi_free( &D_blind );
1350#else
1351 mbedtls_mpi_free( &DP_blind );
1352 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001353#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001354 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001355
1356 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
1359 return( 0 );
1360}
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001363/**
1364 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1365 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001366 * \param dst buffer to mask
1367 * \param dlen length of destination buffer
1368 * \param src source of the mask generation
1369 * \param slen length of the source buffer
1370 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001371 */
Paul Bakker48377d92013-08-30 12:06:24 +02001372static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001374{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001376 unsigned char counter[4];
1377 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001378 unsigned int hlen;
1379 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001382 memset( counter, 0, 4 );
1383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001385
Simon Butcher02037452016-03-01 21:19:12 +00001386 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001387 p = dst;
1388
1389 while( dlen > 0 )
1390 {
1391 use_len = hlen;
1392 if( dlen < hlen )
1393 use_len = dlen;
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 mbedtls_md_starts( md_ctx );
1396 mbedtls_md_update( md_ctx, src, slen );
1397 mbedtls_md_update( md_ctx, counter, 4 );
1398 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001399
1400 for( i = 0; i < use_len; ++i )
1401 *p++ ^= mask[i];
1402
1403 counter[3]++;
1404
1405 dlen -= use_len;
1406 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001407
1408 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001413/*
1414 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001417 int (*f_rng)(void *, unsigned char *, size_t),
1418 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001419 int mode,
1420 const unsigned char *label, size_t label_len,
1421 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001422 const unsigned char *input,
1423 unsigned char *output )
1424{
1425 size_t olen;
1426 int ret;
1427 unsigned char *p = output;
1428 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 const mbedtls_md_info_t *md_info;
1430 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1433 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001434
1435 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001439 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001441
1442 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001444
Simon Butcher02037452016-03-01 21:19:12 +00001445 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001446 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001448
1449 memset( output, 0, olen );
1450
1451 *p++ = 0;
1452
Simon Butcher02037452016-03-01 21:19:12 +00001453 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001454 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001456
1457 p += hlen;
1458
Simon Butcher02037452016-03-01 21:19:12 +00001459 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001461 p += hlen;
1462 p += olen - 2 * hlen - 2 - ilen;
1463 *p++ = 1;
1464 memcpy( p, input, ilen );
1465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001467 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1468 {
1469 mbedtls_md_free( &md_ctx );
1470 return( ret );
1471 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001472
Simon Butcher02037452016-03-01 21:19:12 +00001473 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001474 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1475 &md_ctx );
1476
Simon Butcher02037452016-03-01 21:19:12 +00001477 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001478 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1479 &md_ctx );
1480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 return( ( mode == MBEDTLS_RSA_PUBLIC )
1484 ? mbedtls_rsa_public( ctx, output, output )
1485 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001490/*
1491 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001494 int (*f_rng)(void *, unsigned char *, size_t),
1495 void *p_rng,
1496 int mode, size_t ilen,
1497 const unsigned char *input,
1498 unsigned char *output )
1499{
1500 size_t nb_pad, olen;
1501 int ret;
1502 unsigned char *p = output;
1503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1505 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001506
Janos Follath1ed9f992016-03-18 11:45:44 +00001507 // We don't check p_rng because it won't be dereferenced here
1508 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001510
1511 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001512
Simon Butcher02037452016-03-01 21:19:12 +00001513 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001514 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001516
1517 nb_pad = olen - 3 - ilen;
1518
1519 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001523
1524 while( nb_pad-- > 0 )
1525 {
1526 int rng_dl = 100;
1527
1528 do {
1529 ret = f_rng( p_rng, p, 1 );
1530 } while( *p == 0 && --rng_dl && ret == 0 );
1531
Simon Butcher02037452016-03-01 21:19:12 +00001532 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001533 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001535
1536 p++;
1537 }
1538 }
1539 else
1540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001542
1543 while( nb_pad-- > 0 )
1544 *p++ = 0xFF;
1545 }
1546
1547 *p++ = 0;
1548 memcpy( p, input, ilen );
1549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 return( ( mode == MBEDTLS_RSA_PUBLIC )
1551 ? mbedtls_rsa_public( ctx, output, output )
1552 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001553}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001555
Paul Bakker5121ce52009-01-03 21:22:43 +00001556/*
1557 * Add the message padding, then do an RSA operation
1558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001560 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001561 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001562 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001563 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 unsigned char *output )
1565{
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 switch( ctx->padding )
1567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_PKCS1_V15)
1569 case MBEDTLS_RSA_PKCS_V15:
1570 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001571 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001572#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#if defined(MBEDTLS_PKCS1_V21)
1575 case MBEDTLS_RSA_PKCS_V21:
1576 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001577 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001578#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001579
1580 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583}
1584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001586/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001587 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001590 int (*f_rng)(void *, unsigned char *, size_t),
1591 void *p_rng,
1592 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001593 const unsigned char *label, size_t label_len,
1594 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001595 const unsigned char *input,
1596 unsigned char *output,
1597 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001598{
Paul Bakker23986e52011-04-24 08:57:21 +00001599 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001600 size_t ilen, i, pad_len;
1601 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1603 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001604 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 const mbedtls_md_info_t *md_info;
1606 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001607
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001608 /*
1609 * Parameters sanity checks
1610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1612 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
1614 ilen = ctx->len;
1615
Paul Bakker27fdf462011-06-09 13:55:13 +00001616 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001620 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001622
Janos Follathc17cda12016-02-11 11:08:18 +00001623 hlen = mbedtls_md_get_size( md_info );
1624
1625 // checking for integer underflow
1626 if( 2 * hlen + 2 > ilen )
1627 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1628
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001629 /*
1630 * RSA operation
1631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1633 ? mbedtls_rsa_public( ctx, input, buf )
1634 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
1636 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001637 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001638
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001639 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001640 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001643 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1644 {
1645 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001646 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001647 }
1648
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001649
1650 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001652
1653 /* seed: Apply seedMask to maskedSeed */
1654 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1655 &md_ctx );
1656
1657 /* DB: Apply dbMask to maskedDB */
1658 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1659 &md_ctx );
1660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001662
1663 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001664 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001665 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001666 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001667 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001668
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001669 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001670
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001671 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001672
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001673 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001674 for( i = 0; i < hlen; i++ )
1675 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001676
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001677 /* Get zero-padding len, but always read till end of buffer
1678 * (minus one, for the 01 byte) */
1679 pad_len = 0;
1680 pad_done = 0;
1681 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1682 {
1683 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001684 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001685 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001686
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001687 p += pad_len;
1688 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001689
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001690 /*
1691 * The only information "leaked" is whether the padding was correct or not
1692 * (eg, no data is copied if it was not correct). This meets the
1693 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1694 * the different error conditions.
1695 */
1696 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001697 {
1698 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1699 goto cleanup;
1700 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001701
Paul Bakker66d5d072014-06-17 16:39:18 +02001702 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001703 {
1704 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1705 goto cleanup;
1706 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001707
1708 *olen = ilen - (p - buf);
1709 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001710 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001711
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001712cleanup:
1713 mbedtls_zeroize( buf, sizeof( buf ) );
1714 mbedtls_zeroize( lhash, sizeof( lhash ) );
1715
1716 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001717}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001721/*
1722 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001725 int (*f_rng)(void *, unsigned char *, size_t),
1726 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001727 int mode, size_t *olen,
1728 const unsigned char *input,
1729 unsigned char *output,
1730 size_t output_max_len)
1731{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001732 int ret;
1733 size_t ilen, pad_count = 0, i;
1734 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1738 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001739
1740 ilen = ctx->len;
1741
1742 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1746 ? mbedtls_rsa_public( ctx, input, buf )
1747 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001748
1749 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001750 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001751
1752 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001753 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001754
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001755 /*
1756 * Check and get padding len in "constant-time"
1757 */
1758 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001759
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001760 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001764
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001765 /* Get padding len, but always read till end of buffer
1766 * (minus one, for the 00 byte) */
1767 for( i = 0; i < ilen - 3; i++ )
1768 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001769 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1770 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001771 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001772
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001773 p += pad_count;
1774 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001775 }
1776 else
1777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001779
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001780 /* Get padding len, but always read till end of buffer
1781 * (minus one, for the 00 byte) */
1782 for( i = 0; i < ilen - 3; i++ )
1783 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001784 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001785 pad_count += ( pad_done == 0 );
1786 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001787
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001788 p += pad_count;
1789 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 }
1791
Janos Follathc69fa502016-02-12 13:30:09 +00001792 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001793
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001794 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001795 {
1796 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1797 goto cleanup;
1798 }
Paul Bakker8804f692013-02-28 18:06:26 +01001799
Paul Bakker66d5d072014-06-17 16:39:18 +02001800 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001801 {
1802 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1803 goto cleanup;
1804 }
Paul Bakker060c5682009-01-12 21:48:39 +00001805
Paul Bakker27fdf462011-06-09 13:55:13 +00001806 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001808 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001809
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001810cleanup:
1811 mbedtls_zeroize( buf, sizeof( buf ) );
1812
1813 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001814}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001816
1817/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001818 * Do an RSA operation, then remove the message padding
1819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001821 int (*f_rng)(void *, unsigned char *, size_t),
1822 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001823 int mode, size_t *olen,
1824 const unsigned char *input,
1825 unsigned char *output,
1826 size_t output_max_len)
1827{
1828 switch( ctx->padding )
1829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#if defined(MBEDTLS_PKCS1_V15)
1831 case MBEDTLS_RSA_PKCS_V15:
1832 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001833 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001834#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836#if defined(MBEDTLS_PKCS1_V21)
1837 case MBEDTLS_RSA_PKCS_V21:
1838 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001839 olen, input, output,
1840 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001841#endif
1842
1843 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001845 }
1846}
1847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001849/*
1850 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001853 int (*f_rng)(void *, unsigned char *, size_t),
1854 void *p_rng,
1855 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001857 unsigned int hashlen,
1858 const unsigned char *hash,
1859 unsigned char *sig )
1860{
1861 size_t olen;
1862 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001864 unsigned int slen, hlen, offset = 0;
1865 int ret;
1866 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 const mbedtls_md_info_t *md_info;
1868 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1871 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001872
1873 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001875
1876 olen = ctx->len;
1877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001879 {
Simon Butcher02037452016-03-01 21:19:12 +00001880 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001882 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001886 }
1887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001889 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001893 slen = hlen;
1894
1895 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001897
1898 memset( sig, 0, olen );
1899
Simon Butcher02037452016-03-01 21:19:12 +00001900 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001901 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001903
Simon Butcher02037452016-03-01 21:19:12 +00001904 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001905 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001906 p += olen - hlen * 2 - 2;
1907 *p++ = 0x01;
1908 memcpy( p, salt, slen );
1909 p += slen;
1910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001912 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1913 {
1914 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001915 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001916 return( ret );
1917 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001918
Simon Butcher02037452016-03-01 21:19:12 +00001919 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 mbedtls_md_starts( &md_ctx );
1921 mbedtls_md_update( &md_ctx, p, 8 );
1922 mbedtls_md_update( &md_ctx, hash, hashlen );
1923 mbedtls_md_update( &md_ctx, salt, slen );
1924 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001925 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001926
Simon Butcher02037452016-03-01 21:19:12 +00001927 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001928 if( msb % 8 == 0 )
1929 offset = 1;
1930
Simon Butcher02037452016-03-01 21:19:12 +00001931 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001932 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001935
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001936 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001937 sig[0] &= 0xFF >> ( olen * 8 - msb );
1938
1939 p += hlen;
1940 *p++ = 0xBC;
1941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 return( ( mode == MBEDTLS_RSA_PUBLIC )
1943 ? mbedtls_rsa_public( ctx, sig, sig )
1944 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001945}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001949/*
1950 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1951 */
1952/*
1953 * Do an RSA operation to sign the message digest
1954 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001956 int (*f_rng)(void *, unsigned char *, size_t),
1957 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001958 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001960 unsigned int hashlen,
1961 const unsigned char *hash,
1962 unsigned char *sig )
1963{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001964 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001965 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001966 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001967 unsigned char *sig_try = NULL, *verif = NULL;
1968 size_t i;
1969 unsigned char diff;
1970 volatile unsigned char diff_no_optimize;
1971 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1974 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001975
1976 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001977 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001982 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1986 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001987
Paul Bakkerc70b9822013-04-07 22:00:46 +02001988 nb_pad -= 10 + oid_size;
1989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001991 }
1992
Paul Bakkerc70b9822013-04-07 22:00:46 +02001993 nb_pad -= hashlen;
1994
Paul Bakkerb3869132013-02-28 17:21:01 +01001995 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001997
1998 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01002000 memset( p, 0xFF, nb_pad );
2001 p += nb_pad;
2002 *p++ = 0;
2003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002005 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002006 memcpy( p, hash, hashlen );
2007 }
2008 else
2009 {
2010 /*
2011 * DigestInfo ::= SEQUENCE {
2012 * digestAlgorithm DigestAlgorithmIdentifier,
2013 * digest Digest }
2014 *
2015 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2016 *
2017 * Digest ::= OCTET STRING
2018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002020 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002022 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002024 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002025 memcpy( p, oid, oid_size );
2026 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002028 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002030 *p++ = hashlen;
2031 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002032 }
2033
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002034 if( mode == MBEDTLS_RSA_PUBLIC )
2035 return( mbedtls_rsa_public( ctx, sig, sig ) );
2036
2037 /*
2038 * In order to prevent Lenstra's attack, make the signature in a
2039 * temporary buffer and check it before returning it.
2040 */
2041 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002042 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002043 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2044
Simon Butcher1285ab52016-01-01 21:42:47 +00002045 verif = mbedtls_calloc( 1, ctx->len );
2046 if( verif == NULL )
2047 {
2048 mbedtls_free( sig_try );
2049 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2050 }
2051
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002052 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2053 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2054
2055 /* Compare in constant time just in case */
2056 for( diff = 0, i = 0; i < ctx->len; i++ )
2057 diff |= verif[i] ^ sig[i];
2058 diff_no_optimize = diff;
2059
2060 if( diff_no_optimize != 0 )
2061 {
2062 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2063 goto cleanup;
2064 }
2065
2066 memcpy( sig, sig_try, ctx->len );
2067
2068cleanup:
2069 mbedtls_free( sig_try );
2070 mbedtls_free( verif );
2071
2072 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002073}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002075
2076/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 * Do an RSA operation to sign the message digest
2078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002080 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002081 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002084 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002085 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 unsigned char *sig )
2087{
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 switch( ctx->padding )
2089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090#if defined(MBEDTLS_PKCS1_V15)
2091 case MBEDTLS_RSA_PKCS_V15:
2092 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002093 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002094#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096#if defined(MBEDTLS_PKCS1_V21)
2097 case MBEDTLS_RSA_PKCS_V21:
2098 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002099 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002100#endif
2101
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002105}
2106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002108/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002109 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002112 int (*f_rng)(void *, unsigned char *, size_t),
2113 void *p_rng,
2114 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002116 unsigned int hashlen,
2117 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002119 int expected_salt_len,
2120 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121{
Paul Bakker23986e52011-04-24 08:57:21 +00002122 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002123 size_t siglen;
2124 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002126 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002127 unsigned int hlen;
2128 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 const mbedtls_md_info_t *md_info;
2130 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002131 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2134 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002135
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 siglen = ctx->len;
2137
Paul Bakker27fdf462011-06-09 13:55:13 +00002138 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2142 ? mbedtls_rsa_public( ctx, sig, buf )
2143 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002144
2145 if( ret != 0 )
2146 return( ret );
2147
2148 p = buf;
2149
Paul Bakkerb3869132013-02-28 17:21:01 +01002150 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 {
Simon Butcher02037452016-03-01 21:19:12 +00002155 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002157 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002161 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002164 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002168 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002169
Paul Bakkerb3869132013-02-28 17:21:01 +01002170 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002171
Simon Butcher02037452016-03-01 21:19:12 +00002172 /*
2173 * Note: EMSA-PSS verification is over the length of N - 1 bits
2174 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002175 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002176
Simon Butcher02037452016-03-01 21:19:12 +00002177 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002178 if( msb % 8 == 0 )
2179 {
2180 p++;
2181 siglen -= 1;
2182 }
2183 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002187 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2188 {
2189 mbedtls_md_free( &md_ctx );
2190 return( ret );
2191 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002192
Paul Bakkerb3869132013-02-28 17:21:01 +01002193 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002194
Paul Bakkerb3869132013-02-28 17:21:01 +01002195 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002196
Paul Bakker4de44aa2013-12-31 11:43:01 +01002197 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002198 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002199
Paul Bakkerb3869132013-02-28 17:21:01 +01002200 if( p == buf + siglen ||
2201 *p++ != 0x01 )
2202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 mbedtls_md_free( &md_ctx );
2204 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002205 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002206
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002207 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002208 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002211 slen != (size_t) expected_salt_len )
2212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 mbedtls_md_free( &md_ctx );
2214 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002215 }
2216
Simon Butcher02037452016-03-01 21:19:12 +00002217 /*
2218 * Generate H = Hash( M' )
2219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 mbedtls_md_starts( &md_ctx );
2221 mbedtls_md_update( &md_ctx, zeros, 8 );
2222 mbedtls_md_update( &md_ctx, hash, hashlen );
2223 mbedtls_md_update( &md_ctx, p, slen );
2224 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002227
Paul Bakkerb3869132013-02-28 17:21:01 +01002228 if( memcmp( p + slen, result, hlen ) == 0 )
2229 return( 0 );
2230 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002232}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002233
2234/*
2235 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002238 int (*f_rng)(void *, unsigned char *, size_t),
2239 void *p_rng,
2240 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002242 unsigned int hashlen,
2243 const unsigned char *hash,
2244 const unsigned char *sig )
2245{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2247 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002248 : md_alg;
2249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002251 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002253 sig ) );
2254
2255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002259/*
2260 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002263 int (*f_rng)(void *, unsigned char *, size_t),
2264 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002265 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002267 unsigned int hashlen,
2268 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002269 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002270{
2271 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002272 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002273 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 mbedtls_md_type_t msg_md_alg;
2275 const mbedtls_md_info_t *md_info;
2276 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002277 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2280 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002281
2282 siglen = ctx->len;
2283
2284 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2288 ? mbedtls_rsa_public( ctx, sig, buf )
2289 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002290
2291 if( ret != 0 )
2292 return( ret );
2293
2294 p = buf;
2295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2297 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002298
2299 while( *p != 0 )
2300 {
2301 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002303 p++;
2304 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002305 p++; /* skip 00 byte */
2306
2307 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2308 if( p - buf < 11 )
2309 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002310
2311 len = siglen - ( p - buf );
2312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002314 {
2315 if( memcmp( p, hash, hashlen ) == 0 )
2316 return( 0 );
2317 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002319 }
2320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002322 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2324 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002325
2326 end = p + len;
2327
Simon Butcher02037452016-03-01 21:19:12 +00002328 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002329 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2330 * Insist on 2-byte length tags, to protect against variants of
2331 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002332 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002333 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2335 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2336 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002337 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002339
Gilles Peskinee7e76502017-05-04 12:48:39 +02002340 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2342 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2343 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002344 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002346
Gilles Peskinee7e76502017-05-04 12:48:39 +02002347 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2349 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002350 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002351 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002352
2353 oid.p = p;
2354 p += oid.len;
2355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2357 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002358
2359 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002361
2362 /*
2363 * assume the algorithm parameters must be NULL
2364 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002365 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2367 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002368 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002370
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002371 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002372 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2373 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002374 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002376
2377 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002379
2380 p += hashlen;
2381
2382 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002384
2385 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002386}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
2389/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002390 * Do an RSA operation and check the message digest
2391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002393 int (*f_rng)(void *, unsigned char *, size_t),
2394 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002395 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002397 unsigned int hashlen,
2398 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002399 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002400{
2401 switch( ctx->padding )
2402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403#if defined(MBEDTLS_PKCS1_V15)
2404 case MBEDTLS_RSA_PKCS_V15:
2405 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002406 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002407#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409#if defined(MBEDTLS_PKCS1_V21)
2410 case MBEDTLS_RSA_PKCS_V21:
2411 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002412 hashlen, hash, sig );
2413#endif
2414
2415 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002417 }
2418}
2419
2420/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002421 * Copy the components of an RSA key
2422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002424{
2425 int ret;
2426
2427 dst->ver = src->ver;
2428 dst->len = src->len;
2429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2431 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2434 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2435 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002436
2437#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2439 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2440 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2442 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002443#endif
2444
2445 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2448 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002449
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002450 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002451 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002452
2453cleanup:
2454 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002456
2457 return( ret );
2458}
2459
2460/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 * Free the components of an RSA key
2462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002464{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002466 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2467 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002469
Hanno Becker33c30a02017-08-23 07:00:22 +01002470#if !defined(MBEDTLS_RSA_NO_CRT)
2471 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2472 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2473 mbedtls_mpi_free( &ctx->DP );
2474#endif /* MBEDTLS_RSA_NO_CRT */
2475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476#if defined(MBEDTLS_THREADING_C)
2477 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002478#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002479}
2480
Hanno Beckerab377312017-08-23 16:24:51 +01002481#endif /* !MBEDTLS_RSA_ALT */
2482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002484
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002485#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
2487/*
2488 * Example RSA-1024 keypair, for test purposes
2489 */
2490#define KEY_LEN 128
2491
2492#define RSA_N "9292758453063D803DD603D5E777D788" \
2493 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2494 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2495 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2496 "93A89813FBF3C4F8066D2D800F7C38A8" \
2497 "1AE31942917403FF4946B0A83D3D3E05" \
2498 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2499 "5E94BB77B07507233A0BC7BAC8F90F79"
2500
2501#define RSA_E "10001"
2502
2503#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2504 "66CA472BC44D253102F8B4A9D3BFA750" \
2505 "91386C0077937FE33FA3252D28855837" \
2506 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2507 "DF79C5CE07EE72C7F123142198164234" \
2508 "CABB724CF78B8173B9F880FC86322407" \
2509 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2510 "071513A1E85B5DFA031F21ECAE91A34D"
2511
2512#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2513 "2C01CAD19EA484A87EA4377637E75500" \
2514 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2515 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2516
2517#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2518 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2519 "910E4168387E3C30AA1E00C339A79508" \
2520 "8452DD96A9A5EA5D9DCA68DA636032AF"
2521
2522#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2523 "3C94D22288ACD763FD8E5600ED4A702D" \
2524 "F84198A5F06C2E72236AE490C93F07F8" \
2525 "3CC559CD27BC2D1CA488811730BB5725"
2526
2527#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2528 "D8AAEA56749EA28623272E4F7D0592AF" \
2529 "7C1F1313CAC9471B5C523BFE592F517B" \
2530 "407A1BD76C164B93DA2D32A383E58357"
2531
2532#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2533 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2534 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2535 "A74206CEC169D74BF5A8C50D6F48EA08"
2536
2537#define PT_LEN 24
2538#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2539 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002542static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002543{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002544#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002545 size_t i;
2546
Paul Bakker545570e2010-07-18 09:00:25 +00002547 if( rng_state != NULL )
2548 rng_state = NULL;
2549
Paul Bakkera3d195c2011-11-27 21:07:34 +00002550 for( i = 0; i < len; ++i )
2551 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002552#else
2553 if( rng_state != NULL )
2554 rng_state = NULL;
2555
2556 arc4random_buf( output, len );
2557#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002558
Paul Bakkera3d195c2011-11-27 21:07:34 +00002559 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002560}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002562
Paul Bakker5121ce52009-01-03 21:22:43 +00002563/*
2564 * Checkup routine
2565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002567{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002568 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002570 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 unsigned char rsa_plaintext[PT_LEN];
2573 unsigned char rsa_decrypted[PT_LEN];
2574 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002576 unsigned char sha1sum[20];
2577#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Hanno Becker3a701162017-08-22 13:52:43 +01002579 mbedtls_mpi K;
2580
2581 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
Hanno Becker3a701162017-08-22 13:52:43 +01002584 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2585 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2586 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2587 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2588 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2589 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2590 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2591 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2592 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2593 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2594
2595 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
2597 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2601 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 {
2603 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
2606 return( 1 );
2607 }
2608
Hanno Becker3a701162017-08-22 13:52:43 +01002609 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2610 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2611
2612 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2613 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2614
2615 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2616 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2617
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
2621 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 rsa_plaintext, rsa_ciphertext ) != 0 )
2625 {
2626 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
2629 return( 1 );
2630 }
2631
2632 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002636 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002637 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 {
2639 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
2642 return( 1 );
2643 }
2644
2645 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2646 {
2647 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
2650 return( 1 );
2651 }
2652
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002653 if( verbose != 0 )
2654 mbedtls_printf( "passed\n" );
2655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002658 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 sha1sum, rsa_ciphertext ) != 0 )
2664 {
2665 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
2668 return( 1 );
2669 }
2670
2671 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 sha1sum, rsa_ciphertext ) != 0 )
2676 {
2677 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
2680 return( 1 );
2681 }
2682
2683 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002684 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002687 if( verbose != 0 )
2688 mbedtls_printf( "\n" );
2689
Paul Bakker3d8fb632014-04-17 12:42:41 +02002690cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002691 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 mbedtls_rsa_free( &rsa );
2693#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002694 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002696 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002697}
2698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#endif /* MBEDTLS_RSA_C */