blob: 841f48976734c1b6e5a916d93673ca464e1b4951 [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 */
Hanno Becker74716312017-10-02 10:00:37 +010021
Paul Bakker5121ce52009-01-03 21:22:43 +000022/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000023 * The following sources were referenced in the design of this implementation
24 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000025 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000026 * [1] A method for obtaining digital signatures and public-key cryptosystems
27 * R Rivest, A Shamir, and L Adleman
28 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
29 *
30 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
31 * Menezes, van Oorschot and Vanstone
32 *
Janos Follathe81102e2017-03-22 13:38:28 +000033 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
34 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
35 * Stefan Mangard
36 * https://arxiv.org/abs/1702.08719v2
37 *
Paul Bakker5121ce52009-01-03 21:22:43 +000038 */
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020042#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/rsa.h"
49#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000055#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000058#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010063#else
Rich Evans00ab4702015-02-06 13:43:58 +000064#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020066#define mbedtls_calloc calloc
67#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010068#endif
69
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010070/* Implementation that should never be optimized out by the compiler */
71static void mbedtls_zeroize( void *v, size_t n ) {
72 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
73}
74
Paul Bakker5121ce52009-01-03 21:22:43 +000075/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010076 * Context-independent RSA helper functions.
77 *
Hanno Beckerd9431a72017-08-25 08:03:13 +010078 * There are two classes of helper functions:
79 * (1) Parameter-generating helpers. These are:
80 * - mbedtls_rsa_deduce_moduli
81 * - mbedtls_rsa_deduce_private
82 * - mbedtls_rsa_deduce_crt
83 * Each of these functions takes a set of core RSA parameters
84 * and generates some other, or CRT related parameters.
85 * (2) Parameter-checking helpers. These are:
86 * - mbedtls_rsa_validate_params
87 * - mbedtls_rsa_validate_crt
88 * They take a set of core or CRT related RSA parameters
89 * and check their validity.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010090 *
Hanno Beckerd9431a72017-08-25 08:03:13 +010091 * The helper functions do not use the RSA context structure
92 * and therefore do not need to be replaced when providing
93 * an alternative RSA implementation.
94 *
95 * Their main purpose is to provide common MPI operations in the context
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010096 * of RSA that can be easily shared across multiple implementations.
97 */
98
99/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100100 *
101 * Given the modulus N=PQ and a pair of public and private
102 * exponents E and D, respectively, factor N.
103 *
104 * Setting F := lcm(P-1,Q-1), the idea is as follows:
105 *
106 * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
107 * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
108 * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
109 * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
110 * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
111 * factors of N.
112 *
113 * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
114 * construction still applies since (-)^K is the identity on the set of
115 * roots of 1 in Z/NZ.
116 *
117 * The public and private key primitives (-)^E and (-)^D are mutually inverse
118 * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
119 * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
120 * Splitting L = 2^t * K with K odd, we have
121 *
122 * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
123 *
124 * so (F / 2) * K is among the numbers
125 *
126 * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
127 *
128 * where ord is the order of 2 in (DE - 1).
129 * We can therefore iterate through these numbers apply the construction
130 * of (a) and (b) above to attempt to factor N.
131 *
132 */
Hanno Beckerba5b7552017-10-02 09:55:49 +0100133int mbedtls_rsa_deduce_moduli( mbedtls_mpi const *N,
134 mbedtls_mpi const *D, mbedtls_mpi const *E,
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100135 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
136 mbedtls_mpi *P, mbedtls_mpi *Q )
137{
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100138 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
Hanno Beckerba5b7552017-10-02 09:55:49 +0100146 mbedtls_mpi T; /* Holds largest odd divisor of DE - 1 */
147 mbedtls_mpi K; /* During factorization attempts, stores a random integer
148 * in the range of [0,..,N] */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100149
150 if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
151 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
152
153 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
154 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
155 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
156 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
157 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
158 {
159 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
160 }
161
162 /*
163 * Initializations and temporary changes
164 */
165
166 mbedtls_mpi_init( &K );
Hanno Beckerba5b7552017-10-02 09:55:49 +0100167 mbedtls_mpi_init( &T );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100168
Hanno Beckerba5b7552017-10-02 09:55:49 +0100169 /* T := DE - 1 */
170 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, D, E ) );
171 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &T, &T, 1 ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100172
Hanno Beckerba5b7552017-10-02 09:55:49 +0100173 if( ( order = mbedtls_mpi_lsb( &T ) ) == 0 )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100174 {
175 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
176 goto cleanup;
177 }
178
Hanno Beckerba5b7552017-10-02 09:55:49 +0100179 /* After this operation, T holds the largest odd divisor of DE - 1. */
180 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &T, order ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100181
182 /* This is used to generate a few numbers around N / 2
183 * if no PRNG is provided. */
184 if( f_rng == NULL )
185 bitlen_half = mbedtls_mpi_bitlen( N ) / 2;
186
187 /*
188 * Actual work
189 */
190
191 for( attempt = 0; attempt < 30; ++attempt )
192 {
193 /* Generate some number in [0,N], either randomly
194 * if a PRNG is given, or try numbers around N/2 */
195 if( f_rng != NULL )
196 {
197 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &K,
198 mbedtls_mpi_size( N ),
199 f_rng, p_rng ) );
200 }
201 else
202 {
203 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &K, 1 ) ) ;
204 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &K, bitlen_half ) ) ;
205 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, attempt + 1 ) );
206 }
207
208 /* Check if gcd(K,N) = 1 */
209 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
210 if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
211 continue;
212
Hanno Beckerba5b7552017-10-02 09:55:49 +0100213 /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100214 * and check whether they have nontrivial GCD with N. */
Hanno Beckerba5b7552017-10-02 09:55:49 +0100215 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, &T, N,
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100216 Q /* temporarily use Q for storing Montgomery
217 * multiplication helper values */ ) );
218
219 for( iter = 1; iter < order; ++iter )
220 {
221 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
222 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
223
224 if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
225 mbedtls_mpi_cmp_mpi( P, N ) == -1 )
226 {
227 /*
228 * Have found a nontrivial divisor P of N.
Hanno Beckerd56d83a2017-08-25 07:29:35 +0100229 * Set Q := N / P.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100230 */
231
Hanno Beckerba5b7552017-10-02 09:55:49 +0100232 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, NULL, N, P ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100233 goto cleanup;
234 }
235
236 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
237 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
238 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
239 }
240 }
241
242 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
243
244cleanup:
245
246 mbedtls_mpi_free( &K );
Hanno Beckerba5b7552017-10-02 09:55:49 +0100247 mbedtls_mpi_free( &T );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100248 return( ret );
249}
250
251/*
252 * Given P, Q and the public exponent E, deduce D.
253 * This is essentially a modular inversion.
254 */
255
Hanno Beckerbdefff12017-10-02 09:57:50 +0100256int mbedtls_rsa_deduce_private( mbedtls_mpi const *P,
257 mbedtls_mpi const *Q,
258 mbedtls_mpi const *E,
259 mbedtls_mpi *D )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100260{
261 int ret = 0;
Hanno Beckerbdefff12017-10-02 09:57:50 +0100262 mbedtls_mpi K, L;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100263
264 if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
265 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
266
267 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
268 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
269 mbedtls_mpi_cmp_int( E, 0 ) == 0 )
270 {
271 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
272 }
273
274 mbedtls_mpi_init( &K );
Hanno Beckerbdefff12017-10-02 09:57:50 +0100275 mbedtls_mpi_init( &L );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100276
Hanno Beckerbdefff12017-10-02 09:57:50 +0100277 /* Temporarily put K := P-1 and L := Q-1 */
278 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
279 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100280
Hanno Beckerbdefff12017-10-02 09:57:50 +0100281 /* Temporarily put D := gcd(P-1, Q-1) */
282 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100283
Hanno Beckerbdefff12017-10-02 09:57:50 +0100284 /* K := LCM(P-1, Q-1) */
285 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100286 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
287
288 /* Compute modular inverse of E in LCM(P-1, Q-1) */
289 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
290
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100291cleanup:
292
293 mbedtls_mpi_free( &K );
Hanno Beckerbdefff12017-10-02 09:57:50 +0100294 mbedtls_mpi_free( &L );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100295
296 return( ret );
297}
298
299/*
Hanno Beckerd3637992017-08-25 07:55:03 +0100300 * Check that RSA CRT parameters are in accordance with core parameters.
301 */
302
303int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
304 const mbedtls_mpi *D, const mbedtls_mpi *DP,
305 const mbedtls_mpi *DQ, const mbedtls_mpi *QP )
306{
307 int ret = 0;
308
309 mbedtls_mpi K, L;
310 mbedtls_mpi_init( &K );
311 mbedtls_mpi_init( &L );
312
Hanno Becker98838b02017-10-02 13:16:10 +0100313 /* Check that DP - D == 0 mod P - 1 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100314 if( DP != NULL )
315 {
316 if( P == NULL )
317 {
318 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
319 goto cleanup;
320 }
321
322 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
323 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DP, D ) );
324 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
325
326 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
327 {
328 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
329 }
330 }
331
Hanno Becker98838b02017-10-02 13:16:10 +0100332 /* Check that DQ - D == 0 mod Q - 1 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100333 if( DQ != NULL )
334 {
335 if( Q == NULL )
336 {
337 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
338 goto cleanup;
339 }
340
341 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
342 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
343 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
344
345 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
346 {
347 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
348 }
349 }
350
Hanno Becker98838b02017-10-02 13:16:10 +0100351 /* Check that QP * Q - 1 == 0 mod P */
Hanno Beckerd3637992017-08-25 07:55:03 +0100352 if( QP != NULL )
353 {
354 if( P == NULL || Q == NULL )
355 {
356 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
357 goto cleanup;
358 }
359
360 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
361 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
362 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
363 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
364 {
365 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
366 }
367 }
368
369cleanup:
370
371 /* Wrap MPI error codes by RSA check failure error code */
372 if( ret != 0 &&
373 ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
374 ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
375 {
376 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
377 }
378
379 mbedtls_mpi_free( &K );
380 mbedtls_mpi_free( &L );
381
382 return( ret );
383}
384
385/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100386 * Check that core RSA parameters are sane.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100387 */
388
Hanno Becker750e8b42017-08-25 07:54:27 +0100389int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
390 const mbedtls_mpi *Q, const mbedtls_mpi *D,
391 const mbedtls_mpi *E,
392 int (*f_rng)(void *, unsigned char *, size_t),
393 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100394{
395 int ret = 0;
Hanno Becker750e8b42017-08-25 07:54:27 +0100396 mbedtls_mpi K, L;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100397
398 mbedtls_mpi_init( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100399 mbedtls_mpi_init( &L );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100400
401 /*
402 * Step 1: If PRNG provided, check that P and Q are prime
403 */
404
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100405#if defined(MBEDTLS_GENPRIME)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100406 if( f_rng != NULL && P != NULL &&
407 ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
408 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100409 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100410 goto cleanup;
411 }
412
413 if( f_rng != NULL && Q != NULL &&
414 ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
415 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100416 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100417 goto cleanup;
418 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100419#else
420 ((void) f_rng);
421 ((void) p_rng);
422#endif /* MBEDTLS_GENPRIME */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100423
424 /*
425 * Step 2: Check that N = PQ
426 */
427
428 if( P != NULL && Q != NULL && N != NULL )
429 {
430 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
Hanno Becker750e8b42017-08-25 07:54:27 +0100431 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
432 mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100433 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100434 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100435 goto cleanup;
436 }
437 }
438
439 /*
440 * Step 3: Check that D, E are inverse modulo P-1 and Q-1
441 */
442
443 if( P != NULL && Q != NULL && D != NULL && E != NULL )
444 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100445 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
446 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
447 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
448 mbedtls_mpi_cmp_int( E, 1 ) <= 0 )
449 {
450 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
451 goto cleanup;
452 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100453
454 /* Compute DE-1 mod P-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100455 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
456 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
457 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
458 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100459 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
460 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100461 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100462 goto cleanup;
463 }
464
465 /* Compute DE-1 mod Q-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100466 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
467 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
468 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
469 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100470 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
471 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100472 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100473 goto cleanup;
474 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100475 }
476
477cleanup:
478
479 mbedtls_mpi_free( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100480 mbedtls_mpi_free( &L );
481
482 /* Wrap MPI error codes by RSA check failure error code */
483 if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
484 {
485 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
486 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100487
488 return( ret );
489}
490
491int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
492 const mbedtls_mpi *D, mbedtls_mpi *DP,
493 mbedtls_mpi *DQ, mbedtls_mpi *QP )
494{
495 int ret = 0;
496 mbedtls_mpi K;
497 mbedtls_mpi_init( &K );
498
Hanno Beckerd9431a72017-08-25 08:03:13 +0100499 /* DP = D mod P-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100500 if( DP != NULL )
501 {
502 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
503 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
504 }
505
Hanno Beckerd9431a72017-08-25 08:03:13 +0100506 /* DQ = D mod Q-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100507 if( DQ != NULL )
508 {
509 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
510 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
511 }
512
Hanno Beckerd9431a72017-08-25 08:03:13 +0100513 /* QP = Q^{-1} mod P */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100514 if( QP != NULL )
515 {
516 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
517 }
518
519cleanup:
520 mbedtls_mpi_free( &K );
521
522 return( ret );
523}
524
Hanno Becker617c1ae2017-08-23 14:11:24 +0100525
526/*
527 * Default RSA interface implementation
528 */
529
Hanno Beckerab377312017-08-23 16:24:51 +0100530#if !defined(MBEDTLS_RSA_ALT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100531
532int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
533 const mbedtls_mpi *N,
534 const mbedtls_mpi *P, const mbedtls_mpi *Q,
535 const mbedtls_mpi *D, const mbedtls_mpi *E )
536{
537 int ret;
538
539 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
540 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
541 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
542 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
543 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
544 {
545 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
546 }
547
548 if( N != NULL )
549 ctx->len = mbedtls_mpi_size( &ctx->N );
550
551 return( 0 );
552}
553
554int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100555 unsigned char const *N, size_t N_len,
556 unsigned char const *P, size_t P_len,
557 unsigned char const *Q, size_t Q_len,
558 unsigned char const *D, size_t D_len,
559 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100560{
561 int ret;
562
563 if( N != NULL )
564 {
565 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
566 ctx->len = mbedtls_mpi_size( &ctx->N );
567 }
568
569 if( P != NULL )
570 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
571
572 if( Q != NULL )
573 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
574
575 if( D != NULL )
576 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
577
578 if( E != NULL )
579 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
580
581cleanup:
582
583 if( ret != 0 )
584 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
585
586 return( 0 );
587}
588
589int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
590 int (*f_rng)(void *, unsigned char *, size_t),
591 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100592{
593 int ret = 0;
594
Hanno Becker617c1ae2017-08-23 14:11:24 +0100595 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
596 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
597 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
598 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
599 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100600
Hanno Becker617c1ae2017-08-23 14:11:24 +0100601 /*
602 * Check whether provided parameters are enough
603 * to deduce all others. The following incomplete
604 * parameter sets for private keys are supported:
605 *
606 * (1) P, Q missing.
607 * (2) D and potentially N missing.
608 *
609 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100610
Hanno Becker2cca6f32017-09-29 11:46:40 +0100611 const int n_missing = have_P && have_Q && have_D && have_E;
612 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
613 const int d_missing = have_P && have_Q && !have_D && have_E;
614 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
615
616 /* These three alternatives are mutually exclusive */
617 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100618
Hanno Becker617c1ae2017-08-23 14:11:24 +0100619 if( !is_priv && !is_pub )
620 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
621
622 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100623 * Step 1: Deduce N if P, Q are provided.
624 */
625
626 if( !have_N && have_P && have_Q )
627 {
628 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
629 &ctx->Q ) ) != 0 )
630 {
631 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
632 }
633
634 ctx->len = mbedtls_mpi_size( &ctx->N );
635 }
636
637 /*
638 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100639 */
640
641 if( pq_missing )
642 {
643 /* This includes sanity checking of core parameters,
644 * so no further checks necessary. */
645 ret = mbedtls_rsa_deduce_moduli( &ctx->N, &ctx->D, &ctx->E,
646 f_rng, p_rng,
647 &ctx->P, &ctx->Q );
648 if( ret != 0 )
649 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
650
651 }
652 else if( d_missing )
653 {
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100654#if defined(MBEDTLS_GENPRIME)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100655 /* If a PRNG is provided, check if P, Q are prime. */
656 if( f_rng != NULL &&
657 ( ( ret = mbedtls_mpi_is_prime( &ctx->P, f_rng, p_rng ) ) != 0 ||
658 ( ret = mbedtls_mpi_is_prime( &ctx->Q, f_rng, p_rng ) ) != 0 ) )
659 {
660 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
661 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100662#endif /* MBEDTLS_GENPRIME */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100663
Hanno Becker617c1ae2017-08-23 14:11:24 +0100664 /* Deduce private exponent. This includes double-checking of the result,
665 * so together with the primality test above all core parameters are
666 * guaranteed to be sane if this call succeeds. */
667 if( ( ret = mbedtls_rsa_deduce_private( &ctx->P, &ctx->Q,
Hanno Beckerbdefff12017-10-02 09:57:50 +0100668 &ctx->E, &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100669 {
670 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
671 }
672 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100673
674 /* In the remaining case of a public key, there's nothing to check for. */
675
676 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100677 * Step 3: Deduce all additional parameters specific
Hanno Becker617c1ae2017-08-23 14:11:24 +0100678 * to our current RSA implementaiton.
679 */
680
Hanno Becker23344b52017-08-23 07:43:27 +0100681#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100682 if( is_priv )
683 {
684 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
685 &ctx->DP, &ctx->DQ, &ctx->QP );
686 if( ret != 0 )
687 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
688 }
Hanno Becker23344b52017-08-23 07:43:27 +0100689#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100690
691 /*
Hanno Becker98838b02017-10-02 13:16:10 +0100692 * Step 3: Basic sanity check
Hanno Becker617c1ae2017-08-23 14:11:24 +0100693 */
694
695 if( is_priv )
696 {
697 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
698 return( ret );
699 }
700 else
701 {
702 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
703 return( ret );
704 }
705
706 return( 0 );
707}
708
Hanno Becker617c1ae2017-08-23 14:11:24 +0100709int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
710 unsigned char *N, size_t N_len,
711 unsigned char *P, size_t P_len,
712 unsigned char *Q, size_t Q_len,
713 unsigned char *D, size_t D_len,
714 unsigned char *E, size_t E_len )
715{
716 int ret = 0;
717
718 /* Check if key is private or public */
719 const int is_priv =
720 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
721 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
722 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
723 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
724 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
725
726 if( !is_priv )
727 {
728 /* If we're trying to export private parameters for a public key,
729 * something must be wrong. */
730 if( P != NULL || Q != NULL || D != NULL )
731 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
732
733 }
734
735 if( N != NULL )
736 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
737
738 if( P != NULL )
739 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
740
741 if( Q != NULL )
742 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
743
744 if( D != NULL )
745 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
746
747 if( E != NULL )
748 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100749
750cleanup:
751
752 return( ret );
753}
754
Hanno Becker617c1ae2017-08-23 14:11:24 +0100755int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
756 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
757 mbedtls_mpi *D, mbedtls_mpi *E )
758{
759 int ret;
760
761 /* Check if key is private or public */
762 int is_priv =
763 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
764 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
765 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
766 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
767 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
768
769 if( !is_priv )
770 {
771 /* If we're trying to export private parameters for a public key,
772 * something must be wrong. */
773 if( P != NULL || Q != NULL || D != NULL )
774 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
775
776 }
777
778 /* Export all requested core parameters. */
779
780 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
781 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
782 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
783 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
784 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
785 {
786 return( ret );
787 }
788
789 return( 0 );
790}
791
792/*
793 * Export CRT parameters
794 * This must also be implemented if CRT is not used, for being able to
795 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
796 * can be used in this case.
797 */
798int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
799 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
800{
801 int ret;
802
803 /* Check if key is private or public */
804 int is_priv =
805 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
806 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
807 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
808 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
809 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
810
811 if( !is_priv )
812 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
813
Hanno Beckerdc95c892017-08-23 06:57:02 +0100814#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100815 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100816 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
817 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
818 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
819 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100820 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100821 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100822#else
823 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
824 DP, DQ, QP ) ) != 0 )
825 {
826 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
827 }
828#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100829
830 return( 0 );
831}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100832
833/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000834 * Initialize an RSA context
835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000838 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000839{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_THREADING_C)
845 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200846#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000847}
848
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100849/*
850 * Set padding for an existing RSA context
851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100853{
854 ctx->padding = padding;
855 ctx->hash_id = hash_id;
856}
857
Hanno Becker617c1ae2017-08-23 14:11:24 +0100858/*
859 * Get length in bytes of RSA modulus
860 */
861
862size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
863{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100864 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100865}
866
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000869
870/*
871 * Generate an RSA keypair
872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000874 int (*f_rng)(void *, unsigned char *, size_t),
875 void *p_rng,
876 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000877{
878 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100879 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000880
Paul Bakker21eb2802010-08-16 11:10:02 +0000881 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
Janos Follathef441782016-09-21 13:18:12 +0100884 if( nbits % 2 )
885 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
886
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100887 mbedtls_mpi_init( &H );
888 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000889
890 /*
891 * find primes P and Q with Q < P so that:
892 * GCD( E, (P-1)*(Q-1) ) == 1
893 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000895
896 do
897 {
Janos Follath10c575b2016-02-23 14:42:48 +0000898 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100899 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
Janos Follathef441782016-09-21 13:18:12 +0100901 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100902 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 continue;
906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200908 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 continue;
910
Janos Follathef441782016-09-21 13:18:12 +0100911 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100912 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100913
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100914 /* Temporarily replace P,Q by P-1, Q-1 */
915 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
916 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
917 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000921
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100922 /* Restore P,Q */
923 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
924 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
925
926 ctx->len = mbedtls_mpi_size( &ctx->N );
927
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 /*
929 * D = E^-1 mod ((P-1)*(Q-1))
930 * DP = D mod (P - 1)
931 * DQ = D mod (Q - 1)
932 * QP = Q^-1 mod P
933 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100935 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
936
937#if !defined(MBEDTLS_RSA_NO_CRT)
938 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
939 &ctx->DP, &ctx->DQ, &ctx->QP ) );
940#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Hanno Becker83aad1f2017-08-23 06:45:10 +0100942 /* Double-check */
943 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
944
Paul Bakker5121ce52009-01-03 21:22:43 +0000945cleanup:
946
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100947 mbedtls_mpi_free( &H );
948 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000949
950 if( ret != 0 )
951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 mbedtls_rsa_free( ctx );
953 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 }
955
Paul Bakker48377d92013-08-30 12:06:24 +0200956 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000957}
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
961/*
962 * Check a public RSA key
963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000965{
Hanno Becker98838b02017-10-02 13:16:10 +0100966 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
967 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100970 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000971
Hanno Beckerba1ba112017-09-29 11:48:23 +0100972 if( ctx->len != mbedtls_mpi_size( &ctx->N ) )
973 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
974
Hanno Becker98838b02017-10-02 13:16:10 +0100975 if( mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 ||
976 mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 )
977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100979 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200981 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
982 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Hanno Becker98838b02017-10-02 13:16:10 +0100983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100985 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200987 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100991 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
993 return( 0 );
994}
995
996/*
997 * Check a private RSA key
998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001000{
Hanno Becker98838b02017-10-02 13:16:10 +01001001 if( mbedtls_rsa_check_pubkey( ctx ) != 0 )
1002 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1003
1004 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +01001005 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 {
Hanno Beckerb269a852017-08-25 08:03:21 +01001007 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001008 }
Hanno Beckerb269a852017-08-25 08:03:21 +01001009#if !defined(MBEDTLS_RSA_NO_CRT)
1010 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
1011 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
1012 {
1013 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1014 }
1015#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001016
1017 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018}
1019
1020/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001021 * Check if contexts holding a public and private key match
1022 */
Hanno Becker98838b02017-10-02 13:16:10 +01001023int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
1024 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001025{
Hanno Becker98838b02017-10-02 13:16:10 +01001026 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001030 }
1031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1033 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001036 }
1037
1038 return( 0 );
1039}
1040
1041/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 * Do an RSA public key operation
1043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001045 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 unsigned char *output )
1047{
Paul Bakker23986e52011-04-24 08:57:21 +00001048 int ret;
1049 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001054#if defined(MBEDTLS_THREADING_C)
1055 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1056 return( ret );
1057#endif
1058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001063 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1064 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 }
1066
1067 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1069 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
1071cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001073 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1074 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001075#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
1079 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
1082 return( 0 );
1083}
1084
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001085/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001086 * Generate or update blinding values, see section 10 of:
1087 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001088 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001089 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001090 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001091static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001092 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1093{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001094 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001095
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001096 if( ctx->Vf.p != NULL )
1097 {
1098 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1100 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1101 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1102 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001103
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001104 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001105 }
1106
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001107 /* Unblinding value: Vf = random number, invertible mod N */
1108 do {
1109 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1113 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1114 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001115
1116 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1118 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 +02001119
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001120
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001121cleanup:
1122 return( ret );
1123}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001124
Paul Bakker5121ce52009-01-03 21:22:43 +00001125/*
Janos Follathe81102e2017-03-22 13:38:28 +00001126 * Exponent blinding supposed to prevent side-channel attacks using multiple
1127 * traces of measurements to recover the RSA key. The more collisions are there,
1128 * the more bits of the key can be recovered. See [3].
1129 *
1130 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1131 * observations on avarage.
1132 *
1133 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1134 * to make 2^112 observations on avarage.
1135 *
1136 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1137 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1138 * Thus in this sense with 28 byte blinding the security is not reduced by
1139 * side-channel attacks like the one in [3])
1140 *
1141 * This countermeasure does not help if the key recovery is possible with a
1142 * single trace.
1143 */
1144#define RSA_EXPONENT_BLINDING 28
1145
1146/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 * Do an RSA private key operation
1148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001150 int (*f_rng)(void *, unsigned char *, size_t),
1151 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001152 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 unsigned char *output )
1154{
Paul Bakker23986e52011-04-24 08:57:21 +00001155 int ret;
1156 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001158 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001159#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001160 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001161 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001162#else
1163 mbedtls_mpi DP_blind, DQ_blind;
1164 mbedtls_mpi *DP = &ctx->DP;
1165 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001166#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001167
Hanno Becker45037ce2017-08-25 11:03:34 +01001168 /* Sanity-check that all relevant fields are at least set,
1169 * but don't perform a full keycheck. */
1170 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
1171 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
1172 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
1173 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
1174 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
1175 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker45037ce2017-08-25 11:03:34 +01001177 }
1178#if !defined(MBEDTLS_RSA_NO_CRT)
1179 if( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
1180 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
1181 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
1182 {
1183 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1184 }
1185#endif /* MBEDTLS_RSA_NO_CRT */
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001188 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1189
Janos Follathf9203b42017-03-22 15:13:15 +00001190 if( f_rng != NULL )
1191 {
Janos Follathe81102e2017-03-22 13:38:28 +00001192#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001193 mbedtls_mpi_init( &D_blind );
1194#else
1195 mbedtls_mpi_init( &DP_blind );
1196 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001197#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001198 }
Janos Follathe81102e2017-03-22 13:38:28 +00001199
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001201#if defined(MBEDTLS_THREADING_C)
1202 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1203 return( ret );
1204#endif
1205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1207 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001209 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1210 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 }
1212
Paul Bakkerf451bac2013-08-30 15:37:02 +02001213 if( f_rng != NULL )
1214 {
1215 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001216 * Blinding
1217 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001218 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001219 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1220 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001222
Janos Follathe81102e2017-03-22 13:38:28 +00001223 /*
1224 * Exponent blinding
1225 */
1226 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1227 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1228
Janos Follathf9203b42017-03-22 15:13:15 +00001229#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001230 /*
1231 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1232 */
1233 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1234 f_rng, p_rng ) );
1235 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1236 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1237 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1238
1239 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001240#else
1241 /*
1242 * DP_blind = ( P - 1 ) * R + DP
1243 */
1244 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1245 f_rng, p_rng ) );
1246 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1247 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1248 &ctx->DP ) );
1249
1250 DP = &DP_blind;
1251
1252 /*
1253 * DQ_blind = ( Q - 1 ) * R + DQ
1254 */
1255 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1256 f_rng, p_rng ) );
1257 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1258 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1259 &ctx->DQ ) );
1260
1261 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001262#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001263 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001266 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001267#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001268 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001269 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 *
1271 * T1 = input ^ dP mod P
1272 * T2 = input ^ dQ mod Q
1273 */
Janos Follathf9203b42017-03-22 15:13:15 +00001274 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1275 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001276
1277 /*
1278 * T = (T1 - T2) * (Q^-1 mod P) mod P
1279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1281 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1282 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001283
1284 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001285 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1288 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1289#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001290
Paul Bakkerf451bac2013-08-30 15:37:02 +02001291 if( f_rng != NULL )
1292 {
1293 /*
1294 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001295 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001296 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001297 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001300
1301 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
1304cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001306 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1307 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001308#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001311 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1312
1313 if( f_rng != NULL )
1314 {
Janos Follathe81102e2017-03-22 13:38:28 +00001315#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001316 mbedtls_mpi_free( &D_blind );
1317#else
1318 mbedtls_mpi_free( &DP_blind );
1319 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001320#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001321 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
1323 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
1326 return( 0 );
1327}
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001330/**
1331 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1332 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001333 * \param dst buffer to mask
1334 * \param dlen length of destination buffer
1335 * \param src source of the mask generation
1336 * \param slen length of the source buffer
1337 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001338 */
Paul Bakker48377d92013-08-30 12:06:24 +02001339static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001341{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001343 unsigned char counter[4];
1344 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001345 unsigned int hlen;
1346 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001349 memset( counter, 0, 4 );
1350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001352
Simon Butcher02037452016-03-01 21:19:12 +00001353 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001354 p = dst;
1355
1356 while( dlen > 0 )
1357 {
1358 use_len = hlen;
1359 if( dlen < hlen )
1360 use_len = dlen;
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 mbedtls_md_starts( md_ctx );
1363 mbedtls_md_update( md_ctx, src, slen );
1364 mbedtls_md_update( md_ctx, counter, 4 );
1365 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001366
1367 for( i = 0; i < use_len; ++i )
1368 *p++ ^= mask[i];
1369
1370 counter[3]++;
1371
1372 dlen -= use_len;
1373 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001374
1375 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001380/*
1381 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001384 int (*f_rng)(void *, unsigned char *, size_t),
1385 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001386 int mode,
1387 const unsigned char *label, size_t label_len,
1388 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001389 const unsigned char *input,
1390 unsigned char *output )
1391{
1392 size_t olen;
1393 int ret;
1394 unsigned char *p = output;
1395 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 const mbedtls_md_info_t *md_info;
1397 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1400 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001401
1402 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001406 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001408
1409 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001411
Simon Butcher02037452016-03-01 21:19:12 +00001412 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001413 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001415
1416 memset( output, 0, olen );
1417
1418 *p++ = 0;
1419
Simon Butcher02037452016-03-01 21:19:12 +00001420 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001421 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001423
1424 p += hlen;
1425
Simon Butcher02037452016-03-01 21:19:12 +00001426 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001428 p += hlen;
1429 p += olen - 2 * hlen - 2 - ilen;
1430 *p++ = 1;
1431 memcpy( p, input, ilen );
1432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001434 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1435 {
1436 mbedtls_md_free( &md_ctx );
1437 return( ret );
1438 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001439
Simon Butcher02037452016-03-01 21:19:12 +00001440 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001441 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1442 &md_ctx );
1443
Simon Butcher02037452016-03-01 21:19:12 +00001444 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001445 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1446 &md_ctx );
1447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 return( ( mode == MBEDTLS_RSA_PUBLIC )
1451 ? mbedtls_rsa_public( ctx, output, output )
1452 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001457/*
1458 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001461 int (*f_rng)(void *, unsigned char *, size_t),
1462 void *p_rng,
1463 int mode, size_t ilen,
1464 const unsigned char *input,
1465 unsigned char *output )
1466{
1467 size_t nb_pad, olen;
1468 int ret;
1469 unsigned char *p = output;
1470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1472 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001473
Janos Follath1ed9f992016-03-18 11:45:44 +00001474 // We don't check p_rng because it won't be dereferenced here
1475 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001477
1478 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001479
Simon Butcher02037452016-03-01 21:19:12 +00001480 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001481 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001483
1484 nb_pad = olen - 3 - ilen;
1485
1486 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001490
1491 while( nb_pad-- > 0 )
1492 {
1493 int rng_dl = 100;
1494
1495 do {
1496 ret = f_rng( p_rng, p, 1 );
1497 } while( *p == 0 && --rng_dl && ret == 0 );
1498
Simon Butcher02037452016-03-01 21:19:12 +00001499 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001500 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001502
1503 p++;
1504 }
1505 }
1506 else
1507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001509
1510 while( nb_pad-- > 0 )
1511 *p++ = 0xFF;
1512 }
1513
1514 *p++ = 0;
1515 memcpy( p, input, ilen );
1516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 return( ( mode == MBEDTLS_RSA_PUBLIC )
1518 ? mbedtls_rsa_public( ctx, output, output )
1519 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001522
Paul Bakker5121ce52009-01-03 21:22:43 +00001523/*
1524 * Add the message padding, then do an RSA operation
1525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001527 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001528 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001529 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001530 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001531 unsigned char *output )
1532{
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 switch( ctx->padding )
1534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#if defined(MBEDTLS_PKCS1_V15)
1536 case MBEDTLS_RSA_PKCS_V15:
1537 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001538 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001539#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#if defined(MBEDTLS_PKCS1_V21)
1542 case MBEDTLS_RSA_PKCS_V21:
1543 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001544 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001545#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001546
1547 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001549 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001550}
1551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001553/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001554 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001557 int (*f_rng)(void *, unsigned char *, size_t),
1558 void *p_rng,
1559 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001560 const unsigned char *label, size_t label_len,
1561 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001562 const unsigned char *input,
1563 unsigned char *output,
1564 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001565{
Paul Bakker23986e52011-04-24 08:57:21 +00001566 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001567 size_t ilen, i, pad_len;
1568 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1570 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001571 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 const mbedtls_md_info_t *md_info;
1573 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001574
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001575 /*
1576 * Parameters sanity checks
1577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1579 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001580
1581 ilen = ctx->len;
1582
Paul Bakker27fdf462011-06-09 13:55:13 +00001583 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001587 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001589
Janos Follathc17cda12016-02-11 11:08:18 +00001590 hlen = mbedtls_md_get_size( md_info );
1591
1592 // checking for integer underflow
1593 if( 2 * hlen + 2 > ilen )
1594 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1595
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001596 /*
1597 * RSA operation
1598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1600 ? mbedtls_rsa_public( ctx, input, buf )
1601 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001602
1603 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001604 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001606 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001607 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001610 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1611 {
1612 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001613 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001614 }
1615
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001616
1617 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001619
1620 /* seed: Apply seedMask to maskedSeed */
1621 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1622 &md_ctx );
1623
1624 /* DB: Apply dbMask to maskedDB */
1625 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1626 &md_ctx );
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001629
1630 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001631 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001632 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001633 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001634 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001636 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001637
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001638 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001639
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001640 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001641 for( i = 0; i < hlen; i++ )
1642 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001643
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001644 /* Get zero-padding len, but always read till end of buffer
1645 * (minus one, for the 01 byte) */
1646 pad_len = 0;
1647 pad_done = 0;
1648 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1649 {
1650 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001651 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001652 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001653
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001654 p += pad_len;
1655 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001656
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001657 /*
1658 * The only information "leaked" is whether the padding was correct or not
1659 * (eg, no data is copied if it was not correct). This meets the
1660 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1661 * the different error conditions.
1662 */
1663 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001664 {
1665 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1666 goto cleanup;
1667 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001668
Paul Bakker66d5d072014-06-17 16:39:18 +02001669 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001670 {
1671 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1672 goto cleanup;
1673 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001674
1675 *olen = ilen - (p - buf);
1676 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001677 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001678
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001679cleanup:
1680 mbedtls_zeroize( buf, sizeof( buf ) );
1681 mbedtls_zeroize( lhash, sizeof( lhash ) );
1682
1683 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001684}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001688/*
1689 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001692 int (*f_rng)(void *, unsigned char *, size_t),
1693 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001694 int mode, size_t *olen,
1695 const unsigned char *input,
1696 unsigned char *output,
1697 size_t output_max_len)
1698{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001699 int ret;
1700 size_t ilen, pad_count = 0, i;
1701 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1705 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001706
1707 ilen = ctx->len;
1708
1709 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1713 ? mbedtls_rsa_public( ctx, input, buf )
1714 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001715
1716 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001717 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001718
1719 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001720 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001721
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001722 /*
1723 * Check and get padding len in "constant-time"
1724 */
1725 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001726
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001727 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001731
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001732 /* Get padding len, but always read till end of buffer
1733 * (minus one, for the 00 byte) */
1734 for( i = 0; i < ilen - 3; i++ )
1735 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001736 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1737 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001738 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001739
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001740 p += pad_count;
1741 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001742 }
1743 else
1744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001746
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001747 /* Get padding len, but always read till end of buffer
1748 * (minus one, for the 00 byte) */
1749 for( i = 0; i < ilen - 3; i++ )
1750 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001751 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001752 pad_count += ( pad_done == 0 );
1753 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001754
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001755 p += pad_count;
1756 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 }
1758
Janos Follathc69fa502016-02-12 13:30:09 +00001759 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001760
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001761 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001762 {
1763 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1764 goto cleanup;
1765 }
Paul Bakker8804f692013-02-28 18:06:26 +01001766
Paul Bakker66d5d072014-06-17 16:39:18 +02001767 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001768 {
1769 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1770 goto cleanup;
1771 }
Paul Bakker060c5682009-01-12 21:48:39 +00001772
Paul Bakker27fdf462011-06-09 13:55:13 +00001773 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001774 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001775 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001776
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001777cleanup:
1778 mbedtls_zeroize( buf, sizeof( buf ) );
1779
1780 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001783
1784/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001785 * Do an RSA operation, then remove the message padding
1786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001788 int (*f_rng)(void *, unsigned char *, size_t),
1789 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001790 int mode, size_t *olen,
1791 const unsigned char *input,
1792 unsigned char *output,
1793 size_t output_max_len)
1794{
1795 switch( ctx->padding )
1796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797#if defined(MBEDTLS_PKCS1_V15)
1798 case MBEDTLS_RSA_PKCS_V15:
1799 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001800 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001801#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#if defined(MBEDTLS_PKCS1_V21)
1804 case MBEDTLS_RSA_PKCS_V21:
1805 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001806 olen, input, output,
1807 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001808#endif
1809
1810 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001812 }
1813}
1814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001816/*
1817 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001820 int (*f_rng)(void *, unsigned char *, size_t),
1821 void *p_rng,
1822 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001824 unsigned int hashlen,
1825 const unsigned char *hash,
1826 unsigned char *sig )
1827{
1828 size_t olen;
1829 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001831 unsigned int slen, hlen, offset = 0;
1832 int ret;
1833 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 const mbedtls_md_info_t *md_info;
1835 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1838 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001839
1840 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001842
1843 olen = ctx->len;
1844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001846 {
Simon Butcher02037452016-03-01 21:19:12 +00001847 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001849 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001853 }
1854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001856 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001860 slen = hlen;
1861
1862 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001864
1865 memset( sig, 0, olen );
1866
Simon Butcher02037452016-03-01 21:19:12 +00001867 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001868 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001870
Simon Butcher02037452016-03-01 21:19:12 +00001871 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001872 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001873 p += olen - hlen * 2 - 2;
1874 *p++ = 0x01;
1875 memcpy( p, salt, slen );
1876 p += slen;
1877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001879 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1880 {
1881 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001882 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001883 return( ret );
1884 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001885
Simon Butcher02037452016-03-01 21:19:12 +00001886 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 mbedtls_md_starts( &md_ctx );
1888 mbedtls_md_update( &md_ctx, p, 8 );
1889 mbedtls_md_update( &md_ctx, hash, hashlen );
1890 mbedtls_md_update( &md_ctx, salt, slen );
1891 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001892 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001893
Simon Butcher02037452016-03-01 21:19:12 +00001894 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001895 if( msb % 8 == 0 )
1896 offset = 1;
1897
Simon Butcher02037452016-03-01 21:19:12 +00001898 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001899 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001902
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001903 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001904 sig[0] &= 0xFF >> ( olen * 8 - msb );
1905
1906 p += hlen;
1907 *p++ = 0xBC;
1908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 return( ( mode == MBEDTLS_RSA_PUBLIC )
1910 ? mbedtls_rsa_public( ctx, sig, sig )
1911 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001912}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001916/*
1917 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1918 */
1919/*
1920 * Do an RSA operation to sign the message digest
1921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001923 int (*f_rng)(void *, unsigned char *, size_t),
1924 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001925 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001927 unsigned int hashlen,
1928 const unsigned char *hash,
1929 unsigned char *sig )
1930{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001931 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001932 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001933 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001934 unsigned char *sig_try = NULL, *verif = NULL;
1935 size_t i;
1936 unsigned char diff;
1937 volatile unsigned char diff_no_optimize;
1938 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1941 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001942
1943 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001944 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001949 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1953 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001954
Paul Bakkerc70b9822013-04-07 22:00:46 +02001955 nb_pad -= 10 + oid_size;
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001958 }
1959
Paul Bakkerc70b9822013-04-07 22:00:46 +02001960 nb_pad -= hashlen;
1961
Paul Bakkerb3869132013-02-28 17:21:01 +01001962 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001964
1965 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001967 memset( p, 0xFF, nb_pad );
1968 p += nb_pad;
1969 *p++ = 0;
1970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001972 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001973 memcpy( p, hash, hashlen );
1974 }
1975 else
1976 {
1977 /*
1978 * DigestInfo ::= SEQUENCE {
1979 * digestAlgorithm DigestAlgorithmIdentifier,
1980 * digest Digest }
1981 *
1982 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1983 *
1984 * Digest ::= OCTET STRING
1985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001987 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001989 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001991 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001992 memcpy( p, oid, oid_size );
1993 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001995 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001997 *p++ = hashlen;
1998 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001999 }
2000
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002001 if( mode == MBEDTLS_RSA_PUBLIC )
2002 return( mbedtls_rsa_public( ctx, sig, sig ) );
2003
2004 /*
2005 * In order to prevent Lenstra's attack, make the signature in a
2006 * temporary buffer and check it before returning it.
2007 */
2008 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002009 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002010 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2011
Simon Butcher1285ab52016-01-01 21:42:47 +00002012 verif = mbedtls_calloc( 1, ctx->len );
2013 if( verif == NULL )
2014 {
2015 mbedtls_free( sig_try );
2016 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2017 }
2018
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002019 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2020 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2021
2022 /* Compare in constant time just in case */
2023 for( diff = 0, i = 0; i < ctx->len; i++ )
2024 diff |= verif[i] ^ sig[i];
2025 diff_no_optimize = diff;
2026
2027 if( diff_no_optimize != 0 )
2028 {
2029 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2030 goto cleanup;
2031 }
2032
2033 memcpy( sig, sig_try, ctx->len );
2034
2035cleanup:
2036 mbedtls_free( sig_try );
2037 mbedtls_free( verif );
2038
2039 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002040}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002042
2043/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 * Do an RSA operation to sign the message digest
2045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002047 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002048 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002051 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002052 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 unsigned char *sig )
2054{
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 switch( ctx->padding )
2056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057#if defined(MBEDTLS_PKCS1_V15)
2058 case MBEDTLS_RSA_PKCS_V15:
2059 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002060 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002061#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063#if defined(MBEDTLS_PKCS1_V21)
2064 case MBEDTLS_RSA_PKCS_V21:
2065 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002066 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002067#endif
2068
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002072}
2073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002075/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002076 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002079 int (*f_rng)(void *, unsigned char *, size_t),
2080 void *p_rng,
2081 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002083 unsigned int hashlen,
2084 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002086 int expected_salt_len,
2087 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002088{
Paul Bakker23986e52011-04-24 08:57:21 +00002089 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002090 size_t siglen;
2091 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002093 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002094 unsigned int hlen;
2095 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 const mbedtls_md_info_t *md_info;
2097 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002098 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2101 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002102
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 siglen = ctx->len;
2104
Paul Bakker27fdf462011-06-09 13:55:13 +00002105 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2109 ? mbedtls_rsa_public( ctx, sig, buf )
2110 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002111
2112 if( ret != 0 )
2113 return( ret );
2114
2115 p = buf;
2116
Paul Bakkerb3869132013-02-28 17:21:01 +01002117 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 {
Simon Butcher02037452016-03-01 21:19:12 +00002122 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002124 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002128 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002131 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002135 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002136
Paul Bakkerb3869132013-02-28 17:21:01 +01002137 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002138
Simon Butcher02037452016-03-01 21:19:12 +00002139 /*
2140 * Note: EMSA-PSS verification is over the length of N - 1 bits
2141 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002142 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002143
Simon Butcher02037452016-03-01 21:19:12 +00002144 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002145 if( msb % 8 == 0 )
2146 {
2147 p++;
2148 siglen -= 1;
2149 }
2150 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002154 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2155 {
2156 mbedtls_md_free( &md_ctx );
2157 return( ret );
2158 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002159
Paul Bakkerb3869132013-02-28 17:21:01 +01002160 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002161
Paul Bakkerb3869132013-02-28 17:21:01 +01002162 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002163
Paul Bakker4de44aa2013-12-31 11:43:01 +01002164 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002165 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002166
Paul Bakkerb3869132013-02-28 17:21:01 +01002167 if( p == buf + siglen ||
2168 *p++ != 0x01 )
2169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 mbedtls_md_free( &md_ctx );
2171 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002172 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002173
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002174 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002175 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002178 slen != (size_t) expected_salt_len )
2179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 mbedtls_md_free( &md_ctx );
2181 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002182 }
2183
Simon Butcher02037452016-03-01 21:19:12 +00002184 /*
2185 * Generate H = Hash( M' )
2186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 mbedtls_md_starts( &md_ctx );
2188 mbedtls_md_update( &md_ctx, zeros, 8 );
2189 mbedtls_md_update( &md_ctx, hash, hashlen );
2190 mbedtls_md_update( &md_ctx, p, slen );
2191 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002194
Paul Bakkerb3869132013-02-28 17:21:01 +01002195 if( memcmp( p + slen, result, hlen ) == 0 )
2196 return( 0 );
2197 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002199}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002200
2201/*
2202 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002205 int (*f_rng)(void *, unsigned char *, size_t),
2206 void *p_rng,
2207 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002209 unsigned int hashlen,
2210 const unsigned char *hash,
2211 const unsigned char *sig )
2212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2214 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002215 : md_alg;
2216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002218 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002220 sig ) );
2221
2222}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002226/*
2227 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002230 int (*f_rng)(void *, unsigned char *, size_t),
2231 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002232 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002234 unsigned int hashlen,
2235 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002236 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002237{
2238 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002239 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002240 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 mbedtls_md_type_t msg_md_alg;
2242 const mbedtls_md_info_t *md_info;
2243 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002244 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2247 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002248
2249 siglen = ctx->len;
2250
2251 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2255 ? mbedtls_rsa_public( ctx, sig, buf )
2256 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002257
2258 if( ret != 0 )
2259 return( ret );
2260
2261 p = buf;
2262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2264 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002265
2266 while( *p != 0 )
2267 {
2268 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002270 p++;
2271 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002272 p++; /* skip 00 byte */
2273
2274 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2275 if( p - buf < 11 )
2276 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002277
2278 len = siglen - ( p - buf );
2279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002281 {
2282 if( memcmp( p, hash, hashlen ) == 0 )
2283 return( 0 );
2284 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 }
2287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002289 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2291 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002292
2293 end = p + len;
2294
Simon Butcher02037452016-03-01 21:19:12 +00002295 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002296 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2297 * Insist on 2-byte length tags, to protect against variants of
2298 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002299 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002300 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2302 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2303 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002304 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002306
Gilles Peskinee7e76502017-05-04 12:48:39 +02002307 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2309 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2310 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002311 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002313
Gilles Peskinee7e76502017-05-04 12:48:39 +02002314 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2316 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002317 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002318 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002319
2320 oid.p = p;
2321 p += oid.len;
2322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2324 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002325
2326 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002328
2329 /*
2330 * assume the algorithm parameters must be NULL
2331 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002332 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2334 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002335 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002337
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002338 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002339 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2340 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002341 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002343
2344 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002346
2347 p += hashlen;
2348
2349 if( p != end )
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 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002355
2356/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002357 * Do an RSA operation and check the message digest
2358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002360 int (*f_rng)(void *, unsigned char *, size_t),
2361 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002362 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002364 unsigned int hashlen,
2365 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002366 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002367{
2368 switch( ctx->padding )
2369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370#if defined(MBEDTLS_PKCS1_V15)
2371 case MBEDTLS_RSA_PKCS_V15:
2372 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002373 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002374#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376#if defined(MBEDTLS_PKCS1_V21)
2377 case MBEDTLS_RSA_PKCS_V21:
2378 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002379 hashlen, hash, sig );
2380#endif
2381
2382 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002384 }
2385}
2386
2387/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002388 * Copy the components of an RSA key
2389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002391{
2392 int ret;
2393
2394 dst->ver = src->ver;
2395 dst->len = src->len;
2396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2398 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2401 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2402 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002403
2404#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2406 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2407 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2409 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002410#endif
2411
2412 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2415 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002416
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002417 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002418 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002419
2420cleanup:
2421 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002423
2424 return( ret );
2425}
2426
2427/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 * Free the components of an RSA key
2429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002431{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002433 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2434 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002436
Hanno Becker33c30a02017-08-23 07:00:22 +01002437#if !defined(MBEDTLS_RSA_NO_CRT)
2438 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2439 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2440 mbedtls_mpi_free( &ctx->DP );
2441#endif /* MBEDTLS_RSA_NO_CRT */
2442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443#if defined(MBEDTLS_THREADING_C)
2444 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002445#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002446}
2447
Hanno Beckerab377312017-08-23 16:24:51 +01002448#endif /* !MBEDTLS_RSA_ALT */
2449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002452#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
2454/*
2455 * Example RSA-1024 keypair, for test purposes
2456 */
2457#define KEY_LEN 128
2458
2459#define RSA_N "9292758453063D803DD603D5E777D788" \
2460 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2461 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2462 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2463 "93A89813FBF3C4F8066D2D800F7C38A8" \
2464 "1AE31942917403FF4946B0A83D3D3E05" \
2465 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2466 "5E94BB77B07507233A0BC7BAC8F90F79"
2467
2468#define RSA_E "10001"
2469
2470#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2471 "66CA472BC44D253102F8B4A9D3BFA750" \
2472 "91386C0077937FE33FA3252D28855837" \
2473 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2474 "DF79C5CE07EE72C7F123142198164234" \
2475 "CABB724CF78B8173B9F880FC86322407" \
2476 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2477 "071513A1E85B5DFA031F21ECAE91A34D"
2478
2479#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2480 "2C01CAD19EA484A87EA4377637E75500" \
2481 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2482 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2483
2484#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2485 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2486 "910E4168387E3C30AA1E00C339A79508" \
2487 "8452DD96A9A5EA5D9DCA68DA636032AF"
2488
Paul Bakker5121ce52009-01-03 21:22:43 +00002489#define PT_LEN 24
2490#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2491 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002494static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002495{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002496#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002497 size_t i;
2498
Paul Bakker545570e2010-07-18 09:00:25 +00002499 if( rng_state != NULL )
2500 rng_state = NULL;
2501
Paul Bakkera3d195c2011-11-27 21:07:34 +00002502 for( i = 0; i < len; ++i )
2503 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002504#else
2505 if( rng_state != NULL )
2506 rng_state = NULL;
2507
2508 arc4random_buf( output, len );
2509#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002510
Paul Bakkera3d195c2011-11-27 21:07:34 +00002511 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002514
Paul Bakker5121ce52009-01-03 21:22:43 +00002515/*
2516 * Checkup routine
2517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002519{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002520 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002522 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 unsigned char rsa_plaintext[PT_LEN];
2525 unsigned char rsa_decrypted[PT_LEN];
2526 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002528 unsigned char sha1sum[20];
2529#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Hanno Becker3a701162017-08-22 13:52:43 +01002531 mbedtls_mpi K;
2532
2533 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Hanno Becker3a701162017-08-22 13:52:43 +01002536 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2537 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2538 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2539 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2540 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2541 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2542 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2543 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2544 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2545 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2546
2547 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
2549 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2553 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 {
2555 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
2558 return( 1 );
2559 }
2560
2561 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
2564 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2565
Hanno Becker98838b02017-10-02 13:16:10 +01002566 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2567 PT_LEN, rsa_plaintext,
2568 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 {
2570 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
2573 return( 1 );
2574 }
2575
2576 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Hanno Becker98838b02017-10-02 13:16:10 +01002579 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2580 &len, rsa_ciphertext, rsa_decrypted,
2581 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 {
2583 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
2586 return( 1 );
2587 }
2588
2589 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2590 {
2591 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
2594 return( 1 );
2595 }
2596
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002597 if( verbose != 0 )
2598 mbedtls_printf( "passed\n" );
2599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002602 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Hanno Becker98838b02017-10-02 13:16:10 +01002606 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2607 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2608 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 {
2610 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
2613 return( 1 );
2614 }
2615
2616 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
Hanno Becker98838b02017-10-02 13:16:10 +01002619 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2620 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2621 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 {
2623 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
2626 return( 1 );
2627 }
2628
2629 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002630 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002633 if( verbose != 0 )
2634 mbedtls_printf( "\n" );
2635
Paul Bakker3d8fb632014-04-17 12:42:41 +02002636cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002637 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 mbedtls_rsa_free( &rsa );
2639#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002640 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002642 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643}
2644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647#endif /* MBEDTLS_RSA_C */