blob: 00f83a06a495baea39107b475d31177e7dc74a11 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Hanno Becker74716312017-10-02 10:00:37 +010021
Paul Bakker5121ce52009-01-03 21:22:43 +000022/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000023 * The following sources were referenced in the design of this implementation
24 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000025 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000026 * [1] A method for obtaining digital signatures and public-key cryptosystems
27 * R Rivest, A Shamir, and L Adleman
28 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
29 *
30 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
31 * Menezes, van Oorschot and Vanstone
32 *
Janos Follathe81102e2017-03-22 13:38:28 +000033 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
34 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
35 * Stefan Mangard
36 * https://arxiv.org/abs/1702.08719v2
37 *
Paul Bakker5121ce52009-01-03 21:22:43 +000038 */
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020042#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/rsa.h"
49#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000055#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000058#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010063#else
Rich Evans00ab4702015-02-06 13:43:58 +000064#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020066#define mbedtls_calloc calloc
67#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010068#endif
69
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010070/* Implementation that should never be optimized out by the compiler */
71static void mbedtls_zeroize( void *v, size_t n ) {
72 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
73}
74
Paul Bakker5121ce52009-01-03 21:22:43 +000075/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010076 * Context-independent RSA helper functions.
77 *
Hanno Beckerd9431a72017-08-25 08:03:13 +010078 * There are two classes of helper functions:
79 * (1) Parameter-generating helpers. These are:
80 * - mbedtls_rsa_deduce_moduli
81 * - mbedtls_rsa_deduce_private
82 * - mbedtls_rsa_deduce_crt
83 * Each of these functions takes a set of core RSA parameters
84 * and generates some other, or CRT related parameters.
85 * (2) Parameter-checking helpers. These are:
86 * - mbedtls_rsa_validate_params
87 * - mbedtls_rsa_validate_crt
88 * They take a set of core or CRT related RSA parameters
89 * and check their validity.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010090 *
Hanno Beckerd9431a72017-08-25 08:03:13 +010091 * The helper functions do not use the RSA context structure
92 * and therefore do not need to be replaced when providing
93 * an alternative RSA implementation.
94 *
95 * Their main purpose is to provide common MPI operations in the context
Hanno Beckere2e8b8d2017-08-23 14:06:45 +010096 * of RSA that can be easily shared across multiple implementations.
97 */
98
99/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100100 *
101 * Given the modulus N=PQ and a pair of public and private
102 * exponents E and D, respectively, factor N.
103 *
104 * Setting F := lcm(P-1,Q-1), the idea is as follows:
105 *
106 * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2)
107 * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the
108 * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four
109 * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1)
110 * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime
111 * factors of N.
112 *
113 * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same
114 * construction still applies since (-)^K is the identity on the set of
115 * roots of 1 in Z/NZ.
116 *
117 * The public and private key primitives (-)^E and (-)^D are mutually inverse
118 * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e.
119 * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L.
120 * Splitting L = 2^t * K with K odd, we have
121 *
122 * DE - 1 = FL = (F/2) * (2^(t+1)) * K,
123 *
124 * so (F / 2) * K is among the numbers
125 *
126 * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord
127 *
128 * where ord is the order of 2 in (DE - 1).
129 * We can therefore iterate through these numbers apply the construction
130 * of (a) and (b) above to attempt to factor N.
131 *
132 */
Hanno Beckerba5b7552017-10-02 09:55:49 +0100133int mbedtls_rsa_deduce_moduli( mbedtls_mpi const *N,
134 mbedtls_mpi const *D, mbedtls_mpi const *E,
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100135 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
136 mbedtls_mpi *P, mbedtls_mpi *Q )
137{
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100138 int ret = 0;
139
140 uint16_t attempt; /* Number of current attempt */
141 uint16_t iter; /* Number of squares computed in the current attempt */
142
143 uint16_t bitlen_half; /* Half the bitsize of the modulus N */
144 uint16_t order; /* Order of 2 in DE - 1 */
145
Hanno Beckerba5b7552017-10-02 09:55:49 +0100146 mbedtls_mpi T; /* Holds largest odd divisor of DE - 1 */
147 mbedtls_mpi K; /* During factorization attempts, stores a random integer
148 * in the range of [0,..,N] */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100149
150 if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL )
151 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
152
153 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 ||
154 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
155 mbedtls_mpi_cmp_mpi( D, N ) >= 0 ||
156 mbedtls_mpi_cmp_int( E, 1 ) <= 0 ||
157 mbedtls_mpi_cmp_mpi( E, N ) >= 0 )
158 {
159 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
160 }
161
162 /*
163 * Initializations and temporary changes
164 */
165
166 mbedtls_mpi_init( &K );
Hanno Beckerba5b7552017-10-02 09:55:49 +0100167 mbedtls_mpi_init( &T );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100168
Hanno Beckerba5b7552017-10-02 09:55:49 +0100169 /* T := DE - 1 */
170 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, D, E ) );
171 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &T, &T, 1 ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100172
Hanno Beckerba5b7552017-10-02 09:55:49 +0100173 if( ( order = mbedtls_mpi_lsb( &T ) ) == 0 )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100174 {
175 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
176 goto cleanup;
177 }
178
Hanno Beckerba5b7552017-10-02 09:55:49 +0100179 /* After this operation, T holds the largest odd divisor of DE - 1. */
180 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &T, order ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100181
182 /* This is used to generate a few numbers around N / 2
183 * if no PRNG is provided. */
184 if( f_rng == NULL )
185 bitlen_half = mbedtls_mpi_bitlen( N ) / 2;
186
187 /*
188 * Actual work
189 */
190
191 for( attempt = 0; attempt < 30; ++attempt )
192 {
193 /* Generate some number in [0,N], either randomly
194 * if a PRNG is given, or try numbers around N/2 */
195 if( f_rng != NULL )
196 {
197 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &K,
198 mbedtls_mpi_size( N ),
199 f_rng, p_rng ) );
200 }
201 else
202 {
203 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &K, 1 ) ) ;
204 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &K, bitlen_half ) ) ;
205 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, attempt + 1 ) );
206 }
207
208 /* Check if gcd(K,N) = 1 */
209 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
210 if( mbedtls_mpi_cmp_int( P, 1 ) != 0 )
211 continue;
212
Hanno Beckerba5b7552017-10-02 09:55:49 +0100213 /* Go through K^T + 1, K^(2T) + 1, K^(4T) + 1, ...
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100214 * and check whether they have nontrivial GCD with N. */
Hanno Beckerba5b7552017-10-02 09:55:49 +0100215 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, &T, N,
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100216 Q /* temporarily use Q for storing Montgomery
217 * multiplication helper values */ ) );
218
219 for( iter = 1; iter < order; ++iter )
220 {
221 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) );
222 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) );
223
224 if( mbedtls_mpi_cmp_int( P, 1 ) == 1 &&
225 mbedtls_mpi_cmp_mpi( P, N ) == -1 )
226 {
227 /*
228 * Have found a nontrivial divisor P of N.
Hanno Beckerd56d83a2017-08-25 07:29:35 +0100229 * Set Q := N / P.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100230 */
231
Hanno Beckerba5b7552017-10-02 09:55:49 +0100232 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, NULL, N, P ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100233 goto cleanup;
234 }
235
236 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
237 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) );
238 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) );
239 }
240 }
241
242 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
243
244cleanup:
245
246 mbedtls_mpi_free( &K );
Hanno Beckerba5b7552017-10-02 09:55:49 +0100247 mbedtls_mpi_free( &T );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100248 return( ret );
249}
250
251/*
252 * Given P, Q and the public exponent E, deduce D.
253 * This is essentially a modular inversion.
254 */
255
Hanno Beckerbdefff12017-10-02 09:57:50 +0100256int mbedtls_rsa_deduce_private( mbedtls_mpi const *P,
257 mbedtls_mpi const *Q,
258 mbedtls_mpi const *E,
259 mbedtls_mpi *D )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100260{
261 int ret = 0;
Hanno Beckerbdefff12017-10-02 09:57:50 +0100262 mbedtls_mpi K, L;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100263
264 if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 )
265 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
266
267 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
268 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
269 mbedtls_mpi_cmp_int( E, 0 ) == 0 )
270 {
271 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
272 }
273
274 mbedtls_mpi_init( &K );
Hanno Beckerbdefff12017-10-02 09:57:50 +0100275 mbedtls_mpi_init( &L );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100276
Hanno Beckerbdefff12017-10-02 09:57:50 +0100277 /* Temporarily put K := P-1 and L := Q-1 */
278 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
279 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100280
Hanno Beckerbdefff12017-10-02 09:57:50 +0100281 /* Temporarily put D := gcd(P-1, Q-1) */
282 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100283
Hanno Beckerbdefff12017-10-02 09:57:50 +0100284 /* K := LCM(P-1, Q-1) */
285 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100286 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) );
287
288 /* Compute modular inverse of E in LCM(P-1, Q-1) */
289 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) );
290
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100291cleanup:
292
293 mbedtls_mpi_free( &K );
Hanno Beckerbdefff12017-10-02 09:57:50 +0100294 mbedtls_mpi_free( &L );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100295
296 return( ret );
297}
298
299/*
Hanno Beckerd3637992017-08-25 07:55:03 +0100300 * Check that RSA CRT parameters are in accordance with core parameters.
301 */
302
303int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
304 const mbedtls_mpi *D, const mbedtls_mpi *DP,
305 const mbedtls_mpi *DQ, const mbedtls_mpi *QP )
306{
307 int ret = 0;
308
309 mbedtls_mpi K, L;
310 mbedtls_mpi_init( &K );
311 mbedtls_mpi_init( &L );
312
313 /* Check that DP - P == 0 mod P - 1 */
314 if( DP != NULL )
315 {
316 if( P == NULL )
317 {
318 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
319 goto cleanup;
320 }
321
322 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
323 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DP, D ) );
324 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
325
326 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
327 {
328 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
329 }
330 }
331
332 /* Check that DQ - Q == 0 mod Q - 1 */
333 if( DQ != NULL )
334 {
335 if( Q == NULL )
336 {
337 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
338 goto cleanup;
339 }
340
341 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
342 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &L, DQ, D ) );
343 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &L, &L, &K ) );
344
345 if( mbedtls_mpi_cmp_int( &L, 0 ) != 0 )
346 {
347 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
348 }
349 }
350
351 /* Check that QP * P - 1 == 0 mod P */
352 if( QP != NULL )
353 {
354 if( P == NULL || Q == NULL )
355 {
356 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
357 goto cleanup;
358 }
359
360 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, QP, Q ) );
361 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
362 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) );
363 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
364 {
365 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
366 }
367 }
368
369cleanup:
370
371 /* Wrap MPI error codes by RSA check failure error code */
372 if( ret != 0 &&
373 ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
374 ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
375 {
376 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
377 }
378
379 mbedtls_mpi_free( &K );
380 mbedtls_mpi_free( &L );
381
382 return( ret );
383}
384
385/*
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100386 * Check that core RSA parameters are sane.
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100387 */
388
Hanno Becker750e8b42017-08-25 07:54:27 +0100389int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
390 const mbedtls_mpi *Q, const mbedtls_mpi *D,
391 const mbedtls_mpi *E,
392 int (*f_rng)(void *, unsigned char *, size_t),
393 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100394{
395 int ret = 0;
Hanno Becker750e8b42017-08-25 07:54:27 +0100396 mbedtls_mpi K, L;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100397
398 mbedtls_mpi_init( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100399 mbedtls_mpi_init( &L );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100400
401 /*
402 * Step 1: If PRNG provided, check that P and Q are prime
403 */
404
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100405#if defined(MBEDTLS_GENPRIME)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100406 if( f_rng != NULL && P != NULL &&
407 ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 )
408 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100409 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100410 goto cleanup;
411 }
412
413 if( f_rng != NULL && Q != NULL &&
414 ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 )
415 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100416 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100417 goto cleanup;
418 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100419#else
420 ((void) f_rng);
421 ((void) p_rng);
422#endif /* MBEDTLS_GENPRIME */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100423
424 /*
425 * Step 2: Check that N = PQ
426 */
427
428 if( P != NULL && Q != NULL && N != NULL )
429 {
430 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) );
Hanno Becker750e8b42017-08-25 07:54:27 +0100431 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ||
432 mbedtls_mpi_cmp_mpi( &K, N ) != 0 )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100433 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100434 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100435 goto cleanup;
436 }
437 }
438
439 /*
440 * Step 3: Check that D, E are inverse modulo P-1 and Q-1
441 */
442
443 if( P != NULL && Q != NULL && D != NULL && E != NULL )
444 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100445 if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 ||
446 mbedtls_mpi_cmp_int( Q, 1 ) <= 0 ||
447 mbedtls_mpi_cmp_int( D, 1 ) <= 0 ||
448 mbedtls_mpi_cmp_int( E, 1 ) <= 0 )
449 {
450 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
451 goto cleanup;
452 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100453
454 /* Compute DE-1 mod P-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100455 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
456 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
457 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, P, 1 ) );
458 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100459 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
460 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100461 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100462 goto cleanup;
463 }
464
465 /* Compute DE-1 mod Q-1 */
Hanno Becker750e8b42017-08-25 07:54:27 +0100466 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) );
467 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) );
468 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &L, Q, 1 ) );
469 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, &L ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100470 if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 )
471 {
Hanno Becker750e8b42017-08-25 07:54:27 +0100472 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100473 goto cleanup;
474 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100475 }
476
477cleanup:
478
479 mbedtls_mpi_free( &K );
Hanno Becker750e8b42017-08-25 07:54:27 +0100480 mbedtls_mpi_free( &L );
481
482 /* Wrap MPI error codes by RSA check failure error code */
483 if( ret != 0 && ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
484 {
485 ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
486 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100487
488 return( ret );
489}
490
491int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
492 const mbedtls_mpi *D, mbedtls_mpi *DP,
493 mbedtls_mpi *DQ, mbedtls_mpi *QP )
494{
495 int ret = 0;
496 mbedtls_mpi K;
497 mbedtls_mpi_init( &K );
498
Hanno Beckerd9431a72017-08-25 08:03:13 +0100499 /* DP = D mod P-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100500 if( DP != NULL )
501 {
502 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) );
503 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) );
504 }
505
Hanno Beckerd9431a72017-08-25 08:03:13 +0100506 /* DQ = D mod Q-1 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100507 if( DQ != NULL )
508 {
509 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) );
510 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) );
511 }
512
Hanno Beckerd9431a72017-08-25 08:03:13 +0100513 /* QP = Q^{-1} mod P */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100514 if( QP != NULL )
515 {
516 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) );
517 }
518
519cleanup:
520 mbedtls_mpi_free( &K );
521
522 return( ret );
523}
524
Hanno Becker617c1ae2017-08-23 14:11:24 +0100525
526/*
527 * Default RSA interface implementation
528 */
529
Hanno Beckerab377312017-08-23 16:24:51 +0100530#if !defined(MBEDTLS_RSA_ALT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100531
532int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
533 const mbedtls_mpi *N,
534 const mbedtls_mpi *P, const mbedtls_mpi *Q,
535 const mbedtls_mpi *D, const mbedtls_mpi *E )
536{
537 int ret;
538
539 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
540 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
541 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
542 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
543 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
544 {
545 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
546 }
547
548 if( N != NULL )
549 ctx->len = mbedtls_mpi_size( &ctx->N );
550
551 return( 0 );
552}
553
554int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100555 unsigned char const *N, size_t N_len,
556 unsigned char const *P, size_t P_len,
557 unsigned char const *Q, size_t Q_len,
558 unsigned char const *D, size_t D_len,
559 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100560{
561 int ret;
562
563 if( N != NULL )
564 {
565 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
566 ctx->len = mbedtls_mpi_size( &ctx->N );
567 }
568
569 if( P != NULL )
570 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
571
572 if( Q != NULL )
573 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
574
575 if( D != NULL )
576 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
577
578 if( E != NULL )
579 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
580
581cleanup:
582
583 if( ret != 0 )
584 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
585
586 return( 0 );
587}
588
589int mbedtls_rsa_complete( mbedtls_rsa_context *ctx,
590 int (*f_rng)(void *, unsigned char *, size_t),
591 void *p_rng )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100592{
593 int ret = 0;
594
Hanno Becker617c1ae2017-08-23 14:11:24 +0100595 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
596 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
597 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
598 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
599 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100600
Hanno Becker617c1ae2017-08-23 14:11:24 +0100601 /*
602 * Check whether provided parameters are enough
603 * to deduce all others. The following incomplete
604 * parameter sets for private keys are supported:
605 *
606 * (1) P, Q missing.
607 * (2) D and potentially N missing.
608 *
609 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100610
Hanno Becker2cca6f32017-09-29 11:46:40 +0100611 const int n_missing = have_P && have_Q && have_D && have_E;
612 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
613 const int d_missing = have_P && have_Q && !have_D && have_E;
614 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
615
616 /* These three alternatives are mutually exclusive */
617 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100618
Hanno Becker617c1ae2017-08-23 14:11:24 +0100619 if( !is_priv && !is_pub )
620 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
621
622 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100623 * Step 1: Deduce N if P, Q are provided.
624 */
625
626 if( !have_N && have_P && have_Q )
627 {
628 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
629 &ctx->Q ) ) != 0 )
630 {
631 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
632 }
633
634 ctx->len = mbedtls_mpi_size( &ctx->N );
635 }
636
637 /*
638 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100639 */
640
641 if( pq_missing )
642 {
643 /* This includes sanity checking of core parameters,
644 * so no further checks necessary. */
645 ret = mbedtls_rsa_deduce_moduli( &ctx->N, &ctx->D, &ctx->E,
646 f_rng, p_rng,
647 &ctx->P, &ctx->Q );
648 if( ret != 0 )
649 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
650
651 }
652 else if( d_missing )
653 {
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100654#if defined(MBEDTLS_GENPRIME)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100655 /* If a PRNG is provided, check if P, Q are prime. */
656 if( f_rng != NULL &&
657 ( ( ret = mbedtls_mpi_is_prime( &ctx->P, f_rng, p_rng ) ) != 0 ||
658 ( ret = mbedtls_mpi_is_prime( &ctx->Q, f_rng, p_rng ) ) != 0 ) )
659 {
660 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
661 }
Hanno Beckerfb81c0e2017-08-24 06:55:11 +0100662#endif /* MBEDTLS_GENPRIME */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100663
Hanno Becker617c1ae2017-08-23 14:11:24 +0100664 /* Deduce private exponent. This includes double-checking of the result,
665 * so together with the primality test above all core parameters are
666 * guaranteed to be sane if this call succeeds. */
667 if( ( ret = mbedtls_rsa_deduce_private( &ctx->P, &ctx->Q,
Hanno Beckerbdefff12017-10-02 09:57:50 +0100668 &ctx->E, &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100669 {
670 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
671 }
672 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100673
674 /* In the remaining case of a public key, there's nothing to check for. */
675
676 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100677 * Step 3: Deduce all additional parameters specific
Hanno Becker617c1ae2017-08-23 14:11:24 +0100678 * to our current RSA implementaiton.
679 */
680
Hanno Becker23344b52017-08-23 07:43:27 +0100681#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100682 if( is_priv )
683 {
684 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
685 &ctx->DP, &ctx->DQ, &ctx->QP );
686 if( ret != 0 )
687 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
688 }
Hanno Becker23344b52017-08-23 07:43:27 +0100689#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100690
691 /*
692 * Step 3: Double check
693 */
694
695 if( is_priv )
696 {
697 if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 )
698 return( ret );
699 }
700 else
701 {
702 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
703 return( ret );
704 }
705
706 return( 0 );
707}
708
709/*
710 * Check if CRT parameters match RSA context.
711 * This has to be implemented even if CRT is not used,
712 * in order to be able to validate DER encoded RSA keys,
713 * which always contain CRT parameters.
714 */
Hanno Beckerd3637992017-08-25 07:55:03 +0100715int mbedtls_rsa_check_crt( const mbedtls_rsa_context *ctx,
716 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100717{
Hanno Becker23344b52017-08-23 07:43:27 +0100718 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100719
Hanno Becker23344b52017-08-23 07:43:27 +0100720 /* Check if key is private or public */
721 const int is_priv =
722 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
723 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
724 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
725 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
726 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
727
728 if( !is_priv )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100729 {
730 /* Checking optional parameters only makes sense for private keys. */
731 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
732 }
733
Hanno Becker23344b52017-08-23 07:43:27 +0100734#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100735 if( ( DP != NULL && mbedtls_mpi_cmp_mpi( DP, &ctx->DP ) != 0 ) ||
736 ( DQ != NULL && mbedtls_mpi_cmp_mpi( DQ, &ctx->DQ ) != 0 ) ||
737 ( QP != NULL && mbedtls_mpi_cmp_mpi( QP, &ctx->QP ) != 0 ) )
738 {
739 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
740 }
Hanno Becker23344b52017-08-23 07:43:27 +0100741#else /* MBEDTLS_RSA_NO_CRT */
Hanno Beckerd3637992017-08-25 07:55:03 +0100742 if( ( ret = mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
743 DP, DQ, QP ) ) != 0 )
Hanno Becker23344b52017-08-23 07:43:27 +0100744 {
Hanno Beckerd3637992017-08-25 07:55:03 +0100745 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker23344b52017-08-23 07:43:27 +0100746 }
Hanno Becker23344b52017-08-23 07:43:27 +0100747#endif
748
749 if( ret != 0 )
750 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100751
752 return( 0 );
753}
754
755int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
756 unsigned char *N, size_t N_len,
757 unsigned char *P, size_t P_len,
758 unsigned char *Q, size_t Q_len,
759 unsigned char *D, size_t D_len,
760 unsigned char *E, size_t E_len )
761{
762 int ret = 0;
763
764 /* Check if key is private or public */
765 const int is_priv =
766 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
767 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
768 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
769 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
770 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
771
772 if( !is_priv )
773 {
774 /* If we're trying to export private parameters for a public key,
775 * something must be wrong. */
776 if( P != NULL || Q != NULL || D != NULL )
777 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
778
779 }
780
781 if( N != NULL )
782 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
783
784 if( P != NULL )
785 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
786
787 if( Q != NULL )
788 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
789
790 if( D != NULL )
791 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
792
793 if( E != NULL )
794 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100795
796cleanup:
797
798 return( ret );
799}
800
Hanno Becker617c1ae2017-08-23 14:11:24 +0100801int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
802 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
803 mbedtls_mpi *D, mbedtls_mpi *E )
804{
805 int ret;
806
807 /* Check if key is private or public */
808 int is_priv =
809 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
810 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
811 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
812 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
813 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
814
815 if( !is_priv )
816 {
817 /* If we're trying to export private parameters for a public key,
818 * something must be wrong. */
819 if( P != NULL || Q != NULL || D != NULL )
820 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
821
822 }
823
824 /* Export all requested core parameters. */
825
826 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
827 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
828 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
829 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
830 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
831 {
832 return( ret );
833 }
834
835 return( 0 );
836}
837
838/*
839 * Export CRT parameters
840 * This must also be implemented if CRT is not used, for being able to
841 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
842 * can be used in this case.
843 */
844int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
845 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
846{
847 int ret;
848
849 /* Check if key is private or public */
850 int is_priv =
851 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
852 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
853 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
854 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
855 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
856
857 if( !is_priv )
858 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
859
Hanno Beckerdc95c892017-08-23 06:57:02 +0100860#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100861 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100862 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
863 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
864 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
865 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100866 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100867 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100868#else
869 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
870 DP, DQ, QP ) ) != 0 )
871 {
872 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
873 }
874#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100875
876 return( 0 );
877}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100878
879/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000880 * Initialize an RSA context
881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000883 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000884 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000885{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890#if defined(MBEDTLS_THREADING_C)
891 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200892#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000893}
894
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100895/*
896 * Set padding for an existing RSA context
897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100899{
900 ctx->padding = padding;
901 ctx->hash_id = hash_id;
902}
903
Hanno Becker617c1ae2017-08-23 14:11:24 +0100904/*
905 * Get length in bytes of RSA modulus
906 */
907
908size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
909{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100910 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100911}
912
913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
916/*
917 * Generate an RSA keypair
918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000920 int (*f_rng)(void *, unsigned char *, size_t),
921 void *p_rng,
922 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000923{
924 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100925 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Paul Bakker21eb2802010-08-16 11:10:02 +0000927 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Janos Follathef441782016-09-21 13:18:12 +0100930 if( nbits % 2 )
931 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
932
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100933 mbedtls_mpi_init( &H );
934 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
936 /*
937 * find primes P and Q with Q < P so that:
938 * GCD( E, (P-1)*(Q-1) ) == 1
939 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
942 do
943 {
Janos Follath10c575b2016-02-23 14:42:48 +0000944 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100945 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000946
Janos Follathef441782016-09-21 13:18:12 +0100947 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100948 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 continue;
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200954 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 continue;
956
Janos Follathef441782016-09-21 13:18:12 +0100957 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100958 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100959
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100960 /* Temporarily replace P,Q by P-1, Q-1 */
961 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
962 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
963 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000965 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100968 /* Restore P,Q */
969 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
970 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
971
972 ctx->len = mbedtls_mpi_size( &ctx->N );
973
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 /*
975 * D = E^-1 mod ((P-1)*(Q-1))
976 * DP = D mod (P - 1)
977 * DQ = D mod (Q - 1)
978 * QP = Q^-1 mod P
979 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100981 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
982
983#if !defined(MBEDTLS_RSA_NO_CRT)
984 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
985 &ctx->DP, &ctx->DQ, &ctx->QP ) );
986#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
Hanno Becker83aad1f2017-08-23 06:45:10 +0100988 /* Double-check */
989 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
990
Paul Bakker5121ce52009-01-03 21:22:43 +0000991cleanup:
992
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100993 mbedtls_mpi_free( &H );
994 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
996 if( ret != 0 )
997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_rsa_free( ctx );
999 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 }
1001
Paul Bakker48377d92013-08-30 12:06:24 +02001002 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001003}
1004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
1007/*
1008 * Check a public RSA key
1009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001011{
Paul Bakker37940d9f2009-07-10 22:38:58 +00001012 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +00001014
Hanno Beckerba1ba112017-09-29 11:48:23 +01001015 if( ctx->len != mbedtls_mpi_size( &ctx->N ) )
1016 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1017
Paul Bakker48377d92013-08-30 12:06:24 +02001018 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001022 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
1023 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001026 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
1028 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
1030 return( 0 );
1031}
1032
1033/*
1034 * Check a private RSA key
1035 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001037{
Hanno Beckerb269a852017-08-25 08:03:21 +01001038 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
1039 mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
1040 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 {
Hanno Beckerb269a852017-08-25 08:03:21 +01001042 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 }
Hanno Beckerb269a852017-08-25 08:03:21 +01001044#if !defined(MBEDTLS_RSA_NO_CRT)
1045 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
1046 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
1047 {
1048 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
1049 }
1050#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001051
1052 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053}
1054
1055/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001056 * Check if contexts holding a public and private key match
1057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
1061 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001064 }
1065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
1067 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001070 }
1071
1072 return( 0 );
1073}
1074
1075/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 * Do an RSA public key operation
1077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001079 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 unsigned char *output )
1081{
Paul Bakker23986e52011-04-24 08:57:21 +00001082 int ret;
1083 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001088#if defined(MBEDTLS_THREADING_C)
1089 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1090 return( ret );
1091#endif
1092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001097 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1098 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001099 }
1100
1101 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
1103 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001104
1105cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001107 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1108 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001109#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001112
1113 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
1116 return( 0 );
1117}
1118
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001119/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001120 * Generate or update blinding values, see section 10 of:
1121 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001122 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001123 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001124 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001125static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001126 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1127{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001128 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001129
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001130 if( ctx->Vf.p != NULL )
1131 {
1132 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
1134 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
1135 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
1136 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001137
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001138 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001139 }
1140
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001141 /* Unblinding value: Vf = random number, invertible mod N */
1142 do {
1143 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
1147 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1148 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001149
1150 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
1152 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 +02001153
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001154
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001155cleanup:
1156 return( ret );
1157}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001158
Paul Bakker5121ce52009-01-03 21:22:43 +00001159/*
Janos Follathe81102e2017-03-22 13:38:28 +00001160 * Exponent blinding supposed to prevent side-channel attacks using multiple
1161 * traces of measurements to recover the RSA key. The more collisions are there,
1162 * the more bits of the key can be recovered. See [3].
1163 *
1164 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1165 * observations on avarage.
1166 *
1167 * For example with 28 byte blinding to achieve 2 collisions the adversary has
1168 * to make 2^112 observations on avarage.
1169 *
1170 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1171 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1172 * Thus in this sense with 28 byte blinding the security is not reduced by
1173 * side-channel attacks like the one in [3])
1174 *
1175 * This countermeasure does not help if the key recovery is possible with a
1176 * single trace.
1177 */
1178#define RSA_EXPONENT_BLINDING 28
1179
1180/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 * Do an RSA private key operation
1182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001184 int (*f_rng)(void *, unsigned char *, size_t),
1185 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001186 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 unsigned char *output )
1188{
Paul Bakker23986e52011-04-24 08:57:21 +00001189 int ret;
1190 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +00001192 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +00001193#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001194 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001195 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +00001196#else
1197 mbedtls_mpi DP_blind, DQ_blind;
1198 mbedtls_mpi *DP = &ctx->DP;
1199 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +00001200#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001201
Hanno Becker45037ce2017-08-25 11:03:34 +01001202 /* Sanity-check that all relevant fields are at least set,
1203 * but don't perform a full keycheck. */
1204 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
1205 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
1206 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
1207 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
1208 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
1209 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001210 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker45037ce2017-08-25 11:03:34 +01001211 }
1212#if !defined(MBEDTLS_RSA_NO_CRT)
1213 if( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
1214 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
1215 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
1216 {
1217 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1218 }
1219#endif /* MBEDTLS_RSA_NO_CRT */
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001222 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
1223
Janos Follathf9203b42017-03-22 15:13:15 +00001224 if( f_rng != NULL )
1225 {
Janos Follathe81102e2017-03-22 13:38:28 +00001226#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001227 mbedtls_mpi_init( &D_blind );
1228#else
1229 mbedtls_mpi_init( &DP_blind );
1230 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001231#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001232 }
Janos Follathe81102e2017-03-22 13:38:28 +00001233
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001235#if defined(MBEDTLS_THREADING_C)
1236 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
1237 return( ret );
1238#endif
1239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
1241 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001243 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1244 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 }
1246
Paul Bakkerf451bac2013-08-30 15:37:02 +02001247 if( f_rng != NULL )
1248 {
1249 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001250 * Blinding
1251 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001252 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001253 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
1254 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +00001256
Janos Follathe81102e2017-03-22 13:38:28 +00001257 /*
1258 * Exponent blinding
1259 */
1260 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
1261 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
1262
Janos Follathf9203b42017-03-22 15:13:15 +00001263#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001264 /*
1265 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1266 */
1267 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1268 f_rng, p_rng ) );
1269 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
1270 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
1271 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
1272
1273 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001274#else
1275 /*
1276 * DP_blind = ( P - 1 ) * R + DP
1277 */
1278 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1279 f_rng, p_rng ) );
1280 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
1281 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
1282 &ctx->DP ) );
1283
1284 DP = &DP_blind;
1285
1286 /*
1287 * DQ_blind = ( Q - 1 ) * R + DQ
1288 */
1289 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1290 f_rng, p_rng ) );
1291 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1292 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1293 &ctx->DQ ) );
1294
1295 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001296#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001297 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001300 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001301#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001302 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001303 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 *
1305 * T1 = input ^ dP mod P
1306 * T2 = input ^ dQ mod Q
1307 */
Janos Follathf9203b42017-03-22 15:13:15 +00001308 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
1309 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
1311 /*
1312 * T = (T1 - T2) * (Q^-1 mod P) mod P
1313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
1315 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
1316 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
1318 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +02001319 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
1322 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
1323#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001324
Paul Bakkerf451bac2013-08-30 15:37:02 +02001325 if( f_rng != NULL )
1326 {
1327 /*
1328 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001329 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001330 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001331 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001333 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
1335 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
1338cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001340 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1341 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001342#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +00001345 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
1346
1347 if( f_rng != NULL )
1348 {
Janos Follathe81102e2017-03-22 13:38:28 +00001349#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001350 mbedtls_mpi_free( &D_blind );
1351#else
1352 mbedtls_mpi_free( &DP_blind );
1353 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001354#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001355 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001356
1357 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
1360 return( 0 );
1361}
1362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001364/**
1365 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1366 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001367 * \param dst buffer to mask
1368 * \param dlen length of destination buffer
1369 * \param src source of the mask generation
1370 * \param slen length of the source buffer
1371 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001372 */
Paul Bakker48377d92013-08-30 12:06:24 +02001373static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001375{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001377 unsigned char counter[4];
1378 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001379 unsigned int hlen;
1380 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001383 memset( counter, 0, 4 );
1384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001386
Simon Butcher02037452016-03-01 21:19:12 +00001387 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001388 p = dst;
1389
1390 while( dlen > 0 )
1391 {
1392 use_len = hlen;
1393 if( dlen < hlen )
1394 use_len = dlen;
1395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 mbedtls_md_starts( md_ctx );
1397 mbedtls_md_update( md_ctx, src, slen );
1398 mbedtls_md_update( md_ctx, counter, 4 );
1399 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001400
1401 for( i = 0; i < use_len; ++i )
1402 *p++ ^= mask[i];
1403
1404 counter[3]++;
1405
1406 dlen -= use_len;
1407 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001408
1409 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001414/*
1415 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001418 int (*f_rng)(void *, unsigned char *, size_t),
1419 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001420 int mode,
1421 const unsigned char *label, size_t label_len,
1422 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001423 const unsigned char *input,
1424 unsigned char *output )
1425{
1426 size_t olen;
1427 int ret;
1428 unsigned char *p = output;
1429 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 const mbedtls_md_info_t *md_info;
1431 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1434 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001435
1436 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001440 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001442
1443 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
Simon Butcher02037452016-03-01 21:19:12 +00001446 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001447 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001449
1450 memset( output, 0, olen );
1451
1452 *p++ = 0;
1453
Simon Butcher02037452016-03-01 21:19:12 +00001454 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001455 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001457
1458 p += hlen;
1459
Simon Butcher02037452016-03-01 21:19:12 +00001460 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001462 p += hlen;
1463 p += olen - 2 * hlen - 2 - ilen;
1464 *p++ = 1;
1465 memcpy( p, input, ilen );
1466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001468 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1469 {
1470 mbedtls_md_free( &md_ctx );
1471 return( ret );
1472 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001473
Simon Butcher02037452016-03-01 21:19:12 +00001474 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001475 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1476 &md_ctx );
1477
Simon Butcher02037452016-03-01 21:19:12 +00001478 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001479 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1480 &md_ctx );
1481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 return( ( mode == MBEDTLS_RSA_PUBLIC )
1485 ? mbedtls_rsa_public( ctx, output, output )
1486 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001491/*
1492 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001495 int (*f_rng)(void *, unsigned char *, size_t),
1496 void *p_rng,
1497 int mode, size_t ilen,
1498 const unsigned char *input,
1499 unsigned char *output )
1500{
1501 size_t nb_pad, olen;
1502 int ret;
1503 unsigned char *p = output;
1504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1506 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001507
Janos Follath1ed9f992016-03-18 11:45:44 +00001508 // We don't check p_rng because it won't be dereferenced here
1509 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001511
1512 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001513
Simon Butcher02037452016-03-01 21:19:12 +00001514 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001515 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001517
1518 nb_pad = olen - 3 - ilen;
1519
1520 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001524
1525 while( nb_pad-- > 0 )
1526 {
1527 int rng_dl = 100;
1528
1529 do {
1530 ret = f_rng( p_rng, p, 1 );
1531 } while( *p == 0 && --rng_dl && ret == 0 );
1532
Simon Butcher02037452016-03-01 21:19:12 +00001533 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001534 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001536
1537 p++;
1538 }
1539 }
1540 else
1541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001543
1544 while( nb_pad-- > 0 )
1545 *p++ = 0xFF;
1546 }
1547
1548 *p++ = 0;
1549 memcpy( p, input, ilen );
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 return( ( mode == MBEDTLS_RSA_PUBLIC )
1552 ? mbedtls_rsa_public( ctx, output, output )
1553 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001554}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001556
Paul Bakker5121ce52009-01-03 21:22:43 +00001557/*
1558 * Add the message padding, then do an RSA operation
1559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001561 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001562 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001563 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001564 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 unsigned char *output )
1566{
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 switch( ctx->padding )
1568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#if defined(MBEDTLS_PKCS1_V15)
1570 case MBEDTLS_RSA_PKCS_V15:
1571 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001572 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001573#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_PKCS1_V21)
1576 case MBEDTLS_RSA_PKCS_V21:
1577 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001578 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001579#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001580
1581 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001584}
1585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001587/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001588 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001591 int (*f_rng)(void *, unsigned char *, size_t),
1592 void *p_rng,
1593 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001594 const unsigned char *label, size_t label_len,
1595 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001596 const unsigned char *input,
1597 unsigned char *output,
1598 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001599{
Paul Bakker23986e52011-04-24 08:57:21 +00001600 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001601 size_t ilen, i, pad_len;
1602 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1604 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001605 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 const mbedtls_md_info_t *md_info;
1607 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001608
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001609 /*
1610 * Parameters sanity checks
1611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1613 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
1615 ilen = ctx->len;
1616
Paul Bakker27fdf462011-06-09 13:55:13 +00001617 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001621 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001623
Janos Follathc17cda12016-02-11 11:08:18 +00001624 hlen = mbedtls_md_get_size( md_info );
1625
1626 // checking for integer underflow
1627 if( 2 * hlen + 2 > ilen )
1628 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1629
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001630 /*
1631 * RSA operation
1632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1634 ? mbedtls_rsa_public( ctx, input, buf )
1635 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
1637 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001638 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001639
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001640 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001641 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001644 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1645 {
1646 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001647 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001648 }
1649
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001650
1651 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001653
1654 /* seed: Apply seedMask to maskedSeed */
1655 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1656 &md_ctx );
1657
1658 /* DB: Apply dbMask to maskedDB */
1659 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1660 &md_ctx );
1661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001663
1664 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001665 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001666 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001667 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001668 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001670 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001671
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001672 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001673
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001674 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001675 for( i = 0; i < hlen; i++ )
1676 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001677
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001678 /* Get zero-padding len, but always read till end of buffer
1679 * (minus one, for the 01 byte) */
1680 pad_len = 0;
1681 pad_done = 0;
1682 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1683 {
1684 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001685 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001686 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001687
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001688 p += pad_len;
1689 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001690
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001691 /*
1692 * The only information "leaked" is whether the padding was correct or not
1693 * (eg, no data is copied if it was not correct). This meets the
1694 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1695 * the different error conditions.
1696 */
1697 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001698 {
1699 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1700 goto cleanup;
1701 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001702
Paul Bakker66d5d072014-06-17 16:39:18 +02001703 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001704 {
1705 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1706 goto cleanup;
1707 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001708
1709 *olen = ilen - (p - buf);
1710 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001711 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001712
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001713cleanup:
1714 mbedtls_zeroize( buf, sizeof( buf ) );
1715 mbedtls_zeroize( lhash, sizeof( lhash ) );
1716
1717 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001718}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001722/*
1723 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001726 int (*f_rng)(void *, unsigned char *, size_t),
1727 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001728 int mode, size_t *olen,
1729 const unsigned char *input,
1730 unsigned char *output,
1731 size_t output_max_len)
1732{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001733 int ret;
1734 size_t ilen, pad_count = 0, i;
1735 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1739 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001740
1741 ilen = ctx->len;
1742
1743 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1747 ? mbedtls_rsa_public( ctx, input, buf )
1748 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001749
1750 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001751 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001752
1753 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001754 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001755
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001756 /*
1757 * Check and get padding len in "constant-time"
1758 */
1759 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001760
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001761 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
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 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001770 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1771 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001772 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001773
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001774 p += pad_count;
1775 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001776 }
1777 else
1778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001780
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001781 /* Get padding len, but always read till end of buffer
1782 * (minus one, for the 00 byte) */
1783 for( i = 0; i < ilen - 3; i++ )
1784 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001785 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001786 pad_count += ( pad_done == 0 );
1787 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001788
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001789 p += pad_count;
1790 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 }
1792
Janos Follathc69fa502016-02-12 13:30:09 +00001793 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001794
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001795 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001796 {
1797 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1798 goto cleanup;
1799 }
Paul Bakker8804f692013-02-28 18:06:26 +01001800
Paul Bakker66d5d072014-06-17 16:39:18 +02001801 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001802 {
1803 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1804 goto cleanup;
1805 }
Paul Bakker060c5682009-01-12 21:48:39 +00001806
Paul Bakker27fdf462011-06-09 13:55:13 +00001807 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001808 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001809 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001810
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001811cleanup:
1812 mbedtls_zeroize( buf, sizeof( buf ) );
1813
1814 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001817
1818/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001819 * Do an RSA operation, then remove the message padding
1820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001822 int (*f_rng)(void *, unsigned char *, size_t),
1823 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001824 int mode, size_t *olen,
1825 const unsigned char *input,
1826 unsigned char *output,
1827 size_t output_max_len)
1828{
1829 switch( ctx->padding )
1830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831#if defined(MBEDTLS_PKCS1_V15)
1832 case MBEDTLS_RSA_PKCS_V15:
1833 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001834 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001835#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837#if defined(MBEDTLS_PKCS1_V21)
1838 case MBEDTLS_RSA_PKCS_V21:
1839 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001840 olen, input, output,
1841 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001842#endif
1843
1844 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001846 }
1847}
1848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001850/*
1851 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001854 int (*f_rng)(void *, unsigned char *, size_t),
1855 void *p_rng,
1856 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001858 unsigned int hashlen,
1859 const unsigned char *hash,
1860 unsigned char *sig )
1861{
1862 size_t olen;
1863 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001865 unsigned int slen, hlen, offset = 0;
1866 int ret;
1867 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 const mbedtls_md_info_t *md_info;
1869 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1872 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001873
1874 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001876
1877 olen = ctx->len;
1878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001880 {
Simon Butcher02037452016-03-01 21:19:12 +00001881 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001883 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001887 }
1888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001890 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001894 slen = hlen;
1895
1896 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001898
1899 memset( sig, 0, olen );
1900
Simon Butcher02037452016-03-01 21:19:12 +00001901 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001902 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001904
Simon Butcher02037452016-03-01 21:19:12 +00001905 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001906 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001907 p += olen - hlen * 2 - 2;
1908 *p++ = 0x01;
1909 memcpy( p, salt, slen );
1910 p += slen;
1911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001913 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1914 {
1915 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001916 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001917 return( ret );
1918 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001919
Simon Butcher02037452016-03-01 21:19:12 +00001920 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 mbedtls_md_starts( &md_ctx );
1922 mbedtls_md_update( &md_ctx, p, 8 );
1923 mbedtls_md_update( &md_ctx, hash, hashlen );
1924 mbedtls_md_update( &md_ctx, salt, slen );
1925 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001926 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001927
Simon Butcher02037452016-03-01 21:19:12 +00001928 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001929 if( msb % 8 == 0 )
1930 offset = 1;
1931
Simon Butcher02037452016-03-01 21:19:12 +00001932 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001933 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001936
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001937 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001938 sig[0] &= 0xFF >> ( olen * 8 - msb );
1939
1940 p += hlen;
1941 *p++ = 0xBC;
1942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 return( ( mode == MBEDTLS_RSA_PUBLIC )
1944 ? mbedtls_rsa_public( ctx, sig, sig )
1945 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001946}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001950/*
1951 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1952 */
1953/*
1954 * Do an RSA operation to sign the message digest
1955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001957 int (*f_rng)(void *, unsigned char *, size_t),
1958 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001959 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001961 unsigned int hashlen,
1962 const unsigned char *hash,
1963 unsigned char *sig )
1964{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001965 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001966 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001967 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001968 unsigned char *sig_try = NULL, *verif = NULL;
1969 size_t i;
1970 unsigned char diff;
1971 volatile unsigned char diff_no_optimize;
1972 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1975 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001976
1977 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001978 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001983 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1987 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001988
Paul Bakkerc70b9822013-04-07 22:00:46 +02001989 nb_pad -= 10 + oid_size;
1990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001992 }
1993
Paul Bakkerc70b9822013-04-07 22:00:46 +02001994 nb_pad -= hashlen;
1995
Paul Bakkerb3869132013-02-28 17:21:01 +01001996 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001998
1999 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01002001 memset( p, 0xFF, nb_pad );
2002 p += nb_pad;
2003 *p++ = 0;
2004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002006 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002007 memcpy( p, hash, hashlen );
2008 }
2009 else
2010 {
2011 /*
2012 * DigestInfo ::= SEQUENCE {
2013 * digestAlgorithm DigestAlgorithmIdentifier,
2014 * digest Digest }
2015 *
2016 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2017 *
2018 * Digest ::= OCTET STRING
2019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002021 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002023 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002025 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002026 memcpy( p, oid, oid_size );
2027 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002029 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002031 *p++ = hashlen;
2032 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01002033 }
2034
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002035 if( mode == MBEDTLS_RSA_PUBLIC )
2036 return( mbedtls_rsa_public( ctx, sig, sig ) );
2037
2038 /*
2039 * In order to prevent Lenstra's attack, make the signature in a
2040 * temporary buffer and check it before returning it.
2041 */
2042 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002043 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002044 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2045
Simon Butcher1285ab52016-01-01 21:42:47 +00002046 verif = mbedtls_calloc( 1, ctx->len );
2047 if( verif == NULL )
2048 {
2049 mbedtls_free( sig_try );
2050 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2051 }
2052
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002053 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2054 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2055
2056 /* Compare in constant time just in case */
2057 for( diff = 0, i = 0; i < ctx->len; i++ )
2058 diff |= verif[i] ^ sig[i];
2059 diff_no_optimize = diff;
2060
2061 if( diff_no_optimize != 0 )
2062 {
2063 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2064 goto cleanup;
2065 }
2066
2067 memcpy( sig, sig_try, ctx->len );
2068
2069cleanup:
2070 mbedtls_free( sig_try );
2071 mbedtls_free( verif );
2072
2073 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002074}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002076
2077/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 * Do an RSA operation to sign the message digest
2079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002081 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002082 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002085 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002086 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 unsigned char *sig )
2088{
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 switch( ctx->padding )
2090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#if defined(MBEDTLS_PKCS1_V15)
2092 case MBEDTLS_RSA_PKCS_V15:
2093 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002094 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002095#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#if defined(MBEDTLS_PKCS1_V21)
2098 case MBEDTLS_RSA_PKCS_V21:
2099 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002100 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002101#endif
2102
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002106}
2107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002109/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002110 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002113 int (*f_rng)(void *, unsigned char *, size_t),
2114 void *p_rng,
2115 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002117 unsigned int hashlen,
2118 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002120 int expected_salt_len,
2121 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002122{
Paul Bakker23986e52011-04-24 08:57:21 +00002123 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002124 size_t siglen;
2125 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002127 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002128 unsigned int hlen;
2129 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 const mbedtls_md_info_t *md_info;
2131 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002132 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2135 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002136
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 siglen = ctx->len;
2138
Paul Bakker27fdf462011-06-09 13:55:13 +00002139 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2143 ? mbedtls_rsa_public( ctx, sig, buf )
2144 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145
2146 if( ret != 0 )
2147 return( ret );
2148
2149 p = buf;
2150
Paul Bakkerb3869132013-02-28 17:21:01 +01002151 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 {
Simon Butcher02037452016-03-01 21:19:12 +00002156 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002158 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002162 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002165 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002169 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00002170
Paul Bakkerb3869132013-02-28 17:21:01 +01002171 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002172
Simon Butcher02037452016-03-01 21:19:12 +00002173 /*
2174 * Note: EMSA-PSS verification is over the length of N - 1 bits
2175 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002176 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002177
Simon Butcher02037452016-03-01 21:19:12 +00002178 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002179 if( msb % 8 == 0 )
2180 {
2181 p++;
2182 siglen -= 1;
2183 }
2184 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002188 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
2189 {
2190 mbedtls_md_free( &md_ctx );
2191 return( ret );
2192 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002193
Paul Bakkerb3869132013-02-28 17:21:01 +01002194 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01002195
Paul Bakkerb3869132013-02-28 17:21:01 +01002196 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002197
Paul Bakker4de44aa2013-12-31 11:43:01 +01002198 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002199 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002200
Paul Bakkerb3869132013-02-28 17:21:01 +01002201 if( p == buf + siglen ||
2202 *p++ != 0x01 )
2203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 mbedtls_md_free( &md_ctx );
2205 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002206 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002207
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002208 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01002209 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002212 slen != (size_t) expected_salt_len )
2213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 mbedtls_md_free( &md_ctx );
2215 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002216 }
2217
Simon Butcher02037452016-03-01 21:19:12 +00002218 /*
2219 * Generate H = Hash( M' )
2220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 mbedtls_md_starts( &md_ctx );
2222 mbedtls_md_update( &md_ctx, zeros, 8 );
2223 mbedtls_md_update( &md_ctx, hash, hashlen );
2224 mbedtls_md_update( &md_ctx, p, slen );
2225 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00002226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002228
Paul Bakkerb3869132013-02-28 17:21:01 +01002229 if( memcmp( p + slen, result, hlen ) == 0 )
2230 return( 0 );
2231 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01002233}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002234
2235/*
2236 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002239 int (*f_rng)(void *, unsigned char *, size_t),
2240 void *p_rng,
2241 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002243 unsigned int hashlen,
2244 const unsigned char *hash,
2245 const unsigned char *sig )
2246{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2248 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002249 : md_alg;
2250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002252 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002254 sig ) );
2255
2256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002260/*
2261 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002264 int (*f_rng)(void *, unsigned char *, size_t),
2265 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002266 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002268 unsigned int hashlen,
2269 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002270 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002271{
2272 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002273 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002274 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 mbedtls_md_type_t msg_md_alg;
2276 const mbedtls_md_info_t *md_info;
2277 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002278 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2281 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002282
2283 siglen = ctx->len;
2284
2285 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2289 ? mbedtls_rsa_public( ctx, sig, buf )
2290 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01002291
2292 if( ret != 0 )
2293 return( ret );
2294
2295 p = buf;
2296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
2298 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002299
2300 while( *p != 0 )
2301 {
2302 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002304 p++;
2305 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02002306 p++; /* skip 00 byte */
2307
2308 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
2309 if( p - buf < 11 )
2310 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002311
2312 len = siglen - ( p - buf );
2313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01002315 {
2316 if( memcmp( p, hash, hashlen ) == 0 )
2317 return( 0 );
2318 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002320 }
2321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002323 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2325 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002326
2327 end = p + len;
2328
Simon Butcher02037452016-03-01 21:19:12 +00002329 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02002330 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
2331 * Insist on 2-byte length tags, to protect against variants of
2332 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00002333 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002334 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2336 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2337 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002338 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002340
Gilles Peskinee7e76502017-05-04 12:48:39 +02002341 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
2343 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
2344 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002345 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002347
Gilles Peskinee7e76502017-05-04 12:48:39 +02002348 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
2350 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002351 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002352 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002353
2354 oid.p = p;
2355 p += oid.len;
2356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
2358 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002359
2360 if( md_alg != msg_md_alg )
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 /*
2364 * assume the algorithm parameters must be NULL
2365 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02002366 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
2368 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02002369 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002371
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002372 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002373 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
2374 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02002375 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002377
2378 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002380
2381 p += hashlen;
2382
2383 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002385
2386 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
2390/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002391 * Do an RSA operation and check the message digest
2392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002394 int (*f_rng)(void *, unsigned char *, size_t),
2395 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002396 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002398 unsigned int hashlen,
2399 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002400 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002401{
2402 switch( ctx->padding )
2403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404#if defined(MBEDTLS_PKCS1_V15)
2405 case MBEDTLS_RSA_PKCS_V15:
2406 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002407 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002408#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410#if defined(MBEDTLS_PKCS1_V21)
2411 case MBEDTLS_RSA_PKCS_V21:
2412 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002413 hashlen, hash, sig );
2414#endif
2415
2416 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002418 }
2419}
2420
2421/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002422 * Copy the components of an RSA key
2423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002425{
2426 int ret;
2427
2428 dst->ver = src->ver;
2429 dst->len = src->len;
2430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2432 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2435 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2436 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002437
2438#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2440 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2441 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2443 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002444#endif
2445
2446 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2449 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002450
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002451 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002452 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002453
2454cleanup:
2455 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002457
2458 return( ret );
2459}
2460
2461/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 * Free the components of an RSA key
2463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002465{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002467 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2468 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002470
Hanno Becker33c30a02017-08-23 07:00:22 +01002471#if !defined(MBEDTLS_RSA_NO_CRT)
2472 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2473 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2474 mbedtls_mpi_free( &ctx->DP );
2475#endif /* MBEDTLS_RSA_NO_CRT */
2476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477#if defined(MBEDTLS_THREADING_C)
2478 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002479#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002480}
2481
Hanno Beckerab377312017-08-23 16:24:51 +01002482#endif /* !MBEDTLS_RSA_ALT */
2483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002486#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
2488/*
2489 * Example RSA-1024 keypair, for test purposes
2490 */
2491#define KEY_LEN 128
2492
2493#define RSA_N "9292758453063D803DD603D5E777D788" \
2494 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2495 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2496 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2497 "93A89813FBF3C4F8066D2D800F7C38A8" \
2498 "1AE31942917403FF4946B0A83D3D3E05" \
2499 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2500 "5E94BB77B07507233A0BC7BAC8F90F79"
2501
2502#define RSA_E "10001"
2503
2504#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2505 "66CA472BC44D253102F8B4A9D3BFA750" \
2506 "91386C0077937FE33FA3252D28855837" \
2507 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2508 "DF79C5CE07EE72C7F123142198164234" \
2509 "CABB724CF78B8173B9F880FC86322407" \
2510 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2511 "071513A1E85B5DFA031F21ECAE91A34D"
2512
2513#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2514 "2C01CAD19EA484A87EA4377637E75500" \
2515 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2516 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2517
2518#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2519 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2520 "910E4168387E3C30AA1E00C339A79508" \
2521 "8452DD96A9A5EA5D9DCA68DA636032AF"
2522
2523#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2524 "3C94D22288ACD763FD8E5600ED4A702D" \
2525 "F84198A5F06C2E72236AE490C93F07F8" \
2526 "3CC559CD27BC2D1CA488811730BB5725"
2527
2528#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2529 "D8AAEA56749EA28623272E4F7D0592AF" \
2530 "7C1F1313CAC9471B5C523BFE592F517B" \
2531 "407A1BD76C164B93DA2D32A383E58357"
2532
2533#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2534 "F38D18D2B2F0E2DD275AA977E2BF4411" \
2535 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
2536 "A74206CEC169D74BF5A8C50D6F48EA08"
2537
2538#define PT_LEN 24
2539#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2540 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002543static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002544{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002545#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002546 size_t i;
2547
Paul Bakker545570e2010-07-18 09:00:25 +00002548 if( rng_state != NULL )
2549 rng_state = NULL;
2550
Paul Bakkera3d195c2011-11-27 21:07:34 +00002551 for( i = 0; i < len; ++i )
2552 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002553#else
2554 if( rng_state != NULL )
2555 rng_state = NULL;
2556
2557 arc4random_buf( output, len );
2558#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002559
Paul Bakkera3d195c2011-11-27 21:07:34 +00002560 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002563
Paul Bakker5121ce52009-01-03 21:22:43 +00002564/*
2565 * Checkup routine
2566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002568{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002569 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002571 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 unsigned char rsa_plaintext[PT_LEN];
2574 unsigned char rsa_decrypted[PT_LEN];
2575 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002577 unsigned char sha1sum[20];
2578#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Hanno Becker3a701162017-08-22 13:52:43 +01002580 mbedtls_mpi K;
2581
2582 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Hanno Becker3a701162017-08-22 13:52:43 +01002585 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2586 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2587 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2588 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2589 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2590 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2591 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2592 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2593 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2594 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2595
2596 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
2598 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2602 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 {
2604 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
2607 return( 1 );
2608 }
2609
Hanno Becker3a701162017-08-22 13:52:43 +01002610 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) );
2611 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) );
2612
2613 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) );
2614 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) );
2615
2616 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) );
2617 MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) );
2618
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002621
2622 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 rsa_plaintext, rsa_ciphertext ) != 0 )
2626 {
2627 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002629
2630 return( 1 );
2631 }
2632
2633 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00002637 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00002638 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 {
2640 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
2643 return( 1 );
2644 }
2645
2646 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2647 {
2648 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
2651 return( 1 );
2652 }
2653
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002654 if( verbose != 0 )
2655 mbedtls_printf( "passed\n" );
2656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002658 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002659 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 sha1sum, rsa_ciphertext ) != 0 )
2665 {
2666 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
2669 return( 1 );
2670 }
2671
2672 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 sha1sum, rsa_ciphertext ) != 0 )
2677 {
2678 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
2681 return( 1 );
2682 }
2683
2684 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002685 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002688 if( verbose != 0 )
2689 mbedtls_printf( "\n" );
2690
Paul Bakker3d8fb632014-04-17 12:42:41 +02002691cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002692 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 mbedtls_rsa_free( &rsa );
2694#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002695 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002697 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002698}
2699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#endif /* MBEDTLS_RSA_C */