blob: bb456df496490c3f56c39846ed288cf7cccbf571 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000022 * The following sources were referenced in the design of this implementation
23 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000024 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000025 * [1] A method for obtaining digital signatures and public-key cryptosystems
26 * R Rivest, A Shamir, and L Adleman
27 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
28 *
29 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
30 * Menezes, van Oorschot and Vanstone
31 *
Janos Follathe81102e2017-03-22 13:38:28 +000032 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
33 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
34 * Stefan Mangard
35 * https://arxiv.org/abs/1702.08719v2
36 *
Paul Bakker5121ce52009-01-03 21:22:43 +000037 */
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020041#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/rsa.h"
48#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#else
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020065#define mbedtls_calloc calloc
66#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#endif
68
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010069/* Implementation that should never be optimized out by the compiler */
70static void mbedtls_zeroize( void *v, size_t n ) {
71 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
72}
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010075 * Context-independent RSA helper functions.
76 *
Hanno Beckerd9431a72017-08-25 08:03:13 +010077 * There are two classes of helper functions:
78 * (1) Parameter-generating helpers. These are:
79 * - mbedtls_rsa_deduce_moduli
80 * - mbedtls_rsa_deduce_private
81 * - mbedtls_rsa_deduce_crt
82 * Each of these functions takes a set of core RSA parameters
83 * and generates some other, or CRT related parameters.
84 * (2) Parameter-checking helpers. These are:
85 * - mbedtls_rsa_validate_params
86 * - mbedtls_rsa_validate_crt
87 * They take a set of core or CRT related RSA parameters
88 * and check their validity.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010089 *
Hanno Beckerd9431a72017-08-25 08:03:13 +010090 * The helper functions do not use the RSA context structure
91 * and therefore do not need to be replaced when providing
92 * an alternative RSA implementation.
93 *
94 * Their main purpose is to provide common MPI operations in the context
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010095 * of RSA that can be easily shared across multiple implementations.
96 */
97
98/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010099 *
100 * Given the modulus N=PQ and a pair of public and private
101 * exponents E and D, respectively, factor N.
102 *
103 * Setting F := lcm(P-1,Q-1), the idea is as follows:
104 *
105 * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
106 * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
107 * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
108 * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
109 * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
110 * factors of N.
111 *
112 * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
113 * construction still applies since (-)^K is the identity on the set of
114 * roots of 1 in Z/NZ.
115 *
116 * The public and private key primitives (-)^E and (-)^D are mutually inverse
117 * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
118 * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
119 * Splitting L = 2^t * K with K odd, we have
120 *
121 * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
122 *
123 * so (F / 2) * K is among the numbers
124 *
125 * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
126 *
127 * where ord is the order of 2 in (DE - 1).
128 * We can therefore iterate through these numbers apply the construction
129 * of (a) and (b) above to attempt to factor N.
130 *
131 */
132int mbedtls_rsa_deduce_moduli( mbedtls_mpi *N, mbedtls_mpi *D, mbedtls_mpi *E,
133 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
134 mbedtls_mpi *P, mbedtls_mpi *Q )
135{
136 /* Implementation note:
137 *
138 * Space-efficiency is given preference over time-efficiency here:
139 * several calculations are done in place and temporarily change
140 * the values of D and E.
141 *
Hanno Beckerbead7172017-09-29 12:41:06 +0100142 * Specifically, D is replaced by the largest odd divisor of DE - 1
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100143 * throughout the calculations.
144 */
145
146 int ret = 0;
147
148 uint16_t attempt; /* Number of current attempt */
149 uint16_t iter; /* Number of squares computed in the current attempt */
150
151 uint16_t bitlen_half; /* Half the bitsize of the modulus N */
152 uint16_t order; /* Order of 2 in DE - 1 */
153
154 mbedtls_mpi K; /* Temporary used for two purposes:
Hanno Becker56bae952017-09-29 15:33:10 +0100155 * - During factorization attempts, stores a random integer
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100156 * in the range of [0,..,N]
157 * - During verification, holding intermediate results.
158 */
159
160 if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
161 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
162
163 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
164 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
165 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
166 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
167 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
168 {
169 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
170 }
171
172 /*
173 * Initializations and temporary changes
174 */
175
176 mbedtls_mpi_init( &K );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100177
178 /* Replace D by DE - 1 */
179 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( D, D, E ) );
180 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( D, D, 1 ) );
181
182 if( ( order = mbedtls_mpi_lsb( D ) ) == 0 )
183 {
184 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
185 goto cleanup;
186 }
187
188 /* After this operation, D holds the largest odd divisor
189 * of DE - 1 for the original values of D and E. */
190 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( D, order ) );
191
192 /* This is used to generate a few numbers around N / 2
193 * if no PRNG is provided. */
194 if( f_rng == NULL )
195 bitlen_half = mbedtls_mpi_bitlen( N ) / 2;
196
197 /*
198 * Actual work
199 */
200
201 for( attempt = 0; attempt < 30; ++attempt )
202 {
203 /* Generate some number in [0,N], either randomly
204 * if a PRNG is given, or try numbers around N/2 */
205 if( f_rng != NULL )
206 {
207 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &K,
208 mbedtls_mpi_size( N ),
209 f_rng, p_rng ) );
210 }
211 else
212 {
213 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &K, 1 ) ) ;
214 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &K, bitlen_half ) ) ;
215 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, attempt + 1 ) );
216 }
217
218 /* Check if gcd(K,N) = 1 */
219 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
220 if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
221 continue;
222
223 /* Go through K^X + 1, K^(2X) + 1, K^(4X) + 1, ...
224 * and check whether they have nontrivial GCD with N. */
225 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, D, N,
226 Q /* temporarily use Q for storing Montgomery
227 * multiplication helper values */ ) );
228
229 for( iter = 1; iter < order; ++iter )
230 {
231 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
232 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
233
234 if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
235 mbedtls_mpi_cmp_mpi( P, N ) == -1 )
236 {
237 /*
238 * Have found a nontrivial divisor P of N.
Hanno Beckerd56d83a2017-08-25 07:29:35 +0100239 * Set Q := N / P.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100240 */
241
242 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, &K, N, P ) );
243
Hanno Beckerd56d83a2017-08-25 07:29:35 +0100244 /* Restore D */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100245
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100246 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( D, order ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100247 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( D, D, 1 ) );
248 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( D, NULL, D, E ) );
249
250 goto cleanup;
251 }
252
253 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
254 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
255 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
256 }
257 }
258
259 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
260
261cleanup:
262
263 mbedtls_mpi_free( &K );
264 return( ret );
265}
266
267/*
268 * Given P, Q and the public exponent E, deduce D.
269 * This is essentially a modular inversion.
270 */
271
272int mbedtls_rsa_deduce_private( mbedtls_mpi *P, mbedtls_mpi *Q,
273 mbedtls_mpi *D, mbedtls_mpi *E )
274{
275 int ret = 0;
276 mbedtls_mpi K;
277
278 if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
279 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
280
281 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
282 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
283 mbedtls_mpi_cmp_int( E, 0 ) == 0 )
284 {
285 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
286 }
287
288 mbedtls_mpi_init( &K );
289
290 /* Temporarily replace P and Q by P-1 and Q-1, respectively. */
291 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) );
292 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) );
293
294 /* Temporarily compute the gcd(P-1, Q-1) in D. */
295 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, P, Q ) );
296
297 /* Compute LCM(P-1, Q-1) in K */
298 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
299 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
300
301 /* Compute modular inverse of E in LCM(P-1, Q-1) */
302 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
303
304 /* Restore P and Q. */
305 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) );
306 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) );
307
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100308cleanup:
309
310 mbedtls_mpi_free( &K );
311
312 return( ret );
313}
314
315/*
Hanno Beckerd3637992017-08-25 07:55:03 +0100316 * Check that RSA CRT parameters are in accordance with core parameters.
317 */
318
319int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
320 const mbedtls_mpi *D, const mbedtls_mpi *DP,
321 const mbedtls_mpi *DQ, const mbedtls_mpi *QP )
322{
323 int ret = 0;
324
325 mbedtls_mpi K, L;
326 mbedtls_mpi_init( &K );
327 mbedtls_mpi_init( &L );
328
329 /* Check that DP - P == 0 mod P - 1 */
330 if( DP != NULL )
331 {
332 if( P == NULL )
333 {
334 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
335 goto cleanup;
336 }
337
338 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
339 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DP, D ) );
340 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
341
342 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
343 {
344 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
345 }
346 }
347
348 /* Check that DQ - Q == 0 mod Q - 1 */
349 if( DQ != NULL )
350 {
351 if( Q == NULL )
352 {
353 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
354 goto cleanup;
355 }
356
357 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
358 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
359 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
360
361 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
362 {
363 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
364 }
365 }
366
367 /* Check that QP * P - 1 == 0 mod P */
368 if( QP != NULL )
369 {
370 if( P == NULL || Q == NULL )
371 {
372 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
373 goto cleanup;
374 }
375
376 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
377 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
378 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
379 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
380 {
381 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
382 }
383 }
384
385cleanup:
386
387 /* Wrap MPI error codes by RSA check failure error code */
388 if( ret != 0 &&
389 ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
390 ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
391 {
392 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
393 }
394
395 mbedtls_mpi_free( &K );
396 mbedtls_mpi_free( &L );
397
398 return( ret );
399}
400
401/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100402 * Check that core RSA parameters are sane.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100403 */
404
Hanno Becker750e8b42017-08-25 07:54:27 +0100405int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
406 const mbedtls_mpi *Q, const mbedtls_mpi *D,
407 const mbedtls_mpi *E,
408 int (*f_rng)(void *, unsigned char *, size_t),
409 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100410{
411 int ret = 0;
Hanno Becker750e8b42017-08-25 07:54:27 +0100412 mbedtls_mpi K, L;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100413
414 mbedtls_mpi_init( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100415 mbedtls_mpi_init( &L );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100416
417 /*
418 * Step 1: If PRNG provided, check that P and Q are prime
419 */
420
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100421#if defined(MBEDTLS_GENPRIME)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100422 if( f_rng != NULL && P != NULL &&
423 ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
424 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100425 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100426 goto cleanup;
427 }
428
429 if( f_rng != NULL && Q != NULL &&
430 ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
431 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100432 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100433 goto cleanup;
434 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100435#else
436 ((void) f_rng);
437 ((void) p_rng);
438#endif /* MBEDTLS_GENPRIME */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100439
440 /*
441 * Step 2: Check that N = PQ
442 */
443
444 if( P != NULL && Q != NULL && N != NULL )
445 {
446 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
Hanno Becker750e8b42017-08-25 07:54:27 +0100447 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
448 mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100449 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100450 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100451 goto cleanup;
452 }
453 }
454
455 /*
456 * Step 3: Check that D, E are inverse modulo P-1 and Q-1
457 */
458
459 if( P != NULL && Q != NULL && D != NULL && E != NULL )
460 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100461 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
462 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
463 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
464 mbedtls_mpi_cmp_int( E, 1 ) <= 0 )
465 {
466 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
467 goto cleanup;
468 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100469
470 /* Compute DE-1 mod P-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100471 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
472 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
473 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
474 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100475 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
476 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100477 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100478 goto cleanup;
479 }
480
481 /* Compute DE-1 mod Q-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100482 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
483 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
484 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
485 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100486 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
487 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100488 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100489 goto cleanup;
490 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100491 }
492
493cleanup:
494
495 mbedtls_mpi_free( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100496 mbedtls_mpi_free( &L );
497
498 /* Wrap MPI error codes by RSA check failure error code */
499 if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
500 {
501 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
502 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100503
504 return( ret );
505}
506
507int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
508 const mbedtls_mpi *D, mbedtls_mpi *DP,
509 mbedtls_mpi *DQ, mbedtls_mpi *QP )
510{
511 int ret = 0;
512 mbedtls_mpi K;
513 mbedtls_mpi_init( &K );
514
Hanno Beckerd9431a72017-08-25 08:03:13 +0100515 /* DP = D mod P-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100516 if( DP != NULL )
517 {
518 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
519 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
520 }
521
Hanno Beckerd9431a72017-08-25 08:03:13 +0100522 /* DQ = D mod Q-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100523 if( DQ != NULL )
524 {
525 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
526 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
527 }
528
Hanno Beckerd9431a72017-08-25 08:03:13 +0100529 /* QP = Q^{-1} mod P */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100530 if( QP != NULL )
531 {
532 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
533 }
534
535cleanup:
536 mbedtls_mpi_free( &K );
537
538 return( ret );
539}
540
Hanno Becker617c1ae2017-08-23 14:11:24 +0100541
542/*
543 * Default RSA interface implementation
544 */
545
Hanno Beckerab377312017-08-23 16:24:51 +0100546#if !defined(MBEDTLS_RSA_ALT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100547
548int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
549 const mbedtls_mpi *N,
550 const mbedtls_mpi *P, const mbedtls_mpi *Q,
551 const mbedtls_mpi *D, const mbedtls_mpi *E )
552{
553 int ret;
554
555 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
556 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
557 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
558 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
559 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
560 {
561 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
562 }
563
564 if( N != NULL )
565 ctx->len = mbedtls_mpi_size( &ctx->N );
566
567 return( 0 );
568}
569
570int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
571 unsigned char *N, size_t N_len,
572 unsigned char *P, size_t P_len,
573 unsigned char *Q, size_t Q_len,
574 unsigned char *D, size_t D_len,
575 unsigned char *E, size_t E_len )
576{
577 int ret;
578
579 if( N != NULL )
580 {
581 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
582 ctx->len = mbedtls_mpi_size( &ctx->N );
583 }
584
585 if( P != NULL )
586 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
587
588 if( Q != NULL )
589 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
590
591 if( D != NULL )
592 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
593
594 if( E != NULL )
595 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
596
597cleanup:
598
599 if( ret != 0 )
600 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
601
602 return( 0 );
603}
604
605int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
606 int (*f_rng)(void *, unsigned char *, size_t),
607 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100608{
609 int ret = 0;
610
Hanno Becker617c1ae2017-08-23 14:11:24 +0100611 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
612 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
613 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
614 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
615 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100616
Hanno Becker617c1ae2017-08-23 14:11:24 +0100617 /*
618 * Check whether provided parameters are enough
619 * to deduce all others. The following incomplete
620 * parameter sets for private keys are supported:
621 *
622 * (1) P, Q missing.
623 * (2) D and potentially N missing.
624 *
625 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100626
Hanno Becker2cca6f32017-09-29 11:46:40 +0100627 const int n_missing = have_P && have_Q && have_D && have_E;
628 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
629 const int d_missing = have_P && have_Q && !have_D && have_E;
630 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
631
632 /* These three alternatives are mutually exclusive */
633 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100634
Hanno Becker617c1ae2017-08-23 14:11:24 +0100635 if( !is_priv && !is_pub )
636 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
637
638 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100639 * Step 1: Deduce N if P, Q are provided.
640 */
641
642 if( !have_N && have_P && have_Q )
643 {
644 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
645 &ctx->Q ) ) != 0 )
646 {
647 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
648 }
649
650 ctx->len = mbedtls_mpi_size( &ctx->N );
651 }
652
653 /*
654 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100655 */
656
657 if( pq_missing )
658 {
659 /* This includes sanity checking of core parameters,
660 * so no further checks necessary. */
661 ret = mbedtls_rsa_deduce_moduli( &ctx->N, &ctx->D, &ctx->E,
662 f_rng, p_rng,
663 &ctx->P, &ctx->Q );
664 if( ret != 0 )
665 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
666
667 }
668 else if( d_missing )
669 {
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100670#if defined(MBEDTLS_GENPRIME)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100671 /* If a PRNG is provided, check if P, Q are prime. */
672 if( f_rng != NULL &&
673 ( ( ret = mbedtls_mpi_is_prime( &ctx->P, f_rng, p_rng ) ) != 0 ||
674 ( ret = mbedtls_mpi_is_prime( &ctx->Q, f_rng, p_rng ) ) != 0 ) )
675 {
676 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
677 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100678#endif /* MBEDTLS_GENPRIME */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100679
Hanno Becker617c1ae2017-08-23 14:11:24 +0100680 /* Deduce private exponent. This includes double-checking of the result,
681 * so together with the primality test above all core parameters are
682 * guaranteed to be sane if this call succeeds. */
683 if( ( ret = mbedtls_rsa_deduce_private( &ctx->P, &ctx->Q,
684 &ctx->D, &ctx->E ) ) != 0 )
685 {
686 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
687 }
688 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100689
690 /* In the remaining case of a public key, there's nothing to check for. */
691
692 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100693 * Step 3: Deduce all additional parameters specific
Hanno Becker617c1ae2017-08-23 14:11:24 +0100694 * to our current RSA implementaiton.
695 */
696
Hanno Becker23344b52017-08-23 07:43:27 +0100697#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100698 if( is_priv )
699 {
700 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
701 &ctx->DP, &ctx->DQ, &ctx->QP );
702 if( ret != 0 )
703 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
704 }
Hanno Becker23344b52017-08-23 07:43:27 +0100705#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100706
707 /*
708 * Step 3: Double check
709 */
710
711 if( is_priv )
712 {
713 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
714 return( ret );
715 }
716 else
717 {
718 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
719 return( ret );
720 }
721
722 return( 0 );
723}
724
725/*
726 * Check if CRT parameters match RSA context.
727 * This has to be implemented even if CRT is not used,
728 * in order to be able to validate DER encoded RSA keys,
729 * which always contain CRT parameters.
730 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100731int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx,
732 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100733{
Hanno Becker23344b52017-08-23 07:43:27 +0100734 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100735
Hanno Becker23344b52017-08-23 07:43:27 +0100736 /* Check if key is private or public */
737 const int is_priv =
738 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
739 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
740 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
741 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
742 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
743
744 if( !is_priv )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100745 {
746 /* Checking optional parameters only makes sense for private keys. */
747 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
748 }
749
Hanno Becker23344b52017-08-23 07:43:27 +0100750#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100751 if( ( DP != NULL && mbedtls_mpi_cmp_mpi( DP, &ctx->DP ) != 0 ) ||
752 ( DQ != NULL && mbedtls_mpi_cmp_mpi( DQ, &ctx->DQ ) != 0 ) ||
753 ( QP != NULL && mbedtls_mpi_cmp_mpi( QP, &ctx->QP ) != 0 ) )
754 {
755 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
756 }
Hanno Becker23344b52017-08-23 07:43:27 +0100757#else /* MBEDTLS_RSA_NO_CRT */
Hanno Beckerd3637992017-08-25 07:55:03 +0100758 if( ( ret = mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
759 DP, DQ, QP ) ) != 0 )
Hanno Becker23344b52017-08-23 07:43:27 +0100760 {
Hanno Beckerd3637992017-08-25 07:55:03 +0100761 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker23344b52017-08-23 07:43:27 +0100762 }
Hanno Becker23344b52017-08-23 07:43:27 +0100763#endif
764
765 if( ret != 0 )
766 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100767
768 return( 0 );
769}
770
771int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
772 unsigned char *N, size_t N_len,
773 unsigned char *P, size_t P_len,
774 unsigned char *Q, size_t Q_len,
775 unsigned char *D, size_t D_len,
776 unsigned char *E, size_t E_len )
777{
778 int ret = 0;
779
780 /* Check if key is private or public */
781 const int is_priv =
782 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
783 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
784 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
785 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
786 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
787
788 if( !is_priv )
789 {
790 /* If we're trying to export private parameters for a public key,
791 * something must be wrong. */
792 if( P != NULL || Q != NULL || D != NULL )
793 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
794
795 }
796
797 if( N != NULL )
798 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
799
800 if( P != NULL )
801 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
802
803 if( Q != NULL )
804 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
805
806 if( D != NULL )
807 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
808
809 if( E != NULL )
810 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100811
812cleanup:
813
814 return( ret );
815}
816
Hanno Becker617c1ae2017-08-23 14:11:24 +0100817int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
818 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
819 mbedtls_mpi *D, mbedtls_mpi *E )
820{
821 int ret;
822
823 /* Check if key is private or public */
824 int is_priv =
825 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
826 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
827 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
828 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
829 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
830
831 if( !is_priv )
832 {
833 /* If we're trying to export private parameters for a public key,
834 * something must be wrong. */
835 if( P != NULL || Q != NULL || D != NULL )
836 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
837
838 }
839
840 /* Export all requested core parameters. */
841
842 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
843 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
844 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
845 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
846 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
847 {
848 return( ret );
849 }
850
851 return( 0 );
852}
853
854/*
855 * Export CRT parameters
856 * This must also be implemented if CRT is not used, for being able to
857 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
858 * can be used in this case.
859 */
860int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
861 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
862{
863 int ret;
864
865 /* Check if key is private or public */
866 int is_priv =
867 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
868 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
869 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
870 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
871 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
872
873 if( !is_priv )
874 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
875
Hanno Beckerdc95c892017-08-23 06:57:02 +0100876#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100877 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100878 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
879 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
880 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
881 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100882 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100883 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100884#else
885 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
886 DP, DQ, QP ) ) != 0 )
887 {
888 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
889 }
890#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100891
892 return( 0 );
893}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100894
895/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000896 * Initialize an RSA context
897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000899 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000900 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000901{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#if defined(MBEDTLS_THREADING_C)
907 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200908#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000909}
910
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100911/*
912 * Set padding for an existing RSA context
913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100915{
916 ctx->padding = padding;
917 ctx->hash_id = hash_id;
918}
919
Hanno Becker617c1ae2017-08-23 14:11:24 +0100920/*
921 * Get length in bytes of RSA modulus
922 */
923
924size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
925{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100926 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100927}
928
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
932/*
933 * Generate an RSA keypair
934 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000936 int (*f_rng)(void *, unsigned char *, size_t),
937 void *p_rng,
938 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000939{
940 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100941 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000942
Paul Bakker21eb2802010-08-16 11:10:02 +0000943 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Janos Follathef441782016-09-21 13:18:12 +0100946 if( nbits % 2 )
947 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
948
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100949 mbedtls_mpi_init( &H );
950 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
952 /*
953 * find primes P and Q with Q < P so that:
954 * GCD( E, (P-1)*(Q-1) ) == 1
955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000957
958 do
959 {
Janos Follath10c575b2016-02-23 14:42:48 +0000960 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100961 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000962
Janos Follathef441782016-09-21 13:18:12 +0100963 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100964 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000967 continue;
968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200970 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000971 continue;
972
Janos Follathef441782016-09-21 13:18:12 +0100973 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100974 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100975
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100976 /* Temporarily replace P,Q by P-1, Q-1 */
977 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
978 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
979 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000981 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100984 /* Restore P,Q */
985 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
986 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
987
988 ctx->len = mbedtls_mpi_size( &ctx->N );
989
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 /*
991 * D = E^-1 mod ((P-1)*(Q-1))
992 * DP = D mod (P - 1)
993 * DQ = D mod (Q - 1)
994 * QP = Q^-1 mod P
995 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000996
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100997 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
998
999#if !defined(MBEDTLS_RSA_NO_CRT)
1000 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
1001 &ctx->DP, &ctx->DQ, &ctx->QP ) );
1002#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
Hanno Becker83aad1f2017-08-23 06:45:10 +01001004 /* Double-check */
1005 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
1006
Paul Bakker5121ce52009-01-03 21:22:43 +00001007cleanup:
1008
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001009 mbedtls_mpi_free( &H );
1010 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +00001011
1012 if( ret != 0 )
1013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_rsa_free( ctx );
1015 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001016 }
1017
Paul Bakker48377d92013-08-30 12:06:24 +02001018 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001019}
1020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
1023/*
1024 * Check a public RSA key
1025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001027{
Paul Bakker37940d92009-07-10 22:38:58 +00001028 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +00001030
Hanno Beckerba1ba112017-09-29 11:48:23 +01001031 if( ctx->len != mbedtls_mpi_size( &ctx->N ) )
1032 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1033
Paul Bakker48377d92013-08-30 12:06:24 +02001034 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001038 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
1039 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001042 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
1044 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
1046 return( 0 );
1047}
1048
1049/*
1050 * Check a private RSA key
1051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001053{
Hanno Beckerb269a852017-08-25 08:03:21 +01001054 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
1055 mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
1056 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 {
Hanno Beckerb269a852017-08-25 08:03:21 +01001058 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 }
Hanno Beckerb269a852017-08-25 08:03:21 +01001060#if !defined(MBEDTLS_RSA_NO_CRT)
1061 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
1062 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
1063 {
1064 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1065 }
1066#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001067
1068 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001069}
1070
1071/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001072 * Check if contexts holding a public and private key match
1073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1077 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001080 }
1081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1083 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001086 }
1087
1088 return( 0 );
1089}
1090
1091/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 * Do an RSA public key operation
1093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001095 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 unsigned char *output )
1097{
Paul Bakker23986e52011-04-24 08:57:21 +00001098 int ret;
1099 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001104#if defined(MBEDTLS_THREADING_C)
1105 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1106 return( ret );
1107#endif
1108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001113 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1114 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 }
1116
1117 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1119 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
1121cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001123 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1124 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001125#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001128
1129 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001131
1132 return( 0 );
1133}
1134
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001135/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001136 * Generate or update blinding values, see section 10 of:
1137 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001138 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001139 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001140 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001141static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001142 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1143{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001144 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001145
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001146 if( ctx->Vf.p != NULL )
1147 {
1148 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1150 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1151 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1152 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001153
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001154 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001155 }
1156
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001157 /* Unblinding value: Vf = random number, invertible mod N */
1158 do {
1159 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1163 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1164 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001165
1166 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1168 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 +02001169
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001170
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001171cleanup:
1172 return( ret );
1173}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001174
Paul Bakker5121ce52009-01-03 21:22:43 +00001175/*
Janos Follathe81102e2017-03-22 13:38:28 +00001176 * Exponent blinding supposed to prevent side-channel attacks using multiple
1177 * traces of measurements to recover the RSA key. The more collisions are there,
1178 * the more bits of the key can be recovered. See [3].
1179 *
1180 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1181 * observations on avarage.
1182 *
1183 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1184 * to make 2^112 observations on avarage.
1185 *
1186 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1187 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1188 * Thus in this sense with 28 byte blinding the security is not reduced by
1189 * side-channel attacks like the one in [3])
1190 *
1191 * This countermeasure does not help if the key recovery is possible with a
1192 * single trace.
1193 */
1194#define RSA_EXPONENT_BLINDING 28
1195
1196/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 * Do an RSA private key operation
1198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001200 int (*f_rng)(void *, unsigned char *, size_t),
1201 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001202 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 unsigned char *output )
1204{
Paul Bakker23986e52011-04-24 08:57:21 +00001205 int ret;
1206 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001208 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001209#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001210 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001211 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001212#else
1213 mbedtls_mpi DP_blind, DQ_blind;
1214 mbedtls_mpi *DP = &ctx->DP;
1215 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001216#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001217
Hanno Becker45037ce2017-08-25 11:03:34 +01001218 /* Sanity-check that all relevant fields are at least set,
1219 * but don't perform a full keycheck. */
1220 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
1221 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
1222 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
1223 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
1224 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
1225 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001226 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker45037ce2017-08-25 11:03:34 +01001227 }
1228#if !defined(MBEDTLS_RSA_NO_CRT)
1229 if( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
1230 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
1231 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
1232 {
1233 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1234 }
1235#endif /* MBEDTLS_RSA_NO_CRT */
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001238 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1239
Janos Follathf9203b42017-03-22 15:13:15 +00001240 if( f_rng != NULL )
1241 {
Janos Follathe81102e2017-03-22 13:38:28 +00001242#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001243 mbedtls_mpi_init( &D_blind );
1244#else
1245 mbedtls_mpi_init( &DP_blind );
1246 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001247#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001248 }
Janos Follathe81102e2017-03-22 13:38:28 +00001249
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001251#if defined(MBEDTLS_THREADING_C)
1252 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1253 return( ret );
1254#endif
1255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1257 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001259 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1260 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 }
1262
Paul Bakkerf451bac2013-08-30 15:37:02 +02001263 if( f_rng != NULL )
1264 {
1265 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001266 * Blinding
1267 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001268 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001269 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1270 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001272
Janos Follathe81102e2017-03-22 13:38:28 +00001273 /*
1274 * Exponent blinding
1275 */
1276 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1277 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1278
Janos Follathf9203b42017-03-22 15:13:15 +00001279#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001280 /*
1281 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1282 */
1283 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1284 f_rng, p_rng ) );
1285 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1286 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1287 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1288
1289 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001290#else
1291 /*
1292 * DP_blind = ( P - 1 ) * R + DP
1293 */
1294 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1295 f_rng, p_rng ) );
1296 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1297 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1298 &ctx->DP ) );
1299
1300 DP = &DP_blind;
1301
1302 /*
1303 * DQ_blind = ( Q - 1 ) * R + DQ
1304 */
1305 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1306 f_rng, p_rng ) );
1307 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1308 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1309 &ctx->DQ ) );
1310
1311 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001312#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001313 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001316 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001317#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001318 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001319 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 *
1321 * T1 = input ^ dP mod P
1322 * T2 = input ^ dQ mod Q
1323 */
Janos Follathf9203b42017-03-22 15:13:15 +00001324 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1325 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
1327 /*
1328 * T = (T1 - T2) * (Q^-1 mod P) mod P
1329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1331 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1332 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
1334 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001335 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1338 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1339#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001340
Paul Bakkerf451bac2013-08-30 15:37:02 +02001341 if( f_rng != NULL )
1342 {
1343 /*
1344 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001345 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001346 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001347 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
1351 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
1354cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001356 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1357 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001358#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001361 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1362
1363 if( f_rng != NULL )
1364 {
Janos Follathe81102e2017-03-22 13:38:28 +00001365#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001366 mbedtls_mpi_free( &D_blind );
1367#else
1368 mbedtls_mpi_free( &DP_blind );
1369 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001370#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001372
1373 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001375
1376 return( 0 );
1377}
1378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001380/**
1381 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1382 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001383 * \param dst buffer to mask
1384 * \param dlen length of destination buffer
1385 * \param src source of the mask generation
1386 * \param slen length of the source buffer
1387 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001388 */
Paul Bakker48377d92013-08-30 12:06:24 +02001389static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001391{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001393 unsigned char counter[4];
1394 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001395 unsigned int hlen;
1396 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001399 memset( counter, 0, 4 );
1400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001402
Simon Butcher02037452016-03-01 21:19:12 +00001403 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001404 p = dst;
1405
1406 while( dlen > 0 )
1407 {
1408 use_len = hlen;
1409 if( dlen < hlen )
1410 use_len = dlen;
1411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 mbedtls_md_starts( md_ctx );
1413 mbedtls_md_update( md_ctx, src, slen );
1414 mbedtls_md_update( md_ctx, counter, 4 );
1415 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001416
1417 for( i = 0; i < use_len; ++i )
1418 *p++ ^= mask[i];
1419
1420 counter[3]++;
1421
1422 dlen -= use_len;
1423 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001424
1425 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001426}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001430/*
1431 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001434 int (*f_rng)(void *, unsigned char *, size_t),
1435 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001436 int mode,
1437 const unsigned char *label, size_t label_len,
1438 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001439 const unsigned char *input,
1440 unsigned char *output )
1441{
1442 size_t olen;
1443 int ret;
1444 unsigned char *p = output;
1445 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 const mbedtls_md_info_t *md_info;
1447 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1450 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001451
1452 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001456 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001458
1459 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001461
Simon Butcher02037452016-03-01 21:19:12 +00001462 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001463 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001465
1466 memset( output, 0, olen );
1467
1468 *p++ = 0;
1469
Simon Butcher02037452016-03-01 21:19:12 +00001470 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001471 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001473
1474 p += hlen;
1475
Simon Butcher02037452016-03-01 21:19:12 +00001476 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001478 p += hlen;
1479 p += olen - 2 * hlen - 2 - ilen;
1480 *p++ = 1;
1481 memcpy( p, input, ilen );
1482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001484 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1485 {
1486 mbedtls_md_free( &md_ctx );
1487 return( ret );
1488 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001489
Simon Butcher02037452016-03-01 21:19:12 +00001490 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001491 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1492 &md_ctx );
1493
Simon Butcher02037452016-03-01 21:19:12 +00001494 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001495 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1496 &md_ctx );
1497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 return( ( mode == MBEDTLS_RSA_PUBLIC )
1501 ? mbedtls_rsa_public( ctx, output, output )
1502 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001507/*
1508 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001511 int (*f_rng)(void *, unsigned char *, size_t),
1512 void *p_rng,
1513 int mode, size_t ilen,
1514 const unsigned char *input,
1515 unsigned char *output )
1516{
1517 size_t nb_pad, olen;
1518 int ret;
1519 unsigned char *p = output;
1520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1522 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001523
Janos Follath1ed9f992016-03-18 11:45:44 +00001524 // We don't check p_rng because it won't be dereferenced here
1525 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001527
1528 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001529
Simon Butcher02037452016-03-01 21:19:12 +00001530 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001531 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001533
1534 nb_pad = olen - 3 - ilen;
1535
1536 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001540
1541 while( nb_pad-- > 0 )
1542 {
1543 int rng_dl = 100;
1544
1545 do {
1546 ret = f_rng( p_rng, p, 1 );
1547 } while( *p == 0 && --rng_dl && ret == 0 );
1548
Simon Butcher02037452016-03-01 21:19:12 +00001549 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001550 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001552
1553 p++;
1554 }
1555 }
1556 else
1557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001559
1560 while( nb_pad-- > 0 )
1561 *p++ = 0xFF;
1562 }
1563
1564 *p++ = 0;
1565 memcpy( p, input, ilen );
1566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 return( ( mode == MBEDTLS_RSA_PUBLIC )
1568 ? mbedtls_rsa_public( ctx, output, output )
1569 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001572
Paul Bakker5121ce52009-01-03 21:22:43 +00001573/*
1574 * Add the message padding, then do an RSA operation
1575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001577 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001578 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001579 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001580 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001581 unsigned char *output )
1582{
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 switch( ctx->padding )
1584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#if defined(MBEDTLS_PKCS1_V15)
1586 case MBEDTLS_RSA_PKCS_V15:
1587 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001588 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001589#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#if defined(MBEDTLS_PKCS1_V21)
1592 case MBEDTLS_RSA_PKCS_V21:
1593 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001594 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001595#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001596
1597 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001599 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001600}
1601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001603/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001604 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001607 int (*f_rng)(void *, unsigned char *, size_t),
1608 void *p_rng,
1609 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001610 const unsigned char *label, size_t label_len,
1611 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001612 const unsigned char *input,
1613 unsigned char *output,
1614 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001615{
Paul Bakker23986e52011-04-24 08:57:21 +00001616 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001617 size_t ilen, i, pad_len;
1618 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1620 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001621 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 const mbedtls_md_info_t *md_info;
1623 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001624
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001625 /*
1626 * Parameters sanity checks
1627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1629 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
1631 ilen = ctx->len;
1632
Paul Bakker27fdf462011-06-09 13:55:13 +00001633 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001637 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001639
Janos Follathc17cda12016-02-11 11:08:18 +00001640 hlen = mbedtls_md_get_size( md_info );
1641
1642 // checking for integer underflow
1643 if( 2 * hlen + 2 > ilen )
1644 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1645
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001646 /*
1647 * RSA operation
1648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1650 ? mbedtls_rsa_public( ctx, input, buf )
1651 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001652
1653 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001654 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001656 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001657 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001660 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1661 {
1662 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001663 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001664 }
1665
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001666
1667 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001669
1670 /* seed: Apply seedMask to maskedSeed */
1671 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1672 &md_ctx );
1673
1674 /* DB: Apply dbMask to maskedDB */
1675 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1676 &md_ctx );
1677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001679
1680 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001681 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001682 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001683 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001684 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001686 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001687
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001688 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001689
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001690 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001691 for( i = 0; i < hlen; i++ )
1692 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001693
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001694 /* Get zero-padding len, but always read till end of buffer
1695 * (minus one, for the 01 byte) */
1696 pad_len = 0;
1697 pad_done = 0;
1698 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1699 {
1700 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001701 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001702 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001703
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001704 p += pad_len;
1705 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001706
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001707 /*
1708 * The only information "leaked" is whether the padding was correct or not
1709 * (eg, no data is copied if it was not correct). This meets the
1710 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1711 * the different error conditions.
1712 */
1713 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001714 {
1715 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1716 goto cleanup;
1717 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001718
Paul Bakker66d5d072014-06-17 16:39:18 +02001719 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001720 {
1721 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1722 goto cleanup;
1723 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001724
1725 *olen = ilen - (p - buf);
1726 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001727 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001728
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001729cleanup:
1730 mbedtls_zeroize( buf, sizeof( buf ) );
1731 mbedtls_zeroize( lhash, sizeof( lhash ) );
1732
1733 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001734}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001738/*
1739 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001742 int (*f_rng)(void *, unsigned char *, size_t),
1743 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001744 int mode, size_t *olen,
1745 const unsigned char *input,
1746 unsigned char *output,
1747 size_t output_max_len)
1748{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001749 int ret;
1750 size_t ilen, pad_count = 0, i;
1751 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1755 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001756
1757 ilen = ctx->len;
1758
1759 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1763 ? mbedtls_rsa_public( ctx, input, buf )
1764 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001765
1766 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001767 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001768
1769 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001770 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001771
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001772 /*
1773 * Check and get padding len in "constant-time"
1774 */
1775 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001776
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001777 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001782 /* Get padding len, but always read till end of buffer
1783 * (minus one, for the 00 byte) */
1784 for( i = 0; i < ilen - 3; i++ )
1785 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001786 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1787 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001788 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001789
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001790 p += pad_count;
1791 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001792 }
1793 else
1794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001796
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001797 /* Get padding len, but always read till end of buffer
1798 * (minus one, for the 00 byte) */
1799 for( i = 0; i < ilen - 3; i++ )
1800 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001801 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001802 pad_count += ( pad_done == 0 );
1803 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001804
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001805 p += pad_count;
1806 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 }
1808
Janos Follathc69fa502016-02-12 13:30:09 +00001809 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001810
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001811 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001812 {
1813 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1814 goto cleanup;
1815 }
Paul Bakker8804f692013-02-28 18:06:26 +01001816
Paul Bakker66d5d072014-06-17 16:39:18 +02001817 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001818 {
1819 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1820 goto cleanup;
1821 }
Paul Bakker060c5682009-01-12 21:48:39 +00001822
Paul Bakker27fdf462011-06-09 13:55:13 +00001823 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001824 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001825 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001826
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001827cleanup:
1828 mbedtls_zeroize( buf, sizeof( buf ) );
1829
1830 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001831}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
1834/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001835 * Do an RSA operation, then remove the message padding
1836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001838 int (*f_rng)(void *, unsigned char *, size_t),
1839 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001840 int mode, size_t *olen,
1841 const unsigned char *input,
1842 unsigned char *output,
1843 size_t output_max_len)
1844{
1845 switch( ctx->padding )
1846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847#if defined(MBEDTLS_PKCS1_V15)
1848 case MBEDTLS_RSA_PKCS_V15:
1849 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001850 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001851#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853#if defined(MBEDTLS_PKCS1_V21)
1854 case MBEDTLS_RSA_PKCS_V21:
1855 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001856 olen, input, output,
1857 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001858#endif
1859
1860 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001862 }
1863}
1864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001866/*
1867 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001870 int (*f_rng)(void *, unsigned char *, size_t),
1871 void *p_rng,
1872 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001874 unsigned int hashlen,
1875 const unsigned char *hash,
1876 unsigned char *sig )
1877{
1878 size_t olen;
1879 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001881 unsigned int slen, hlen, offset = 0;
1882 int ret;
1883 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 const mbedtls_md_info_t *md_info;
1885 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1888 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001889
1890 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001892
1893 olen = ctx->len;
1894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001896 {
Simon Butcher02037452016-03-01 21:19:12 +00001897 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001899 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001903 }
1904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001906 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001910 slen = hlen;
1911
1912 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001914
1915 memset( sig, 0, olen );
1916
Simon Butcher02037452016-03-01 21:19:12 +00001917 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001918 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001920
Simon Butcher02037452016-03-01 21:19:12 +00001921 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001922 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001923 p += olen - hlen * 2 - 2;
1924 *p++ = 0x01;
1925 memcpy( p, salt, slen );
1926 p += slen;
1927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001929 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1930 {
1931 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001932 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001933 return( ret );
1934 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001935
Simon Butcher02037452016-03-01 21:19:12 +00001936 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 mbedtls_md_starts( &md_ctx );
1938 mbedtls_md_update( &md_ctx, p, 8 );
1939 mbedtls_md_update( &md_ctx, hash, hashlen );
1940 mbedtls_md_update( &md_ctx, salt, slen );
1941 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001942 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001943
Simon Butcher02037452016-03-01 21:19:12 +00001944 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001945 if( msb % 8 == 0 )
1946 offset = 1;
1947
Simon Butcher02037452016-03-01 21:19:12 +00001948 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001949 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001952
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001953 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001954 sig[0] &= 0xFF >> ( olen * 8 - msb );
1955
1956 p += hlen;
1957 *p++ = 0xBC;
1958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 return( ( mode == MBEDTLS_RSA_PUBLIC )
1960 ? mbedtls_rsa_public( ctx, sig, sig )
1961 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001962}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001966/*
1967 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1968 */
1969/*
1970 * Do an RSA operation to sign the message digest
1971 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001973 int (*f_rng)(void *, unsigned char *, size_t),
1974 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001975 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001977 unsigned int hashlen,
1978 const unsigned char *hash,
1979 unsigned char *sig )
1980{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001981 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001982 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001983 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001984 unsigned char *sig_try = NULL, *verif = NULL;
1985 size_t i;
1986 unsigned char diff;
1987 volatile unsigned char diff_no_optimize;
1988 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1991 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001992
1993 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001994 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001999 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
2003 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002004
Paul Bakkerc70b9822013-04-07 22:00:46 +02002005 nb_pad -= 10 + oid_size;
2006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002008 }
2009
Paul Bakkerc70b9822013-04-07 22:00:46 +02002010 nb_pad -= hashlen;
2011
Paul Bakkerb3869132013-02-28 17:21:01 +01002012 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002014
2015 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01002017 memset( p, 0xFF, nb_pad );
2018 p += nb_pad;
2019 *p++ = 0;
2020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002022 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002023 memcpy( p, hash, hashlen );
2024 }
2025 else
2026 {
2027 /*
2028 * DigestInfo ::= SEQUENCE {
2029 * digestAlgorithm DigestAlgorithmIdentifier,
2030 * digest Digest }
2031 *
2032 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2033 *
2034 * Digest ::= OCTET STRING
2035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002037 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002039 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002041 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002042 memcpy( p, oid, oid_size );
2043 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002045 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002047 *p++ = hashlen;
2048 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002049 }
2050
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002051 if( mode == MBEDTLS_RSA_PUBLIC )
2052 return( mbedtls_rsa_public( ctx, sig, sig ) );
2053
2054 /*
2055 * In order to prevent Lenstra's attack, make the signature in a
2056 * temporary buffer and check it before returning it.
2057 */
2058 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002059 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002060 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2061
Simon Butcher1285ab52016-01-01 21:42:47 +00002062 verif = mbedtls_calloc( 1, ctx->len );
2063 if( verif == NULL )
2064 {
2065 mbedtls_free( sig_try );
2066 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2067 }
2068
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002069 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2070 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2071
2072 /* Compare in constant time just in case */
2073 for( diff = 0, i = 0; i < ctx->len; i++ )
2074 diff |= verif[i] ^ sig[i];
2075 diff_no_optimize = diff;
2076
2077 if( diff_no_optimize != 0 )
2078 {
2079 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2080 goto cleanup;
2081 }
2082
2083 memcpy( sig, sig_try, ctx->len );
2084
2085cleanup:
2086 mbedtls_free( sig_try );
2087 mbedtls_free( verif );
2088
2089 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002090}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002092
2093/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 * Do an RSA operation to sign the message digest
2095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002097 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002098 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002101 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002102 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 unsigned char *sig )
2104{
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 switch( ctx->padding )
2106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#if defined(MBEDTLS_PKCS1_V15)
2108 case MBEDTLS_RSA_PKCS_V15:
2109 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002110 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002111#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#if defined(MBEDTLS_PKCS1_V21)
2114 case MBEDTLS_RSA_PKCS_V21:
2115 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002116 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002117#endif
2118
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002122}
2123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002125/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002126 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002129 int (*f_rng)(void *, unsigned char *, size_t),
2130 void *p_rng,
2131 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002133 unsigned int hashlen,
2134 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002136 int expected_salt_len,
2137 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002138{
Paul Bakker23986e52011-04-24 08:57:21 +00002139 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002140 size_t siglen;
2141 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002143 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002144 unsigned int hlen;
2145 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 const mbedtls_md_info_t *md_info;
2147 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002148 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2151 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002152
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 siglen = ctx->len;
2154
Paul Bakker27fdf462011-06-09 13:55:13 +00002155 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2159 ? mbedtls_rsa_public( ctx, sig, buf )
2160 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161
2162 if( ret != 0 )
2163 return( ret );
2164
2165 p = buf;
2166
Paul Bakkerb3869132013-02-28 17:21:01 +01002167 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002171 {
Simon Butcher02037452016-03-01 21:19:12 +00002172 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002174 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002178 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002181 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002185 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002186
Paul Bakkerb3869132013-02-28 17:21:01 +01002187 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002188
Simon Butcher02037452016-03-01 21:19:12 +00002189 /*
2190 * Note: EMSA-PSS verification is over the length of N - 1 bits
2191 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002192 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002193
Simon Butcher02037452016-03-01 21:19:12 +00002194 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002195 if( msb % 8 == 0 )
2196 {
2197 p++;
2198 siglen -= 1;
2199 }
2200 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002204 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2205 {
2206 mbedtls_md_free( &md_ctx );
2207 return( ret );
2208 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002209
Paul Bakkerb3869132013-02-28 17:21:01 +01002210 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002211
Paul Bakkerb3869132013-02-28 17:21:01 +01002212 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002213
Paul Bakker4de44aa2013-12-31 11:43:01 +01002214 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002215 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002216
Paul Bakkerb3869132013-02-28 17:21:01 +01002217 if( p == buf + siglen ||
2218 *p++ != 0x01 )
2219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 mbedtls_md_free( &md_ctx );
2221 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002222 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002223
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002224 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002225 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002228 slen != (size_t) expected_salt_len )
2229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 mbedtls_md_free( &md_ctx );
2231 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002232 }
2233
Simon Butcher02037452016-03-01 21:19:12 +00002234 /*
2235 * Generate H = Hash( M' )
2236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 mbedtls_md_starts( &md_ctx );
2238 mbedtls_md_update( &md_ctx, zeros, 8 );
2239 mbedtls_md_update( &md_ctx, hash, hashlen );
2240 mbedtls_md_update( &md_ctx, p, slen );
2241 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002244
Paul Bakkerb3869132013-02-28 17:21:01 +01002245 if( memcmp( p + slen, result, hlen ) == 0 )
2246 return( 0 );
2247 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002249}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002250
2251/*
2252 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002255 int (*f_rng)(void *, unsigned char *, size_t),
2256 void *p_rng,
2257 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002259 unsigned int hashlen,
2260 const unsigned char *hash,
2261 const unsigned char *sig )
2262{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2264 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002265 : md_alg;
2266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002268 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002270 sig ) );
2271
2272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002276/*
2277 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002280 int (*f_rng)(void *, unsigned char *, size_t),
2281 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002282 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002284 unsigned int hashlen,
2285 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002286 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002287{
2288 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002289 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002290 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 mbedtls_md_type_t msg_md_alg;
2292 const mbedtls_md_info_t *md_info;
2293 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002294 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2297 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002298
2299 siglen = ctx->len;
2300
2301 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2305 ? mbedtls_rsa_public( ctx, sig, buf )
2306 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002307
2308 if( ret != 0 )
2309 return( ret );
2310
2311 p = buf;
2312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2314 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002315
2316 while( *p != 0 )
2317 {
2318 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002320 p++;
2321 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002322 p++; /* skip 00 byte */
2323
2324 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2325 if( p - buf < 11 )
2326 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002327
2328 len = siglen - ( p - buf );
2329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002331 {
2332 if( memcmp( p, hash, hashlen ) == 0 )
2333 return( 0 );
2334 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002336 }
2337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002339 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2341 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002342
2343 end = p + len;
2344
Simon Butcher02037452016-03-01 21:19:12 +00002345 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002346 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2347 * Insist on 2-byte length tags, to protect against variants of
2348 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002349 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002350 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2352 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2353 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002354 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002356
Gilles Peskinee7e76502017-05-04 12:48:39 +02002357 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2359 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2360 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002361 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002363
Gilles Peskinee7e76502017-05-04 12:48:39 +02002364 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2366 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002367 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002368 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002369
2370 oid.p = p;
2371 p += oid.len;
2372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2374 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002375
2376 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002378
2379 /*
2380 * assume the algorithm parameters must be NULL
2381 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002382 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2384 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002385 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002387
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002388 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002389 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2390 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002391 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002393
2394 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002396
2397 p += hashlen;
2398
2399 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002401
2402 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
2406/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002407 * Do an RSA operation and check the message digest
2408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002410 int (*f_rng)(void *, unsigned char *, size_t),
2411 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002412 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002414 unsigned int hashlen,
2415 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002416 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002417{
2418 switch( ctx->padding )
2419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420#if defined(MBEDTLS_PKCS1_V15)
2421 case MBEDTLS_RSA_PKCS_V15:
2422 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002423 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002424#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426#if defined(MBEDTLS_PKCS1_V21)
2427 case MBEDTLS_RSA_PKCS_V21:
2428 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002429 hashlen, hash, sig );
2430#endif
2431
2432 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002434 }
2435}
2436
2437/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002438 * Copy the components of an RSA key
2439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002441{
2442 int ret;
2443
2444 dst->ver = src->ver;
2445 dst->len = src->len;
2446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2448 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2451 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2452 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002453
2454#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2456 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2457 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2459 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002460#endif
2461
2462 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2465 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002466
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002467 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002468 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002469
2470cleanup:
2471 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002473
2474 return( ret );
2475}
2476
2477/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 * Free the components of an RSA key
2479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002481{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002483 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2484 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002486
Hanno Becker33c30a02017-08-23 07:00:22 +01002487#if !defined(MBEDTLS_RSA_NO_CRT)
2488 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2489 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2490 mbedtls_mpi_free( &ctx->DP );
2491#endif /* MBEDTLS_RSA_NO_CRT */
2492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493#if defined(MBEDTLS_THREADING_C)
2494 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002495#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002496}
2497
Hanno Beckerab377312017-08-23 16:24:51 +01002498#endif /* !MBEDTLS_RSA_ALT */
2499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002501
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002502#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
2504/*
2505 * Example RSA-1024 keypair, for test purposes
2506 */
2507#define KEY_LEN 128
2508
2509#define RSA_N "9292758453063D803DD603D5E777D788" \
2510 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2511 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2512 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2513 "93A89813FBF3C4F8066D2D800F7C38A8" \
2514 "1AE31942917403FF4946B0A83D3D3E05" \
2515 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2516 "5E94BB77B07507233A0BC7BAC8F90F79"
2517
2518#define RSA_E "10001"
2519
2520#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2521 "66CA472BC44D253102F8B4A9D3BFA750" \
2522 "91386C0077937FE33FA3252D28855837" \
2523 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2524 "DF79C5CE07EE72C7F123142198164234" \
2525 "CABB724CF78B8173B9F880FC86322407" \
2526 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2527 "071513A1E85B5DFA031F21ECAE91A34D"
2528
2529#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2530 "2C01CAD19EA484A87EA4377637E75500" \
2531 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2532 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2533
2534#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2535 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2536 "910E4168387E3C30AA1E00C339A79508" \
2537 "8452DD96A9A5EA5D9DCA68DA636032AF"
2538
2539#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2540 "3C94D22288ACD763FD8E5600ED4A702D" \
2541 "F84198A5F06C2E72236AE490C93F07F8" \
2542 "3CC559CD27BC2D1CA488811730BB5725"
2543
2544#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2545 "D8AAEA56749EA28623272E4F7D0592AF" \
2546 "7C1F1313CAC9471B5C523BFE592F517B" \
2547 "407A1BD76C164B93DA2D32A383E58357"
2548
2549#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2550 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2551 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2552 "A74206CEC169D74BF5A8C50D6F48EA08"
2553
2554#define PT_LEN 24
2555#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2556 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002559static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002560{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002561#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002562 size_t i;
2563
Paul Bakker545570e2010-07-18 09:00:25 +00002564 if( rng_state != NULL )
2565 rng_state = NULL;
2566
Paul Bakkera3d195c2011-11-27 21:07:34 +00002567 for( i = 0; i < len; ++i )
2568 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002569#else
2570 if( rng_state != NULL )
2571 rng_state = NULL;
2572
2573 arc4random_buf( output, len );
2574#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002575
Paul Bakkera3d195c2011-11-27 21:07:34 +00002576 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002579
Paul Bakker5121ce52009-01-03 21:22:43 +00002580/*
2581 * Checkup routine
2582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002584{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002585 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002587 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 unsigned char rsa_plaintext[PT_LEN];
2590 unsigned char rsa_decrypted[PT_LEN];
2591 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002593 unsigned char sha1sum[20];
2594#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Hanno Becker3a701162017-08-22 13:52:43 +01002596 mbedtls_mpi K;
2597
2598 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
Hanno Becker3a701162017-08-22 13:52:43 +01002601 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2602 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2603 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2604 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2605 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2606 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2607 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2608 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2609 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2610 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2611
2612 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002613
2614 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2618 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 {
2620 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
2623 return( 1 );
2624 }
2625
Hanno Becker3a701162017-08-22 13:52:43 +01002626 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2627 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2628
2629 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2630 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2631
2632 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2633 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2634
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
2638 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 rsa_plaintext, rsa_ciphertext ) != 0 )
2642 {
2643 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
2646 return( 1 );
2647 }
2648
2649 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002653 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002654 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 {
2656 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
2659 return( 1 );
2660 }
2661
2662 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2663 {
2664 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
2667 return( 1 );
2668 }
2669
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002670 if( verbose != 0 )
2671 mbedtls_printf( "passed\n" );
2672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002675 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 sha1sum, rsa_ciphertext ) != 0 )
2681 {
2682 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
2685 return( 1 );
2686 }
2687
2688 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 sha1sum, rsa_ciphertext ) != 0 )
2693 {
2694 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
2697 return( 1 );
2698 }
2699
2700 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002701 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002704 if( verbose != 0 )
2705 mbedtls_printf( "\n" );
2706
Paul Bakker3d8fb632014-04-17 12:42:41 +02002707cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002708 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 mbedtls_rsa_free( &rsa );
2710#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002711 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002713 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714}
2715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718#endif /* MBEDTLS_RSA_C */