blob: b6801882913cbb49fa4de01a3d7ca2c7f499fbf1 [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"
Hanno Beckera565f542017-10-11 11:00:19 +010049#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050051#include "mbedtls/platform_util.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000052
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <string.h>
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000061#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010065#else
Rich Evans00ab4702015-02-06 13:43:58 +000066#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020068#define mbedtls_calloc calloc
69#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010070#endif
71
Hanno Beckera565f542017-10-11 11:00:19 +010072#if !defined(MBEDTLS_RSA_ALT)
73
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010074#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +010075/* constant-time buffer comparison */
76static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
77{
78 size_t i;
79 const unsigned char *A = (const unsigned char *) a;
80 const unsigned char *B = (const unsigned char *) b;
81 unsigned char diff = 0;
82
83 for( i = 0; i < n; i++ )
84 diff |= A[i] ^ B[i];
85
86 return( diff );
87}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010088#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +010089
Hanno Becker617c1ae2017-08-23 14:11:24 +010090int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
91 const mbedtls_mpi *N,
92 const mbedtls_mpi *P, const mbedtls_mpi *Q,
93 const mbedtls_mpi *D, const mbedtls_mpi *E )
94{
95 int ret;
96
97 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
98 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
99 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
100 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
101 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
102 {
103 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
104 }
105
106 if( N != NULL )
107 ctx->len = mbedtls_mpi_size( &ctx->N );
108
109 return( 0 );
110}
111
112int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100113 unsigned char const *N, size_t N_len,
114 unsigned char const *P, size_t P_len,
115 unsigned char const *Q, size_t Q_len,
116 unsigned char const *D, size_t D_len,
117 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100118{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000119 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100120
121 if( N != NULL )
122 {
123 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
124 ctx->len = mbedtls_mpi_size( &ctx->N );
125 }
126
127 if( P != NULL )
128 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
129
130 if( Q != NULL )
131 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
132
133 if( D != NULL )
134 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
135
136 if( E != NULL )
137 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
138
139cleanup:
140
141 if( ret != 0 )
142 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
143
144 return( 0 );
145}
146
Hanno Becker705fc682017-10-10 17:57:02 +0100147/*
148 * Checks whether the context fields are set in such a way
149 * that the RSA primitives will be able to execute without error.
150 * It does *not* make guarantees for consistency of the parameters.
151 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100152static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
153 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100154{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100155#if !defined(MBEDTLS_RSA_NO_CRT)
156 /* blinding_needed is only used for NO_CRT to decide whether
157 * P,Q need to be present or not. */
158 ((void) blinding_needed);
159#endif
160
Hanno Becker3a760a12018-01-05 08:14:49 +0000161 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
162 ctx->len > MBEDTLS_MPI_MAX_SIZE )
163 {
Hanno Becker705fc682017-10-10 17:57:02 +0100164 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000165 }
Hanno Becker705fc682017-10-10 17:57:02 +0100166
167 /*
168 * 1. Modular exponentiation needs positive, odd moduli.
169 */
170
171 /* Modular exponentiation wrt. N is always used for
172 * RSA public key operations. */
173 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
174 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
175 {
176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
177 }
178
179#if !defined(MBEDTLS_RSA_NO_CRT)
180 /* Modular exponentiation for P and Q is only
181 * used for private key operations and if CRT
182 * is used. */
183 if( is_priv &&
184 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
185 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
186 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
187 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
188 {
189 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
190 }
191#endif /* !MBEDTLS_RSA_NO_CRT */
192
193 /*
194 * 2. Exponents must be positive
195 */
196
197 /* Always need E for public key operations */
198 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
199 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
200
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100201#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100202 /* For private key operations, use D or DP & DQ
203 * as (unblinded) exponents. */
204 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
206#else
207 if( is_priv &&
208 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
209 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
210 {
211 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
212 }
213#endif /* MBEDTLS_RSA_NO_CRT */
214
215 /* Blinding shouldn't make exponents negative either,
216 * so check that P, Q >= 1 if that hasn't yet been
217 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100218#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100219 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100220 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
221 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
222 {
223 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
224 }
225#endif
226
227 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100228 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100229#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100230 if( is_priv &&
231 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
232 {
233 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
234 }
235#endif
236
237 return( 0 );
238}
239
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100240int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100241{
242 int ret = 0;
243
Hanno Becker617c1ae2017-08-23 14:11:24 +0100244 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
245 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
246 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
247 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
248 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100249
Hanno Becker617c1ae2017-08-23 14:11:24 +0100250 /*
251 * Check whether provided parameters are enough
252 * to deduce all others. The following incomplete
253 * parameter sets for private keys are supported:
254 *
255 * (1) P, Q missing.
256 * (2) D and potentially N missing.
257 *
258 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100259
Hanno Becker2cca6f32017-09-29 11:46:40 +0100260 const int n_missing = have_P && have_Q && have_D && have_E;
261 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
262 const int d_missing = have_P && have_Q && !have_D && have_E;
263 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
264
265 /* These three alternatives are mutually exclusive */
266 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100267
Hanno Becker617c1ae2017-08-23 14:11:24 +0100268 if( !is_priv && !is_pub )
269 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
270
271 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100272 * Step 1: Deduce N if P, Q are provided.
273 */
274
275 if( !have_N && have_P && have_Q )
276 {
277 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
278 &ctx->Q ) ) != 0 )
279 {
280 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
281 }
282
283 ctx->len = mbedtls_mpi_size( &ctx->N );
284 }
285
286 /*
287 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100288 */
289
290 if( pq_missing )
291 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100292 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100293 &ctx->P, &ctx->Q );
294 if( ret != 0 )
295 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
296
297 }
298 else if( d_missing )
299 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100300 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
301 &ctx->Q,
302 &ctx->E,
303 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100304 {
305 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
306 }
307 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100308
Hanno Becker617c1ae2017-08-23 14:11:24 +0100309 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100310 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100311 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100312 */
313
Hanno Becker23344b52017-08-23 07:43:27 +0100314#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100315 if( is_priv )
316 {
317 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
318 &ctx->DP, &ctx->DQ, &ctx->QP );
319 if( ret != 0 )
320 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
321 }
Hanno Becker23344b52017-08-23 07:43:27 +0100322#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100323
324 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100325 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100326 */
327
Hanno Beckerebd2c022017-10-12 10:54:53 +0100328 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100329}
330
Hanno Becker617c1ae2017-08-23 14:11:24 +0100331int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
332 unsigned char *N, size_t N_len,
333 unsigned char *P, size_t P_len,
334 unsigned char *Q, size_t Q_len,
335 unsigned char *D, size_t D_len,
336 unsigned char *E, size_t E_len )
337{
338 int ret = 0;
339
340 /* Check if key is private or public */
341 const int is_priv =
342 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
343 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
344 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
345 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
346 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
347
348 if( !is_priv )
349 {
350 /* If we're trying to export private parameters for a public key,
351 * something must be wrong. */
352 if( P != NULL || Q != NULL || D != NULL )
353 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
354
355 }
356
357 if( N != NULL )
358 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
359
360 if( P != NULL )
361 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
362
363 if( Q != NULL )
364 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
365
366 if( D != NULL )
367 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
368
369 if( E != NULL )
370 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100371
372cleanup:
373
374 return( ret );
375}
376
Hanno Becker617c1ae2017-08-23 14:11:24 +0100377int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
378 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
379 mbedtls_mpi *D, mbedtls_mpi *E )
380{
381 int ret;
382
383 /* Check if key is private or public */
384 int is_priv =
385 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
386 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
387 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
388 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
389 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
390
391 if( !is_priv )
392 {
393 /* If we're trying to export private parameters for a public key,
394 * something must be wrong. */
395 if( P != NULL || Q != NULL || D != NULL )
396 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
397
398 }
399
400 /* Export all requested core parameters. */
401
402 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
403 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
404 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
405 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
406 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
407 {
408 return( ret );
409 }
410
411 return( 0 );
412}
413
414/*
415 * Export CRT parameters
416 * This must also be implemented if CRT is not used, for being able to
417 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
418 * can be used in this case.
419 */
420int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
421 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
422{
423 int ret;
424
425 /* Check if key is private or public */
426 int is_priv =
427 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
428 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
429 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
430 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
431 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
432
433 if( !is_priv )
434 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
435
Hanno Beckerdc95c892017-08-23 06:57:02 +0100436#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100437 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100438 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
439 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
440 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
441 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100442 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100443 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100444#else
445 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
446 DP, DQ, QP ) ) != 0 )
447 {
448 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
449 }
450#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100451
452 return( 0 );
453}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100454
Paul Bakker5121ce52009-01-03 21:22:43 +0000455/*
456 * Initialize an RSA context
457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000460 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000461{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#if defined(MBEDTLS_THREADING_C)
467 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200468#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000469}
470
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100471/*
472 * Set padding for an existing RSA context
473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100475{
476 ctx->padding = padding;
477 ctx->hash_id = hash_id;
478}
479
Hanno Becker617c1ae2017-08-23 14:11:24 +0100480/*
481 * Get length in bytes of RSA modulus
482 */
Gilles Peskinee19b7d52018-11-12 18:42:43 +0100483
Hanno Becker617c1ae2017-08-23 14:11:24 +0100484size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
485{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100486 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100487}
488
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492/*
493 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800494 *
495 * This generation method follows the RSA key pair generation procedure of
496 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000499 int (*f_rng)(void *, unsigned char *, size_t),
500 void *p_rng,
501 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000502{
503 int ret;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800504 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100505 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
Paul Bakker21eb2802010-08-16 11:10:02 +0000507 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
Janos Follathef441782016-09-21 13:18:12 +0100510 if( nbits % 2 )
511 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
512
Janos Follathb8fc1b02018-09-03 15:37:01 +0100513 /*
514 * If the modulus is 1024 bit long or shorter, then the security strength of
515 * the RSA algorithm is less than or equal to 80 bits and therefore an error
516 * rate of 2^-80 is sufficient.
517 */
518 if( nbits > 1024 )
519 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
520
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100521 mbedtls_mpi_init( &H );
522 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800523 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 /*
526 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800527 * 1. |P-Q| > 2^( nbits / 2 - 100 )
528 * 2. GCD( E, (P-1)*(Q-1) ) == 1
529 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
533 do
534 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100535 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
536 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
Janos Follathb8fc1b02018-09-03 15:37:01 +0100538 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
539 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800541 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
542 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
543 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 continue;
545
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800546 /* not required by any standards, but some users rely on the fact that P > Q */
547 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100548 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100549
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100550 /* Temporarily replace P,Q by P-1, Q-1 */
551 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
552 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
553 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800554
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800555 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800557 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
558 continue;
559
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800560 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Jethro Beekman97f95c92018-02-13 15:50:36 -0800561 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
562 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
563 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
564
565 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
566 continue;
567
568 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800570 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100572 /* Restore P,Q */
573 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
574 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
575
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800576 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
577
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100578 ctx->len = mbedtls_mpi_size( &ctx->N );
579
Jethro Beekman97f95c92018-02-13 15:50:36 -0800580#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 * DP = D mod (P - 1)
583 * DQ = D mod (Q - 1)
584 * QP = Q^-1 mod P
585 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100586 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
587 &ctx->DP, &ctx->DQ, &ctx->QP ) );
588#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
Hanno Becker83aad1f2017-08-23 06:45:10 +0100590 /* Double-check */
591 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
593cleanup:
594
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100595 mbedtls_mpi_free( &H );
596 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800597 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 if( ret != 0 )
600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_rsa_free( ctx );
602 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 }
604
Paul Bakker48377d92013-08-30 12:06:24 +0200605 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606}
607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
610/*
611 * Check a public RSA key
612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000614{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100615 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000617
Hanno Becker3a760a12018-01-05 08:14:49 +0000618 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100621 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
Hanno Becker705fc682017-10-10 17:57:02 +0100623 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
624 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100628 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630 return( 0 );
631}
632
633/*
Hanno Becker705fc682017-10-10 17:57:02 +0100634 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000637{
Hanno Becker705fc682017-10-10 17:57:02 +0100638 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100639 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 {
Hanno Becker98838b02017-10-02 13:16:10 +0100641 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 }
Paul Bakker48377d92013-08-30 12:06:24 +0200643
Hanno Becker98838b02017-10-02 13:16:10 +0100644 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100645 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100647 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000649
Hanno Beckerb269a852017-08-25 08:03:21 +0100650#if !defined(MBEDTLS_RSA_NO_CRT)
651 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
652 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
653 {
654 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
655 }
656#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000657
658 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000659}
660
661/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100662 * Check if contexts holding a public and private key match
663 */
Hanno Becker98838b02017-10-02 13:16:10 +0100664int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
665 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100666{
Hanno Becker98838b02017-10-02 13:16:10 +0100667 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100671 }
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
674 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100677 }
678
679 return( 0 );
680}
681
682/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 * Do an RSA public key operation
684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000686 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 unsigned char *output )
688{
Paul Bakker23986e52011-04-24 08:57:21 +0000689 int ret;
690 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Hanno Beckerebd2c022017-10-12 10:54:53 +0100693 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100694 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000697
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200698#if defined(MBEDTLS_THREADING_C)
699 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
700 return( ret );
701#endif
702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200707 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
708 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 }
710
711 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
713 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000714
715cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200717 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
718 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100719#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
723 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
726 return( 0 );
727}
728
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200729/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200730 * Generate or update blinding values, see section 10 of:
731 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200732 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200733 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200734 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200735static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200736 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
737{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200738 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200739
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200740 if( ctx->Vf.p != NULL )
741 {
742 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
744 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
745 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
746 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200747
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200748 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200749 }
750
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200751 /* Unblinding value: Vf = random number, invertible mod N */
752 do {
753 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
757 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
758 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200759
760 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
762 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 +0200763
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200764
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200765cleanup:
766 return( ret );
767}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200768
Paul Bakker5121ce52009-01-03 21:22:43 +0000769/*
Janos Follathe81102e2017-03-22 13:38:28 +0000770 * Exponent blinding supposed to prevent side-channel attacks using multiple
771 * traces of measurements to recover the RSA key. The more collisions are there,
772 * the more bits of the key can be recovered. See [3].
773 *
774 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
775 * observations on avarage.
776 *
777 * For example with 28 byte blinding to achieve 2 collisions the adversary has
778 * to make 2^112 observations on avarage.
779 *
780 * (With the currently (as of 2017 April) known best algorithms breaking 2048
781 * bit RSA requires approximately as much time as trying out 2^112 random keys.
782 * Thus in this sense with 28 byte blinding the security is not reduced by
783 * side-channel attacks like the one in [3])
784 *
785 * This countermeasure does not help if the key recovery is possible with a
786 * single trace.
787 */
788#define RSA_EXPONENT_BLINDING 28
789
790/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000791 * Do an RSA private key operation
792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200794 int (*f_rng)(void *, unsigned char *, size_t),
795 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000796 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000797 unsigned char *output )
798{
Paul Bakker23986e52011-04-24 08:57:21 +0000799 int ret;
800 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100801
802 /* Temporary holding the result */
803 mbedtls_mpi T;
804
805 /* Temporaries holding P-1, Q-1 and the
806 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000807 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100808
809#if !defined(MBEDTLS_RSA_NO_CRT)
810 /* Temporaries holding the results mod p resp. mod q. */
811 mbedtls_mpi TP, TQ;
812
813 /* Temporaries holding the blinded exponents for
814 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000815 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100816
817 /* Pointers to actual exponents to be used - either the unblinded
818 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000819 mbedtls_mpi *DP = &ctx->DP;
820 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100821#else
822 /* Temporary holding the blinded exponent (if used). */
823 mbedtls_mpi D_blind;
824
825 /* Pointer to actual exponent to be used - either the unblinded
826 * or the blinded one, depending on the presence of a PRNG. */
827 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100828#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100829
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100830 /* Temporaries holding the initial input and the double
831 * checked result; should be the same in the end. */
832 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000833
Hanno Beckerebd2c022017-10-12 10:54:53 +0100834 if( rsa_check_context( ctx, 1 /* private key checks */,
835 f_rng != NULL /* blinding y/n */ ) != 0 )
836 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100837 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100838 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100839
Hanno Becker06811ce2017-05-03 15:10:34 +0100840#if defined(MBEDTLS_THREADING_C)
841 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
842 return( ret );
843#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000844
Hanno Becker06811ce2017-05-03 15:10:34 +0100845 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100846 mbedtls_mpi_init( &T );
847
848 mbedtls_mpi_init( &P1 );
849 mbedtls_mpi_init( &Q1 );
850 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000851
Janos Follathf9203b42017-03-22 15:13:15 +0000852 if( f_rng != NULL )
853 {
Janos Follathe81102e2017-03-22 13:38:28 +0000854#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000855 mbedtls_mpi_init( &D_blind );
856#else
857 mbedtls_mpi_init( &DP_blind );
858 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000859#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000860 }
Janos Follathe81102e2017-03-22 13:38:28 +0000861
Hanno Becker06811ce2017-05-03 15:10:34 +0100862#if !defined(MBEDTLS_RSA_NO_CRT)
863 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200864#endif
865
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100866 mbedtls_mpi_init( &I );
867 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100868
869 /* End of MPI initialization */
870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
872 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200874 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
875 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 }
877
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100878 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100879
Paul Bakkerf451bac2013-08-30 15:37:02 +0200880 if( f_rng != NULL )
881 {
882 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200883 * Blinding
884 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200885 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200886 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
887 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000889
Janos Follathe81102e2017-03-22 13:38:28 +0000890 /*
891 * Exponent blinding
892 */
893 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
894 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
895
Janos Follathf9203b42017-03-22 15:13:15 +0000896#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000897 /*
898 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
899 */
900 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
901 f_rng, p_rng ) );
902 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
903 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
904 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
905
906 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000907#else
908 /*
909 * DP_blind = ( P - 1 ) * R + DP
910 */
911 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
912 f_rng, p_rng ) );
913 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
914 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
915 &ctx->DP ) );
916
917 DP = &DP_blind;
918
919 /*
920 * DQ_blind = ( Q - 1 ) * R + DQ
921 */
922 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
923 f_rng, p_rng ) );
924 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
925 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
926 &ctx->DQ ) );
927
928 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000929#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200930 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000933 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100934#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200935 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000936 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000937 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100938 * TP = input ^ dP mod P
939 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100941
942 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
943 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000944
945 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100946 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100948 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
949 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
950 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
952 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100953 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100955 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
956 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200958
Paul Bakkerf451bac2013-08-30 15:37:02 +0200959 if( f_rng != NULL )
960 {
961 /*
962 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200963 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200964 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200965 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200967 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
Hanno Becker2dec5e82017-10-03 07:49:52 +0100969 /* Verify the result to prevent glitching attacks. */
970 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
971 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100972 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +0100973 {
Hanno Becker06811ce2017-05-03 15:10:34 +0100974 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
975 goto cleanup;
976 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100977
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
981cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200983 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
984 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200985#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200986
Hanno Becker06811ce2017-05-03 15:10:34 +0100987 mbedtls_mpi_free( &P1 );
988 mbedtls_mpi_free( &Q1 );
989 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000990
991 if( f_rng != NULL )
992 {
Janos Follathe81102e2017-03-22 13:38:28 +0000993#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000994 mbedtls_mpi_free( &D_blind );
995#else
996 mbedtls_mpi_free( &DP_blind );
997 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000998#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000999 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
Hanno Becker06811ce2017-05-03 15:10:34 +01001001 mbedtls_mpi_free( &T );
1002
1003#if !defined(MBEDTLS_RSA_NO_CRT)
1004 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1005#endif
1006
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001007 mbedtls_mpi_free( &C );
1008 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001009
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
1013 return( 0 );
1014}
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001017/**
1018 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1019 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001020 * \param dst buffer to mask
1021 * \param dlen length of destination buffer
1022 * \param src source of the mask generation
1023 * \param slen length of the source buffer
1024 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001025 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001026static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001030 unsigned char counter[4];
1031 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001032 unsigned int hlen;
1033 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001034 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001037 memset( counter, 0, 4 );
1038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001040
Simon Butcher02037452016-03-01 21:19:12 +00001041 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001042 p = dst;
1043
1044 while( dlen > 0 )
1045 {
1046 use_len = hlen;
1047 if( dlen < hlen )
1048 use_len = dlen;
1049
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001050 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1051 goto exit;
1052 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1053 goto exit;
1054 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1055 goto exit;
1056 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1057 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001058
1059 for( i = 0; i < use_len; ++i )
1060 *p++ ^= mask[i];
1061
1062 counter[3]++;
1063
1064 dlen -= use_len;
1065 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001066
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001067exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001068 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001069
1070 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001071}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001075/*
1076 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001079 int (*f_rng)(void *, unsigned char *, size_t),
1080 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001081 int mode,
1082 const unsigned char *label, size_t label_len,
1083 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001084 const unsigned char *input,
1085 unsigned char *output )
1086{
1087 size_t olen;
1088 int ret;
1089 unsigned char *p = output;
1090 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 const mbedtls_md_info_t *md_info;
1092 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1095 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001096
1097 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001101 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001103
1104 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001106
Simon Butcher02037452016-03-01 21:19:12 +00001107 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001108 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001110
1111 memset( output, 0, olen );
1112
1113 *p++ = 0;
1114
Simon Butcher02037452016-03-01 21:19:12 +00001115 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001116 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001118
1119 p += hlen;
1120
Simon Butcher02037452016-03-01 21:19:12 +00001121 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001122 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1123 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001124 p += hlen;
1125 p += olen - 2 * hlen - 2 - ilen;
1126 *p++ = 1;
Gilles Peskine69e033a2018-07-06 15:47:54 +02001127 if( ilen != 0 )
1128 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001131 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001132 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001133
Simon Butcher02037452016-03-01 21:19:12 +00001134 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001135 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1136 &md_ctx ) ) != 0 )
1137 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001138
Simon Butcher02037452016-03-01 21:19:12 +00001139 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001140 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1141 &md_ctx ) ) != 0 )
1142 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001143
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001144exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001146
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001147 if( ret != 0 )
1148 return( ret );
1149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( ( mode == MBEDTLS_RSA_PUBLIC )
1151 ? mbedtls_rsa_public( ctx, output, output )
1152 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001157/*
1158 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001161 int (*f_rng)(void *, unsigned char *, size_t),
1162 void *p_rng,
1163 int mode, size_t ilen,
1164 const unsigned char *input,
1165 unsigned char *output )
1166{
1167 size_t nb_pad, olen;
1168 int ret;
1169 unsigned char *p = output;
1170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1172 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001173
Janos Follath1ed9f992016-03-18 11:45:44 +00001174 // We don't check p_rng because it won't be dereferenced here
Gilles Peskine69e033a2018-07-06 15:47:54 +02001175 if( f_rng == NULL || output == NULL )
1176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1177 if( ilen != 0 && input == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001179
1180 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001181
Simon Butcher02037452016-03-01 21:19:12 +00001182 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001183 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001185
1186 nb_pad = olen - 3 - ilen;
1187
1188 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001192
1193 while( nb_pad-- > 0 )
1194 {
1195 int rng_dl = 100;
1196
1197 do {
1198 ret = f_rng( p_rng, p, 1 );
1199 } while( *p == 0 && --rng_dl && ret == 0 );
1200
Simon Butcher02037452016-03-01 21:19:12 +00001201 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001202 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001204
1205 p++;
1206 }
1207 }
1208 else
1209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001211
1212 while( nb_pad-- > 0 )
1213 *p++ = 0xFF;
1214 }
1215
1216 *p++ = 0;
Gilles Peskine69e033a2018-07-06 15:47:54 +02001217 if( ilen != 0 )
1218 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 return( ( mode == MBEDTLS_RSA_PUBLIC )
1221 ? mbedtls_rsa_public( ctx, output, output )
1222 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001223}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001225
Paul Bakker5121ce52009-01-03 21:22:43 +00001226/*
1227 * Add the message padding, then do an RSA operation
1228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001230 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001231 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001232 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001233 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 unsigned char *output )
1235{
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 switch( ctx->padding )
1237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#if defined(MBEDTLS_PKCS1_V15)
1239 case MBEDTLS_RSA_PKCS_V15:
1240 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001241 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001242#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#if defined(MBEDTLS_PKCS1_V21)
1245 case MBEDTLS_RSA_PKCS_V21:
1246 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001247 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001248#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
1250 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001253}
1254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001256/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001257 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001260 int (*f_rng)(void *, unsigned char *, size_t),
1261 void *p_rng,
1262 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001263 const unsigned char *label, size_t label_len,
1264 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001265 const unsigned char *input,
1266 unsigned char *output,
1267 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001268{
Paul Bakker23986e52011-04-24 08:57:21 +00001269 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001270 size_t ilen, i, pad_len;
1271 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1273 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001274 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 const mbedtls_md_info_t *md_info;
1276 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001277
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001278 /*
1279 * Parameters sanity checks
1280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1282 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001283
1284 ilen = ctx->len;
1285
Paul Bakker27fdf462011-06-09 13:55:13 +00001286 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001290 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001292
Janos Follathc17cda12016-02-11 11:08:18 +00001293 hlen = mbedtls_md_get_size( md_info );
1294
1295 // checking for integer underflow
1296 if( 2 * hlen + 2 > ilen )
1297 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1298
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001299 /*
1300 * RSA operation
1301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1303 ? mbedtls_rsa_public( ctx, input, buf )
1304 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001305
1306 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001307 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001309 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001310 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001313 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1314 {
1315 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001316 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001317 }
1318
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001319 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001320 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1321 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001322 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001323 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1324 &md_ctx ) ) != 0 )
1325 {
1326 mbedtls_md_free( &md_ctx );
1327 goto cleanup;
1328 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001331
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001332 /* Generate lHash */
1333 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1334 goto cleanup;
1335
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001336 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001337 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001338 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001340 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001342 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001343
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001344 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001345
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001346 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001347 for( i = 0; i < hlen; i++ )
1348 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001349
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001350 /* Get zero-padding len, but always read till end of buffer
1351 * (minus one, for the 01 byte) */
1352 pad_len = 0;
1353 pad_done = 0;
1354 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1355 {
1356 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001357 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001358 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001359
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001360 p += pad_len;
1361 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001362
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001363 /*
1364 * The only information "leaked" is whether the padding was correct or not
1365 * (eg, no data is copied if it was not correct). This meets the
1366 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1367 * the different error conditions.
1368 */
1369 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001370 {
1371 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1372 goto cleanup;
1373 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001374
Paul Bakker66d5d072014-06-17 16:39:18 +02001375 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001376 {
1377 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1378 goto cleanup;
1379 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001380
1381 *olen = ilen - (p - buf);
Gilles Peskine69e033a2018-07-06 15:47:54 +02001382 if( *olen != 0 )
1383 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001384 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001385
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001386cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001387 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1388 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001389
1390 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001395/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1396 *
1397 * \param value The value to analyze.
Gilles Peskine331d80e2018-10-04 18:32:29 +02001398 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001399 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001400static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001401{
1402 /* MSVC has a warning about unary minus on unsigned, but this is
1403 * well-defined and precisely what we want to do here */
1404#if defined(_MSC_VER)
1405#pragma warning( push )
1406#pragma warning( disable : 4146 )
1407#endif
1408 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1409#if defined(_MSC_VER)
1410#pragma warning( pop )
1411#endif
1412}
1413
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001414/** Check whether a size is out of bounds, without branches.
1415 *
1416 * This is equivalent to `size > max`, but is likely to be compiled to
1417 * to code using bitwise operation rather than a branch.
1418 *
1419 * \param size Size to check.
1420 * \param max Maximum desired value for \p size.
1421 * \return \c 0 if `size <= max`.
1422 * \return \c 1 if `size > max`.
1423 */
1424static unsigned size_greater_than( size_t size, size_t max )
1425{
1426 /* Return the sign bit (1 for negative) of (max - size). */
1427 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1428}
1429
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001430/** Choose between two integer values, without branches.
1431 *
Gilles Peskine331d80e2018-10-04 18:32:29 +02001432 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1433 * to code using bitwise operation rather than a branch.
1434 *
1435 * \param cond Condition to test.
1436 * \param if1 Value to use if \p cond is nonzero.
1437 * \param if0 Value to use if \p cond is zero.
1438 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001439 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001440static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001441{
Gilles Peskine331d80e2018-10-04 18:32:29 +02001442 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001443 return( ( mask & if1 ) | (~mask & if0 ) );
1444}
1445
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001446/** Shift some data towards the left inside a buffer without leaking
1447 * the length of the data through side channels.
1448 *
1449 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1450 * ```
1451 * memmove(start, start + offset, total - offset);
1452 * memset(start + offset, 0, total - offset);
1453 * ```
1454 * but it strives to use a memory access pattern (and thus total timing)
1455 * that does not depend on \p offset. This timing independence comes at
1456 * the expense of performance.
1457 *
1458 * \param start Pointer to the start of the buffer.
1459 * \param total Total size of the buffer.
1460 * \param offset Offset from which to copy \p total - \p offset bytes.
1461 */
1462static void mem_move_to_left( void *start,
1463 size_t total,
1464 size_t offset )
1465{
1466 volatile unsigned char *buf = start;
1467 size_t i, n;
1468 if( total == 0 )
1469 return;
1470 for( i = 0; i < total; i++ )
1471 {
1472 unsigned no_op = size_greater_than( total - offset, i );
1473 /* The first `total - offset` passes are a no-op. The last
1474 * `offset` passes shift the data one byte to the left and
1475 * zero out the last byte. */
1476 for( n = 0; n < total - 1; n++ )
Gilles Peskine9b430702018-10-12 19:15:34 +02001477 {
1478 unsigned char current = buf[n];
1479 unsigned char next = buf[n+1];
1480 buf[n] = if_int( no_op, current, next );
1481 }
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001482 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1483 }
1484}
1485
Paul Bakkerb3869132013-02-28 17:21:01 +01001486/*
1487 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001490 int (*f_rng)(void *, unsigned char *, size_t),
1491 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001492 int mode, size_t *olen,
1493 const unsigned char *input,
1494 unsigned char *output,
Gilles Peskine5908dd42018-10-02 22:43:06 +02001495 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001496{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001497 int ret;
Gilles Peskine5908dd42018-10-02 22:43:06 +02001498 size_t ilen = ctx->len;
Gilles Peskine5908dd42018-10-02 22:43:06 +02001499 size_t i;
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001500 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1501 ilen - 11 :
1502 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine85a74422018-10-05 14:50:21 +02001504 /* The following variables take sensitive values: their value must
1505 * not leak into the observable behavior of the function other than
1506 * the designated outputs (output, olen, return value). Otherwise
1507 * this would open the execution of the function to
1508 * side-channel-based variants of the Bleichenbacher padding oracle
1509 * attack. Potential side channels include overall timing, memory
1510 * access patterns (especially visible to an adversary who has access
1511 * to a shared memory cache), and branches (especially visible to
1512 * an adversary who has access to a shared code cache or to a shared
1513 * branch predictor). */
1514 size_t pad_count = 0;
1515 unsigned bad = 0;
1516 unsigned char pad_done = 0;
1517 size_t plaintext_size = 0;
1518 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1521 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001522
Paul Bakkerb3869132013-02-28 17:21:01 +01001523 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1527 ? mbedtls_rsa_public( ctx, input, buf )
1528 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001529
1530 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001531 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001532
Gilles Peskine40b57f42018-10-05 15:06:12 +02001533 /* Check and get padding length in constant time and constant
1534 * memory trace. The first byte must be 0. */
1535 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001538 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001539 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1540 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001541 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001542
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001543 /* Read the whole buffer. Set pad_done to nonzero if we find
1544 * the 0x00 byte and remember the padding length in pad_count. */
1545 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001546 {
Gilles Peskine85a74422018-10-05 14:50:21 +02001547 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001548 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001549 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001550 }
1551 else
1552 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001553 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1554 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001555 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001556
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001557 /* Read the whole buffer. Set pad_done to nonzero if we find
1558 * the 0x00 byte and remember the padding length in pad_count.
1559 * If there's a non-0xff byte in the padding, the padding is bad. */
1560 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001561 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001562 pad_done |= if_int( buf[i], 0, 1 );
1563 pad_count += if_int( pad_done, 0, 1 );
1564 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001565 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 }
1567
Gilles Peskine40b57f42018-10-05 15:06:12 +02001568 /* If pad_done is still zero, there's no data, only unfinished padding. */
1569 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001570
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001571 /* There must be at least 8 bytes of padding. */
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001572 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001573
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001574 /* If the padding is valid, set plaintext_size to the number of
1575 * remaining bytes after stripping the padding. If the padding
1576 * is invalid, avoid leaking this fact through the size of the
1577 * output: use the maximum message size that fits in the output
1578 * buffer. Do it without branches to avoid leaking the padding
1579 * validity through timing. RSA keys are small enough that all the
1580 * size_t values involved fit in unsigned int. */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001581 plaintext_size = if_int( bad,
1582 (unsigned) plaintext_max_size,
Gilles Peskine85a74422018-10-05 14:50:21 +02001583 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001584
Gilles Peskine9265ff42018-10-04 19:13:43 +02001585 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001586 * buffer and to 1 otherwise. */
1587 output_too_large = size_greater_than( plaintext_size,
1588 plaintext_max_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001589
Gilles Peskine9265ff42018-10-04 19:13:43 +02001590 /* Set ret without branches to avoid timing attacks. Return:
1591 * - INVALID_PADDING if the padding is bad (bad != 0).
1592 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1593 * plaintext does not fit in the output buffer.
1594 * - 0 if the padding is correct. */
Gilles Peskine48992472018-10-12 19:19:12 +02001595 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1596 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1597 0 ) );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001598
Gilles Peskine9265ff42018-10-04 19:13:43 +02001599 /* If the padding is bad or the plaintext is too large, zero the
1600 * data that we're about to copy to the output buffer.
1601 * We need to copy the same amount of data
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001602 * from the same buffer whether the padding is good or not to
1603 * avoid leaking the padding validity through overall timing or
1604 * through memory or cache access patterns. */
Gilles Peskine40b57f42018-10-05 15:06:12 +02001605 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001606 for( i = 11; i < ilen; i++ )
Gilles Peskine40b57f42018-10-05 15:06:12 +02001607 buf[i] &= ~bad;
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001608
Gilles Peskine9265ff42018-10-04 19:13:43 +02001609 /* If the plaintext is too large, truncate it to the buffer size.
1610 * Copy anyway to avoid revealing the length through timing, because
1611 * revealing the length is as bad as revealing the padding validity
1612 * for a Bleichenbacher attack. */
1613 plaintext_size = if_int( output_too_large,
1614 (unsigned) plaintext_max_size,
1615 (unsigned) plaintext_size );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001616
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001617 /* Move the plaintext to the leftmost position where it can start in
1618 * the working buffer, i.e. make it start plaintext_max_size from
1619 * the end of the buffer. Do this with a memory access trace that
1620 * does not depend on the plaintext size. After this move, the
1621 * starting location of the plaintext is no longer sensitive
1622 * information. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001623 mem_move_to_left( buf + ilen - plaintext_max_size,
1624 plaintext_max_size,
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001625 plaintext_max_size - plaintext_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001626
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001627 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskine9265ff42018-10-04 19:13:43 +02001628 * into the output buffer. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001629 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001630
1631 /* Report the amount of data we copied to the output buffer. In case
1632 * of errors (bad padding or output too large), the value of *olen
1633 * when this function returns is not specified. Making it equivalent
1634 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001635 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001637cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001638 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001639
1640 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001641}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001643
1644/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001645 * Do an RSA operation, then remove the message padding
1646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001648 int (*f_rng)(void *, unsigned char *, size_t),
1649 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001650 int mode, size_t *olen,
1651 const unsigned char *input,
1652 unsigned char *output,
1653 size_t output_max_len)
1654{
1655 switch( ctx->padding )
1656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_PKCS1_V15)
1658 case MBEDTLS_RSA_PKCS_V15:
1659 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001660 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001661#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#if defined(MBEDTLS_PKCS1_V21)
1664 case MBEDTLS_RSA_PKCS_V21:
1665 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001666 olen, input, output,
1667 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001668#endif
1669
1670 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001672 }
1673}
1674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001676/*
1677 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001680 int (*f_rng)(void *, unsigned char *, size_t),
1681 void *p_rng,
1682 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001684 unsigned int hashlen,
1685 const unsigned char *hash,
1686 unsigned char *sig )
1687{
1688 size_t olen;
1689 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001691 size_t slen, min_slen, hlen, offset = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001692 int ret;
1693 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 const mbedtls_md_info_t *md_info;
1695 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1698 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001699
1700 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001702
1703 olen = ctx->len;
1704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001706 {
Simon Butcher02037452016-03-01 21:19:12 +00001707 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001709 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001713 }
1714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001716 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001720
Jaeden Amero3725bb22018-09-07 19:12:36 +01001721 /* Calculate the largest possible salt length. Normally this is the hash
1722 * length, which is the maximum length the salt can have. If there is not
1723 * enough room, use the maximum salt length that fits. The constraint is
1724 * that the hash length plus the salt length plus 2 bytes must be at most
1725 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1726 * (PKCS#1 v2.2) §9.1.1 step 3. */
1727 min_slen = hlen - 2;
1728 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001730 else if( olen >= hlen + hlen + 2 )
1731 slen = hlen;
1732 else
1733 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001734
1735 memset( sig, 0, olen );
1736
Simon Butcher02037452016-03-01 21:19:12 +00001737 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001738 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001740
Simon Butcher02037452016-03-01 21:19:12 +00001741 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001742 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001743 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001744 *p++ = 0x01;
1745 memcpy( p, salt, slen );
1746 p += slen;
1747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001749 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001750 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001751
Simon Butcher02037452016-03-01 21:19:12 +00001752 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001753 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1754 goto exit;
1755 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1756 goto exit;
1757 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1758 goto exit;
1759 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1760 goto exit;
1761 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1762 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001763
Simon Butcher02037452016-03-01 21:19:12 +00001764 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001765 if( msb % 8 == 0 )
1766 offset = 1;
1767
Simon Butcher02037452016-03-01 21:19:12 +00001768 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001769 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1770 &md_ctx ) ) != 0 )
1771 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001772
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001773 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001774 sig[0] &= 0xFF >> ( olen * 8 - msb );
1775
1776 p += hlen;
1777 *p++ = 0xBC;
1778
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001779 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001780
1781exit:
1782 mbedtls_md_free( &md_ctx );
1783
1784 if( ret != 0 )
1785 return( ret );
1786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 return( ( mode == MBEDTLS_RSA_PUBLIC )
1788 ? mbedtls_rsa_public( ctx, sig, sig )
1789 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001790}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001794/*
1795 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1796 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001797
1798/* Construct a PKCS v1.5 encoding of a hashed message
1799 *
1800 * This is used both for signature generation and verification.
1801 *
1802 * Parameters:
1803 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001804 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001805 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001806 * - hash: Buffer containing the hashed message or the raw data.
1807 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001808 * - dst: Buffer to hold the encoded message.
1809 *
1810 * Assumptions:
1811 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1812 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001813 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001814 *
1815 */
1816static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1817 unsigned int hashlen,
1818 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001819 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001820 unsigned char *dst )
1821{
1822 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001823 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001824 unsigned char *p = dst;
1825 const char *oid = NULL;
1826
1827 /* Are we signing hashed or raw data? */
1828 if( md_alg != MBEDTLS_MD_NONE )
1829 {
1830 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1831 if( md_info == NULL )
1832 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1833
1834 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1835 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1836
1837 hashlen = mbedtls_md_get_size( md_info );
1838
1839 /* Double-check that 8 + hashlen + oid_size can be used as a
1840 * 1-byte ASN.1 length encoding and that there's no overflow. */
1841 if( 8 + hashlen + oid_size >= 0x80 ||
1842 10 + hashlen < hashlen ||
1843 10 + hashlen + oid_size < 10 + hashlen )
1844 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1845
1846 /*
1847 * Static bounds check:
1848 * - Need 10 bytes for five tag-length pairs.
1849 * (Insist on 1-byte length encodings to protect against variants of
1850 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1851 * - Need hashlen bytes for hash
1852 * - Need oid_size bytes for hash alg OID.
1853 */
1854 if( nb_pad < 10 + hashlen + oid_size )
1855 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1856 nb_pad -= 10 + hashlen + oid_size;
1857 }
1858 else
1859 {
1860 if( nb_pad < hashlen )
1861 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1862
1863 nb_pad -= hashlen;
1864 }
1865
Hanno Becker2b2f8982017-09-27 17:10:03 +01001866 /* Need space for signature header and padding delimiter (3 bytes),
1867 * and 8 bytes for the minimal padding */
1868 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001869 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1870 nb_pad -= 3;
1871
1872 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001873 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001874
1875 /* Write signature header and padding */
1876 *p++ = 0;
1877 *p++ = MBEDTLS_RSA_SIGN;
1878 memset( p, 0xFF, nb_pad );
1879 p += nb_pad;
1880 *p++ = 0;
1881
1882 /* Are we signing raw data? */
1883 if( md_alg == MBEDTLS_MD_NONE )
1884 {
1885 memcpy( p, hash, hashlen );
1886 return( 0 );
1887 }
1888
1889 /* Signing hashed data, add corresponding ASN.1 structure
1890 *
1891 * DigestInfo ::= SEQUENCE {
1892 * digestAlgorithm DigestAlgorithmIdentifier,
1893 * digest Digest }
1894 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1895 * Digest ::= OCTET STRING
1896 *
1897 * Schematic:
1898 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1899 * TAG-NULL + LEN [ NULL ] ]
1900 * TAG-OCTET + LEN [ HASH ] ]
1901 */
1902 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001903 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001904 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001905 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001906 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001907 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001908 memcpy( p, oid, oid_size );
1909 p += oid_size;
1910 *p++ = MBEDTLS_ASN1_NULL;
1911 *p++ = 0x00;
1912 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001913 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001914 memcpy( p, hash, hashlen );
1915 p += hashlen;
1916
1917 /* Just a sanity-check, should be automatic
1918 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001919 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001920 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001921 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001922 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1923 }
1924
1925 return( 0 );
1926}
1927
Paul Bakkerb3869132013-02-28 17:21:01 +01001928/*
1929 * Do an RSA operation to sign the message digest
1930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001932 int (*f_rng)(void *, unsigned char *, size_t),
1933 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001934 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001936 unsigned int hashlen,
1937 const unsigned char *hash,
1938 unsigned char *sig )
1939{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001940 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001941 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1944 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001945
Hanno Beckerfdf38032017-09-06 12:35:55 +01001946 /*
1947 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1948 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001949
Hanno Beckerfdf38032017-09-06 12:35:55 +01001950 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1951 ctx->len, sig ) ) != 0 )
1952 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001953
1954 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001955 * Call respective RSA primitive
1956 */
1957
1958 if( mode == MBEDTLS_RSA_PUBLIC )
1959 {
1960 /* Skip verification on a public key operation */
1961 return( mbedtls_rsa_public( ctx, sig, sig ) );
1962 }
1963
1964 /* Private key operation
1965 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001966 * In order to prevent Lenstra's attack, make the signature in a
1967 * temporary buffer and check it before returning it.
1968 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001969
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001970 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001971 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001972 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1973
Hanno Beckerfdf38032017-09-06 12:35:55 +01001974 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001975 if( verif == NULL )
1976 {
1977 mbedtls_free( sig_try );
1978 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1979 }
1980
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001981 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1982 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1983
Hanno Becker171a8f12017-09-06 12:32:16 +01001984 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001985 {
1986 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1987 goto cleanup;
1988 }
1989
1990 memcpy( sig, sig_try, ctx->len );
1991
1992cleanup:
1993 mbedtls_free( sig_try );
1994 mbedtls_free( verif );
1995
1996 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001997}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001999
2000/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 * Do an RSA operation to sign the message digest
2002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002004 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002005 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002008 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002009 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 unsigned char *sig )
2011{
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 switch( ctx->padding )
2013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#if defined(MBEDTLS_PKCS1_V15)
2015 case MBEDTLS_RSA_PKCS_V15:
2016 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002017 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002018#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020#if defined(MBEDTLS_PKCS1_V21)
2021 case MBEDTLS_RSA_PKCS_V21:
2022 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002023 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002024#endif
2025
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002029}
2030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002032/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002033 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002036 int (*f_rng)(void *, unsigned char *, size_t),
2037 void *p_rng,
2038 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002040 unsigned int hashlen,
2041 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002043 int expected_salt_len,
2044 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002045{
Paul Bakker23986e52011-04-24 08:57:21 +00002046 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002047 size_t siglen;
2048 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002049 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002051 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002052 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002053 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 const mbedtls_md_info_t *md_info;
2055 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002056 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2059 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002060
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 siglen = ctx->len;
2062
Paul Bakker27fdf462011-06-09 13:55:13 +00002063 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2067 ? mbedtls_rsa_public( ctx, sig, buf )
2068 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002069
2070 if( ret != 0 )
2071 return( ret );
2072
2073 p = buf;
2074
Paul Bakkerb3869132013-02-28 17:21:01 +01002075 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 {
Simon Butcher02037452016-03-01 21:19:12 +00002080 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002082 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002086 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002089 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002093
Paul Bakkerb3869132013-02-28 17:21:01 +01002094 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002095
Simon Butcher02037452016-03-01 21:19:12 +00002096 /*
2097 * Note: EMSA-PSS verification is over the length of N - 1 bits
2098 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002099 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002100
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002101 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2102 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2103
Simon Butcher02037452016-03-01 21:19:12 +00002104 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002105 if( msb % 8 == 0 )
2106 {
2107 p++;
2108 siglen -= 1;
2109 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002110
Gilles Peskine139108a2017-10-18 19:03:42 +02002111 if( siglen < hlen + 2 )
2112 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2113 hash_start = p + siglen - hlen - 1;
2114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002116 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002117 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002118
Jaeden Amero66954e12018-01-25 16:05:54 +00002119 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2120 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002121 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002122
Paul Bakkerb3869132013-02-28 17:21:01 +01002123 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002124
Gilles Peskine6a54b022017-10-17 19:02:13 +02002125 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002126 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002127
Gilles Peskine91048a32017-10-19 17:46:14 +02002128 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002129 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002130 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2131 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002132 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002133
Gilles Peskine6a54b022017-10-17 19:02:13 +02002134 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002137 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002138 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002139 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2140 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002141 }
2142
Simon Butcher02037452016-03-01 21:19:12 +00002143 /*
2144 * Generate H = Hash( M' )
2145 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002146 ret = mbedtls_md_starts( &md_ctx );
2147 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002148 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002149 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2150 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002151 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002152 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2153 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002154 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002155 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2156 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002157 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002158 ret = mbedtls_md_finish( &md_ctx, result );
2159 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002160 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002161
Jaeden Amero66954e12018-01-25 16:05:54 +00002162 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002163 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002164 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002165 goto exit;
2166 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002167
2168exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002170
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002171 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002172}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002173
2174/*
2175 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002178 int (*f_rng)(void *, unsigned char *, size_t),
2179 void *p_rng,
2180 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002182 unsigned int hashlen,
2183 const unsigned char *hash,
2184 const unsigned char *sig )
2185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2187 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002188 : md_alg;
2189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002191 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002193 sig ) );
2194
2195}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002199/*
2200 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002203 int (*f_rng)(void *, unsigned char *, size_t),
2204 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002205 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002207 unsigned int hashlen,
2208 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002209 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002210{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002211 int ret = 0;
2212 const size_t sig_len = ctx->len;
2213 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2216 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002217
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002218 /*
2219 * Prepare expected PKCS1 v1.5 encoding of hash.
2220 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002221
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002222 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2223 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2224 {
2225 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2226 goto cleanup;
2227 }
2228
2229 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2230 encoded_expected ) ) != 0 )
2231 goto cleanup;
2232
2233 /*
2234 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2235 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002238 ? mbedtls_rsa_public( ctx, sig, encoded )
2239 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002240 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002241 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002242
Simon Butcher02037452016-03-01 21:19:12 +00002243 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002244 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002245 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002246
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002247 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2248 sig_len ) ) != 0 )
2249 {
2250 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2251 goto cleanup;
2252 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002253
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002254cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002255
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002256 if( encoded != NULL )
2257 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002258 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002259 mbedtls_free( encoded );
2260 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002261
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002262 if( encoded_expected != NULL )
2263 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002264 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002265 mbedtls_free( encoded_expected );
2266 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002267
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002268 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002269}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002271
2272/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002273 * Do an RSA operation and check the message digest
2274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002276 int (*f_rng)(void *, unsigned char *, size_t),
2277 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002278 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002280 unsigned int hashlen,
2281 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002282 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002283{
2284 switch( ctx->padding )
2285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#if defined(MBEDTLS_PKCS1_V15)
2287 case MBEDTLS_RSA_PKCS_V15:
2288 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002289 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002290#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292#if defined(MBEDTLS_PKCS1_V21)
2293 case MBEDTLS_RSA_PKCS_V21:
2294 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002295 hashlen, hash, sig );
2296#endif
2297
2298 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002300 }
2301}
2302
2303/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002304 * Copy the components of an RSA key
2305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002307{
2308 int ret;
2309
2310 dst->ver = src->ver;
2311 dst->len = src->len;
2312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2314 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2317 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2318 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002319
2320#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2322 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2323 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2325 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002326#endif
2327
2328 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2331 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002332
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002333 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002334 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002335
2336cleanup:
2337 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002339
2340 return( ret );
2341}
2342
2343/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 * Free the components of an RSA key
2345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002349 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2350 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002352
Hanno Becker33c30a02017-08-23 07:00:22 +01002353#if !defined(MBEDTLS_RSA_NO_CRT)
2354 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2355 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2356 mbedtls_mpi_free( &ctx->DP );
2357#endif /* MBEDTLS_RSA_NO_CRT */
2358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359#if defined(MBEDTLS_THREADING_C)
2360 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002361#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002362}
2363
Hanno Beckerab377312017-08-23 16:24:51 +01002364#endif /* !MBEDTLS_RSA_ALT */
2365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002368#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
2370/*
2371 * Example RSA-1024 keypair, for test purposes
2372 */
2373#define KEY_LEN 128
2374
2375#define RSA_N "9292758453063D803DD603D5E777D788" \
2376 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2377 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2378 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2379 "93A89813FBF3C4F8066D2D800F7C38A8" \
2380 "1AE31942917403FF4946B0A83D3D3E05" \
2381 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2382 "5E94BB77B07507233A0BC7BAC8F90F79"
2383
2384#define RSA_E "10001"
2385
2386#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2387 "66CA472BC44D253102F8B4A9D3BFA750" \
2388 "91386C0077937FE33FA3252D28855837" \
2389 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2390 "DF79C5CE07EE72C7F123142198164234" \
2391 "CABB724CF78B8173B9F880FC86322407" \
2392 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2393 "071513A1E85B5DFA031F21ECAE91A34D"
2394
2395#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2396 "2C01CAD19EA484A87EA4377637E75500" \
2397 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2398 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2399
2400#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2401 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2402 "910E4168387E3C30AA1E00C339A79508" \
2403 "8452DD96A9A5EA5D9DCA68DA636032AF"
2404
Paul Bakker5121ce52009-01-03 21:22:43 +00002405#define PT_LEN 24
2406#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2407 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002410static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002411{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002412#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002413 size_t i;
2414
Paul Bakker545570e2010-07-18 09:00:25 +00002415 if( rng_state != NULL )
2416 rng_state = NULL;
2417
Paul Bakkera3d195c2011-11-27 21:07:34 +00002418 for( i = 0; i < len; ++i )
2419 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002420#else
2421 if( rng_state != NULL )
2422 rng_state = NULL;
2423
2424 arc4random_buf( output, len );
2425#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002426
Paul Bakkera3d195c2011-11-27 21:07:34 +00002427 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002428}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002430
Paul Bakker5121ce52009-01-03 21:22:43 +00002431/*
2432 * Checkup routine
2433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002435{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002436 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002438 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 unsigned char rsa_plaintext[PT_LEN];
2441 unsigned char rsa_decrypted[PT_LEN];
2442 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002444 unsigned char sha1sum[20];
2445#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
Hanno Becker3a701162017-08-22 13:52:43 +01002447 mbedtls_mpi K;
2448
2449 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
Hanno Becker3a701162017-08-22 13:52:43 +01002452 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2453 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2454 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2455 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2456 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2457 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2458 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2459 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2460 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2461 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2462
Hanno Becker7f25f852017-10-10 16:56:22 +01002463 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
2465 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2469 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 {
2471 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Hanno Becker5bc87292017-05-03 15:09:31 +01002474 ret = 1;
2475 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 }
2477
2478 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
2481 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2482
Hanno Becker98838b02017-10-02 13:16:10 +01002483 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2484 PT_LEN, rsa_plaintext,
2485 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 {
2487 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
Hanno Becker5bc87292017-05-03 15:09:31 +01002490 ret = 1;
2491 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 }
2493
2494 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Hanno Becker98838b02017-10-02 13:16:10 +01002497 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2498 &len, rsa_ciphertext, rsa_decrypted,
2499 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 {
2501 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Hanno Becker5bc87292017-05-03 15:09:31 +01002504 ret = 1;
2505 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 }
2507
2508 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2509 {
2510 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Hanno Becker5bc87292017-05-03 15:09:31 +01002513 ret = 1;
2514 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 }
2516
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002517 if( verbose != 0 )
2518 mbedtls_printf( "passed\n" );
2519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002522 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002524 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002525 {
2526 if( verbose != 0 )
2527 mbedtls_printf( "failed\n" );
2528
2529 return( 1 );
2530 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Hanno Becker98838b02017-10-02 13:16:10 +01002532 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2533 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2534 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 {
2536 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002538
Hanno Becker5bc87292017-05-03 15:09:31 +01002539 ret = 1;
2540 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 }
2542
2543 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Hanno Becker98838b02017-10-02 13:16:10 +01002546 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2547 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2548 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 {
2550 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
Hanno Becker5bc87292017-05-03 15:09:31 +01002553 ret = 1;
2554 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 }
2556
2557 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002558 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002561 if( verbose != 0 )
2562 mbedtls_printf( "\n" );
2563
Paul Bakker3d8fb632014-04-17 12:42:41 +02002564cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002565 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 mbedtls_rsa_free( &rsa );
2567#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002568 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002570 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571}
2572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575#endif /* MBEDTLS_RSA_C */