blob: d0cc9e0334b525c5b4f824dfd7efd3e2bf18559e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000022 * The following sources were referenced in the design of this implementation
23 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000024 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000025 * [1] A method for obtaining digital signatures and public-key cryptosystems
26 * R Rivest, A Shamir, and L Adleman
27 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
28 *
29 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
30 * Menezes, van Oorschot and Vanstone
31 *
Janos Follathe81102e2017-03-22 13:38:28 +000032 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
33 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
34 * Stefan Mangard
35 * https://arxiv.org/abs/1702.08719v2
36 *
Paul Bakker5121ce52009-01-03 21:22:43 +000037 */
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020041#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/rsa.h"
48#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#else
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020065#define mbedtls_calloc calloc
66#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#endif
68
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010069/* Implementation that should never be optimized out by the compiler */
70static void mbedtls_zeroize( void *v, size_t n ) {
71 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
72}
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010075 * Context-independent RSA helper functions.
76 *
77 * The following three functions
78 * - mbedtls_rsa_deduce_moduli
79 * - mbedtls_rsa_deduce_private
80 * - mbedtls_rsa_check_params
81 * are helper functions operating on the core RSA parameters
82 * (represented as MPI's). They do not use the RSA context structure
83 * and therefore need not be replaced when providing an alternative
84 * RSA implementation.
85 *
86 * Their purpose is to provide common MPI operations in the context
87 * of RSA that can be easily shared across multiple implementations.
88 */
89
90/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010091 *
92 * Given the modulus N=PQ and a pair of public and private
93 * exponents E and D, respectively, factor N.
94 *
95 * Setting F := lcm(P-1,Q-1), the idea is as follows:
96 *
97 * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
98 * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
99 * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
100 * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
101 * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
102 * factors of N.
103 *
104 * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
105 * construction still applies since (-)^K is the identity on the set of
106 * roots of 1 in Z/NZ.
107 *
108 * The public and private key primitives (-)^E and (-)^D are mutually inverse
109 * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
110 * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
111 * Splitting L = 2^t * K with K odd, we have
112 *
113 * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
114 *
115 * so (F / 2) * K is among the numbers
116 *
117 * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
118 *
119 * where ord is the order of 2 in (DE - 1).
120 * We can therefore iterate through these numbers apply the construction
121 * of (a) and (b) above to attempt to factor N.
122 *
123 */
124int mbedtls_rsa_deduce_moduli( mbedtls_mpi *N, mbedtls_mpi *D, mbedtls_mpi *E,
125 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
126 mbedtls_mpi *P, mbedtls_mpi *Q )
127{
128 /* Implementation note:
129 *
130 * Space-efficiency is given preference over time-efficiency here:
131 * several calculations are done in place and temporarily change
132 * the values of D and E.
133 *
134 * Specifically, D is replaced the largest odd divisor of DE - 1
135 * throughout the calculations.
136 */
137
138 int ret = 0;
139
140 uint16_t attempt; /* Number of current attempt */
141 uint16_t iter; /* Number of squares computed in the current attempt */
142
143 uint16_t bitlen_half; /* Half the bitsize of the modulus N */
144 uint16_t order; /* Order of 2 in DE - 1 */
145
146 mbedtls_mpi K; /* Temporary used for two purposes:
147 * - During factorization attempts, stores a andom integer
148 * in the range of [0,..,N]
149 * - During verification, holding intermediate results.
150 */
151
152 if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
153 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
154
155 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
156 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
157 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
158 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
159 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
160 {
161 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
162 }
163
164 /*
165 * Initializations and temporary changes
166 */
167
168 mbedtls_mpi_init( &K );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100169
170 /* Replace D by DE - 1 */
171 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( D, D, E ) );
172 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( D, D, 1 ) );
173
174 if( ( order = mbedtls_mpi_lsb( D ) ) == 0 )
175 {
176 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
177 goto cleanup;
178 }
179
180 /* After this operation, D holds the largest odd divisor
181 * of DE - 1 for the original values of D and E. */
182 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( D, order ) );
183
184 /* This is used to generate a few numbers around N / 2
185 * if no PRNG is provided. */
186 if( f_rng == NULL )
187 bitlen_half = mbedtls_mpi_bitlen( N ) / 2;
188
189 /*
190 * Actual work
191 */
192
193 for( attempt = 0; attempt < 30; ++attempt )
194 {
195 /* Generate some number in [0,N], either randomly
196 * if a PRNG is given, or try numbers around N/2 */
197 if( f_rng != NULL )
198 {
199 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &K,
200 mbedtls_mpi_size( N ),
201 f_rng, p_rng ) );
202 }
203 else
204 {
205 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &K, 1 ) ) ;
206 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &K, bitlen_half ) ) ;
207 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, attempt + 1 ) );
208 }
209
210 /* Check if gcd(K,N) = 1 */
211 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
212 if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
213 continue;
214
215 /* Go through K^X + 1, K^(2X) + 1, K^(4X) + 1, ...
216 * and check whether they have nontrivial GCD with N. */
217 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, D, N,
218 Q /* temporarily use Q for storing Montgomery
219 * multiplication helper values */ ) );
220
221 for( iter = 1; iter < order; ++iter )
222 {
223 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
225
226 if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
227 mbedtls_mpi_cmp_mpi( P, N ) == -1 )
228 {
229 /*
230 * Have found a nontrivial divisor P of N.
Hanno Beckerd56d83a2017-08-25 07:29:35 +0100231 * Set Q := N / P.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100232 */
233
234 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, &K, N, P ) );
235
Hanno Beckerd56d83a2017-08-25 07:29:35 +0100236 /* Restore D */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100237
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100238 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( D, order ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100239 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( D, D, 1 ) );
240 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( D, NULL, D, E ) );
241
242 goto cleanup;
243 }
244
245 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
246 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
247 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
248 }
249 }
250
251 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
252
253cleanup:
254
255 mbedtls_mpi_free( &K );
256 return( ret );
257}
258
259/*
260 * Given P, Q and the public exponent E, deduce D.
261 * This is essentially a modular inversion.
262 */
263
264int mbedtls_rsa_deduce_private( mbedtls_mpi *P, mbedtls_mpi *Q,
265 mbedtls_mpi *D, mbedtls_mpi *E )
266{
267 int ret = 0;
268 mbedtls_mpi K;
269
270 if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
271 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
272
273 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
274 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
275 mbedtls_mpi_cmp_int( E, 0 ) == 0 )
276 {
277 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
278 }
279
280 mbedtls_mpi_init( &K );
281
282 /* Temporarily replace P and Q by P-1 and Q-1, respectively. */
283 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) );
284 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) );
285
286 /* Temporarily compute the gcd(P-1, Q-1) in D. */
287 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, P, Q ) );
288
289 /* Compute LCM(P-1, Q-1) in K */
290 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
291 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
292
293 /* Compute modular inverse of E in LCM(P-1, Q-1) */
294 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
295
296 /* Restore P and Q. */
297 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) );
298 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) );
299
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100300cleanup:
301
302 mbedtls_mpi_free( &K );
303
304 return( ret );
305}
306
307/*
308 * Check that core RSA parameters are sane.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100309 */
310
Hanno Becker750e8b42017-08-25 07:54:27 +0100311int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
312 const mbedtls_mpi *Q, const mbedtls_mpi *D,
313 const mbedtls_mpi *E,
314 int (*f_rng)(void *, unsigned char *, size_t),
315 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100316{
317 int ret = 0;
Hanno Becker750e8b42017-08-25 07:54:27 +0100318 mbedtls_mpi K, L;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100319
320 mbedtls_mpi_init( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100321 mbedtls_mpi_init( &L );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100322
323 /*
324 * Step 1: If PRNG provided, check that P and Q are prime
325 */
326
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100327#if defined(MBEDTLS_GENPRIME)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100328 if( f_rng != NULL && P != NULL &&
329 ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
330 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100331 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100332 goto cleanup;
333 }
334
335 if( f_rng != NULL && Q != NULL &&
336 ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
337 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100338 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100339 goto cleanup;
340 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100341#else
342 ((void) f_rng);
343 ((void) p_rng);
344#endif /* MBEDTLS_GENPRIME */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100345
346 /*
347 * Step 2: Check that N = PQ
348 */
349
350 if( P != NULL && Q != NULL && N != NULL )
351 {
352 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
Hanno Becker750e8b42017-08-25 07:54:27 +0100353 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
354 mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100355 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100356 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100357 goto cleanup;
358 }
359 }
360
361 /*
362 * Step 3: Check that D, E are inverse modulo P-1 and Q-1
363 */
364
365 if( P != NULL && Q != NULL && D != NULL && E != NULL )
366 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100367 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
368 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
369 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
370 mbedtls_mpi_cmp_int( E, 1 ) <= 0 )
371 {
372 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
373 goto cleanup;
374 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100375
376 /* Compute DE-1 mod P-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100377 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
378 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
379 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
380 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100381 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
382 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100383 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100384 goto cleanup;
385 }
386
387 /* Compute DE-1 mod Q-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100388 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
389 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
390 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
391 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100392 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
393 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100394 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100395 goto cleanup;
396 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100397 }
398
399cleanup:
400
401 mbedtls_mpi_free( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100402 mbedtls_mpi_free( &L );
403
404 /* Wrap MPI error codes by RSA check failure error code */
405 if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
406 {
407 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
408 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100409
410 return( ret );
411}
412
413int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
414 const mbedtls_mpi *D, mbedtls_mpi *DP,
415 mbedtls_mpi *DQ, mbedtls_mpi *QP )
416{
417 int ret = 0;
418 mbedtls_mpi K;
419 mbedtls_mpi_init( &K );
420
421 if( DP != NULL )
422 {
423 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
424 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
425 }
426
427 if( DQ != NULL )
428 {
429 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
430 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
431 }
432
433 if( QP != NULL )
434 {
435 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
436 }
437
438cleanup:
439 mbedtls_mpi_free( &K );
440
441 return( ret );
442}
443
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444
445/*
446 * Default RSA interface implementation
447 */
448
Hanno Beckerab377312017-08-23 16:24:51 +0100449#if !defined(MBEDTLS_RSA_ALT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100450
451int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
452 const mbedtls_mpi *N,
453 const mbedtls_mpi *P, const mbedtls_mpi *Q,
454 const mbedtls_mpi *D, const mbedtls_mpi *E )
455{
456 int ret;
457
458 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
459 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
460 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
461 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
462 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
463 {
464 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
465 }
466
467 if( N != NULL )
468 ctx->len = mbedtls_mpi_size( &ctx->N );
469
470 return( 0 );
471}
472
473int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
474 unsigned char *N, size_t N_len,
475 unsigned char *P, size_t P_len,
476 unsigned char *Q, size_t Q_len,
477 unsigned char *D, size_t D_len,
478 unsigned char *E, size_t E_len )
479{
480 int ret;
481
482 if( N != NULL )
483 {
484 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
485 ctx->len = mbedtls_mpi_size( &ctx->N );
486 }
487
488 if( P != NULL )
489 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
490
491 if( Q != NULL )
492 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
493
494 if( D != NULL )
495 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
496
497 if( E != NULL )
498 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
499
500cleanup:
501
502 if( ret != 0 )
503 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
504
505 return( 0 );
506}
507
508int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
509 int (*f_rng)(void *, unsigned char *, size_t),
510 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100511{
512 int ret = 0;
513
Hanno Becker617c1ae2017-08-23 14:11:24 +0100514 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
515 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
516 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
517 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
518 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100519
Hanno Becker617c1ae2017-08-23 14:11:24 +0100520 /*
521 * Check whether provided parameters are enough
522 * to deduce all others. The following incomplete
523 * parameter sets for private keys are supported:
524 *
525 * (1) P, Q missing.
526 * (2) D and potentially N missing.
527 *
528 */
529 const int complete = have_N && have_P && have_Q && have_D && have_E;
530 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
531 const int d_missing = have_P && have_Q && !have_D && have_E;
532 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100533
Hanno Becker617c1ae2017-08-23 14:11:24 +0100534 const int is_priv = complete || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100535
Hanno Becker617c1ae2017-08-23 14:11:24 +0100536 if( !is_priv && !is_pub )
537 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
538
539 /*
540 * Step 1: Deduce and verify all core parameters.
541 */
542
543 if( pq_missing )
544 {
545 /* This includes sanity checking of core parameters,
546 * so no further checks necessary. */
547 ret = mbedtls_rsa_deduce_moduli( &ctx->N, &ctx->D, &ctx->E,
548 f_rng, p_rng,
549 &ctx->P, &ctx->Q );
550 if( ret != 0 )
551 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
552
553 }
554 else if( d_missing )
555 {
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100556#if defined(MBEDTLS_GENPRIME)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100557 /* If a PRNG is provided, check if P, Q are prime. */
558 if( f_rng != NULL &&
559 ( ( ret = mbedtls_mpi_is_prime( &ctx->P, f_rng, p_rng ) ) != 0 ||
560 ( ret = mbedtls_mpi_is_prime( &ctx->Q, f_rng, p_rng ) ) != 0 ) )
561 {
562 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
563 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100564#endif /* MBEDTLS_GENPRIME */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100565
566 /* Compute N if missing. */
567 if( !have_N &&
568 ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) ) != 0 )
569 {
570 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
571 }
572
573 /* Deduce private exponent. This includes double-checking of the result,
574 * so together with the primality test above all core parameters are
575 * guaranteed to be sane if this call succeeds. */
576 if( ( ret = mbedtls_rsa_deduce_private( &ctx->P, &ctx->Q,
577 &ctx->D, &ctx->E ) ) != 0 )
578 {
579 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
580 }
581 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100582
583 /* In the remaining case of a public key, there's nothing to check for. */
584
585 /*
586 * Step 2: Deduce all additional parameters specific
587 * to our current RSA implementaiton.
588 */
589
Hanno Becker23344b52017-08-23 07:43:27 +0100590#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100591 if( is_priv )
592 {
593 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
594 &ctx->DP, &ctx->DQ, &ctx->QP );
595 if( ret != 0 )
596 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
597 }
Hanno Becker23344b52017-08-23 07:43:27 +0100598#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100599
600 /*
601 * Step 3: Double check
602 */
603
604 if( is_priv )
605 {
606 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
607 return( ret );
608 }
609 else
610 {
611 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
612 return( ret );
613 }
614
615 return( 0 );
616}
617
618/*
619 * Check if CRT parameters match RSA context.
620 * This has to be implemented even if CRT is not used,
621 * in order to be able to validate DER encoded RSA keys,
622 * which always contain CRT parameters.
623 */
624int mbedtls_rsa_check_crt( mbedtls_rsa_context *ctx, mbedtls_mpi *DP,
625 mbedtls_mpi *DQ, mbedtls_mpi *QP )
626{
Hanno Becker23344b52017-08-23 07:43:27 +0100627 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100628
Hanno Becker23344b52017-08-23 07:43:27 +0100629 /* Check if key is private or public */
630 const int is_priv =
631 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
632 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
633 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
634 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
635 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
636
637 if( !is_priv )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100638 {
639 /* Checking optional parameters only makes sense for private keys. */
640 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
641 }
642
Hanno Becker23344b52017-08-23 07:43:27 +0100643#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100644 if( ( DP != NULL && mbedtls_mpi_cmp_mpi( DP, &ctx->DP ) != 0 ) ||
645 ( DQ != NULL && mbedtls_mpi_cmp_mpi( DQ, &ctx->DQ ) != 0 ) ||
646 ( QP != NULL && mbedtls_mpi_cmp_mpi( QP, &ctx->QP ) != 0 ) )
647 {
648 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
649 }
Hanno Becker23344b52017-08-23 07:43:27 +0100650#else /* MBEDTLS_RSA_NO_CRT */
651
652 /*
653 * Check that DP, DQ and QP are in accordance with core parameters.
654 * (1) Check that DP - P == 0 mod P - 1
655 * (2) Check that DQ - Q == 0 mod Q - 1
656 * (3) Check that QP * P - 1 == 0 mod P
657
658 * Alternative implementation also not using DP, DQ and QP
659 * should be able to reuse this codepath.
660 */
661
662 /* Check (1) */
663 if( DP != NULL )
664 {
665 /* Temporarily replace P by P-1 and compute DP - D mod P-1 */
666 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
667 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( DP, DP, &ctx->D ) );
668 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, DP, &ctx->P ) );
669 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
670
671 if( mbedtls_mpi_cmp_int( DP, 0 ) != 0 )
672 {
673 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
674 }
675 }
676
677 /* Check (1) */
678 if( DQ != NULL )
679 {
680 /* Temporarily replace Q by Q-1 and compute DQ - D mod Q-1 */
681 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
682 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( DQ, DQ, &ctx->D ) );
683 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, DQ, &ctx->Q ) );
684 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
685
686 if( mbedtls_mpi_cmp_int( DQ, 0 ) != 0 )
687 {
688 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
689 }
690 }
691
692 /* Check (3) */
693 if( QP != NULL )
694 {
695 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( QP, QP, &ctx->Q ) );
696 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( QP, QP, 1 ) );
697 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( QP, QP, &ctx->P ) );
698 if( mbedtls_mpi_cmp_int( QP, 0 ) != 0 )
699 {
700 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
701 }
702 }
703
704cleanup:
705
706#endif
707
708 if( ret != 0 )
709 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100710
711 return( 0 );
712}
713
714int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
715 unsigned char *N, size_t N_len,
716 unsigned char *P, size_t P_len,
717 unsigned char *Q, size_t Q_len,
718 unsigned char *D, size_t D_len,
719 unsigned char *E, size_t E_len )
720{
721 int ret = 0;
722
723 /* Check if key is private or public */
724 const int is_priv =
725 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
726 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
727 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
728 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
729 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
730
731 if( !is_priv )
732 {
733 /* If we're trying to export private parameters for a public key,
734 * something must be wrong. */
735 if( P != NULL || Q != NULL || D != NULL )
736 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
737
738 }
739
740 if( N != NULL )
741 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
742
743 if( P != NULL )
744 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
745
746 if( Q != NULL )
747 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
748
749 if( D != NULL )
750 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
751
752 if( E != NULL )
753 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100754
755cleanup:
756
757 return( ret );
758}
759
Hanno Becker617c1ae2017-08-23 14:11:24 +0100760int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
761 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
762 mbedtls_mpi *D, mbedtls_mpi *E )
763{
764 int ret;
765
766 /* Check if key is private or public */
767 int is_priv =
768 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
769 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
770 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
771 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
772 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
773
774 if( !is_priv )
775 {
776 /* If we're trying to export private parameters for a public key,
777 * something must be wrong. */
778 if( P != NULL || Q != NULL || D != NULL )
779 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
780
781 }
782
783 /* Export all requested core parameters. */
784
785 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
786 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
787 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
788 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
789 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
790 {
791 return( ret );
792 }
793
794 return( 0 );
795}
796
797/*
798 * Export CRT parameters
799 * This must also be implemented if CRT is not used, for being able to
800 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
801 * can be used in this case.
802 */
803int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
804 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
805{
806 int ret;
807
808 /* Check if key is private or public */
809 int is_priv =
810 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
811 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
812 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
813 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
814 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
815
816 if( !is_priv )
817 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
818
Hanno Beckerdc95c892017-08-23 06:57:02 +0100819#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100820 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100821 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
822 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
823 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
824 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100825 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100826 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100827#else
828 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
829 DP, DQ, QP ) ) != 0 )
830 {
831 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
832 }
833#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100834
835 return( 0 );
836}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100837
838/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 * Initialize an RSA context
840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000843 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000844{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#if defined(MBEDTLS_THREADING_C)
850 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200851#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000852}
853
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100854/*
855 * Set padding for an existing RSA context
856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100858{
859 ctx->padding = padding;
860 ctx->hash_id = hash_id;
861}
862
Hanno Becker617c1ae2017-08-23 14:11:24 +0100863/*
864 * Get length in bytes of RSA modulus
865 */
866
867size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
868{
869 return( mbedtls_mpi_size( &ctx->N ) );
870}
871
872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000874
875/*
876 * Generate an RSA keypair
877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000879 int (*f_rng)(void *, unsigned char *, size_t),
880 void *p_rng,
881 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000882{
883 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100884 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000885
Paul Bakker21eb2802010-08-16 11:10:02 +0000886 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000888
Janos Follathef441782016-09-21 13:18:12 +0100889 if( nbits % 2 )
890 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
891
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100892 mbedtls_mpi_init( &H );
893 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000894
895 /*
896 * find primes P and Q with Q < P so that:
897 * GCD( E, (P-1)*(Q-1) ) == 1
898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
901 do
902 {
Janos Follath10c575b2016-02-23 14:42:48 +0000903 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100904 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Janos Follathef441782016-09-21 13:18:12 +0100906 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100907 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 continue;
911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200913 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 continue;
915
Janos Follathef441782016-09-21 13:18:12 +0100916 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100917 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100918
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100919 /* Temporarily replace P,Q by P-1, Q-1 */
920 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
921 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
922 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000924 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100927 /* Restore P,Q */
928 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
929 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
930
931 ctx->len = mbedtls_mpi_size( &ctx->N );
932
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 /*
934 * D = E^-1 mod ((P-1)*(Q-1))
935 * DP = D mod (P - 1)
936 * DQ = D mod (Q - 1)
937 * QP = Q^-1 mod P
938 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000939
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100940 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
941
942#if !defined(MBEDTLS_RSA_NO_CRT)
943 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
944 &ctx->DP, &ctx->DQ, &ctx->QP ) );
945#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000946
Hanno Becker83aad1f2017-08-23 06:45:10 +0100947 /* Double-check */
948 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
949
Paul Bakker5121ce52009-01-03 21:22:43 +0000950cleanup:
951
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100952 mbedtls_mpi_free( &H );
953 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
955 if( ret != 0 )
956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 mbedtls_rsa_free( ctx );
958 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000959 }
960
Paul Bakker48377d92013-08-30 12:06:24 +0200961 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000962}
963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
966/*
967 * Check a public RSA key
968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000970{
Paul Bakker37940d92009-07-10 22:38:58 +0000971 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000973
Paul Bakker48377d92013-08-30 12:06:24 +0200974 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000977
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200978 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
979 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200982 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
984 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000985
986 return( 0 );
987}
988
989/*
990 * Check a private RSA key
991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000993{
994 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 return( ret );
999
Paul Bakker37940d92009-07-10 22:38:58 +00001000 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +00001002
Hanno Becker6345dd32017-08-23 06:59:48 +01001003 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 );
1004 mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I );
1005 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 ); mbedtls_mpi_init( &L1 );
1006 mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
1010 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
1011 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1012 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1013 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
1014 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
1017 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
1018 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +00001019
Hanno Becker6345dd32017-08-23 06:59:48 +01001020#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
1022 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
1023 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Hanno Becker6345dd32017-08-23 06:59:48 +01001024#endif
1025
Paul Bakkerb572adf2010-07-18 08:29:32 +00001026 /*
1027 * Check for a valid PKCS1v2 private key
1028 */
Hanno Becker6345dd32017-08-23 06:59:48 +01001029 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
1030#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
1032 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
1033 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
Hanno Becker6345dd32017-08-23 06:59:48 +01001034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
Hanno Becker6345dd32017-08-23 06:59:48 +01001036 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 }
Paul Bakker48377d92013-08-30 12:06:24 +02001041
Paul Bakker5121ce52009-01-03 21:22:43 +00001042cleanup:
Hanno Becker6345dd32017-08-23 06:59:48 +01001043 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 );
1044 mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I );
1045 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 ); mbedtls_mpi_free( &L1 );
1046 mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +00001050 return( ret );
1051
Paul Bakker6c591fa2011-05-05 11:49:20 +00001052 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001054
1055 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001056}
1057
1058/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001059 * Check if contexts holding a public and private key match
1060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1064 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001067 }
1068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1070 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001073 }
1074
1075 return( 0 );
1076}
1077
1078/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 * Do an RSA public key operation
1080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001082 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001083 unsigned char *output )
1084{
Paul Bakker23986e52011-04-24 08:57:21 +00001085 int ret;
1086 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001091#if defined(MBEDTLS_THREADING_C)
1092 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1093 return( ret );
1094#endif
1095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001099 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001100 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1101 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 }
1103
1104 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1106 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
1108cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001110 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1111 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001112#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
1116 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
1119 return( 0 );
1120}
1121
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001122/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001123 * Generate or update blinding values, see section 10 of:
1124 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001125 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001126 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001127 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001128static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001129 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1130{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001131 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001132
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001133 if( ctx->Vf.p != NULL )
1134 {
1135 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1137 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1138 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1139 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001140
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001141 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001142 }
1143
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001144 /* Unblinding value: Vf = random number, invertible mod N */
1145 do {
1146 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1150 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1151 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001152
1153 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1155 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 +02001156
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001157
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001158cleanup:
1159 return( ret );
1160}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001161
Paul Bakker5121ce52009-01-03 21:22:43 +00001162/*
Janos Follathe81102e2017-03-22 13:38:28 +00001163 * Exponent blinding supposed to prevent side-channel attacks using multiple
1164 * traces of measurements to recover the RSA key. The more collisions are there,
1165 * the more bits of the key can be recovered. See [3].
1166 *
1167 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1168 * observations on avarage.
1169 *
1170 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1171 * to make 2^112 observations on avarage.
1172 *
1173 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1174 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1175 * Thus in this sense with 28 byte blinding the security is not reduced by
1176 * side-channel attacks like the one in [3])
1177 *
1178 * This countermeasure does not help if the key recovery is possible with a
1179 * single trace.
1180 */
1181#define RSA_EXPONENT_BLINDING 28
1182
1183/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 * Do an RSA private key operation
1185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001187 int (*f_rng)(void *, unsigned char *, size_t),
1188 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001189 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 unsigned char *output )
1191{
Paul Bakker23986e52011-04-24 08:57:21 +00001192 int ret;
1193 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001195 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001196#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001197 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001198 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001199#else
1200 mbedtls_mpi DP_blind, DQ_blind;
1201 mbedtls_mpi *DP = &ctx->DP;
1202 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001203#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001204
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001205 /* Make sure we have private key info, prevent possible misuse */
1206 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
1207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001210 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1211
1212
1213 if( f_rng != NULL )
1214 {
Janos Follathe81102e2017-03-22 13:38:28 +00001215#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001216 mbedtls_mpi_init( &D_blind );
1217#else
1218 mbedtls_mpi_init( &DP_blind );
1219 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001220#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001221 }
Janos Follathe81102e2017-03-22 13:38:28 +00001222
Paul Bakker5121ce52009-01-03 21:22:43 +00001223
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001224#if defined(MBEDTLS_THREADING_C)
1225 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1226 return( ret );
1227#endif
1228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1230 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001232 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1233 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 }
1235
Paul Bakkerf451bac2013-08-30 15:37:02 +02001236 if( f_rng != NULL )
1237 {
1238 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001239 * Blinding
1240 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001241 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001242 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1243 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001245
Janos Follathe81102e2017-03-22 13:38:28 +00001246 /*
1247 * Exponent blinding
1248 */
1249 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1250 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1251
Janos Follathf9203b42017-03-22 15:13:15 +00001252#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001253 /*
1254 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1255 */
1256 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1257 f_rng, p_rng ) );
1258 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1259 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1260 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1261
1262 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001263#else
1264 /*
1265 * DP_blind = ( P - 1 ) * R + DP
1266 */
1267 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1268 f_rng, p_rng ) );
1269 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1270 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1271 &ctx->DP ) );
1272
1273 DP = &DP_blind;
1274
1275 /*
1276 * DQ_blind = ( Q - 1 ) * R + DQ
1277 */
1278 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1279 f_rng, p_rng ) );
1280 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1281 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1282 &ctx->DQ ) );
1283
1284 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001285#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001286 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001289 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001290#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001291 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001292 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 *
1294 * T1 = input ^ dP mod P
1295 * T2 = input ^ dQ mod Q
1296 */
Janos Follathf9203b42017-03-22 15:13:15 +00001297 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1298 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
1300 /*
1301 * T = (T1 - T2) * (Q^-1 mod P) mod P
1302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1304 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1305 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
1307 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001308 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1311 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1312#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001313
Paul Bakkerf451bac2013-08-30 15:37:02 +02001314 if( f_rng != NULL )
1315 {
1316 /*
1317 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001318 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001319 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001320 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001323
1324 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
1327cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001329 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1330 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001331#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001334 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1335
1336 if( f_rng != NULL )
1337 {
Janos Follathe81102e2017-03-22 13:38:28 +00001338#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001339 mbedtls_mpi_free( &D_blind );
1340#else
1341 mbedtls_mpi_free( &DP_blind );
1342 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001343#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001344 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
1346 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
1349 return( 0 );
1350}
1351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001353/**
1354 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1355 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001356 * \param dst buffer to mask
1357 * \param dlen length of destination buffer
1358 * \param src source of the mask generation
1359 * \param slen length of the source buffer
1360 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001361 */
Paul Bakker48377d92013-08-30 12:06:24 +02001362static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001364{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001366 unsigned char counter[4];
1367 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001368 unsigned int hlen;
1369 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001372 memset( counter, 0, 4 );
1373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001375
Simon Butcher02037452016-03-01 21:19:12 +00001376 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001377 p = dst;
1378
1379 while( dlen > 0 )
1380 {
1381 use_len = hlen;
1382 if( dlen < hlen )
1383 use_len = dlen;
1384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 mbedtls_md_starts( md_ctx );
1386 mbedtls_md_update( md_ctx, src, slen );
1387 mbedtls_md_update( md_ctx, counter, 4 );
1388 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001389
1390 for( i = 0; i < use_len; ++i )
1391 *p++ ^= mask[i];
1392
1393 counter[3]++;
1394
1395 dlen -= use_len;
1396 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001397
1398 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001403/*
1404 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001407 int (*f_rng)(void *, unsigned char *, size_t),
1408 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001409 int mode,
1410 const unsigned char *label, size_t label_len,
1411 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001412 const unsigned char *input,
1413 unsigned char *output )
1414{
1415 size_t olen;
1416 int ret;
1417 unsigned char *p = output;
1418 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 const mbedtls_md_info_t *md_info;
1420 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1423 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001424
1425 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001429 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001431
1432 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001434
Simon Butcher02037452016-03-01 21:19:12 +00001435 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001436 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001438
1439 memset( output, 0, olen );
1440
1441 *p++ = 0;
1442
Simon Butcher02037452016-03-01 21:19:12 +00001443 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001444 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001446
1447 p += hlen;
1448
Simon Butcher02037452016-03-01 21:19:12 +00001449 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001451 p += hlen;
1452 p += olen - 2 * hlen - 2 - ilen;
1453 *p++ = 1;
1454 memcpy( p, input, ilen );
1455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001457 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1458 {
1459 mbedtls_md_free( &md_ctx );
1460 return( ret );
1461 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001462
Simon Butcher02037452016-03-01 21:19:12 +00001463 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001464 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1465 &md_ctx );
1466
Simon Butcher02037452016-03-01 21:19:12 +00001467 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001468 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1469 &md_ctx );
1470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 return( ( mode == MBEDTLS_RSA_PUBLIC )
1474 ? mbedtls_rsa_public( ctx, output, output )
1475 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001476}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001480/*
1481 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001484 int (*f_rng)(void *, unsigned char *, size_t),
1485 void *p_rng,
1486 int mode, size_t ilen,
1487 const unsigned char *input,
1488 unsigned char *output )
1489{
1490 size_t nb_pad, olen;
1491 int ret;
1492 unsigned char *p = output;
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1495 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001496
Janos Follath1ed9f992016-03-18 11:45:44 +00001497 // We don't check p_rng because it won't be dereferenced here
1498 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001500
1501 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001502
Simon Butcher02037452016-03-01 21:19:12 +00001503 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001504 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001506
1507 nb_pad = olen - 3 - ilen;
1508
1509 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001513
1514 while( nb_pad-- > 0 )
1515 {
1516 int rng_dl = 100;
1517
1518 do {
1519 ret = f_rng( p_rng, p, 1 );
1520 } while( *p == 0 && --rng_dl && ret == 0 );
1521
Simon Butcher02037452016-03-01 21:19:12 +00001522 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001523 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001525
1526 p++;
1527 }
1528 }
1529 else
1530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001532
1533 while( nb_pad-- > 0 )
1534 *p++ = 0xFF;
1535 }
1536
1537 *p++ = 0;
1538 memcpy( p, input, ilen );
1539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 return( ( mode == MBEDTLS_RSA_PUBLIC )
1541 ? mbedtls_rsa_public( ctx, output, output )
1542 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001545
Paul Bakker5121ce52009-01-03 21:22:43 +00001546/*
1547 * Add the message padding, then do an RSA operation
1548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001550 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001551 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001552 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001553 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 unsigned char *output )
1555{
Paul Bakker5121ce52009-01-03 21:22:43 +00001556 switch( ctx->padding )
1557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558#if defined(MBEDTLS_PKCS1_V15)
1559 case MBEDTLS_RSA_PKCS_V15:
1560 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001561 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001562#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#if defined(MBEDTLS_PKCS1_V21)
1565 case MBEDTLS_RSA_PKCS_V21:
1566 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001567 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001568#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001569
1570 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001572 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001573}
1574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001576/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001577 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001580 int (*f_rng)(void *, unsigned char *, size_t),
1581 void *p_rng,
1582 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001583 const unsigned char *label, size_t label_len,
1584 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001585 const unsigned char *input,
1586 unsigned char *output,
1587 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001588{
Paul Bakker23986e52011-04-24 08:57:21 +00001589 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001590 size_t ilen, i, pad_len;
1591 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1593 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001594 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 const mbedtls_md_info_t *md_info;
1596 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001597
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001598 /*
1599 * Parameters sanity checks
1600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1602 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
1604 ilen = ctx->len;
1605
Paul Bakker27fdf462011-06-09 13:55:13 +00001606 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001610 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001612
Janos Follathc17cda12016-02-11 11:08:18 +00001613 hlen = mbedtls_md_get_size( md_info );
1614
1615 // checking for integer underflow
1616 if( 2 * hlen + 2 > ilen )
1617 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1618
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001619 /*
1620 * RSA operation
1621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1623 ? mbedtls_rsa_public( ctx, input, buf )
1624 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001625
1626 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001627 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001629 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001630 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001633 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1634 {
1635 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001636 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001637 }
1638
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001639
1640 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001642
1643 /* seed: Apply seedMask to maskedSeed */
1644 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1645 &md_ctx );
1646
1647 /* DB: Apply dbMask to maskedDB */
1648 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1649 &md_ctx );
1650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001652
1653 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001654 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001655 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001657 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001659 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001660
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001661 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001662
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001663 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001664 for( i = 0; i < hlen; i++ )
1665 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001666
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001667 /* Get zero-padding len, but always read till end of buffer
1668 * (minus one, for the 01 byte) */
1669 pad_len = 0;
1670 pad_done = 0;
1671 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1672 {
1673 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001674 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001675 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001676
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001677 p += pad_len;
1678 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001679
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001680 /*
1681 * The only information "leaked" is whether the padding was correct or not
1682 * (eg, no data is copied if it was not correct). This meets the
1683 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1684 * the different error conditions.
1685 */
1686 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001687 {
1688 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1689 goto cleanup;
1690 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001691
Paul Bakker66d5d072014-06-17 16:39:18 +02001692 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001693 {
1694 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1695 goto cleanup;
1696 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001697
1698 *olen = ilen - (p - buf);
1699 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001700 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001701
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001702cleanup:
1703 mbedtls_zeroize( buf, sizeof( buf ) );
1704 mbedtls_zeroize( lhash, sizeof( lhash ) );
1705
1706 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001711/*
1712 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001715 int (*f_rng)(void *, unsigned char *, size_t),
1716 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001717 int mode, size_t *olen,
1718 const unsigned char *input,
1719 unsigned char *output,
1720 size_t output_max_len)
1721{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001722 int ret;
1723 size_t ilen, pad_count = 0, i;
1724 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1728 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001729
1730 ilen = ctx->len;
1731
1732 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1736 ? mbedtls_rsa_public( ctx, input, buf )
1737 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001738
1739 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001740 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001741
1742 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001743 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001744
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001745 /*
1746 * Check and get padding len in "constant-time"
1747 */
1748 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001749
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001750 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001755 /* Get padding len, but always read till end of buffer
1756 * (minus one, for the 00 byte) */
1757 for( i = 0; i < ilen - 3; i++ )
1758 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001759 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1760 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001761 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001762
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001763 p += pad_count;
1764 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001765 }
1766 else
1767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001769
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001770 /* Get padding len, but always read till end of buffer
1771 * (minus one, for the 00 byte) */
1772 for( i = 0; i < ilen - 3; i++ )
1773 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001774 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001775 pad_count += ( pad_done == 0 );
1776 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001777
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001778 p += pad_count;
1779 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001780 }
1781
Janos Follathc69fa502016-02-12 13:30:09 +00001782 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001783
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001784 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001785 {
1786 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1787 goto cleanup;
1788 }
Paul Bakker8804f692013-02-28 18:06:26 +01001789
Paul Bakker66d5d072014-06-17 16:39:18 +02001790 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001791 {
1792 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1793 goto cleanup;
1794 }
Paul Bakker060c5682009-01-12 21:48:39 +00001795
Paul Bakker27fdf462011-06-09 13:55:13 +00001796 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001797 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001798 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001799
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001800cleanup:
1801 mbedtls_zeroize( buf, sizeof( buf ) );
1802
1803 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001804}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001806
1807/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001808 * Do an RSA operation, then remove the message padding
1809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001811 int (*f_rng)(void *, unsigned char *, size_t),
1812 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001813 int mode, size_t *olen,
1814 const unsigned char *input,
1815 unsigned char *output,
1816 size_t output_max_len)
1817{
1818 switch( ctx->padding )
1819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#if defined(MBEDTLS_PKCS1_V15)
1821 case MBEDTLS_RSA_PKCS_V15:
1822 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001823 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001824#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#if defined(MBEDTLS_PKCS1_V21)
1827 case MBEDTLS_RSA_PKCS_V21:
1828 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001829 olen, input, output,
1830 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001831#endif
1832
1833 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001835 }
1836}
1837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001839/*
1840 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001843 int (*f_rng)(void *, unsigned char *, size_t),
1844 void *p_rng,
1845 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001847 unsigned int hashlen,
1848 const unsigned char *hash,
1849 unsigned char *sig )
1850{
1851 size_t olen;
1852 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001854 unsigned int slen, hlen, offset = 0;
1855 int ret;
1856 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 const mbedtls_md_info_t *md_info;
1858 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1861 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001862
1863 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001865
1866 olen = ctx->len;
1867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001869 {
Simon Butcher02037452016-03-01 21:19:12 +00001870 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001872 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001876 }
1877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001879 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001883 slen = hlen;
1884
1885 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001887
1888 memset( sig, 0, olen );
1889
Simon Butcher02037452016-03-01 21:19:12 +00001890 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001891 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001893
Simon Butcher02037452016-03-01 21:19:12 +00001894 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001895 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001896 p += olen - hlen * 2 - 2;
1897 *p++ = 0x01;
1898 memcpy( p, salt, slen );
1899 p += slen;
1900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001902 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1903 {
1904 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001905 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001906 return( ret );
1907 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001908
Simon Butcher02037452016-03-01 21:19:12 +00001909 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 mbedtls_md_starts( &md_ctx );
1911 mbedtls_md_update( &md_ctx, p, 8 );
1912 mbedtls_md_update( &md_ctx, hash, hashlen );
1913 mbedtls_md_update( &md_ctx, salt, slen );
1914 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001915 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001916
Simon Butcher02037452016-03-01 21:19:12 +00001917 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001918 if( msb % 8 == 0 )
1919 offset = 1;
1920
Simon Butcher02037452016-03-01 21:19:12 +00001921 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001922 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001925
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001926 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001927 sig[0] &= 0xFF >> ( olen * 8 - msb );
1928
1929 p += hlen;
1930 *p++ = 0xBC;
1931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 return( ( mode == MBEDTLS_RSA_PUBLIC )
1933 ? mbedtls_rsa_public( ctx, sig, sig )
1934 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001935}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001939/*
1940 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1941 */
1942/*
1943 * Do an RSA operation to sign the message digest
1944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001946 int (*f_rng)(void *, unsigned char *, size_t),
1947 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001948 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001950 unsigned int hashlen,
1951 const unsigned char *hash,
1952 unsigned char *sig )
1953{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001954 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001955 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001956 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001957 unsigned char *sig_try = NULL, *verif = NULL;
1958 size_t i;
1959 unsigned char diff;
1960 volatile unsigned char diff_no_optimize;
1961 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1964 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001965
1966 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001967 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001972 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1976 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001977
Paul Bakkerc70b9822013-04-07 22:00:46 +02001978 nb_pad -= 10 + oid_size;
1979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001981 }
1982
Paul Bakkerc70b9822013-04-07 22:00:46 +02001983 nb_pad -= hashlen;
1984
Paul Bakkerb3869132013-02-28 17:21:01 +01001985 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001987
1988 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001990 memset( p, 0xFF, nb_pad );
1991 p += nb_pad;
1992 *p++ = 0;
1993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001995 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001996 memcpy( p, hash, hashlen );
1997 }
1998 else
1999 {
2000 /*
2001 * DigestInfo ::= SEQUENCE {
2002 * digestAlgorithm DigestAlgorithmIdentifier,
2003 * digest Digest }
2004 *
2005 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2006 *
2007 * Digest ::= OCTET STRING
2008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002010 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002012 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002014 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002015 memcpy( p, oid, oid_size );
2016 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002018 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002020 *p++ = hashlen;
2021 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002022 }
2023
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002024 if( mode == MBEDTLS_RSA_PUBLIC )
2025 return( mbedtls_rsa_public( ctx, sig, sig ) );
2026
2027 /*
2028 * In order to prevent Lenstra's attack, make the signature in a
2029 * temporary buffer and check it before returning it.
2030 */
2031 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002032 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002033 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2034
Simon Butcher1285ab52016-01-01 21:42:47 +00002035 verif = mbedtls_calloc( 1, ctx->len );
2036 if( verif == NULL )
2037 {
2038 mbedtls_free( sig_try );
2039 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2040 }
2041
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002042 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2043 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2044
2045 /* Compare in constant time just in case */
2046 for( diff = 0, i = 0; i < ctx->len; i++ )
2047 diff |= verif[i] ^ sig[i];
2048 diff_no_optimize = diff;
2049
2050 if( diff_no_optimize != 0 )
2051 {
2052 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2053 goto cleanup;
2054 }
2055
2056 memcpy( sig, sig_try, ctx->len );
2057
2058cleanup:
2059 mbedtls_free( sig_try );
2060 mbedtls_free( verif );
2061
2062 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002065
2066/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 * Do an RSA operation to sign the message digest
2068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002070 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002071 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002074 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002075 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 unsigned char *sig )
2077{
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 switch( ctx->padding )
2079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080#if defined(MBEDTLS_PKCS1_V15)
2081 case MBEDTLS_RSA_PKCS_V15:
2082 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002083 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002084#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#if defined(MBEDTLS_PKCS1_V21)
2087 case MBEDTLS_RSA_PKCS_V21:
2088 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002089 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002090#endif
2091
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002095}
2096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002098/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002099 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002102 int (*f_rng)(void *, unsigned char *, size_t),
2103 void *p_rng,
2104 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002106 unsigned int hashlen,
2107 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002109 int expected_salt_len,
2110 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002111{
Paul Bakker23986e52011-04-24 08:57:21 +00002112 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002113 size_t siglen;
2114 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002116 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002117 unsigned int hlen;
2118 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 const mbedtls_md_info_t *md_info;
2120 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002121 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2124 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002125
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 siglen = ctx->len;
2127
Paul Bakker27fdf462011-06-09 13:55:13 +00002128 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2132 ? mbedtls_rsa_public( ctx, sig, buf )
2133 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002134
2135 if( ret != 0 )
2136 return( ret );
2137
2138 p = buf;
2139
Paul Bakkerb3869132013-02-28 17:21:01 +01002140 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 {
Simon Butcher02037452016-03-01 21:19:12 +00002145 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002147 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002151 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002154 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002158 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002159
Paul Bakkerb3869132013-02-28 17:21:01 +01002160 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002161
Simon Butcher02037452016-03-01 21:19:12 +00002162 /*
2163 * Note: EMSA-PSS verification is over the length of N - 1 bits
2164 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002165 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002166
Simon Butcher02037452016-03-01 21:19:12 +00002167 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002168 if( msb % 8 == 0 )
2169 {
2170 p++;
2171 siglen -= 1;
2172 }
2173 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002177 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2178 {
2179 mbedtls_md_free( &md_ctx );
2180 return( ret );
2181 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002182
Paul Bakkerb3869132013-02-28 17:21:01 +01002183 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002184
Paul Bakkerb3869132013-02-28 17:21:01 +01002185 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002186
Paul Bakker4de44aa2013-12-31 11:43:01 +01002187 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002188 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002189
Paul Bakkerb3869132013-02-28 17:21:01 +01002190 if( p == buf + siglen ||
2191 *p++ != 0x01 )
2192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 mbedtls_md_free( &md_ctx );
2194 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002195 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002196
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002197 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002198 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002201 slen != (size_t) expected_salt_len )
2202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 mbedtls_md_free( &md_ctx );
2204 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002205 }
2206
Simon Butcher02037452016-03-01 21:19:12 +00002207 /*
2208 * Generate H = Hash( M' )
2209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 mbedtls_md_starts( &md_ctx );
2211 mbedtls_md_update( &md_ctx, zeros, 8 );
2212 mbedtls_md_update( &md_ctx, hash, hashlen );
2213 mbedtls_md_update( &md_ctx, p, slen );
2214 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002217
Paul Bakkerb3869132013-02-28 17:21:01 +01002218 if( memcmp( p + slen, result, hlen ) == 0 )
2219 return( 0 );
2220 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002222}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002223
2224/*
2225 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002228 int (*f_rng)(void *, unsigned char *, size_t),
2229 void *p_rng,
2230 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002232 unsigned int hashlen,
2233 const unsigned char *hash,
2234 const unsigned char *sig )
2235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2237 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002238 : md_alg;
2239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002241 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002243 sig ) );
2244
2245}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002249/*
2250 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002253 int (*f_rng)(void *, unsigned char *, size_t),
2254 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002255 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002257 unsigned int hashlen,
2258 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002259 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002260{
2261 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002262 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002263 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 mbedtls_md_type_t msg_md_alg;
2265 const mbedtls_md_info_t *md_info;
2266 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002267 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2270 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002271
2272 siglen = ctx->len;
2273
2274 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2278 ? mbedtls_rsa_public( ctx, sig, buf )
2279 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002280
2281 if( ret != 0 )
2282 return( ret );
2283
2284 p = buf;
2285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2287 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002288
2289 while( *p != 0 )
2290 {
2291 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002293 p++;
2294 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002295 p++; /* skip 00 byte */
2296
2297 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2298 if( p - buf < 11 )
2299 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002300
2301 len = siglen - ( p - buf );
2302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002304 {
2305 if( memcmp( p, hash, hashlen ) == 0 )
2306 return( 0 );
2307 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 }
2310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002312 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2314 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002315
2316 end = p + len;
2317
Simon Butcher02037452016-03-01 21:19:12 +00002318 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002319 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2320 * Insist on 2-byte length tags, to protect against variants of
2321 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002322 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002323 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2325 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2326 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002327 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002329
Gilles Peskinee7e76502017-05-04 12:48:39 +02002330 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2332 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2333 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002334 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002336
Gilles Peskinee7e76502017-05-04 12:48:39 +02002337 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2339 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002340 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002341 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002342
2343 oid.p = p;
2344 p += oid.len;
2345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2347 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002348
2349 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002351
2352 /*
2353 * assume the algorithm parameters must be NULL
2354 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002355 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2357 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002358 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002360
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002361 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002362 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2363 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002364 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002366
2367 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002369
2370 p += hashlen;
2371
2372 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002374
2375 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
2379/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002380 * Do an RSA operation and check the message digest
2381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002383 int (*f_rng)(void *, unsigned char *, size_t),
2384 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002385 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002387 unsigned int hashlen,
2388 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002389 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002390{
2391 switch( ctx->padding )
2392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393#if defined(MBEDTLS_PKCS1_V15)
2394 case MBEDTLS_RSA_PKCS_V15:
2395 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002396 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002397#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399#if defined(MBEDTLS_PKCS1_V21)
2400 case MBEDTLS_RSA_PKCS_V21:
2401 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002402 hashlen, hash, sig );
2403#endif
2404
2405 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002407 }
2408}
2409
2410/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002411 * Copy the components of an RSA key
2412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002414{
2415 int ret;
2416
2417 dst->ver = src->ver;
2418 dst->len = src->len;
2419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2421 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2424 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2425 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002426
2427#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2429 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2430 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2432 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002433#endif
2434
2435 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2438 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002439
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002440 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002441 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002442
2443cleanup:
2444 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002446
2447 return( ret );
2448}
2449
2450/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 * Free the components of an RSA key
2452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002454{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002456 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2457 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002459
Hanno Becker33c30a02017-08-23 07:00:22 +01002460#if !defined(MBEDTLS_RSA_NO_CRT)
2461 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2462 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2463 mbedtls_mpi_free( &ctx->DP );
2464#endif /* MBEDTLS_RSA_NO_CRT */
2465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466#if defined(MBEDTLS_THREADING_C)
2467 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002468#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002469}
2470
Hanno Beckerab377312017-08-23 16:24:51 +01002471#endif /* !MBEDTLS_RSA_ALT */
2472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002475#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
2477/*
2478 * Example RSA-1024 keypair, for test purposes
2479 */
2480#define KEY_LEN 128
2481
2482#define RSA_N "9292758453063D803DD603D5E777D788" \
2483 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2484 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2485 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2486 "93A89813FBF3C4F8066D2D800F7C38A8" \
2487 "1AE31942917403FF4946B0A83D3D3E05" \
2488 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2489 "5E94BB77B07507233A0BC7BAC8F90F79"
2490
2491#define RSA_E "10001"
2492
2493#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2494 "66CA472BC44D253102F8B4A9D3BFA750" \
2495 "91386C0077937FE33FA3252D28855837" \
2496 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2497 "DF79C5CE07EE72C7F123142198164234" \
2498 "CABB724CF78B8173B9F880FC86322407" \
2499 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2500 "071513A1E85B5DFA031F21ECAE91A34D"
2501
2502#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2503 "2C01CAD19EA484A87EA4377637E75500" \
2504 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2505 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2506
2507#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2508 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2509 "910E4168387E3C30AA1E00C339A79508" \
2510 "8452DD96A9A5EA5D9DCA68DA636032AF"
2511
2512#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2513 "3C94D22288ACD763FD8E5600ED4A702D" \
2514 "F84198A5F06C2E72236AE490C93F07F8" \
2515 "3CC559CD27BC2D1CA488811730BB5725"
2516
2517#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2518 "D8AAEA56749EA28623272E4F7D0592AF" \
2519 "7C1F1313CAC9471B5C523BFE592F517B" \
2520 "407A1BD76C164B93DA2D32A383E58357"
2521
2522#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2523 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2524 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2525 "A74206CEC169D74BF5A8C50D6F48EA08"
2526
2527#define PT_LEN 24
2528#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2529 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002532static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002533{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002534#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002535 size_t i;
2536
Paul Bakker545570e2010-07-18 09:00:25 +00002537 if( rng_state != NULL )
2538 rng_state = NULL;
2539
Paul Bakkera3d195c2011-11-27 21:07:34 +00002540 for( i = 0; i < len; ++i )
2541 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002542#else
2543 if( rng_state != NULL )
2544 rng_state = NULL;
2545
2546 arc4random_buf( output, len );
2547#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002548
Paul Bakkera3d195c2011-11-27 21:07:34 +00002549 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002552
Paul Bakker5121ce52009-01-03 21:22:43 +00002553/*
2554 * Checkup routine
2555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002557{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002558 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002560 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 unsigned char rsa_plaintext[PT_LEN];
2563 unsigned char rsa_decrypted[PT_LEN];
2564 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002566 unsigned char sha1sum[20];
2567#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
Hanno Becker3a701162017-08-22 13:52:43 +01002569 mbedtls_mpi K;
2570
2571 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Hanno Becker3a701162017-08-22 13:52:43 +01002574 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2575 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2576 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2577 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2578 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2579 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2580 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2581 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2582 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2583 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2584
2585 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
2587 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2591 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 {
2593 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
2596 return( 1 );
2597 }
2598
Hanno Becker3a701162017-08-22 13:52:43 +01002599 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2600 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2601
2602 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2603 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2604
2605 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2606 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2607
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
2611 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 rsa_plaintext, rsa_ciphertext ) != 0 )
2615 {
2616 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
2619 return( 1 );
2620 }
2621
2622 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002626 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002627 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 {
2629 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
2632 return( 1 );
2633 }
2634
2635 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2636 {
2637 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
2640 return( 1 );
2641 }
2642
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002643 if( verbose != 0 )
2644 mbedtls_printf( "passed\n" );
2645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002648 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 sha1sum, rsa_ciphertext ) != 0 )
2654 {
2655 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
2658 return( 1 );
2659 }
2660
2661 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 sha1sum, rsa_ciphertext ) != 0 )
2666 {
2667 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
2670 return( 1 );
2671 }
2672
2673 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002674 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002677 if( verbose != 0 )
2678 mbedtls_printf( "\n" );
2679
Paul Bakker3d8fb632014-04-17 12:42:41 +02002680cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002681 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 mbedtls_rsa_free( &rsa );
2683#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002684 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002686 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002687}
2688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#endif /* MBEDTLS_RSA_C */