blob: 1fcffdfc3aec47abbcf4a4cc8f9efd1785bcfaba [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
709/*
710 * Check if CRT parameters match RSA context.
711 * This has to be implemented even if CRT is not used,
712 * in order to be able to validate DER encoded RSA keys,
713 * which always contain CRT parameters.
714 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100715int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx,
716 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100717{
Hanno Becker23344b52017-08-23 07:43:27 +0100718 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100719
Hanno Becker23344b52017-08-23 07:43:27 +0100720 /* Check if key is private or public */
721 const int is_priv =
722 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
723 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
724 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
725 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
726 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
727
728 if( !is_priv )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100729 {
730 /* Checking optional parameters only makes sense for private keys. */
731 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
732 }
733
Hanno Becker23344b52017-08-23 07:43:27 +0100734#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100735 if( ( DP != NULL && mbedtls_mpi_cmp_mpi( DP, &ctx->DP ) != 0 ) ||
736 ( DQ != NULL && mbedtls_mpi_cmp_mpi( DQ, &ctx->DQ ) != 0 ) ||
737 ( QP != NULL && mbedtls_mpi_cmp_mpi( QP, &ctx->QP ) != 0 ) )
738 {
739 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
740 }
Hanno Becker23344b52017-08-23 07:43:27 +0100741#else /* MBEDTLS_RSA_NO_CRT */
Hanno Beckerd3637992017-08-25 07:55:03 +0100742 if( ( ret = mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
743 DP, DQ, QP ) ) != 0 )
Hanno Becker23344b52017-08-23 07:43:27 +0100744 {
Hanno Beckerd3637992017-08-25 07:55:03 +0100745 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker23344b52017-08-23 07:43:27 +0100746 }
Hanno Becker23344b52017-08-23 07:43:27 +0100747#endif
748
749 if( ret != 0 )
750 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100751
752 return( 0 );
753}
754
755int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
756 unsigned char *N, size_t N_len,
757 unsigned char *P, size_t P_len,
758 unsigned char *Q, size_t Q_len,
759 unsigned char *D, size_t D_len,
760 unsigned char *E, size_t E_len )
761{
762 int ret = 0;
763
764 /* Check if key is private or public */
765 const int is_priv =
766 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
767 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
768 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
769 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
770 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
771
772 if( !is_priv )
773 {
774 /* If we're trying to export private parameters for a public key,
775 * something must be wrong. */
776 if( P != NULL || Q != NULL || D != NULL )
777 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
778
779 }
780
781 if( N != NULL )
782 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
783
784 if( P != NULL )
785 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
786
787 if( Q != NULL )
788 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
789
790 if( D != NULL )
791 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
792
793 if( E != NULL )
794 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100795
796cleanup:
797
798 return( ret );
799}
800
Hanno Becker617c1ae2017-08-23 14:11:24 +0100801int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
802 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
803 mbedtls_mpi *D, mbedtls_mpi *E )
804{
805 int ret;
806
807 /* Check if key is private or public */
808 int is_priv =
809 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
810 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
811 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
812 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
813 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
814
815 if( !is_priv )
816 {
817 /* If we're trying to export private parameters for a public key,
818 * something must be wrong. */
819 if( P != NULL || Q != NULL || D != NULL )
820 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
821
822 }
823
824 /* Export all requested core parameters. */
825
826 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
827 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
828 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
829 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
830 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
831 {
832 return( ret );
833 }
834
835 return( 0 );
836}
837
838/*
839 * Export CRT parameters
840 * This must also be implemented if CRT is not used, for being able to
841 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
842 * can be used in this case.
843 */
844int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
845 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
846{
847 int ret;
848
849 /* Check if key is private or public */
850 int is_priv =
851 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
852 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
853 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
854 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
855 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
856
857 if( !is_priv )
858 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
859
Hanno Beckerdc95c892017-08-23 06:57:02 +0100860#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100861 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100862 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
863 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
864 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
865 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100866 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100867 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100868#else
869 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
870 DP, DQ, QP ) ) != 0 )
871 {
872 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
873 }
874#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100875
876 return( 0 );
877}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100878
879/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000880 * Initialize an RSA context
881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000883 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000884 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000885{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890#if defined(MBEDTLS_THREADING_C)
891 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200892#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000893}
894
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100895/*
896 * Set padding for an existing RSA context
897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100899{
900 ctx->padding = padding;
901 ctx->hash_id = hash_id;
902}
903
Hanno Becker617c1ae2017-08-23 14:11:24 +0100904/*
905 * Get length in bytes of RSA modulus
906 */
907
908size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
909{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100910 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100911}
912
913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
916/*
917 * Generate an RSA keypair
918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000920 int (*f_rng)(void *, unsigned char *, size_t),
921 void *p_rng,
922 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000923{
924 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100925 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Paul Bakker21eb2802010-08-16 11:10:02 +0000927 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Janos Follathef441782016-09-21 13:18:12 +0100930 if( nbits % 2 )
931 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
932
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100933 mbedtls_mpi_init( &H );
934 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
936 /*
937 * find primes P and Q with Q < P so that:
938 * GCD( E, (P-1)*(Q-1) ) == 1
939 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
942 do
943 {
Janos Follath10c575b2016-02-23 14:42:48 +0000944 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100945 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000946
Janos Follathef441782016-09-21 13:18:12 +0100947 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100948 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 continue;
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200954 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 continue;
956
Janos Follathef441782016-09-21 13:18:12 +0100957 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100958 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100959
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100960 /* Temporarily replace P,Q by P-1, Q-1 */
961 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
962 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
963 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000965 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100968 /* Restore P,Q */
969 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
970 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
971
972 ctx->len = mbedtls_mpi_size( &ctx->N );
973
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 /*
975 * D = E^-1 mod ((P-1)*(Q-1))
976 * DP = D mod (P - 1)
977 * DQ = D mod (Q - 1)
978 * QP = Q^-1 mod P
979 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100981 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
982
983#if !defined(MBEDTLS_RSA_NO_CRT)
984 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
985 &ctx->DP, &ctx->DQ, &ctx->QP ) );
986#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
Hanno Becker83aad1f2017-08-23 06:45:10 +0100988 /* Double-check */
989 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
990
Paul Bakker5121ce52009-01-03 21:22:43 +0000991cleanup:
992
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100993 mbedtls_mpi_free( &H );
994 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
996 if( ret != 0 )
997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_rsa_free( ctx );
999 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 }
1001
Paul Bakker48377d92013-08-30 12:06:24 +02001002 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001003}
1004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
1007/*
1008 * Check a public RSA key
1009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001011{
Hanno Becker98838b02017-10-02 13:16:10 +01001012 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
1013 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
1014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +01001016 }
Paul Bakker37940d9f2009-07-10 22:38:58 +00001017
Hanno Beckerba1ba112017-09-29 11:48:23 +01001018 if( ctx->len != mbedtls_mpi_size( &ctx->N ) )
1019 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1020
Hanno Becker98838b02017-10-02 13:16:10 +01001021 if( mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 ||
1022 mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 )
1023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +01001025 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001027 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
1028 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Hanno Becker98838b02017-10-02 13:16:10 +01001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +01001031 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001033 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +01001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +01001037 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001038
1039 return( 0 );
1040}
1041
1042/*
1043 * Check a private RSA key
1044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001046{
Hanno Becker98838b02017-10-02 13:16:10 +01001047 if( mbedtls_rsa_check_pubkey( ctx ) != 0 )
1048 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1049
1050 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +01001051 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 {
Hanno Beckerb269a852017-08-25 08:03:21 +01001053 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 }
Hanno Beckerb269a852017-08-25 08:03:21 +01001055#if !defined(MBEDTLS_RSA_NO_CRT)
1056 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
1057 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
1058 {
1059 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1060 }
1061#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001062
1063 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001064}
1065
1066/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001067 * Check if contexts holding a public and private key match
1068 */
Hanno Becker98838b02017-10-02 13:16:10 +01001069int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
1070 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001071{
Hanno Becker98838b02017-10-02 13:16:10 +01001072 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001076 }
1077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1079 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001082 }
1083
1084 return( 0 );
1085}
1086
1087/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 * Do an RSA public key operation
1089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001091 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 unsigned char *output )
1093{
Paul Bakker23986e52011-04-24 08:57:21 +00001094 int ret;
1095 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001099
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001100#if defined(MBEDTLS_THREADING_C)
1101 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1102 return( ret );
1103#endif
1104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001109 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1110 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 }
1112
1113 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1115 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001116
1117cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001119 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1120 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
1125 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001127
1128 return( 0 );
1129}
1130
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001131/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001132 * Generate or update blinding values, see section 10 of:
1133 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001134 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001135 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001136 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001137static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001138 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1139{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001140 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001141
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001142 if( ctx->Vf.p != NULL )
1143 {
1144 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1146 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1147 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1148 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001149
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001150 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001151 }
1152
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001153 /* Unblinding value: Vf = random number, invertible mod N */
1154 do {
1155 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1159 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1160 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001161
1162 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1164 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 +02001165
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001166
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001167cleanup:
1168 return( ret );
1169}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001170
Paul Bakker5121ce52009-01-03 21:22:43 +00001171/*
Janos Follathe81102e2017-03-22 13:38:28 +00001172 * Exponent blinding supposed to prevent side-channel attacks using multiple
1173 * traces of measurements to recover the RSA key. The more collisions are there,
1174 * the more bits of the key can be recovered. See [3].
1175 *
1176 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1177 * observations on avarage.
1178 *
1179 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1180 * to make 2^112 observations on avarage.
1181 *
1182 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1183 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1184 * Thus in this sense with 28 byte blinding the security is not reduced by
1185 * side-channel attacks like the one in [3])
1186 *
1187 * This countermeasure does not help if the key recovery is possible with a
1188 * single trace.
1189 */
1190#define RSA_EXPONENT_BLINDING 28
1191
1192/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 * Do an RSA private key operation
1194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001196 int (*f_rng)(void *, unsigned char *, size_t),
1197 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001198 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 unsigned char *output )
1200{
Paul Bakker23986e52011-04-24 08:57:21 +00001201 int ret;
1202 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001204 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001205#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001206 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001207 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001208#else
1209 mbedtls_mpi DP_blind, DQ_blind;
1210 mbedtls_mpi *DP = &ctx->DP;
1211 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001212#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001213
Hanno Becker45037ce2017-08-25 11:03:34 +01001214 /* Sanity-check that all relevant fields are at least set,
1215 * but don't perform a full keycheck. */
1216 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
1217 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
1218 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
1219 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
1220 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
1221 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001222 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker45037ce2017-08-25 11:03:34 +01001223 }
1224#if !defined(MBEDTLS_RSA_NO_CRT)
1225 if( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
1226 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
1227 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
1228 {
1229 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1230 }
1231#endif /* MBEDTLS_RSA_NO_CRT */
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001234 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1235
Janos Follathf9203b42017-03-22 15:13:15 +00001236 if( f_rng != NULL )
1237 {
Janos Follathe81102e2017-03-22 13:38:28 +00001238#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001239 mbedtls_mpi_init( &D_blind );
1240#else
1241 mbedtls_mpi_init( &DP_blind );
1242 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001243#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001244 }
Janos Follathe81102e2017-03-22 13:38:28 +00001245
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001247#if defined(MBEDTLS_THREADING_C)
1248 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1249 return( ret );
1250#endif
1251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1253 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001255 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1256 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 }
1258
Paul Bakkerf451bac2013-08-30 15:37:02 +02001259 if( f_rng != NULL )
1260 {
1261 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001262 * Blinding
1263 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001264 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001265 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1266 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001268
Janos Follathe81102e2017-03-22 13:38:28 +00001269 /*
1270 * Exponent blinding
1271 */
1272 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1273 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1274
Janos Follathf9203b42017-03-22 15:13:15 +00001275#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001276 /*
1277 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1278 */
1279 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1280 f_rng, p_rng ) );
1281 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1282 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1283 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1284
1285 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001286#else
1287 /*
1288 * DP_blind = ( P - 1 ) * R + DP
1289 */
1290 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1291 f_rng, p_rng ) );
1292 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1293 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1294 &ctx->DP ) );
1295
1296 DP = &DP_blind;
1297
1298 /*
1299 * DQ_blind = ( Q - 1 ) * R + DQ
1300 */
1301 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1302 f_rng, p_rng ) );
1303 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1304 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1305 &ctx->DQ ) );
1306
1307 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001308#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001309 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001312 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001313#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001314 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001315 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 *
1317 * T1 = input ^ dP mod P
1318 * T2 = input ^ dQ mod Q
1319 */
Janos Follathf9203b42017-03-22 15:13:15 +00001320 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1321 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
1323 /*
1324 * T = (T1 - T2) * (Q^-1 mod P) mod P
1325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1327 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1328 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
1330 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001331 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1334 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1335#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001336
Paul Bakkerf451bac2013-08-30 15:37:02 +02001337 if( f_rng != NULL )
1338 {
1339 /*
1340 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001341 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001342 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001343 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001345 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
1347 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001349
1350cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001352 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1353 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001354#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001357 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1358
1359 if( f_rng != NULL )
1360 {
Janos Follathe81102e2017-03-22 13:38:28 +00001361#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001362 mbedtls_mpi_free( &D_blind );
1363#else
1364 mbedtls_mpi_free( &DP_blind );
1365 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001366#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001368
1369 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
1372 return( 0 );
1373}
1374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001376/**
1377 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1378 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001379 * \param dst buffer to mask
1380 * \param dlen length of destination buffer
1381 * \param src source of the mask generation
1382 * \param slen length of the source buffer
1383 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001384 */
Paul Bakker48377d92013-08-30 12:06:24 +02001385static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001389 unsigned char counter[4];
1390 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001391 unsigned int hlen;
1392 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001395 memset( counter, 0, 4 );
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001398
Simon Butcher02037452016-03-01 21:19:12 +00001399 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001400 p = dst;
1401
1402 while( dlen > 0 )
1403 {
1404 use_len = hlen;
1405 if( dlen < hlen )
1406 use_len = dlen;
1407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_md_starts( md_ctx );
1409 mbedtls_md_update( md_ctx, src, slen );
1410 mbedtls_md_update( md_ctx, counter, 4 );
1411 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001412
1413 for( i = 0; i < use_len; ++i )
1414 *p++ ^= mask[i];
1415
1416 counter[3]++;
1417
1418 dlen -= use_len;
1419 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001420
1421 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001426/*
1427 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001430 int (*f_rng)(void *, unsigned char *, size_t),
1431 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001432 int mode,
1433 const unsigned char *label, size_t label_len,
1434 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001435 const unsigned char *input,
1436 unsigned char *output )
1437{
1438 size_t olen;
1439 int ret;
1440 unsigned char *p = output;
1441 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 const mbedtls_md_info_t *md_info;
1443 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1446 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001447
1448 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001452 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001454
1455 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001457
Simon Butcher02037452016-03-01 21:19:12 +00001458 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001459 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001461
1462 memset( output, 0, olen );
1463
1464 *p++ = 0;
1465
Simon Butcher02037452016-03-01 21:19:12 +00001466 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001467 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001469
1470 p += hlen;
1471
Simon Butcher02037452016-03-01 21:19:12 +00001472 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001474 p += hlen;
1475 p += olen - 2 * hlen - 2 - ilen;
1476 *p++ = 1;
1477 memcpy( p, input, ilen );
1478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001480 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1481 {
1482 mbedtls_md_free( &md_ctx );
1483 return( ret );
1484 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001485
Simon Butcher02037452016-03-01 21:19:12 +00001486 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001487 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1488 &md_ctx );
1489
Simon Butcher02037452016-03-01 21:19:12 +00001490 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001491 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1492 &md_ctx );
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 return( ( mode == MBEDTLS_RSA_PUBLIC )
1497 ? mbedtls_rsa_public( ctx, output, output )
1498 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001503/*
1504 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001507 int (*f_rng)(void *, unsigned char *, size_t),
1508 void *p_rng,
1509 int mode, size_t ilen,
1510 const unsigned char *input,
1511 unsigned char *output )
1512{
1513 size_t nb_pad, olen;
1514 int ret;
1515 unsigned char *p = output;
1516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1518 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001519
Janos Follath1ed9f992016-03-18 11:45:44 +00001520 // We don't check p_rng because it won't be dereferenced here
1521 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001523
1524 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001525
Simon Butcher02037452016-03-01 21:19:12 +00001526 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001527 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001529
1530 nb_pad = olen - 3 - ilen;
1531
1532 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001536
1537 while( nb_pad-- > 0 )
1538 {
1539 int rng_dl = 100;
1540
1541 do {
1542 ret = f_rng( p_rng, p, 1 );
1543 } while( *p == 0 && --rng_dl && ret == 0 );
1544
Simon Butcher02037452016-03-01 21:19:12 +00001545 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001546 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001548
1549 p++;
1550 }
1551 }
1552 else
1553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001555
1556 while( nb_pad-- > 0 )
1557 *p++ = 0xFF;
1558 }
1559
1560 *p++ = 0;
1561 memcpy( p, input, ilen );
1562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 return( ( mode == MBEDTLS_RSA_PUBLIC )
1564 ? mbedtls_rsa_public( ctx, output, output )
1565 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001568
Paul Bakker5121ce52009-01-03 21:22:43 +00001569/*
1570 * Add the message padding, then do an RSA operation
1571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001573 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001574 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001575 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001576 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001577 unsigned char *output )
1578{
Paul Bakker5121ce52009-01-03 21:22:43 +00001579 switch( ctx->padding )
1580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581#if defined(MBEDTLS_PKCS1_V15)
1582 case MBEDTLS_RSA_PKCS_V15:
1583 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001584 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001585#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#if defined(MBEDTLS_PKCS1_V21)
1588 case MBEDTLS_RSA_PKCS_V21:
1589 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001590 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001591#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001592
1593 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001596}
1597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001599/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001600 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001603 int (*f_rng)(void *, unsigned char *, size_t),
1604 void *p_rng,
1605 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001606 const unsigned char *label, size_t label_len,
1607 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001608 const unsigned char *input,
1609 unsigned char *output,
1610 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001611{
Paul Bakker23986e52011-04-24 08:57:21 +00001612 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001613 size_t ilen, i, pad_len;
1614 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1616 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001617 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 const mbedtls_md_info_t *md_info;
1619 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001620
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001621 /*
1622 * Parameters sanity checks
1623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1625 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
1627 ilen = ctx->len;
1628
Paul Bakker27fdf462011-06-09 13:55:13 +00001629 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001633 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001635
Janos Follathc17cda12016-02-11 11:08:18 +00001636 hlen = mbedtls_md_get_size( md_info );
1637
1638 // checking for integer underflow
1639 if( 2 * hlen + 2 > ilen )
1640 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1641
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001642 /*
1643 * RSA operation
1644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1646 ? mbedtls_rsa_public( ctx, input, buf )
1647 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001648
1649 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001650 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001652 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001653 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001656 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1657 {
1658 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001659 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001660 }
1661
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001662
1663 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001665
1666 /* seed: Apply seedMask to maskedSeed */
1667 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1668 &md_ctx );
1669
1670 /* DB: Apply dbMask to maskedDB */
1671 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1672 &md_ctx );
1673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001675
1676 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001677 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001678 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001680 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001682 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001683
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001684 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001685
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001686 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001687 for( i = 0; i < hlen; i++ )
1688 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001689
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001690 /* Get zero-padding len, but always read till end of buffer
1691 * (minus one, for the 01 byte) */
1692 pad_len = 0;
1693 pad_done = 0;
1694 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1695 {
1696 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001697 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001698 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001699
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001700 p += pad_len;
1701 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001702
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001703 /*
1704 * The only information "leaked" is whether the padding was correct or not
1705 * (eg, no data is copied if it was not correct). This meets the
1706 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1707 * the different error conditions.
1708 */
1709 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001710 {
1711 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1712 goto cleanup;
1713 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001714
Paul Bakker66d5d072014-06-17 16:39:18 +02001715 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001716 {
1717 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1718 goto cleanup;
1719 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001720
1721 *olen = ilen - (p - buf);
1722 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001723 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001724
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001725cleanup:
1726 mbedtls_zeroize( buf, sizeof( buf ) );
1727 mbedtls_zeroize( lhash, sizeof( lhash ) );
1728
1729 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001730}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001734/*
1735 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001738 int (*f_rng)(void *, unsigned char *, size_t),
1739 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001740 int mode, size_t *olen,
1741 const unsigned char *input,
1742 unsigned char *output,
1743 size_t output_max_len)
1744{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001745 int ret;
1746 size_t ilen, pad_count = 0, i;
1747 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1751 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001752
1753 ilen = ctx->len;
1754
1755 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1759 ? mbedtls_rsa_public( ctx, input, buf )
1760 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001761
1762 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001763 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001764
1765 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001766 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001767
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001768 /*
1769 * Check and get padding len in "constant-time"
1770 */
1771 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001772
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001773 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001777
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001778 /* Get padding len, but always read till end of buffer
1779 * (minus one, for the 00 byte) */
1780 for( i = 0; i < ilen - 3; i++ )
1781 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001782 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1783 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001784 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001785
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001786 p += pad_count;
1787 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001788 }
1789 else
1790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001792
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001793 /* Get padding len, but always read till end of buffer
1794 * (minus one, for the 00 byte) */
1795 for( i = 0; i < ilen - 3; i++ )
1796 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001797 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001798 pad_count += ( pad_done == 0 );
1799 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001800
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001801 p += pad_count;
1802 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001803 }
1804
Janos Follathc69fa502016-02-12 13:30:09 +00001805 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001806
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001807 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001808 {
1809 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1810 goto cleanup;
1811 }
Paul Bakker8804f692013-02-28 18:06:26 +01001812
Paul Bakker66d5d072014-06-17 16:39:18 +02001813 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001814 {
1815 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1816 goto cleanup;
1817 }
Paul Bakker060c5682009-01-12 21:48:39 +00001818
Paul Bakker27fdf462011-06-09 13:55:13 +00001819 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001820 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001821 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001823cleanup:
1824 mbedtls_zeroize( buf, sizeof( buf ) );
1825
1826 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001827}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001829
1830/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001831 * Do an RSA operation, then remove the message padding
1832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001834 int (*f_rng)(void *, unsigned char *, size_t),
1835 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001836 int mode, size_t *olen,
1837 const unsigned char *input,
1838 unsigned char *output,
1839 size_t output_max_len)
1840{
1841 switch( ctx->padding )
1842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843#if defined(MBEDTLS_PKCS1_V15)
1844 case MBEDTLS_RSA_PKCS_V15:
1845 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001846 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001847#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849#if defined(MBEDTLS_PKCS1_V21)
1850 case MBEDTLS_RSA_PKCS_V21:
1851 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001852 olen, input, output,
1853 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001854#endif
1855
1856 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001858 }
1859}
1860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001862/*
1863 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001866 int (*f_rng)(void *, unsigned char *, size_t),
1867 void *p_rng,
1868 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001870 unsigned int hashlen,
1871 const unsigned char *hash,
1872 unsigned char *sig )
1873{
1874 size_t olen;
1875 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001877 unsigned int slen, hlen, offset = 0;
1878 int ret;
1879 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 const mbedtls_md_info_t *md_info;
1881 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1884 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001885
1886 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001888
1889 olen = ctx->len;
1890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001892 {
Simon Butcher02037452016-03-01 21:19:12 +00001893 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001895 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001899 }
1900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001902 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001906 slen = hlen;
1907
1908 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001910
1911 memset( sig, 0, olen );
1912
Simon Butcher02037452016-03-01 21:19:12 +00001913 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001914 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001916
Simon Butcher02037452016-03-01 21:19:12 +00001917 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001918 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001919 p += olen - hlen * 2 - 2;
1920 *p++ = 0x01;
1921 memcpy( p, salt, slen );
1922 p += slen;
1923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001925 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1926 {
1927 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001928 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001929 return( ret );
1930 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001931
Simon Butcher02037452016-03-01 21:19:12 +00001932 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 mbedtls_md_starts( &md_ctx );
1934 mbedtls_md_update( &md_ctx, p, 8 );
1935 mbedtls_md_update( &md_ctx, hash, hashlen );
1936 mbedtls_md_update( &md_ctx, salt, slen );
1937 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001938 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001939
Simon Butcher02037452016-03-01 21:19:12 +00001940 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001941 if( msb % 8 == 0 )
1942 offset = 1;
1943
Simon Butcher02037452016-03-01 21:19:12 +00001944 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001945 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001948
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001949 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001950 sig[0] &= 0xFF >> ( olen * 8 - msb );
1951
1952 p += hlen;
1953 *p++ = 0xBC;
1954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 return( ( mode == MBEDTLS_RSA_PUBLIC )
1956 ? mbedtls_rsa_public( ctx, sig, sig )
1957 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001962/*
1963 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1964 */
1965/*
1966 * Do an RSA operation to sign the message digest
1967 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001969 int (*f_rng)(void *, unsigned char *, size_t),
1970 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001971 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001973 unsigned int hashlen,
1974 const unsigned char *hash,
1975 unsigned char *sig )
1976{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001977 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001978 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001979 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001980 unsigned char *sig_try = NULL, *verif = NULL;
1981 size_t i;
1982 unsigned char diff;
1983 volatile unsigned char diff_no_optimize;
1984 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1987 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001988
1989 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001990 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001995 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1999 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002000
Paul Bakkerc70b9822013-04-07 22:00:46 +02002001 nb_pad -= 10 + oid_size;
2002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002004 }
2005
Paul Bakkerc70b9822013-04-07 22:00:46 +02002006 nb_pad -= hashlen;
2007
Paul Bakkerb3869132013-02-28 17:21:01 +01002008 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002010
2011 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01002013 memset( p, 0xFF, nb_pad );
2014 p += nb_pad;
2015 *p++ = 0;
2016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002018 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002019 memcpy( p, hash, hashlen );
2020 }
2021 else
2022 {
2023 /*
2024 * DigestInfo ::= SEQUENCE {
2025 * digestAlgorithm DigestAlgorithmIdentifier,
2026 * digest Digest }
2027 *
2028 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2029 *
2030 * Digest ::= OCTET STRING
2031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002033 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002035 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002037 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002038 memcpy( p, oid, oid_size );
2039 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002041 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002043 *p++ = hashlen;
2044 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002045 }
2046
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002047 if( mode == MBEDTLS_RSA_PUBLIC )
2048 return( mbedtls_rsa_public( ctx, sig, sig ) );
2049
2050 /*
2051 * In order to prevent Lenstra's attack, make the signature in a
2052 * temporary buffer and check it before returning it.
2053 */
2054 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002055 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002056 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2057
Simon Butcher1285ab52016-01-01 21:42:47 +00002058 verif = mbedtls_calloc( 1, ctx->len );
2059 if( verif == NULL )
2060 {
2061 mbedtls_free( sig_try );
2062 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2063 }
2064
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002065 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2066 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2067
2068 /* Compare in constant time just in case */
2069 for( diff = 0, i = 0; i < ctx->len; i++ )
2070 diff |= verif[i] ^ sig[i];
2071 diff_no_optimize = diff;
2072
2073 if( diff_no_optimize != 0 )
2074 {
2075 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2076 goto cleanup;
2077 }
2078
2079 memcpy( sig, sig_try, ctx->len );
2080
2081cleanup:
2082 mbedtls_free( sig_try );
2083 mbedtls_free( verif );
2084
2085 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002086}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002088
2089/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 * Do an RSA operation to sign the message digest
2091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002093 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002094 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002097 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002098 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 unsigned char *sig )
2100{
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 switch( ctx->padding )
2102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#if defined(MBEDTLS_PKCS1_V15)
2104 case MBEDTLS_RSA_PKCS_V15:
2105 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002106 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002107#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109#if defined(MBEDTLS_PKCS1_V21)
2110 case MBEDTLS_RSA_PKCS_V21:
2111 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002112 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002113#endif
2114
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002118}
2119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002121/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002122 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002125 int (*f_rng)(void *, unsigned char *, size_t),
2126 void *p_rng,
2127 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002129 unsigned int hashlen,
2130 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002132 int expected_salt_len,
2133 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002134{
Paul Bakker23986e52011-04-24 08:57:21 +00002135 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002136 size_t siglen;
2137 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002139 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002140 unsigned int hlen;
2141 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 const mbedtls_md_info_t *md_info;
2143 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002144 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2147 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002148
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 siglen = ctx->len;
2150
Paul Bakker27fdf462011-06-09 13:55:13 +00002151 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2155 ? mbedtls_rsa_public( ctx, sig, buf )
2156 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
2158 if( ret != 0 )
2159 return( ret );
2160
2161 p = buf;
2162
Paul Bakkerb3869132013-02-28 17:21:01 +01002163 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 {
Simon Butcher02037452016-03-01 21:19:12 +00002168 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002170 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002174 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002177 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002181 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002182
Paul Bakkerb3869132013-02-28 17:21:01 +01002183 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002184
Simon Butcher02037452016-03-01 21:19:12 +00002185 /*
2186 * Note: EMSA-PSS verification is over the length of N - 1 bits
2187 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002188 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002189
Simon Butcher02037452016-03-01 21:19:12 +00002190 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002191 if( msb % 8 == 0 )
2192 {
2193 p++;
2194 siglen -= 1;
2195 }
2196 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002200 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2201 {
2202 mbedtls_md_free( &md_ctx );
2203 return( ret );
2204 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002205
Paul Bakkerb3869132013-02-28 17:21:01 +01002206 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002207
Paul Bakkerb3869132013-02-28 17:21:01 +01002208 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002209
Paul Bakker4de44aa2013-12-31 11:43:01 +01002210 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002211 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002212
Paul Bakkerb3869132013-02-28 17:21:01 +01002213 if( p == buf + siglen ||
2214 *p++ != 0x01 )
2215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 mbedtls_md_free( &md_ctx );
2217 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002218 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002219
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002220 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002221 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002224 slen != (size_t) expected_salt_len )
2225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 mbedtls_md_free( &md_ctx );
2227 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002228 }
2229
Simon Butcher02037452016-03-01 21:19:12 +00002230 /*
2231 * Generate H = Hash( M' )
2232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 mbedtls_md_starts( &md_ctx );
2234 mbedtls_md_update( &md_ctx, zeros, 8 );
2235 mbedtls_md_update( &md_ctx, hash, hashlen );
2236 mbedtls_md_update( &md_ctx, p, slen );
2237 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002240
Paul Bakkerb3869132013-02-28 17:21:01 +01002241 if( memcmp( p + slen, result, hlen ) == 0 )
2242 return( 0 );
2243 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002245}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002246
2247/*
2248 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002251 int (*f_rng)(void *, unsigned char *, size_t),
2252 void *p_rng,
2253 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002255 unsigned int hashlen,
2256 const unsigned char *hash,
2257 const unsigned char *sig )
2258{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2260 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002261 : md_alg;
2262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002264 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002266 sig ) );
2267
2268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002272/*
2273 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002276 int (*f_rng)(void *, unsigned char *, size_t),
2277 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002278 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002280 unsigned int hashlen,
2281 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002282 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002283{
2284 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002285 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002286 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 mbedtls_md_type_t msg_md_alg;
2288 const mbedtls_md_info_t *md_info;
2289 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002290 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2293 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002294
2295 siglen = ctx->len;
2296
2297 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2301 ? mbedtls_rsa_public( ctx, sig, buf )
2302 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002303
2304 if( ret != 0 )
2305 return( ret );
2306
2307 p = buf;
2308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2310 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002311
2312 while( *p != 0 )
2313 {
2314 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002316 p++;
2317 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002318 p++; /* skip 00 byte */
2319
2320 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2321 if( p - buf < 11 )
2322 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002323
2324 len = siglen - ( p - buf );
2325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002327 {
2328 if( memcmp( p, hash, hashlen ) == 0 )
2329 return( 0 );
2330 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 }
2333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002335 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2337 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002338
2339 end = p + len;
2340
Simon Butcher02037452016-03-01 21:19:12 +00002341 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002342 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2343 * Insist on 2-byte length tags, to protect against variants of
2344 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002345 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002346 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2348 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2349 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002350 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002352
Gilles Peskinee7e76502017-05-04 12:48:39 +02002353 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2355 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2356 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002357 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002359
Gilles Peskinee7e76502017-05-04 12:48:39 +02002360 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2362 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002363 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002364 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002365
2366 oid.p = p;
2367 p += oid.len;
2368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2370 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002371
2372 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002374
2375 /*
2376 * assume the algorithm parameters must be NULL
2377 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002378 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2380 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002381 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002383
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002384 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002385 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2386 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002387 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002389
2390 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002392
2393 p += hashlen;
2394
2395 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002397
2398 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
2402/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002403 * Do an RSA operation and check the message digest
2404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002406 int (*f_rng)(void *, unsigned char *, size_t),
2407 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002408 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002410 unsigned int hashlen,
2411 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002412 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002413{
2414 switch( ctx->padding )
2415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416#if defined(MBEDTLS_PKCS1_V15)
2417 case MBEDTLS_RSA_PKCS_V15:
2418 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002419 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002420#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422#if defined(MBEDTLS_PKCS1_V21)
2423 case MBEDTLS_RSA_PKCS_V21:
2424 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002425 hashlen, hash, sig );
2426#endif
2427
2428 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002430 }
2431}
2432
2433/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002434 * Copy the components of an RSA key
2435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002437{
2438 int ret;
2439
2440 dst->ver = src->ver;
2441 dst->len = src->len;
2442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2444 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2447 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2448 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002449
2450#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2452 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2453 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2455 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002456#endif
2457
2458 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2461 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002462
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002463 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002464 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002465
2466cleanup:
2467 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002469
2470 return( ret );
2471}
2472
2473/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 * Free the components of an RSA key
2475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002477{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002479 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2480 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002482
Hanno Becker33c30a02017-08-23 07:00:22 +01002483#if !defined(MBEDTLS_RSA_NO_CRT)
2484 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2485 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2486 mbedtls_mpi_free( &ctx->DP );
2487#endif /* MBEDTLS_RSA_NO_CRT */
2488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#if defined(MBEDTLS_THREADING_C)
2490 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002491#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002492}
2493
Hanno Beckerab377312017-08-23 16:24:51 +01002494#endif /* !MBEDTLS_RSA_ALT */
2495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002498#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
2500/*
2501 * Example RSA-1024 keypair, for test purposes
2502 */
2503#define KEY_LEN 128
2504
2505#define RSA_N "9292758453063D803DD603D5E777D788" \
2506 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2507 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2508 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2509 "93A89813FBF3C4F8066D2D800F7C38A8" \
2510 "1AE31942917403FF4946B0A83D3D3E05" \
2511 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2512 "5E94BB77B07507233A0BC7BAC8F90F79"
2513
2514#define RSA_E "10001"
2515
2516#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2517 "66CA472BC44D253102F8B4A9D3BFA750" \
2518 "91386C0077937FE33FA3252D28855837" \
2519 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2520 "DF79C5CE07EE72C7F123142198164234" \
2521 "CABB724CF78B8173B9F880FC86322407" \
2522 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2523 "071513A1E85B5DFA031F21ECAE91A34D"
2524
2525#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2526 "2C01CAD19EA484A87EA4377637E75500" \
2527 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2528 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2529
2530#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2531 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2532 "910E4168387E3C30AA1E00C339A79508" \
2533 "8452DD96A9A5EA5D9DCA68DA636032AF"
2534
2535#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2536 "3C94D22288ACD763FD8E5600ED4A702D" \
2537 "F84198A5F06C2E72236AE490C93F07F8" \
2538 "3CC559CD27BC2D1CA488811730BB5725"
2539
2540#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2541 "D8AAEA56749EA28623272E4F7D0592AF" \
2542 "7C1F1313CAC9471B5C523BFE592F517B" \
2543 "407A1BD76C164B93DA2D32A383E58357"
2544
2545#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2546 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2547 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2548 "A74206CEC169D74BF5A8C50D6F48EA08"
2549
2550#define PT_LEN 24
2551#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2552 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002555static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002556{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002557#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002558 size_t i;
2559
Paul Bakker545570e2010-07-18 09:00:25 +00002560 if( rng_state != NULL )
2561 rng_state = NULL;
2562
Paul Bakkera3d195c2011-11-27 21:07:34 +00002563 for( i = 0; i < len; ++i )
2564 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002565#else
2566 if( rng_state != NULL )
2567 rng_state = NULL;
2568
2569 arc4random_buf( output, len );
2570#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002571
Paul Bakkera3d195c2011-11-27 21:07:34 +00002572 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002575
Paul Bakker5121ce52009-01-03 21:22:43 +00002576/*
2577 * Checkup routine
2578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002580{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002581 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002583 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 unsigned char rsa_plaintext[PT_LEN];
2586 unsigned char rsa_decrypted[PT_LEN];
2587 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002589 unsigned char sha1sum[20];
2590#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Hanno Becker3a701162017-08-22 13:52:43 +01002592 mbedtls_mpi K;
2593
2594 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
Hanno Becker3a701162017-08-22 13:52:43 +01002597 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2598 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2599 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2600 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2601 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2602 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2603 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2604 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2605 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2606 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2607
2608 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
2610 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2614 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 {
2616 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
2619 return( 1 );
2620 }
2621
Hanno Becker3a701162017-08-22 13:52:43 +01002622 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2623 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2624
2625 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2626 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2627
2628 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2629 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2630
Paul Bakker5121ce52009-01-03 21:22:43 +00002631 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
2634 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2635
Hanno Becker98838b02017-10-02 13:16:10 +01002636 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2637 PT_LEN, rsa_plaintext,
2638 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 {
2640 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
2643 return( 1 );
2644 }
2645
2646 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Hanno Becker98838b02017-10-02 13:16:10 +01002649 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2650 &len, rsa_ciphertext, rsa_decrypted,
2651 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 {
2653 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
2656 return( 1 );
2657 }
2658
2659 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2660 {
2661 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
2664 return( 1 );
2665 }
2666
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002667 if( verbose != 0 )
2668 mbedtls_printf( "passed\n" );
2669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002672 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Hanno Becker98838b02017-10-02 13:16:10 +01002676 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2677 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2678 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 {
2680 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
2683 return( 1 );
2684 }
2685
2686 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Hanno Becker98838b02017-10-02 13:16:10 +01002689 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2690 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2691 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 {
2693 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
2696 return( 1 );
2697 }
2698
2699 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002700 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002703 if( verbose != 0 )
2704 mbedtls_printf( "\n" );
2705
Paul Bakker3d8fb632014-04-17 12:42:41 +02002706cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002707 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 mbedtls_rsa_free( &rsa );
2709#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002710 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002712 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002713}
2714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#endif /* MBEDTLS_RSA_C */