blob: 031dc2c4357a461cb3696c36d0955ae4d9cc43d3 [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
Hanno Becker8ba6ce42017-10-03 14:36:26 +010081 * - mbedtls_rsa_deduce_private_exponent
Hanno Beckerd9431a72017-08-25 08:03:13 +010082 * - 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 Becker8ba6ce42017-10-03 14:36:26 +0100256int mbedtls_rsa_deduce_private_exponent( 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 {
Hanno Becker45a0ef32017-10-03 14:32:56 +0100328 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
329 goto cleanup;
Hanno Beckerd3637992017-08-25 07:55:03 +0100330 }
331 }
332
Hanno Becker98838b02017-10-02 13:16:10 +0100333 /* Check that DQ - D == 0 mod Q - 1 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100334 if( DQ != NULL )
335 {
336 if( Q == NULL )
337 {
338 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
339 goto cleanup;
340 }
341
342 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
343 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
344 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
345
346 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
347 {
Hanno Becker45a0ef32017-10-03 14:32:56 +0100348 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
349 goto cleanup;
Hanno Beckerd3637992017-08-25 07:55:03 +0100350 }
351 }
352
Hanno Becker98838b02017-10-02 13:16:10 +0100353 /* Check that QP * Q - 1 == 0 mod P */
Hanno Beckerd3637992017-08-25 07:55:03 +0100354 if( QP != NULL )
355 {
356 if( P == NULL || Q == NULL )
357 {
358 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
359 goto cleanup;
360 }
361
362 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
363 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
364 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
365 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
366 {
Hanno Becker45a0ef32017-10-03 14:32:56 +0100367 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
368 goto cleanup;
Hanno Beckerd3637992017-08-25 07:55:03 +0100369 }
370 }
371
372cleanup:
373
374 /* Wrap MPI error codes by RSA check failure error code */
375 if( ret != 0 &&
376 ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
377 ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
378 {
379 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
380 }
381
382 mbedtls_mpi_free( &K );
383 mbedtls_mpi_free( &L );
384
385 return( ret );
386}
387
388/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100389 * Check that core RSA parameters are sane.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100390 */
391
Hanno Becker750e8b42017-08-25 07:54:27 +0100392int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
393 const mbedtls_mpi *Q, const mbedtls_mpi *D,
394 const mbedtls_mpi *E,
395 int (*f_rng)(void *, unsigned char *, size_t),
396 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100397{
398 int ret = 0;
Hanno Becker750e8b42017-08-25 07:54:27 +0100399 mbedtls_mpi K, L;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100400
401 mbedtls_mpi_init( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100402 mbedtls_mpi_init( &L );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100403
404 /*
405 * Step 1: If PRNG provided, check that P and Q are prime
406 */
407
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100408#if defined(MBEDTLS_GENPRIME)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100409 if( f_rng != NULL && P != NULL &&
410 ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
411 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100412 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100413 goto cleanup;
414 }
415
416 if( f_rng != NULL && Q != NULL &&
417 ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
418 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100419 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100420 goto cleanup;
421 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100422#else
423 ((void) f_rng);
424 ((void) p_rng);
425#endif /* MBEDTLS_GENPRIME */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100426
427 /*
Hanno Beckerb5beaa82017-10-02 13:01:43 +0100428 * Step 2: Check that 1 < N = PQ
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100429 */
430
431 if( P != NULL && Q != NULL && N != NULL )
432 {
433 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
Hanno Beckerb5beaa82017-10-02 13:01:43 +0100434 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
Hanno Becker750e8b42017-08-25 07:54:27 +0100435 mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100436 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100437 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100438 goto cleanup;
439 }
440 }
441
442 /*
Hanno Beckerb5beaa82017-10-02 13:01:43 +0100443 * Step 3: Check and 1 < D, E < N if present.
444 */
445
446 if( N != NULL && D != NULL && E != NULL )
447 {
448 if ( mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
449 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
450 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
451 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
452 {
453 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
454 goto cleanup;
455 }
456 }
457
458 /*
459 * Step 4: Check that D, E are inverse modulo P-1 and Q-1
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100460 */
461
462 if( P != NULL && Q != NULL && D != NULL && E != NULL )
463 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100464 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
Hanno Beckerb5beaa82017-10-02 13:01:43 +0100465 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 )
Hanno Becker750e8b42017-08-25 07:54:27 +0100466 {
467 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
468 goto cleanup;
469 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100470
471 /* Compute DE-1 mod P-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100472 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
473 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
474 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
475 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100476 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
477 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100478 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100479 goto cleanup;
480 }
481
482 /* Compute DE-1 mod Q-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100483 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
484 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
485 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
486 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100487 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
488 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100489 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100490 goto cleanup;
491 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100492 }
493
494cleanup:
495
496 mbedtls_mpi_free( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100497 mbedtls_mpi_free( &L );
498
499 /* Wrap MPI error codes by RSA check failure error code */
500 if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
501 {
502 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
503 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100504
505 return( ret );
506}
507
508int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
509 const mbedtls_mpi *D, mbedtls_mpi *DP,
510 mbedtls_mpi *DQ, mbedtls_mpi *QP )
511{
512 int ret = 0;
513 mbedtls_mpi K;
514 mbedtls_mpi_init( &K );
515
Hanno Beckerd9431a72017-08-25 08:03:13 +0100516 /* DP = D mod P-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100517 if( DP != NULL )
518 {
519 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
520 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
521 }
522
Hanno Beckerd9431a72017-08-25 08:03:13 +0100523 /* DQ = D mod Q-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100524 if( DQ != NULL )
525 {
526 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
527 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
528 }
529
Hanno Beckerd9431a72017-08-25 08:03:13 +0100530 /* QP = Q^{-1} mod P */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100531 if( QP != NULL )
532 {
533 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
534 }
535
536cleanup:
537 mbedtls_mpi_free( &K );
538
539 return( ret );
540}
541
Hanno Becker617c1ae2017-08-23 14:11:24 +0100542
543/*
544 * Default RSA interface implementation
545 */
546
Hanno Beckerab377312017-08-23 16:24:51 +0100547#if !defined(MBEDTLS_RSA_ALT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100548
549int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
550 const mbedtls_mpi *N,
551 const mbedtls_mpi *P, const mbedtls_mpi *Q,
552 const mbedtls_mpi *D, const mbedtls_mpi *E )
553{
554 int ret;
555
556 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
557 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
558 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
559 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
560 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
561 {
562 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
563 }
564
565 if( N != NULL )
566 ctx->len = mbedtls_mpi_size( &ctx->N );
567
568 return( 0 );
569}
570
571int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100572 unsigned char const *N, size_t N_len,
573 unsigned char const *P, size_t P_len,
574 unsigned char const *Q, size_t Q_len,
575 unsigned char const *D, size_t D_len,
576 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100577{
578 int ret;
579
580 if( N != NULL )
581 {
582 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
583 ctx->len = mbedtls_mpi_size( &ctx->N );
584 }
585
586 if( P != NULL )
587 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
588
589 if( Q != NULL )
590 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
591
592 if( D != NULL )
593 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
594
595 if( E != NULL )
596 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
597
598cleanup:
599
600 if( ret != 0 )
601 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
602
603 return( 0 );
604}
605
606int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
607 int (*f_rng)(void *, unsigned char *, size_t),
608 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100609{
610 int ret = 0;
611
Hanno Becker617c1ae2017-08-23 14:11:24 +0100612 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
613 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
614 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
615 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
616 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100617
Hanno Becker617c1ae2017-08-23 14:11:24 +0100618 /*
619 * Check whether provided parameters are enough
620 * to deduce all others. The following incomplete
621 * parameter sets for private keys are supported:
622 *
623 * (1) P, Q missing.
624 * (2) D and potentially N missing.
625 *
626 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100627
Hanno Becker2cca6f32017-09-29 11:46:40 +0100628 const int n_missing = have_P && have_Q && have_D && have_E;
629 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
630 const int d_missing = have_P && have_Q && !have_D && have_E;
631 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
632
633 /* These three alternatives are mutually exclusive */
634 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100635
Hanno Becker617c1ae2017-08-23 14:11:24 +0100636 if( !is_priv && !is_pub )
637 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
638
639 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100640 * Step 1: Deduce N if P, Q are provided.
641 */
642
643 if( !have_N && have_P && have_Q )
644 {
645 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
646 &ctx->Q ) ) != 0 )
647 {
648 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
649 }
650
651 ctx->len = mbedtls_mpi_size( &ctx->N );
652 }
653
654 /*
655 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100656 */
657
658 if( pq_missing )
659 {
660 /* This includes sanity checking of core parameters,
661 * so no further checks necessary. */
662 ret = mbedtls_rsa_deduce_moduli( &ctx->N, &ctx->D, &ctx->E,
663 f_rng, p_rng,
664 &ctx->P, &ctx->Q );
665 if( ret != 0 )
666 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
667
668 }
669 else if( d_missing )
670 {
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100671#if defined(MBEDTLS_GENPRIME)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100672 /* If a PRNG is provided, check if P, Q are prime. */
673 if( f_rng != NULL &&
674 ( ( ret = mbedtls_mpi_is_prime( &ctx->P, f_rng, p_rng ) ) != 0 ||
675 ( ret = mbedtls_mpi_is_prime( &ctx->Q, f_rng, p_rng ) ) != 0 ) )
676 {
677 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
678 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100679#endif /* MBEDTLS_GENPRIME */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100680
Hanno Becker617c1ae2017-08-23 14:11:24 +0100681 /* Deduce private exponent. This includes double-checking of the result,
682 * so together with the primality test above all core parameters are
683 * guaranteed to be sane if this call succeeds. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100684 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
685 &ctx->Q,
686 &ctx->E,
687 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100688 {
689 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
690 }
691 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100692
693 /* In the remaining case of a public key, there's nothing to check for. */
694
695 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100696 * Step 3: Deduce all additional parameters specific
Hanno Becker617c1ae2017-08-23 14:11:24 +0100697 * to our current RSA implementaiton.
698 */
699
Hanno Becker23344b52017-08-23 07:43:27 +0100700#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100701 if( is_priv )
702 {
703 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
704 &ctx->DP, &ctx->DQ, &ctx->QP );
705 if( ret != 0 )
706 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
707 }
Hanno Becker23344b52017-08-23 07:43:27 +0100708#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100709
710 /*
Hanno Becker98838b02017-10-02 13:16:10 +0100711 * Step 3: Basic sanity check
Hanno Becker617c1ae2017-08-23 14:11:24 +0100712 */
713
714 if( is_priv )
715 {
716 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
717 return( ret );
718 }
719 else
720 {
721 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
722 return( ret );
723 }
724
725 return( 0 );
726}
727
Hanno Becker617c1ae2017-08-23 14:11:24 +0100728int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
729 unsigned char *N, size_t N_len,
730 unsigned char *P, size_t P_len,
731 unsigned char *Q, size_t Q_len,
732 unsigned char *D, size_t D_len,
733 unsigned char *E, size_t E_len )
734{
735 int ret = 0;
736
737 /* Check if key is private or public */
738 const int is_priv =
739 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
740 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
741 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
742 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
743 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
744
745 if( !is_priv )
746 {
747 /* If we're trying to export private parameters for a public key,
748 * something must be wrong. */
749 if( P != NULL || Q != NULL || D != NULL )
750 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
751
752 }
753
754 if( N != NULL )
755 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
756
757 if( P != NULL )
758 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
759
760 if( Q != NULL )
761 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
762
763 if( D != NULL )
764 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
765
766 if( E != NULL )
767 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100768
769cleanup:
770
771 return( ret );
772}
773
Hanno Becker617c1ae2017-08-23 14:11:24 +0100774int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
775 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
776 mbedtls_mpi *D, mbedtls_mpi *E )
777{
778 int ret;
779
780 /* Check if key is private or public */
781 int is_priv =
782 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
783 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
784 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
785 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
786 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
787
788 if( !is_priv )
789 {
790 /* If we're trying to export private parameters for a public key,
791 * something must be wrong. */
792 if( P != NULL || Q != NULL || D != NULL )
793 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
794
795 }
796
797 /* Export all requested core parameters. */
798
799 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
800 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
801 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
802 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
803 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
804 {
805 return( ret );
806 }
807
808 return( 0 );
809}
810
811/*
812 * Export CRT parameters
813 * This must also be implemented if CRT is not used, for being able to
814 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
815 * can be used in this case.
816 */
817int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
818 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
819{
820 int ret;
821
822 /* Check if key is private or public */
823 int is_priv =
824 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
825 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
826 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
827 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
828 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
829
830 if( !is_priv )
831 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
832
Hanno Beckerdc95c892017-08-23 06:57:02 +0100833#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100834 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100835 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
836 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
837 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
838 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100839 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100840 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100841#else
842 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
843 DP, DQ, QP ) ) != 0 )
844 {
845 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
846 }
847#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100848
849 return( 0 );
850}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100851
852/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000853 * Initialize an RSA context
854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000857 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000858{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#if defined(MBEDTLS_THREADING_C)
864 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200865#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000866}
867
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100868/*
869 * Set padding for an existing RSA context
870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100872{
873 ctx->padding = padding;
874 ctx->hash_id = hash_id;
875}
876
Hanno Becker617c1ae2017-08-23 14:11:24 +0100877/*
878 * Get length in bytes of RSA modulus
879 */
880
881size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
882{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100883 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100884}
885
886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000888
889/*
890 * Generate an RSA keypair
891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000893 int (*f_rng)(void *, unsigned char *, size_t),
894 void *p_rng,
895 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000896{
897 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100898 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
Paul Bakker21eb2802010-08-16 11:10:02 +0000900 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Janos Follathef441782016-09-21 13:18:12 +0100903 if( nbits % 2 )
904 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
905
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100906 mbedtls_mpi_init( &H );
907 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
909 /*
910 * find primes P and Q with Q < P so that:
911 * GCD( E, (P-1)*(Q-1) ) == 1
912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
915 do
916 {
Janos Follath10c575b2016-02-23 14:42:48 +0000917 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100918 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
Janos Follathef441782016-09-21 13:18:12 +0100920 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100921 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000924 continue;
925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200927 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 continue;
929
Janos Follathef441782016-09-21 13:18:12 +0100930 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100931 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100932
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100933 /* Temporarily replace P,Q by P-1, Q-1 */
934 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
935 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
936 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100941 /* Restore P,Q */
942 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
943 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
944
945 ctx->len = mbedtls_mpi_size( &ctx->N );
946
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 /*
948 * D = E^-1 mod ((P-1)*(Q-1))
949 * DP = D mod (P - 1)
950 * DQ = D mod (Q - 1)
951 * QP = Q^-1 mod P
952 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100954 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
955
956#if !defined(MBEDTLS_RSA_NO_CRT)
957 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
958 &ctx->DP, &ctx->DQ, &ctx->QP ) );
959#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
Hanno Becker83aad1f2017-08-23 06:45:10 +0100961 /* Double-check */
962 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
963
Paul Bakker5121ce52009-01-03 21:22:43 +0000964cleanup:
965
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100966 mbedtls_mpi_free( &H );
967 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
969 if( ret != 0 )
970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_rsa_free( ctx );
972 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 }
974
Paul Bakker48377d92013-08-30 12:06:24 +0200975 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000976}
977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
980/*
981 * Check a public RSA key
982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000984{
Hanno Becker98838b02017-10-02 13:16:10 +0100985 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
986 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100989 }
Paul Bakker37940d9f2009-07-10 22:38:58 +0000990
Hanno Beckerba1ba112017-09-29 11:48:23 +0100991 if( ctx->len != mbedtls_mpi_size( &ctx->N ) )
992 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
993
Hanno Becker98838b02017-10-02 13:16:10 +0100994 if( mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 ||
995 mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 )
996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100998 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000999
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001000 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
1001 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Hanno Becker98838b02017-10-02 13:16:10 +01001002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +01001004 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001005
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001006 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +01001008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +01001010 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001011
1012 return( 0 );
1013}
1014
1015/*
1016 * Check a private RSA key
1017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001019{
Hanno Becker98838b02017-10-02 13:16:10 +01001020 if( mbedtls_rsa_check_pubkey( ctx ) != 0 )
1021 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1022
1023 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +01001024 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001025 {
Hanno Beckerb269a852017-08-25 08:03:21 +01001026 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 }
Hanno Beckerb269a852017-08-25 08:03:21 +01001028#if !defined(MBEDTLS_RSA_NO_CRT)
1029 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
1030 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
1031 {
1032 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1033 }
1034#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001035
1036 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001037}
1038
1039/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001040 * Check if contexts holding a public and private key match
1041 */
Hanno Becker98838b02017-10-02 13:16:10 +01001042int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
1043 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001044{
Hanno Becker98838b02017-10-02 13:16:10 +01001045 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001049 }
1050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1052 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001055 }
1056
1057 return( 0 );
1058}
1059
1060/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001061 * Do an RSA public key operation
1062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001064 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 unsigned char *output )
1066{
Paul Bakker23986e52011-04-24 08:57:21 +00001067 int ret;
1068 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001073#if defined(MBEDTLS_THREADING_C)
1074 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1075 return( ret );
1076#endif
1077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001082 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1083 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 }
1085
1086 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1088 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089
1090cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001092 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1093 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001094#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
1098 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001100
1101 return( 0 );
1102}
1103
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001104/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001105 * Generate or update blinding values, see section 10 of:
1106 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001107 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001108 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001109 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001110static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001111 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1112{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001113 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001114
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001115 if( ctx->Vf.p != NULL )
1116 {
1117 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1119 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1120 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1121 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001122
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001123 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001124 }
1125
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001126 /* Unblinding value: Vf = random number, invertible mod N */
1127 do {
1128 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1132 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1133 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001134
1135 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1137 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 +02001138
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001139
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001140cleanup:
1141 return( ret );
1142}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001143
Paul Bakker5121ce52009-01-03 21:22:43 +00001144/*
Janos Follathe81102e2017-03-22 13:38:28 +00001145 * Exponent blinding supposed to prevent side-channel attacks using multiple
1146 * traces of measurements to recover the RSA key. The more collisions are there,
1147 * the more bits of the key can be recovered. See [3].
1148 *
1149 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1150 * observations on avarage.
1151 *
1152 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1153 * to make 2^112 observations on avarage.
1154 *
1155 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1156 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1157 * Thus in this sense with 28 byte blinding the security is not reduced by
1158 * side-channel attacks like the one in [3])
1159 *
1160 * This countermeasure does not help if the key recovery is possible with a
1161 * single trace.
1162 */
1163#define RSA_EXPONENT_BLINDING 28
1164
1165/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 * Do an RSA private key operation
1167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001169 int (*f_rng)(void *, unsigned char *, size_t),
1170 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001171 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 unsigned char *output )
1173{
Paul Bakker23986e52011-04-24 08:57:21 +00001174 int ret;
1175 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001177 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001178#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001179 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001180 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001181#else
1182 mbedtls_mpi DP_blind, DQ_blind;
1183 mbedtls_mpi *DP = &ctx->DP;
1184 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001185#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001186
Hanno Becker45037ce2017-08-25 11:03:34 +01001187 /* Sanity-check that all relevant fields are at least set,
1188 * but don't perform a full keycheck. */
1189 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
1190 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
1191 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
1192 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
1193 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
1194 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001195 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker45037ce2017-08-25 11:03:34 +01001196 }
1197#if !defined(MBEDTLS_RSA_NO_CRT)
1198 if( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
1199 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
1200 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
1201 {
1202 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1203 }
1204#endif /* MBEDTLS_RSA_NO_CRT */
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001207 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1208
Janos Follathf9203b42017-03-22 15:13:15 +00001209 if( f_rng != NULL )
1210 {
Janos Follathe81102e2017-03-22 13:38:28 +00001211#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001212 mbedtls_mpi_init( &D_blind );
1213#else
1214 mbedtls_mpi_init( &DP_blind );
1215 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001216#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001217 }
Janos Follathe81102e2017-03-22 13:38:28 +00001218
Paul Bakker5121ce52009-01-03 21:22:43 +00001219
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001220#if defined(MBEDTLS_THREADING_C)
1221 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1222 return( ret );
1223#endif
1224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1226 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001228 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1229 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 }
1231
Paul Bakkerf451bac2013-08-30 15:37:02 +02001232 if( f_rng != NULL )
1233 {
1234 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001235 * Blinding
1236 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001237 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001238 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1239 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001241
Janos Follathe81102e2017-03-22 13:38:28 +00001242 /*
1243 * Exponent blinding
1244 */
1245 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1246 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1247
Janos Follathf9203b42017-03-22 15:13:15 +00001248#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001249 /*
1250 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1251 */
1252 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1253 f_rng, p_rng ) );
1254 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1255 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1256 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1257
1258 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001259#else
1260 /*
1261 * DP_blind = ( P - 1 ) * R + DP
1262 */
1263 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1264 f_rng, p_rng ) );
1265 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1266 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1267 &ctx->DP ) );
1268
1269 DP = &DP_blind;
1270
1271 /*
1272 * DQ_blind = ( Q - 1 ) * R + DQ
1273 */
1274 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1275 f_rng, p_rng ) );
1276 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1277 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1278 &ctx->DQ ) );
1279
1280 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001281#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001282 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001285 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001286#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001287 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001288 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 *
1290 * T1 = input ^ dP mod P
1291 * T2 = input ^ dQ mod Q
1292 */
Janos Follathf9203b42017-03-22 15:13:15 +00001293 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1294 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001295
1296 /*
1297 * T = (T1 - T2) * (Q^-1 mod P) mod P
1298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1300 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1301 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001302
1303 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001304 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1307 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1308#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001309
Paul Bakkerf451bac2013-08-30 15:37:02 +02001310 if( f_rng != NULL )
1311 {
1312 /*
1313 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001314 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001315 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001316 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001318 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001319
1320 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
1323cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001325 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1326 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001327#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001330 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1331
1332 if( f_rng != NULL )
1333 {
Janos Follathe81102e2017-03-22 13:38:28 +00001334#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001335 mbedtls_mpi_free( &D_blind );
1336#else
1337 mbedtls_mpi_free( &DP_blind );
1338 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001339#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001340 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
1342 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
1345 return( 0 );
1346}
1347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001349/**
1350 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1351 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001352 * \param dst buffer to mask
1353 * \param dlen length of destination buffer
1354 * \param src source of the mask generation
1355 * \param slen length of the source buffer
1356 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001357 */
Paul Bakker48377d92013-08-30 12:06:24 +02001358static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001362 unsigned char counter[4];
1363 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001364 unsigned int hlen;
1365 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001368 memset( counter, 0, 4 );
1369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001371
Simon Butcher02037452016-03-01 21:19:12 +00001372 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001373 p = dst;
1374
1375 while( dlen > 0 )
1376 {
1377 use_len = hlen;
1378 if( dlen < hlen )
1379 use_len = dlen;
1380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 mbedtls_md_starts( md_ctx );
1382 mbedtls_md_update( md_ctx, src, slen );
1383 mbedtls_md_update( md_ctx, counter, 4 );
1384 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001385
1386 for( i = 0; i < use_len; ++i )
1387 *p++ ^= mask[i];
1388
1389 counter[3]++;
1390
1391 dlen -= use_len;
1392 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001393
1394 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001395}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001399/*
1400 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001403 int (*f_rng)(void *, unsigned char *, size_t),
1404 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001405 int mode,
1406 const unsigned char *label, size_t label_len,
1407 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001408 const unsigned char *input,
1409 unsigned char *output )
1410{
1411 size_t olen;
1412 int ret;
1413 unsigned char *p = output;
1414 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 const mbedtls_md_info_t *md_info;
1416 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1419 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001420
1421 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001425 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001427
1428 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001430
Simon Butcher02037452016-03-01 21:19:12 +00001431 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001432 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001434
1435 memset( output, 0, olen );
1436
1437 *p++ = 0;
1438
Simon Butcher02037452016-03-01 21:19:12 +00001439 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001440 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001442
1443 p += hlen;
1444
Simon Butcher02037452016-03-01 21:19:12 +00001445 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001447 p += hlen;
1448 p += olen - 2 * hlen - 2 - ilen;
1449 *p++ = 1;
1450 memcpy( p, input, ilen );
1451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001453 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1454 {
1455 mbedtls_md_free( &md_ctx );
1456 return( ret );
1457 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001458
Simon Butcher02037452016-03-01 21:19:12 +00001459 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001460 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1461 &md_ctx );
1462
Simon Butcher02037452016-03-01 21:19:12 +00001463 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001464 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1465 &md_ctx );
1466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 return( ( mode == MBEDTLS_RSA_PUBLIC )
1470 ? mbedtls_rsa_public( ctx, output, output )
1471 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001472}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001476/*
1477 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001480 int (*f_rng)(void *, unsigned char *, size_t),
1481 void *p_rng,
1482 int mode, size_t ilen,
1483 const unsigned char *input,
1484 unsigned char *output )
1485{
1486 size_t nb_pad, olen;
1487 int ret;
1488 unsigned char *p = output;
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1491 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001492
Janos Follath1ed9f992016-03-18 11:45:44 +00001493 // We don't check p_rng because it won't be dereferenced here
1494 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001496
1497 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001498
Simon Butcher02037452016-03-01 21:19:12 +00001499 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001500 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001502
1503 nb_pad = olen - 3 - ilen;
1504
1505 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001509
1510 while( nb_pad-- > 0 )
1511 {
1512 int rng_dl = 100;
1513
1514 do {
1515 ret = f_rng( p_rng, p, 1 );
1516 } while( *p == 0 && --rng_dl && ret == 0 );
1517
Simon Butcher02037452016-03-01 21:19:12 +00001518 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001519 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001521
1522 p++;
1523 }
1524 }
1525 else
1526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001528
1529 while( nb_pad-- > 0 )
1530 *p++ = 0xFF;
1531 }
1532
1533 *p++ = 0;
1534 memcpy( p, input, ilen );
1535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 return( ( mode == MBEDTLS_RSA_PUBLIC )
1537 ? mbedtls_rsa_public( ctx, output, output )
1538 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001541
Paul Bakker5121ce52009-01-03 21:22:43 +00001542/*
1543 * Add the message padding, then do an RSA operation
1544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001546 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001547 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001548 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001549 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001550 unsigned char *output )
1551{
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 switch( ctx->padding )
1553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_PKCS1_V15)
1555 case MBEDTLS_RSA_PKCS_V15:
1556 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001557 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001558#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560#if defined(MBEDTLS_PKCS1_V21)
1561 case MBEDTLS_RSA_PKCS_V21:
1562 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001563 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001564#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001565
1566 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001569}
1570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001572/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001573 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001576 int (*f_rng)(void *, unsigned char *, size_t),
1577 void *p_rng,
1578 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001579 const unsigned char *label, size_t label_len,
1580 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001581 const unsigned char *input,
1582 unsigned char *output,
1583 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001584{
Paul Bakker23986e52011-04-24 08:57:21 +00001585 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001586 size_t ilen, i, pad_len;
1587 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1589 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001590 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 const mbedtls_md_info_t *md_info;
1592 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001593
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001594 /*
1595 * Parameters sanity checks
1596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1598 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001599
1600 ilen = ctx->len;
1601
Paul Bakker27fdf462011-06-09 13:55:13 +00001602 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001606 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001608
Janos Follathc17cda12016-02-11 11:08:18 +00001609 hlen = mbedtls_md_get_size( md_info );
1610
1611 // checking for integer underflow
1612 if( 2 * hlen + 2 > ilen )
1613 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1614
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001615 /*
1616 * RSA operation
1617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1619 ? mbedtls_rsa_public( ctx, input, buf )
1620 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001621
1622 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001623 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001624
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001625 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001626 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001629 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1630 {
1631 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001632 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001633 }
1634
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001635
1636 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001638
1639 /* seed: Apply seedMask to maskedSeed */
1640 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1641 &md_ctx );
1642
1643 /* DB: Apply dbMask to maskedDB */
1644 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1645 &md_ctx );
1646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001648
1649 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001650 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001651 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001652 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001653 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001654
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001655 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001656
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001657 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001658
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001659 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001660 for( i = 0; i < hlen; i++ )
1661 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001662
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001663 /* Get zero-padding len, but always read till end of buffer
1664 * (minus one, for the 01 byte) */
1665 pad_len = 0;
1666 pad_done = 0;
1667 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1668 {
1669 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001670 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001671 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001672
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001673 p += pad_len;
1674 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001675
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001676 /*
1677 * The only information "leaked" is whether the padding was correct or not
1678 * (eg, no data is copied if it was not correct). This meets the
1679 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1680 * the different error conditions.
1681 */
1682 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001683 {
1684 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1685 goto cleanup;
1686 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001687
Paul Bakker66d5d072014-06-17 16:39:18 +02001688 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001689 {
1690 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1691 goto cleanup;
1692 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001693
1694 *olen = ilen - (p - buf);
1695 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001696 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001697
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001698cleanup:
1699 mbedtls_zeroize( buf, sizeof( buf ) );
1700 mbedtls_zeroize( lhash, sizeof( lhash ) );
1701
1702 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001707/*
1708 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001711 int (*f_rng)(void *, unsigned char *, size_t),
1712 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001713 int mode, size_t *olen,
1714 const unsigned char *input,
1715 unsigned char *output,
1716 size_t output_max_len)
1717{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001718 int ret;
1719 size_t ilen, pad_count = 0, i;
1720 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1724 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001725
1726 ilen = ctx->len;
1727
1728 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1732 ? mbedtls_rsa_public( ctx, input, buf )
1733 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001734
1735 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001736 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001737
1738 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001739 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001740
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001741 /*
1742 * Check and get padding len in "constant-time"
1743 */
1744 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001745
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001746 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001751 /* Get padding len, but always read till end of buffer
1752 * (minus one, for the 00 byte) */
1753 for( i = 0; i < ilen - 3; i++ )
1754 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001755 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1756 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001757 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001758
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001759 p += pad_count;
1760 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001761 }
1762 else
1763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001765
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001766 /* Get padding len, but always read till end of buffer
1767 * (minus one, for the 00 byte) */
1768 for( i = 0; i < ilen - 3; i++ )
1769 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001770 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001771 pad_count += ( pad_done == 0 );
1772 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001773
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001774 p += pad_count;
1775 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 }
1777
Janos Follathc69fa502016-02-12 13:30:09 +00001778 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001779
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001780 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001781 {
1782 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1783 goto cleanup;
1784 }
Paul Bakker8804f692013-02-28 18:06:26 +01001785
Paul Bakker66d5d072014-06-17 16:39:18 +02001786 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001787 {
1788 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1789 goto cleanup;
1790 }
Paul Bakker060c5682009-01-12 21:48:39 +00001791
Paul Bakker27fdf462011-06-09 13:55:13 +00001792 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001794 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001796cleanup:
1797 mbedtls_zeroize( buf, sizeof( buf ) );
1798
1799 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001800}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001802
1803/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001804 * Do an RSA operation, then remove the message padding
1805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001807 int (*f_rng)(void *, unsigned char *, size_t),
1808 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001809 int mode, size_t *olen,
1810 const unsigned char *input,
1811 unsigned char *output,
1812 size_t output_max_len)
1813{
1814 switch( ctx->padding )
1815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#if defined(MBEDTLS_PKCS1_V15)
1817 case MBEDTLS_RSA_PKCS_V15:
1818 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001819 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001820#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#if defined(MBEDTLS_PKCS1_V21)
1823 case MBEDTLS_RSA_PKCS_V21:
1824 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001825 olen, input, output,
1826 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001827#endif
1828
1829 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001831 }
1832}
1833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001835/*
1836 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001839 int (*f_rng)(void *, unsigned char *, size_t),
1840 void *p_rng,
1841 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001843 unsigned int hashlen,
1844 const unsigned char *hash,
1845 unsigned char *sig )
1846{
1847 size_t olen;
1848 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001850 unsigned int slen, hlen, offset = 0;
1851 int ret;
1852 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 const mbedtls_md_info_t *md_info;
1854 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1857 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001858
1859 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001861
1862 olen = ctx->len;
1863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001865 {
Simon Butcher02037452016-03-01 21:19:12 +00001866 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001868 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001872 }
1873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001875 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001879 slen = hlen;
1880
1881 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001883
1884 memset( sig, 0, olen );
1885
Simon Butcher02037452016-03-01 21:19:12 +00001886 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001887 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001889
Simon Butcher02037452016-03-01 21:19:12 +00001890 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001891 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001892 p += olen - hlen * 2 - 2;
1893 *p++ = 0x01;
1894 memcpy( p, salt, slen );
1895 p += slen;
1896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001898 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1899 {
1900 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001901 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001902 return( ret );
1903 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001904
Simon Butcher02037452016-03-01 21:19:12 +00001905 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 mbedtls_md_starts( &md_ctx );
1907 mbedtls_md_update( &md_ctx, p, 8 );
1908 mbedtls_md_update( &md_ctx, hash, hashlen );
1909 mbedtls_md_update( &md_ctx, salt, slen );
1910 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001911 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001912
Simon Butcher02037452016-03-01 21:19:12 +00001913 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001914 if( msb % 8 == 0 )
1915 offset = 1;
1916
Simon Butcher02037452016-03-01 21:19:12 +00001917 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001918 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001921
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001922 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001923 sig[0] &= 0xFF >> ( olen * 8 - msb );
1924
1925 p += hlen;
1926 *p++ = 0xBC;
1927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 return( ( mode == MBEDTLS_RSA_PUBLIC )
1929 ? mbedtls_rsa_public( ctx, sig, sig )
1930 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001931}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001935/*
1936 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1937 */
1938/*
1939 * Do an RSA operation to sign the message digest
1940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001942 int (*f_rng)(void *, unsigned char *, size_t),
1943 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001944 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001946 unsigned int hashlen,
1947 const unsigned char *hash,
1948 unsigned char *sig )
1949{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001950 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001951 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001952 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001953 unsigned char *sig_try = NULL, *verif = NULL;
1954 size_t i;
1955 unsigned char diff;
1956 volatile unsigned char diff_no_optimize;
1957 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1960 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001961
1962 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001963 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001968 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1972 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001973
Paul Bakkerc70b9822013-04-07 22:00:46 +02001974 nb_pad -= 10 + oid_size;
1975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001977 }
1978
Paul Bakkerc70b9822013-04-07 22:00:46 +02001979 nb_pad -= hashlen;
1980
Paul Bakkerb3869132013-02-28 17:21:01 +01001981 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001983
1984 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001986 memset( p, 0xFF, nb_pad );
1987 p += nb_pad;
1988 *p++ = 0;
1989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001991 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001992 memcpy( p, hash, hashlen );
1993 }
1994 else
1995 {
1996 /*
1997 * DigestInfo ::= SEQUENCE {
1998 * digestAlgorithm DigestAlgorithmIdentifier,
1999 * digest Digest }
2000 *
2001 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2002 *
2003 * Digest ::= OCTET STRING
2004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002006 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002008 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002010 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002011 memcpy( p, oid, oid_size );
2012 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002014 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002016 *p++ = hashlen;
2017 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002018 }
2019
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002020 if( mode == MBEDTLS_RSA_PUBLIC )
2021 return( mbedtls_rsa_public( ctx, sig, sig ) );
2022
2023 /*
2024 * In order to prevent Lenstra's attack, make the signature in a
2025 * temporary buffer and check it before returning it.
2026 */
2027 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002028 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002029 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2030
Simon Butcher1285ab52016-01-01 21:42:47 +00002031 verif = mbedtls_calloc( 1, ctx->len );
2032 if( verif == NULL )
2033 {
2034 mbedtls_free( sig_try );
2035 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2036 }
2037
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002038 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2039 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2040
2041 /* Compare in constant time just in case */
2042 for( diff = 0, i = 0; i < ctx->len; i++ )
2043 diff |= verif[i] ^ sig[i];
2044 diff_no_optimize = diff;
2045
2046 if( diff_no_optimize != 0 )
2047 {
2048 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2049 goto cleanup;
2050 }
2051
2052 memcpy( sig, sig_try, ctx->len );
2053
2054cleanup:
2055 mbedtls_free( sig_try );
2056 mbedtls_free( verif );
2057
2058 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002061
2062/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 * Do an RSA operation to sign the message digest
2064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002066 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002067 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002070 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002071 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 unsigned char *sig )
2073{
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 switch( ctx->padding )
2075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#if defined(MBEDTLS_PKCS1_V15)
2077 case MBEDTLS_RSA_PKCS_V15:
2078 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002079 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002080#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#if defined(MBEDTLS_PKCS1_V21)
2083 case MBEDTLS_RSA_PKCS_V21:
2084 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002085 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002086#endif
2087
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002091}
2092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002094/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002095 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002098 int (*f_rng)(void *, unsigned char *, size_t),
2099 void *p_rng,
2100 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002102 unsigned int hashlen,
2103 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002105 int expected_salt_len,
2106 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002107{
Paul Bakker23986e52011-04-24 08:57:21 +00002108 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002109 size_t siglen;
2110 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002112 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002113 unsigned int hlen;
2114 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 const mbedtls_md_info_t *md_info;
2116 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002117 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2120 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002121
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 siglen = ctx->len;
2123
Paul Bakker27fdf462011-06-09 13:55:13 +00002124 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2128 ? mbedtls_rsa_public( ctx, sig, buf )
2129 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
2131 if( ret != 0 )
2132 return( ret );
2133
2134 p = buf;
2135
Paul Bakkerb3869132013-02-28 17:21:01 +01002136 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 {
Simon Butcher02037452016-03-01 21:19:12 +00002141 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002143 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002147 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002150 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002154 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002155
Paul Bakkerb3869132013-02-28 17:21:01 +01002156 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002157
Simon Butcher02037452016-03-01 21:19:12 +00002158 /*
2159 * Note: EMSA-PSS verification is over the length of N - 1 bits
2160 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002161 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002162
Simon Butcher02037452016-03-01 21:19:12 +00002163 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002164 if( msb % 8 == 0 )
2165 {
2166 p++;
2167 siglen -= 1;
2168 }
2169 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002173 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2174 {
2175 mbedtls_md_free( &md_ctx );
2176 return( ret );
2177 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002178
Paul Bakkerb3869132013-02-28 17:21:01 +01002179 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002180
Paul Bakkerb3869132013-02-28 17:21:01 +01002181 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002182
Paul Bakker4de44aa2013-12-31 11:43:01 +01002183 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002184 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002185
Paul Bakkerb3869132013-02-28 17:21:01 +01002186 if( p == buf + siglen ||
2187 *p++ != 0x01 )
2188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 mbedtls_md_free( &md_ctx );
2190 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002191 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002192
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002193 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002194 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002197 slen != (size_t) expected_salt_len )
2198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 mbedtls_md_free( &md_ctx );
2200 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002201 }
2202
Simon Butcher02037452016-03-01 21:19:12 +00002203 /*
2204 * Generate H = Hash( M' )
2205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 mbedtls_md_starts( &md_ctx );
2207 mbedtls_md_update( &md_ctx, zeros, 8 );
2208 mbedtls_md_update( &md_ctx, hash, hashlen );
2209 mbedtls_md_update( &md_ctx, p, slen );
2210 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002213
Paul Bakkerb3869132013-02-28 17:21:01 +01002214 if( memcmp( p + slen, result, hlen ) == 0 )
2215 return( 0 );
2216 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002218}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002219
2220/*
2221 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002224 int (*f_rng)(void *, unsigned char *, size_t),
2225 void *p_rng,
2226 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002228 unsigned int hashlen,
2229 const unsigned char *hash,
2230 const unsigned char *sig )
2231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2233 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002234 : md_alg;
2235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002237 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002239 sig ) );
2240
2241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002245/*
2246 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002249 int (*f_rng)(void *, unsigned char *, size_t),
2250 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002251 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002253 unsigned int hashlen,
2254 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002255 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002256{
2257 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002258 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002259 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 mbedtls_md_type_t msg_md_alg;
2261 const mbedtls_md_info_t *md_info;
2262 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002263 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2266 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002267
2268 siglen = ctx->len;
2269
2270 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2274 ? mbedtls_rsa_public( ctx, sig, buf )
2275 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002276
2277 if( ret != 0 )
2278 return( ret );
2279
2280 p = buf;
2281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2283 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002284
2285 while( *p != 0 )
2286 {
2287 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002289 p++;
2290 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002291 p++; /* skip 00 byte */
2292
2293 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2294 if( p - buf < 11 )
2295 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002296
2297 len = siglen - ( p - buf );
2298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002300 {
2301 if( memcmp( p, hash, hashlen ) == 0 )
2302 return( 0 );
2303 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 }
2306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002308 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2310 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002311
2312 end = p + len;
2313
Simon Butcher02037452016-03-01 21:19:12 +00002314 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002315 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2316 * Insist on 2-byte length tags, to protect against variants of
2317 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002318 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002319 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2321 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2322 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002323 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002325
Gilles Peskinee7e76502017-05-04 12:48:39 +02002326 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2328 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2329 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002330 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002332
Gilles Peskinee7e76502017-05-04 12:48:39 +02002333 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2335 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002336 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002337 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002338
2339 oid.p = p;
2340 p += oid.len;
2341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2343 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002344
2345 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002347
2348 /*
2349 * assume the algorithm parameters must be NULL
2350 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002351 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2353 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002354 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002356
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002357 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002358 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2359 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002360 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002362
2363 if( memcmp( p, hash, hashlen ) != 0 )
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 p += hashlen;
2367
2368 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002370
2371 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002372}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002374
2375/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002376 * Do an RSA operation and check the message digest
2377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002379 int (*f_rng)(void *, unsigned char *, size_t),
2380 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002381 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002383 unsigned int hashlen,
2384 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002385 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002386{
2387 switch( ctx->padding )
2388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389#if defined(MBEDTLS_PKCS1_V15)
2390 case MBEDTLS_RSA_PKCS_V15:
2391 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002392 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002393#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395#if defined(MBEDTLS_PKCS1_V21)
2396 case MBEDTLS_RSA_PKCS_V21:
2397 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002398 hashlen, hash, sig );
2399#endif
2400
2401 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002403 }
2404}
2405
2406/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002407 * Copy the components of an RSA key
2408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002410{
2411 int ret;
2412
2413 dst->ver = src->ver;
2414 dst->len = src->len;
2415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2417 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2420 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2421 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002422
2423#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2425 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2426 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2428 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002429#endif
2430
2431 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2434 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002435
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002436 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002437 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002438
2439cleanup:
2440 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002442
2443 return( ret );
2444}
2445
2446/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 * Free the components of an RSA key
2448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002450{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002452 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2453 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002455
Hanno Becker33c30a02017-08-23 07:00:22 +01002456#if !defined(MBEDTLS_RSA_NO_CRT)
2457 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2458 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2459 mbedtls_mpi_free( &ctx->DP );
2460#endif /* MBEDTLS_RSA_NO_CRT */
2461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462#if defined(MBEDTLS_THREADING_C)
2463 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002464#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002465}
2466
Hanno Beckerab377312017-08-23 16:24:51 +01002467#endif /* !MBEDTLS_RSA_ALT */
2468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002471#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002472
2473/*
2474 * Example RSA-1024 keypair, for test purposes
2475 */
2476#define KEY_LEN 128
2477
2478#define RSA_N "9292758453063D803DD603D5E777D788" \
2479 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2480 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2481 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2482 "93A89813FBF3C4F8066D2D800F7C38A8" \
2483 "1AE31942917403FF4946B0A83D3D3E05" \
2484 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2485 "5E94BB77B07507233A0BC7BAC8F90F79"
2486
2487#define RSA_E "10001"
2488
2489#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2490 "66CA472BC44D253102F8B4A9D3BFA750" \
2491 "91386C0077937FE33FA3252D28855837" \
2492 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2493 "DF79C5CE07EE72C7F123142198164234" \
2494 "CABB724CF78B8173B9F880FC86322407" \
2495 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2496 "071513A1E85B5DFA031F21ECAE91A34D"
2497
2498#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2499 "2C01CAD19EA484A87EA4377637E75500" \
2500 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2501 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2502
2503#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2504 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2505 "910E4168387E3C30AA1E00C339A79508" \
2506 "8452DD96A9A5EA5D9DCA68DA636032AF"
2507
Paul Bakker5121ce52009-01-03 21:22:43 +00002508#define PT_LEN 24
2509#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2510 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002513static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002514{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002515#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002516 size_t i;
2517
Paul Bakker545570e2010-07-18 09:00:25 +00002518 if( rng_state != NULL )
2519 rng_state = NULL;
2520
Paul Bakkera3d195c2011-11-27 21:07:34 +00002521 for( i = 0; i < len; ++i )
2522 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002523#else
2524 if( rng_state != NULL )
2525 rng_state = NULL;
2526
2527 arc4random_buf( output, len );
2528#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002529
Paul Bakkera3d195c2011-11-27 21:07:34 +00002530 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002531}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002533
Paul Bakker5121ce52009-01-03 21:22:43 +00002534/*
2535 * Checkup routine
2536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002538{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002539 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002541 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 unsigned char rsa_plaintext[PT_LEN];
2544 unsigned char rsa_decrypted[PT_LEN];
2545 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002547 unsigned char sha1sum[20];
2548#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Hanno Becker3a701162017-08-22 13:52:43 +01002550 mbedtls_mpi K;
2551
2552 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Hanno Becker3a701162017-08-22 13:52:43 +01002555 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2556 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2557 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2558 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2559 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2560 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2561 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2562 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2563 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2564 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2565
2566 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
2568 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2572 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 {
2574 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002576
2577 return( 1 );
2578 }
2579
2580 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
2583 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2584
Hanno Becker98838b02017-10-02 13:16:10 +01002585 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2586 PT_LEN, rsa_plaintext,
2587 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 {
2589 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
2592 return( 1 );
2593 }
2594
2595 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
Hanno Becker98838b02017-10-02 13:16:10 +01002598 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2599 &len, rsa_ciphertext, rsa_decrypted,
2600 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 {
2602 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
2605 return( 1 );
2606 }
2607
2608 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2609 {
2610 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
2613 return( 1 );
2614 }
2615
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002616 if( verbose != 0 )
2617 mbedtls_printf( "passed\n" );
2618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002621 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Hanno Becker98838b02017-10-02 13:16:10 +01002625 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2626 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2627 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 {
2629 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
2632 return( 1 );
2633 }
2634
2635 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Hanno Becker98838b02017-10-02 13:16:10 +01002638 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2639 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2640 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 {
2642 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
2645 return( 1 );
2646 }
2647
2648 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002649 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002652 if( verbose != 0 )
2653 mbedtls_printf( "\n" );
2654
Paul Bakker3d8fb632014-04-17 12:42:41 +02002655cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002656 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 mbedtls_rsa_free( &rsa );
2658#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002659 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002661 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002662}
2663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#endif /* MBEDTLS_RSA_C */