blob: b0ba1eb2c699e6c740db97f2607c6baf26c6f367 [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 /*
Hanno Beckerb5beaa82017-10-02 13:01:43 +0100425 * Step 2: Check that 1 < N = PQ
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100426 */
427
428 if( P != NULL && Q != NULL && N != NULL )
429 {
430 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
Hanno Beckerb5beaa82017-10-02 13:01:43 +0100431 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
Hanno Becker750e8b42017-08-25 07:54:27 +0100432 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 /*
Hanno Beckerb5beaa82017-10-02 13:01:43 +0100440 * Step 3: Check and 1 < D, E < N if present.
441 */
442
443 if( N != NULL && D != NULL && E != NULL )
444 {
445 if ( mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
446 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
447 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
448 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
449 {
450 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
451 goto cleanup;
452 }
453 }
454
455 /*
456 * Step 4: Check that D, E are inverse modulo P-1 and Q-1
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100457 */
458
459 if( P != NULL && Q != NULL && D != NULL && E != NULL )
460 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100461 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
Hanno Beckerb5beaa82017-10-02 13:01:43 +0100462 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 )
Hanno Becker750e8b42017-08-25 07:54:27 +0100463 {
464 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
465 goto cleanup;
466 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100467
468 /* Compute DE-1 mod P-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100469 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
470 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
471 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
472 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100473 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
474 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100475 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100476 goto cleanup;
477 }
478
479 /* Compute DE-1 mod Q-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100480 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
481 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
482 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
483 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100484 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
485 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100486 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100487 goto cleanup;
488 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100489 }
490
491cleanup:
492
493 mbedtls_mpi_free( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100494 mbedtls_mpi_free( &L );
495
496 /* Wrap MPI error codes by RSA check failure error code */
497 if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
498 {
499 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
500 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100501
502 return( ret );
503}
504
505int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
506 const mbedtls_mpi *D, mbedtls_mpi *DP,
507 mbedtls_mpi *DQ, mbedtls_mpi *QP )
508{
509 int ret = 0;
510 mbedtls_mpi K;
511 mbedtls_mpi_init( &K );
512
Hanno Beckerd9431a72017-08-25 08:03:13 +0100513 /* DP = D mod P-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100514 if( DP != NULL )
515 {
516 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
517 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
518 }
519
Hanno Beckerd9431a72017-08-25 08:03:13 +0100520 /* DQ = D mod Q-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100521 if( DQ != NULL )
522 {
523 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
524 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
525 }
526
Hanno Beckerd9431a72017-08-25 08:03:13 +0100527 /* QP = Q^{-1} mod P */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100528 if( QP != NULL )
529 {
530 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
531 }
532
533cleanup:
534 mbedtls_mpi_free( &K );
535
536 return( ret );
537}
538
Hanno Becker617c1ae2017-08-23 14:11:24 +0100539
540/*
541 * Default RSA interface implementation
542 */
543
Hanno Beckerab377312017-08-23 16:24:51 +0100544#if !defined(MBEDTLS_RSA_ALT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100545
546int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
547 const mbedtls_mpi *N,
548 const mbedtls_mpi *P, const mbedtls_mpi *Q,
549 const mbedtls_mpi *D, const mbedtls_mpi *E )
550{
551 int ret;
552
553 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
554 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
555 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
556 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
557 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
558 {
559 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
560 }
561
562 if( N != NULL )
563 ctx->len = mbedtls_mpi_size( &ctx->N );
564
565 return( 0 );
566}
567
568int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100569 unsigned char const *N, size_t N_len,
570 unsigned char const *P, size_t P_len,
571 unsigned char const *Q, size_t Q_len,
572 unsigned char const *D, size_t D_len,
573 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100574{
575 int ret;
576
577 if( N != NULL )
578 {
579 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
580 ctx->len = mbedtls_mpi_size( &ctx->N );
581 }
582
583 if( P != NULL )
584 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
585
586 if( Q != NULL )
587 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
588
589 if( D != NULL )
590 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
591
592 if( E != NULL )
593 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
594
595cleanup:
596
597 if( ret != 0 )
598 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
599
600 return( 0 );
601}
602
603int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
604 int (*f_rng)(void *, unsigned char *, size_t),
605 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100606{
607 int ret = 0;
608
Hanno Becker617c1ae2017-08-23 14:11:24 +0100609 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
610 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
611 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
612 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
613 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100614
Hanno Becker617c1ae2017-08-23 14:11:24 +0100615 /*
616 * Check whether provided parameters are enough
617 * to deduce all others. The following incomplete
618 * parameter sets for private keys are supported:
619 *
620 * (1) P, Q missing.
621 * (2) D and potentially N missing.
622 *
623 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100624
Hanno Becker2cca6f32017-09-29 11:46:40 +0100625 const int n_missing = have_P && have_Q && have_D && have_E;
626 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
627 const int d_missing = have_P && have_Q && !have_D && have_E;
628 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
629
630 /* These three alternatives are mutually exclusive */
631 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100632
Hanno Becker617c1ae2017-08-23 14:11:24 +0100633 if( !is_priv && !is_pub )
634 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
635
636 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100637 * Step 1: Deduce N if P, Q are provided.
638 */
639
640 if( !have_N && have_P && have_Q )
641 {
642 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
643 &ctx->Q ) ) != 0 )
644 {
645 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
646 }
647
648 ctx->len = mbedtls_mpi_size( &ctx->N );
649 }
650
651 /*
652 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100653 */
654
655 if( pq_missing )
656 {
657 /* This includes sanity checking of core parameters,
658 * so no further checks necessary. */
659 ret = mbedtls_rsa_deduce_moduli( &ctx->N, &ctx->D, &ctx->E,
660 f_rng, p_rng,
661 &ctx->P, &ctx->Q );
662 if( ret != 0 )
663 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
664
665 }
666 else if( d_missing )
667 {
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100668#if defined(MBEDTLS_GENPRIME)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100669 /* If a PRNG is provided, check if P, Q are prime. */
670 if( f_rng != NULL &&
671 ( ( ret = mbedtls_mpi_is_prime( &ctx->P, f_rng, p_rng ) ) != 0 ||
672 ( ret = mbedtls_mpi_is_prime( &ctx->Q, f_rng, p_rng ) ) != 0 ) )
673 {
674 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
675 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100676#endif /* MBEDTLS_GENPRIME */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100677
Hanno Becker617c1ae2017-08-23 14:11:24 +0100678 /* Deduce private exponent. This includes double-checking of the result,
679 * so together with the primality test above all core parameters are
680 * guaranteed to be sane if this call succeeds. */
681 if( ( ret = mbedtls_rsa_deduce_private( &ctx->P, &ctx->Q,
Hanno Beckerbdefff12017-10-02 09:57:50 +0100682 &ctx->E, &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100683 {
684 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
685 }
686 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100687
688 /* In the remaining case of a public key, there's nothing to check for. */
689
690 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100691 * Step 3: Deduce all additional parameters specific
Hanno Becker617c1ae2017-08-23 14:11:24 +0100692 * to our current RSA implementaiton.
693 */
694
Hanno Becker23344b52017-08-23 07:43:27 +0100695#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100696 if( is_priv )
697 {
698 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
699 &ctx->DP, &ctx->DQ, &ctx->QP );
700 if( ret != 0 )
701 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
702 }
Hanno Becker23344b52017-08-23 07:43:27 +0100703#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100704
705 /*
Hanno Becker98838b02017-10-02 13:16:10 +0100706 * Step 3: Basic sanity check
Hanno Becker617c1ae2017-08-23 14:11:24 +0100707 */
708
709 if( is_priv )
710 {
711 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
712 return( ret );
713 }
714 else
715 {
716 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
717 return( ret );
718 }
719
720 return( 0 );
721}
722
Hanno Becker617c1ae2017-08-23 14:11:24 +0100723int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
724 unsigned char *N, size_t N_len,
725 unsigned char *P, size_t P_len,
726 unsigned char *Q, size_t Q_len,
727 unsigned char *D, size_t D_len,
728 unsigned char *E, size_t E_len )
729{
730 int ret = 0;
731
732 /* Check if key is private or public */
733 const int is_priv =
734 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
735 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
736 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
737 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
738 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
739
740 if( !is_priv )
741 {
742 /* If we're trying to export private parameters for a public key,
743 * something must be wrong. */
744 if( P != NULL || Q != NULL || D != NULL )
745 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
746
747 }
748
749 if( N != NULL )
750 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
751
752 if( P != NULL )
753 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
754
755 if( Q != NULL )
756 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
757
758 if( D != NULL )
759 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
760
761 if( E != NULL )
762 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100763
764cleanup:
765
766 return( ret );
767}
768
Hanno Becker617c1ae2017-08-23 14:11:24 +0100769int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
770 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
771 mbedtls_mpi *D, mbedtls_mpi *E )
772{
773 int ret;
774
775 /* Check if key is private or public */
776 int is_priv =
777 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
778 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
779 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
780 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
781 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
782
783 if( !is_priv )
784 {
785 /* If we're trying to export private parameters for a public key,
786 * something must be wrong. */
787 if( P != NULL || Q != NULL || D != NULL )
788 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
789
790 }
791
792 /* Export all requested core parameters. */
793
794 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
795 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
796 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
797 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
798 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
799 {
800 return( ret );
801 }
802
803 return( 0 );
804}
805
806/*
807 * Export CRT parameters
808 * This must also be implemented if CRT is not used, for being able to
809 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
810 * can be used in this case.
811 */
812int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
813 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
814{
815 int ret;
816
817 /* Check if key is private or public */
818 int is_priv =
819 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
820 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
821 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
822 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
823 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
824
825 if( !is_priv )
826 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
827
Hanno Beckerdc95c892017-08-23 06:57:02 +0100828#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100829 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100830 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
831 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
832 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
833 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100834 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100835 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100836#else
837 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
838 DP, DQ, QP ) ) != 0 )
839 {
840 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
841 }
842#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100843
844 return( 0 );
845}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100846
847/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000848 * Initialize an RSA context
849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000851 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000852 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000853{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#if defined(MBEDTLS_THREADING_C)
859 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200860#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000861}
862
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100863/*
864 * Set padding for an existing RSA context
865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100867{
868 ctx->padding = padding;
869 ctx->hash_id = hash_id;
870}
871
Hanno Becker617c1ae2017-08-23 14:11:24 +0100872/*
873 * Get length in bytes of RSA modulus
874 */
875
876size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
877{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100878 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100879}
880
881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
884/*
885 * Generate an RSA keypair
886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000888 int (*f_rng)(void *, unsigned char *, size_t),
889 void *p_rng,
890 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000891{
892 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100893 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000894
Paul Bakker21eb2802010-08-16 11:10:02 +0000895 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000897
Janos Follathef441782016-09-21 13:18:12 +0100898 if( nbits % 2 )
899 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
900
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100901 mbedtls_mpi_init( &H );
902 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000903
904 /*
905 * find primes P and Q with Q < P so that:
906 * GCD( E, (P-1)*(Q-1) ) == 1
907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000909
910 do
911 {
Janos Follath10c575b2016-02-23 14:42:48 +0000912 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100913 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
Janos Follathef441782016-09-21 13:18:12 +0100915 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100916 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 continue;
920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200922 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000923 continue;
924
Janos Follathef441782016-09-21 13:18:12 +0100925 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100926 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100927
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100928 /* Temporarily replace P,Q by P-1, Q-1 */
929 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
930 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
931 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100936 /* Restore P,Q */
937 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
938 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
939
940 ctx->len = mbedtls_mpi_size( &ctx->N );
941
Paul Bakker5121ce52009-01-03 21:22:43 +0000942 /*
943 * D = E^-1 mod ((P-1)*(Q-1))
944 * DP = D mod (P - 1)
945 * DQ = D mod (Q - 1)
946 * QP = Q^-1 mod P
947 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000948
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100949 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
950
951#if !defined(MBEDTLS_RSA_NO_CRT)
952 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
953 &ctx->DP, &ctx->DQ, &ctx->QP ) );
954#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
Hanno Becker83aad1f2017-08-23 06:45:10 +0100956 /* Double-check */
957 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
958
Paul Bakker5121ce52009-01-03 21:22:43 +0000959cleanup:
960
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100961 mbedtls_mpi_free( &H );
962 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
964 if( ret != 0 )
965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_rsa_free( ctx );
967 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 }
969
Paul Bakker48377d92013-08-30 12:06:24 +0200970 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000971}
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000974
975/*
976 * Check a public RSA key
977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000979{
Hanno Becker98838b02017-10-02 13:16:10 +0100980 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
981 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100984 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000985
Hanno Beckerba1ba112017-09-29 11:48:23 +0100986 if( ctx->len != mbedtls_mpi_size( &ctx->N ) )
987 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
988
Hanno Becker98838b02017-10-02 13:16:10 +0100989 if( mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 ||
990 mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 )
991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100993 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200995 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
996 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Hanno Becker98838b02017-10-02 13:16:10 +0100997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100999 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001001 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +01001003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +01001005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
1007 return( 0 );
1008}
1009
1010/*
1011 * Check a private RSA key
1012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001014{
Hanno Becker98838b02017-10-02 13:16:10 +01001015 if( mbedtls_rsa_check_pubkey( ctx ) != 0 )
1016 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1017
1018 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +01001019 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 {
Hanno Beckerb269a852017-08-25 08:03:21 +01001021 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001022 }
Hanno Beckerb269a852017-08-25 08:03:21 +01001023#if !defined(MBEDTLS_RSA_NO_CRT)
1024 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
1025 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
1026 {
1027 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1028 }
1029#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001030
1031 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001032}
1033
1034/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001035 * Check if contexts holding a public and private key match
1036 */
Hanno Becker98838b02017-10-02 13:16:10 +01001037int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
1038 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001039{
Hanno Becker98838b02017-10-02 13:16:10 +01001040 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001044 }
1045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1047 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001050 }
1051
1052 return( 0 );
1053}
1054
1055/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 * Do an RSA public key operation
1057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001059 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 unsigned char *output )
1061{
Paul Bakker23986e52011-04-24 08:57:21 +00001062 int ret;
1063 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001068#if defined(MBEDTLS_THREADING_C)
1069 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1070 return( ret );
1071#endif
1072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001077 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1078 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 }
1080
1081 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1083 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
1085cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001087 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1088 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001089#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001092
1093 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
1096 return( 0 );
1097}
1098
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001099/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001100 * Generate or update blinding values, see section 10 of:
1101 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001102 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001103 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001104 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001105static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001106 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1107{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001108 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001109
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001110 if( ctx->Vf.p != NULL )
1111 {
1112 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1114 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1115 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1116 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001117
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001118 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001119 }
1120
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001121 /* Unblinding value: Vf = random number, invertible mod N */
1122 do {
1123 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1127 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1128 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001129
1130 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1132 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 +02001133
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001134
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001135cleanup:
1136 return( ret );
1137}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001138
Paul Bakker5121ce52009-01-03 21:22:43 +00001139/*
Janos Follathe81102e2017-03-22 13:38:28 +00001140 * Exponent blinding supposed to prevent side-channel attacks using multiple
1141 * traces of measurements to recover the RSA key. The more collisions are there,
1142 * the more bits of the key can be recovered. See [3].
1143 *
1144 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1145 * observations on avarage.
1146 *
1147 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1148 * to make 2^112 observations on avarage.
1149 *
1150 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1151 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1152 * Thus in this sense with 28 byte blinding the security is not reduced by
1153 * side-channel attacks like the one in [3])
1154 *
1155 * This countermeasure does not help if the key recovery is possible with a
1156 * single trace.
1157 */
1158#define RSA_EXPONENT_BLINDING 28
1159
1160/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 * Do an RSA private key operation
1162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001164 int (*f_rng)(void *, unsigned char *, size_t),
1165 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001166 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 unsigned char *output )
1168{
Paul Bakker23986e52011-04-24 08:57:21 +00001169 int ret;
1170 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001172 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001173#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001174 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001175 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001176#else
1177 mbedtls_mpi DP_blind, DQ_blind;
1178 mbedtls_mpi *DP = &ctx->DP;
1179 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001180#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001181
Hanno Becker45037ce2017-08-25 11:03:34 +01001182 /* Sanity-check that all relevant fields are at least set,
1183 * but don't perform a full keycheck. */
1184 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
1185 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
1186 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
1187 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
1188 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
1189 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001190 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker45037ce2017-08-25 11:03:34 +01001191 }
1192#if !defined(MBEDTLS_RSA_NO_CRT)
1193 if( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
1194 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
1195 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
1196 {
1197 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1198 }
1199#endif /* MBEDTLS_RSA_NO_CRT */
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001202 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1203
Janos Follathf9203b42017-03-22 15:13:15 +00001204 if( f_rng != NULL )
1205 {
Janos Follathe81102e2017-03-22 13:38:28 +00001206#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001207 mbedtls_mpi_init( &D_blind );
1208#else
1209 mbedtls_mpi_init( &DP_blind );
1210 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001211#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001212 }
Janos Follathe81102e2017-03-22 13:38:28 +00001213
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001215#if defined(MBEDTLS_THREADING_C)
1216 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1217 return( ret );
1218#endif
1219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1221 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001223 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1224 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 }
1226
Paul Bakkerf451bac2013-08-30 15:37:02 +02001227 if( f_rng != NULL )
1228 {
1229 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001230 * Blinding
1231 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001232 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001233 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1234 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001236
Janos Follathe81102e2017-03-22 13:38:28 +00001237 /*
1238 * Exponent blinding
1239 */
1240 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1241 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1242
Janos Follathf9203b42017-03-22 15:13:15 +00001243#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001244 /*
1245 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1246 */
1247 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1248 f_rng, p_rng ) );
1249 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1250 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1251 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1252
1253 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001254#else
1255 /*
1256 * DP_blind = ( P - 1 ) * R + DP
1257 */
1258 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1259 f_rng, p_rng ) );
1260 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1261 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1262 &ctx->DP ) );
1263
1264 DP = &DP_blind;
1265
1266 /*
1267 * DQ_blind = ( Q - 1 ) * R + DQ
1268 */
1269 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1270 f_rng, p_rng ) );
1271 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1272 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1273 &ctx->DQ ) );
1274
1275 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001276#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001277 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001280 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001281#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001282 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001283 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 *
1285 * T1 = input ^ dP mod P
1286 * T2 = input ^ dQ mod Q
1287 */
Janos Follathf9203b42017-03-22 15:13:15 +00001288 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1289 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001290
1291 /*
1292 * T = (T1 - T2) * (Q^-1 mod P) mod P
1293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1295 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1296 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001297
1298 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001299 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1302 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1303#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001304
Paul Bakkerf451bac2013-08-30 15:37:02 +02001305 if( f_rng != NULL )
1306 {
1307 /*
1308 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001309 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001310 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001311 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
1315 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
1318cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001320 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1321 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001322#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001325 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1326
1327 if( f_rng != NULL )
1328 {
Janos Follathe81102e2017-03-22 13:38:28 +00001329#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001330 mbedtls_mpi_free( &D_blind );
1331#else
1332 mbedtls_mpi_free( &DP_blind );
1333 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001334#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001335 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
1337 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
1340 return( 0 );
1341}
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001344/**
1345 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1346 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001347 * \param dst buffer to mask
1348 * \param dlen length of destination buffer
1349 * \param src source of the mask generation
1350 * \param slen length of the source buffer
1351 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001352 */
Paul Bakker48377d92013-08-30 12:06:24 +02001353static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001355{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001357 unsigned char counter[4];
1358 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001359 unsigned int hlen;
1360 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001363 memset( counter, 0, 4 );
1364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001366
Simon Butcher02037452016-03-01 21:19:12 +00001367 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001368 p = dst;
1369
1370 while( dlen > 0 )
1371 {
1372 use_len = hlen;
1373 if( dlen < hlen )
1374 use_len = dlen;
1375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 mbedtls_md_starts( md_ctx );
1377 mbedtls_md_update( md_ctx, src, slen );
1378 mbedtls_md_update( md_ctx, counter, 4 );
1379 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001380
1381 for( i = 0; i < use_len; ++i )
1382 *p++ ^= mask[i];
1383
1384 counter[3]++;
1385
1386 dlen -= use_len;
1387 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001388
1389 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001390}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001394/*
1395 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001398 int (*f_rng)(void *, unsigned char *, size_t),
1399 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001400 int mode,
1401 const unsigned char *label, size_t label_len,
1402 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001403 const unsigned char *input,
1404 unsigned char *output )
1405{
1406 size_t olen;
1407 int ret;
1408 unsigned char *p = output;
1409 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 const mbedtls_md_info_t *md_info;
1411 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1414 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001415
1416 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001420 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001422
1423 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001425
Simon Butcher02037452016-03-01 21:19:12 +00001426 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001427 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001429
1430 memset( output, 0, olen );
1431
1432 *p++ = 0;
1433
Simon Butcher02037452016-03-01 21:19:12 +00001434 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001435 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001437
1438 p += hlen;
1439
Simon Butcher02037452016-03-01 21:19:12 +00001440 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001442 p += hlen;
1443 p += olen - 2 * hlen - 2 - ilen;
1444 *p++ = 1;
1445 memcpy( p, input, ilen );
1446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001448 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1449 {
1450 mbedtls_md_free( &md_ctx );
1451 return( ret );
1452 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001453
Simon Butcher02037452016-03-01 21:19:12 +00001454 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001455 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1456 &md_ctx );
1457
Simon Butcher02037452016-03-01 21:19:12 +00001458 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001459 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1460 &md_ctx );
1461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 return( ( mode == MBEDTLS_RSA_PUBLIC )
1465 ? mbedtls_rsa_public( ctx, output, output )
1466 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001471/*
1472 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001475 int (*f_rng)(void *, unsigned char *, size_t),
1476 void *p_rng,
1477 int mode, size_t ilen,
1478 const unsigned char *input,
1479 unsigned char *output )
1480{
1481 size_t nb_pad, olen;
1482 int ret;
1483 unsigned char *p = output;
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1486 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001487
Janos Follath1ed9f992016-03-18 11:45:44 +00001488 // We don't check p_rng because it won't be dereferenced here
1489 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001491
1492 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001493
Simon Butcher02037452016-03-01 21:19:12 +00001494 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001495 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001497
1498 nb_pad = olen - 3 - ilen;
1499
1500 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001504
1505 while( nb_pad-- > 0 )
1506 {
1507 int rng_dl = 100;
1508
1509 do {
1510 ret = f_rng( p_rng, p, 1 );
1511 } while( *p == 0 && --rng_dl && ret == 0 );
1512
Simon Butcher02037452016-03-01 21:19:12 +00001513 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001514 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001516
1517 p++;
1518 }
1519 }
1520 else
1521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001523
1524 while( nb_pad-- > 0 )
1525 *p++ = 0xFF;
1526 }
1527
1528 *p++ = 0;
1529 memcpy( p, input, ilen );
1530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 return( ( mode == MBEDTLS_RSA_PUBLIC )
1532 ? mbedtls_rsa_public( ctx, output, output )
1533 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001536
Paul Bakker5121ce52009-01-03 21:22:43 +00001537/*
1538 * Add the message padding, then do an RSA operation
1539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001541 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001542 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001543 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001544 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 unsigned char *output )
1546{
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 switch( ctx->padding )
1548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_PKCS1_V15)
1550 case MBEDTLS_RSA_PKCS_V15:
1551 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001552 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001553#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#if defined(MBEDTLS_PKCS1_V21)
1556 case MBEDTLS_RSA_PKCS_V21:
1557 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001558 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001559#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001560
1561 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001564}
1565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001567/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001568 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001571 int (*f_rng)(void *, unsigned char *, size_t),
1572 void *p_rng,
1573 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001574 const unsigned char *label, size_t label_len,
1575 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001576 const unsigned char *input,
1577 unsigned char *output,
1578 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001579{
Paul Bakker23986e52011-04-24 08:57:21 +00001580 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001581 size_t ilen, i, pad_len;
1582 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1584 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001585 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 const mbedtls_md_info_t *md_info;
1587 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001588
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001589 /*
1590 * Parameters sanity checks
1591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1593 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
1595 ilen = ctx->len;
1596
Paul Bakker27fdf462011-06-09 13:55:13 +00001597 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001601 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001603
Janos Follathc17cda12016-02-11 11:08:18 +00001604 hlen = mbedtls_md_get_size( md_info );
1605
1606 // checking for integer underflow
1607 if( 2 * hlen + 2 > ilen )
1608 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1609
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001610 /*
1611 * RSA operation
1612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1614 ? mbedtls_rsa_public( ctx, input, buf )
1615 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
1617 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001618 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001619
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001620 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001621 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001624 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1625 {
1626 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001627 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001628 }
1629
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001630
1631 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001633
1634 /* seed: Apply seedMask to maskedSeed */
1635 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1636 &md_ctx );
1637
1638 /* DB: Apply dbMask to maskedDB */
1639 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1640 &md_ctx );
1641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001643
1644 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001645 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001646 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001647 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001648 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001649
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001650 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001651
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001652 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001653
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001654 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001655 for( i = 0; i < hlen; i++ )
1656 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001657
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001658 /* Get zero-padding len, but always read till end of buffer
1659 * (minus one, for the 01 byte) */
1660 pad_len = 0;
1661 pad_done = 0;
1662 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1663 {
1664 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001665 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001666 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001667
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001668 p += pad_len;
1669 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001670
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001671 /*
1672 * The only information "leaked" is whether the padding was correct or not
1673 * (eg, no data is copied if it was not correct). This meets the
1674 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1675 * the different error conditions.
1676 */
1677 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001678 {
1679 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1680 goto cleanup;
1681 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001682
Paul Bakker66d5d072014-06-17 16:39:18 +02001683 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001684 {
1685 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1686 goto cleanup;
1687 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001688
1689 *olen = ilen - (p - buf);
1690 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001691 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001692
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001693cleanup:
1694 mbedtls_zeroize( buf, sizeof( buf ) );
1695 mbedtls_zeroize( lhash, sizeof( lhash ) );
1696
1697 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001702/*
1703 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001706 int (*f_rng)(void *, unsigned char *, size_t),
1707 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001708 int mode, size_t *olen,
1709 const unsigned char *input,
1710 unsigned char *output,
1711 size_t output_max_len)
1712{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001713 int ret;
1714 size_t ilen, pad_count = 0, i;
1715 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1719 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001720
1721 ilen = ctx->len;
1722
1723 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1727 ? mbedtls_rsa_public( ctx, input, buf )
1728 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001729
1730 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001731 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001732
1733 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001734 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001735
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001736 /*
1737 * Check and get padding len in "constant-time"
1738 */
1739 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001740
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001741 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001745
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001746 /* Get padding len, but always read till end of buffer
1747 * (minus one, for the 00 byte) */
1748 for( i = 0; i < ilen - 3; i++ )
1749 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001750 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1751 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001752 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001753
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001754 p += pad_count;
1755 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001756 }
1757 else
1758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001760
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001761 /* Get padding len, but always read till end of buffer
1762 * (minus one, for the 00 byte) */
1763 for( i = 0; i < ilen - 3; i++ )
1764 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001765 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001766 pad_count += ( pad_done == 0 );
1767 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001768
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001769 p += pad_count;
1770 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 }
1772
Janos Follathc69fa502016-02-12 13:30:09 +00001773 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001774
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001775 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001776 {
1777 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1778 goto cleanup;
1779 }
Paul Bakker8804f692013-02-28 18:06:26 +01001780
Paul Bakker66d5d072014-06-17 16:39:18 +02001781 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001782 {
1783 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1784 goto cleanup;
1785 }
Paul Bakker060c5682009-01-12 21:48:39 +00001786
Paul Bakker27fdf462011-06-09 13:55:13 +00001787 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001789 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001790
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001791cleanup:
1792 mbedtls_zeroize( buf, sizeof( buf ) );
1793
1794 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001795}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
1798/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001799 * Do an RSA operation, then remove the message padding
1800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001802 int (*f_rng)(void *, unsigned char *, size_t),
1803 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001804 int mode, size_t *olen,
1805 const unsigned char *input,
1806 unsigned char *output,
1807 size_t output_max_len)
1808{
1809 switch( ctx->padding )
1810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#if defined(MBEDTLS_PKCS1_V15)
1812 case MBEDTLS_RSA_PKCS_V15:
1813 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001814 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001815#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817#if defined(MBEDTLS_PKCS1_V21)
1818 case MBEDTLS_RSA_PKCS_V21:
1819 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001820 olen, input, output,
1821 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001822#endif
1823
1824 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001826 }
1827}
1828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001830/*
1831 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001834 int (*f_rng)(void *, unsigned char *, size_t),
1835 void *p_rng,
1836 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001838 unsigned int hashlen,
1839 const unsigned char *hash,
1840 unsigned char *sig )
1841{
1842 size_t olen;
1843 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001845 unsigned int slen, hlen, offset = 0;
1846 int ret;
1847 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 const mbedtls_md_info_t *md_info;
1849 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1852 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001853
1854 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001856
1857 olen = ctx->len;
1858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001860 {
Simon Butcher02037452016-03-01 21:19:12 +00001861 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001863 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001867 }
1868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001870 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001874 slen = hlen;
1875
1876 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001878
1879 memset( sig, 0, olen );
1880
Simon Butcher02037452016-03-01 21:19:12 +00001881 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001882 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001884
Simon Butcher02037452016-03-01 21:19:12 +00001885 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001886 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001887 p += olen - hlen * 2 - 2;
1888 *p++ = 0x01;
1889 memcpy( p, salt, slen );
1890 p += slen;
1891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001893 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1894 {
1895 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001896 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001897 return( ret );
1898 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001899
Simon Butcher02037452016-03-01 21:19:12 +00001900 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 mbedtls_md_starts( &md_ctx );
1902 mbedtls_md_update( &md_ctx, p, 8 );
1903 mbedtls_md_update( &md_ctx, hash, hashlen );
1904 mbedtls_md_update( &md_ctx, salt, slen );
1905 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001906 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001907
Simon Butcher02037452016-03-01 21:19:12 +00001908 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001909 if( msb % 8 == 0 )
1910 offset = 1;
1911
Simon Butcher02037452016-03-01 21:19:12 +00001912 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001913 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001916
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001917 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001918 sig[0] &= 0xFF >> ( olen * 8 - msb );
1919
1920 p += hlen;
1921 *p++ = 0xBC;
1922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 return( ( mode == MBEDTLS_RSA_PUBLIC )
1924 ? mbedtls_rsa_public( ctx, sig, sig )
1925 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001926}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001930/*
1931 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1932 */
1933/*
1934 * Do an RSA operation to sign the message digest
1935 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001937 int (*f_rng)(void *, unsigned char *, size_t),
1938 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001939 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001941 unsigned int hashlen,
1942 const unsigned char *hash,
1943 unsigned char *sig )
1944{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001945 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001946 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001947 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001948 unsigned char *sig_try = NULL, *verif = NULL;
1949 size_t i;
1950 unsigned char diff;
1951 volatile unsigned char diff_no_optimize;
1952 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1955 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001956
1957 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001958 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001963 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1967 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001968
Paul Bakkerc70b9822013-04-07 22:00:46 +02001969 nb_pad -= 10 + oid_size;
1970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001972 }
1973
Paul Bakkerc70b9822013-04-07 22:00:46 +02001974 nb_pad -= hashlen;
1975
Paul Bakkerb3869132013-02-28 17:21:01 +01001976 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001978
1979 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001981 memset( p, 0xFF, nb_pad );
1982 p += nb_pad;
1983 *p++ = 0;
1984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001986 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001987 memcpy( p, hash, hashlen );
1988 }
1989 else
1990 {
1991 /*
1992 * DigestInfo ::= SEQUENCE {
1993 * digestAlgorithm DigestAlgorithmIdentifier,
1994 * digest Digest }
1995 *
1996 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1997 *
1998 * Digest ::= OCTET STRING
1999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002001 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002003 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002005 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002006 memcpy( p, oid, oid_size );
2007 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002009 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002011 *p++ = hashlen;
2012 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002013 }
2014
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002015 if( mode == MBEDTLS_RSA_PUBLIC )
2016 return( mbedtls_rsa_public( ctx, sig, sig ) );
2017
2018 /*
2019 * In order to prevent Lenstra's attack, make the signature in a
2020 * temporary buffer and check it before returning it.
2021 */
2022 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002023 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002024 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2025
Simon Butcher1285ab52016-01-01 21:42:47 +00002026 verif = mbedtls_calloc( 1, ctx->len );
2027 if( verif == NULL )
2028 {
2029 mbedtls_free( sig_try );
2030 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2031 }
2032
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002033 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2034 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2035
2036 /* Compare in constant time just in case */
2037 for( diff = 0, i = 0; i < ctx->len; i++ )
2038 diff |= verif[i] ^ sig[i];
2039 diff_no_optimize = diff;
2040
2041 if( diff_no_optimize != 0 )
2042 {
2043 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2044 goto cleanup;
2045 }
2046
2047 memcpy( sig, sig_try, ctx->len );
2048
2049cleanup:
2050 mbedtls_free( sig_try );
2051 mbedtls_free( verif );
2052
2053 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002054}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002056
2057/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 * Do an RSA operation to sign the message digest
2059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002061 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002062 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002065 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002066 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 unsigned char *sig )
2068{
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 switch( ctx->padding )
2070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#if defined(MBEDTLS_PKCS1_V15)
2072 case MBEDTLS_RSA_PKCS_V15:
2073 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002074 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002075#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#if defined(MBEDTLS_PKCS1_V21)
2078 case MBEDTLS_RSA_PKCS_V21:
2079 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002080 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002081#endif
2082
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002086}
2087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002089/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002090 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002093 int (*f_rng)(void *, unsigned char *, size_t),
2094 void *p_rng,
2095 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002097 unsigned int hashlen,
2098 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002100 int expected_salt_len,
2101 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002102{
Paul Bakker23986e52011-04-24 08:57:21 +00002103 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002104 size_t siglen;
2105 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002107 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002108 unsigned int hlen;
2109 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 const mbedtls_md_info_t *md_info;
2111 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002112 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2115 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002116
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 siglen = ctx->len;
2118
Paul Bakker27fdf462011-06-09 13:55:13 +00002119 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2123 ? mbedtls_rsa_public( ctx, sig, buf )
2124 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002125
2126 if( ret != 0 )
2127 return( ret );
2128
2129 p = buf;
2130
Paul Bakkerb3869132013-02-28 17:21:01 +01002131 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 {
Simon Butcher02037452016-03-01 21:19:12 +00002136 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002138 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002142 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002145 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002149 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002150
Paul Bakkerb3869132013-02-28 17:21:01 +01002151 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002152
Simon Butcher02037452016-03-01 21:19:12 +00002153 /*
2154 * Note: EMSA-PSS verification is over the length of N - 1 bits
2155 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002156 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002157
Simon Butcher02037452016-03-01 21:19:12 +00002158 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002159 if( msb % 8 == 0 )
2160 {
2161 p++;
2162 siglen -= 1;
2163 }
2164 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002168 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2169 {
2170 mbedtls_md_free( &md_ctx );
2171 return( ret );
2172 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002173
Paul Bakkerb3869132013-02-28 17:21:01 +01002174 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002175
Paul Bakkerb3869132013-02-28 17:21:01 +01002176 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002177
Paul Bakker4de44aa2013-12-31 11:43:01 +01002178 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002179 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002180
Paul Bakkerb3869132013-02-28 17:21:01 +01002181 if( p == buf + siglen ||
2182 *p++ != 0x01 )
2183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 mbedtls_md_free( &md_ctx );
2185 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002186 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002187
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002188 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002189 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002192 slen != (size_t) expected_salt_len )
2193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 mbedtls_md_free( &md_ctx );
2195 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002196 }
2197
Simon Butcher02037452016-03-01 21:19:12 +00002198 /*
2199 * Generate H = Hash( M' )
2200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 mbedtls_md_starts( &md_ctx );
2202 mbedtls_md_update( &md_ctx, zeros, 8 );
2203 mbedtls_md_update( &md_ctx, hash, hashlen );
2204 mbedtls_md_update( &md_ctx, p, slen );
2205 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002208
Paul Bakkerb3869132013-02-28 17:21:01 +01002209 if( memcmp( p + slen, result, hlen ) == 0 )
2210 return( 0 );
2211 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002213}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002214
2215/*
2216 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002219 int (*f_rng)(void *, unsigned char *, size_t),
2220 void *p_rng,
2221 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002223 unsigned int hashlen,
2224 const unsigned char *hash,
2225 const unsigned char *sig )
2226{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2228 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002229 : md_alg;
2230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002232 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002234 sig ) );
2235
2236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002240/*
2241 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002244 int (*f_rng)(void *, unsigned char *, size_t),
2245 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002246 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002248 unsigned int hashlen,
2249 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002250 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002251{
2252 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002253 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002254 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 mbedtls_md_type_t msg_md_alg;
2256 const mbedtls_md_info_t *md_info;
2257 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002258 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2261 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002262
2263 siglen = ctx->len;
2264
2265 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2269 ? mbedtls_rsa_public( ctx, sig, buf )
2270 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002271
2272 if( ret != 0 )
2273 return( ret );
2274
2275 p = buf;
2276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2278 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002279
2280 while( *p != 0 )
2281 {
2282 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002284 p++;
2285 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002286 p++; /* skip 00 byte */
2287
2288 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2289 if( p - buf < 11 )
2290 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002291
2292 len = siglen - ( p - buf );
2293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002295 {
2296 if( memcmp( p, hash, hashlen ) == 0 )
2297 return( 0 );
2298 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002300 }
2301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002303 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2305 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002306
2307 end = p + len;
2308
Simon Butcher02037452016-03-01 21:19:12 +00002309 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002310 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2311 * Insist on 2-byte length tags, to protect against variants of
2312 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002313 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002314 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2316 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2317 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002318 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002320
Gilles Peskinee7e76502017-05-04 12:48:39 +02002321 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2323 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2324 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002325 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002327
Gilles Peskinee7e76502017-05-04 12:48:39 +02002328 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2330 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002331 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002332 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002333
2334 oid.p = p;
2335 p += oid.len;
2336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2338 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002339
2340 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002342
2343 /*
2344 * assume the algorithm parameters must be NULL
2345 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002346 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2348 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002349 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002351
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002352 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002353 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2354 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002355 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002357
2358 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002360
2361 p += hashlen;
2362
2363 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002365
2366 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
2370/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002371 * Do an RSA operation and check the message digest
2372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002374 int (*f_rng)(void *, unsigned char *, size_t),
2375 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002376 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002378 unsigned int hashlen,
2379 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002380 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002381{
2382 switch( ctx->padding )
2383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384#if defined(MBEDTLS_PKCS1_V15)
2385 case MBEDTLS_RSA_PKCS_V15:
2386 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002387 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002388#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#if defined(MBEDTLS_PKCS1_V21)
2391 case MBEDTLS_RSA_PKCS_V21:
2392 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002393 hashlen, hash, sig );
2394#endif
2395
2396 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002398 }
2399}
2400
2401/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002402 * Copy the components of an RSA key
2403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002405{
2406 int ret;
2407
2408 dst->ver = src->ver;
2409 dst->len = src->len;
2410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2412 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2415 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2416 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002417
2418#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2420 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2421 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2423 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002424#endif
2425
2426 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2429 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002430
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002431 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002432 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002433
2434cleanup:
2435 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002437
2438 return( ret );
2439}
2440
2441/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 * Free the components of an RSA key
2443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002445{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002447 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2448 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002450
Hanno Becker33c30a02017-08-23 07:00:22 +01002451#if !defined(MBEDTLS_RSA_NO_CRT)
2452 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2453 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2454 mbedtls_mpi_free( &ctx->DP );
2455#endif /* MBEDTLS_RSA_NO_CRT */
2456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457#if defined(MBEDTLS_THREADING_C)
2458 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002459#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002460}
2461
Hanno Beckerab377312017-08-23 16:24:51 +01002462#endif /* !MBEDTLS_RSA_ALT */
2463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002466#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002467
2468/*
2469 * Example RSA-1024 keypair, for test purposes
2470 */
2471#define KEY_LEN 128
2472
2473#define RSA_N "9292758453063D803DD603D5E777D788" \
2474 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2475 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2476 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2477 "93A89813FBF3C4F8066D2D800F7C38A8" \
2478 "1AE31942917403FF4946B0A83D3D3E05" \
2479 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2480 "5E94BB77B07507233A0BC7BAC8F90F79"
2481
2482#define RSA_E "10001"
2483
2484#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2485 "66CA472BC44D253102F8B4A9D3BFA750" \
2486 "91386C0077937FE33FA3252D28855837" \
2487 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2488 "DF79C5CE07EE72C7F123142198164234" \
2489 "CABB724CF78B8173B9F880FC86322407" \
2490 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2491 "071513A1E85B5DFA031F21ECAE91A34D"
2492
2493#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2494 "2C01CAD19EA484A87EA4377637E75500" \
2495 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2496 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2497
2498#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2499 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2500 "910E4168387E3C30AA1E00C339A79508" \
2501 "8452DD96A9A5EA5D9DCA68DA636032AF"
2502
Paul Bakker5121ce52009-01-03 21:22:43 +00002503#define PT_LEN 24
2504#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2505 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002508static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002509{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002510#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002511 size_t i;
2512
Paul Bakker545570e2010-07-18 09:00:25 +00002513 if( rng_state != NULL )
2514 rng_state = NULL;
2515
Paul Bakkera3d195c2011-11-27 21:07:34 +00002516 for( i = 0; i < len; ++i )
2517 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002518#else
2519 if( rng_state != NULL )
2520 rng_state = NULL;
2521
2522 arc4random_buf( output, len );
2523#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002524
Paul Bakkera3d195c2011-11-27 21:07:34 +00002525 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002526}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002528
Paul Bakker5121ce52009-01-03 21:22:43 +00002529/*
2530 * Checkup routine
2531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002533{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002534 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002536 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 unsigned char rsa_plaintext[PT_LEN];
2539 unsigned char rsa_decrypted[PT_LEN];
2540 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002542 unsigned char sha1sum[20];
2543#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002544
Hanno Becker3a701162017-08-22 13:52:43 +01002545 mbedtls_mpi K;
2546
2547 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Hanno Becker3a701162017-08-22 13:52:43 +01002550 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2551 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2552 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2553 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2554 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2555 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2556 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2557 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2558 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2559 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2560
2561 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
2563 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2567 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 {
2569 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
2572 return( 1 );
2573 }
2574
2575 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
2578 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2579
Hanno Becker98838b02017-10-02 13:16:10 +01002580 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2581 PT_LEN, rsa_plaintext,
2582 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 {
2584 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
2587 return( 1 );
2588 }
2589
2590 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Hanno Becker98838b02017-10-02 13:16:10 +01002593 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2594 &len, rsa_ciphertext, rsa_decrypted,
2595 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 {
2597 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
2600 return( 1 );
2601 }
2602
2603 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2604 {
2605 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
2608 return( 1 );
2609 }
2610
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002611 if( verbose != 0 )
2612 mbedtls_printf( "passed\n" );
2613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002616 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Hanno Becker98838b02017-10-02 13:16:10 +01002620 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2621 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2622 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 {
2624 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
2627 return( 1 );
2628 }
2629
2630 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Hanno Becker98838b02017-10-02 13:16:10 +01002633 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2634 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2635 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 {
2637 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
2640 return( 1 );
2641 }
2642
2643 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002644 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002647 if( verbose != 0 )
2648 mbedtls_printf( "\n" );
2649
Paul Bakker3d8fb632014-04-17 12:42:41 +02002650cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002651 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 mbedtls_rsa_free( &rsa );
2653#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002654 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002656 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657}
2658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661#endif /* MBEDTLS_RSA_C */