blob: f581fd73c3d4ad490b8f25919b6f5866bd4dc3b5 [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 */
483
484size_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;
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
Paul Bakker21eb2802010-08-16 11:10:02 +0000506 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
Janos Follathef441782016-09-21 13:18:12 +0100509 if( nbits % 2 )
510 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
511
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100512 mbedtls_mpi_init( &H );
513 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800514 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
516 /*
517 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800518 * 1. |P-Q| > 2^( nbits / 2 - 100 )
519 * 2. GCD( E, (P-1)*(Q-1) ) == 1
520 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524 do
525 {
Janos Follath10c575b2016-02-23 14:42:48 +0000526 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100527 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
Janos Follathef441782016-09-21 13:18:12 +0100529 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100530 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800532 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
533 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
534 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 continue;
536
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800537 /* not required by any standards, but some users rely on the fact that P > Q */
538 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100539 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100540
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100541 /* Temporarily replace P,Q by P-1, Q-1 */
542 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
543 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
544 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800545
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800546 /* 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 +0200547 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800548 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
549 continue;
550
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800551 /* 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 -0800552 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
553 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
554 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
555
556 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
557 continue;
558
559 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800561 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100563 /* Restore P,Q */
564 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
565 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
566
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800567 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
568
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100569 ctx->len = mbedtls_mpi_size( &ctx->N );
570
Jethro Beekman97f95c92018-02-13 15:50:36 -0800571#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000573 * DP = D mod (P - 1)
574 * DQ = D mod (Q - 1)
575 * QP = Q^-1 mod P
576 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100577 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
578 &ctx->DP, &ctx->DQ, &ctx->QP ) );
579#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Hanno Becker83aad1f2017-08-23 06:45:10 +0100581 /* Double-check */
582 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
584cleanup:
585
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100586 mbedtls_mpi_free( &H );
587 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800588 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590 if( ret != 0 )
591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_rsa_free( ctx );
593 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 }
595
Paul Bakker48377d92013-08-30 12:06:24 +0200596 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597}
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601/*
602 * Check a public RSA key
603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000605{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100606 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000608
Hanno Becker3a760a12018-01-05 08:14:49 +0000609 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100612 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
Hanno Becker705fc682017-10-10 17:57:02 +0100614 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
615 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100619 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
621 return( 0 );
622}
623
624/*
Hanno Becker705fc682017-10-10 17:57:02 +0100625 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000628{
Hanno Becker705fc682017-10-10 17:57:02 +0100629 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100630 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 {
Hanno Becker98838b02017-10-02 13:16:10 +0100632 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 }
Paul Bakker48377d92013-08-30 12:06:24 +0200634
Hanno Becker98838b02017-10-02 13:16:10 +0100635 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100636 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100638 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000640
Hanno Beckerb269a852017-08-25 08:03:21 +0100641#if !defined(MBEDTLS_RSA_NO_CRT)
642 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
643 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
644 {
645 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
646 }
647#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000648
649 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000650}
651
652/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100653 * Check if contexts holding a public and private key match
654 */
Hanno Becker98838b02017-10-02 13:16:10 +0100655int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
656 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100657{
Hanno Becker98838b02017-10-02 13:16:10 +0100658 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100662 }
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
665 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100668 }
669
670 return( 0 );
671}
672
673/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000674 * Do an RSA public key operation
675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000677 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 unsigned char *output )
679{
Paul Bakker23986e52011-04-24 08:57:21 +0000680 int ret;
681 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Hanno Beckerebd2c022017-10-12 10:54:53 +0100684 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100685 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200689#if defined(MBEDTLS_THREADING_C)
690 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
691 return( ret );
692#endif
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200698 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
699 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 }
701
702 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
704 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
706cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200708 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
709 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100710#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000713
714 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
717 return( 0 );
718}
719
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200720/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200721 * Generate or update blinding values, see section 10 of:
722 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200723 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200724 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200725 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200726static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200727 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
728{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200729 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200730
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200731 if( ctx->Vf.p != NULL )
732 {
733 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
735 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
736 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
737 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200738
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200739 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200740 }
741
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200742 /* Unblinding value: Vf = random number, invertible mod N */
743 do {
744 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
748 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
749 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200750
751 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
753 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 +0200754
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200755
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200756cleanup:
757 return( ret );
758}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200759
Paul Bakker5121ce52009-01-03 21:22:43 +0000760/*
Janos Follathe81102e2017-03-22 13:38:28 +0000761 * Exponent blinding supposed to prevent side-channel attacks using multiple
762 * traces of measurements to recover the RSA key. The more collisions are there,
763 * the more bits of the key can be recovered. See [3].
764 *
765 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
766 * observations on avarage.
767 *
768 * For example with 28 byte blinding to achieve 2 collisions the adversary has
769 * to make 2^112 observations on avarage.
770 *
771 * (With the currently (as of 2017 April) known best algorithms breaking 2048
772 * bit RSA requires approximately as much time as trying out 2^112 random keys.
773 * Thus in this sense with 28 byte blinding the security is not reduced by
774 * side-channel attacks like the one in [3])
775 *
776 * This countermeasure does not help if the key recovery is possible with a
777 * single trace.
778 */
779#define RSA_EXPONENT_BLINDING 28
780
781/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000782 * Do an RSA private key operation
783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200785 int (*f_rng)(void *, unsigned char *, size_t),
786 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000787 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000788 unsigned char *output )
789{
Paul Bakker23986e52011-04-24 08:57:21 +0000790 int ret;
791 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100792
793 /* Temporary holding the result */
794 mbedtls_mpi T;
795
796 /* Temporaries holding P-1, Q-1 and the
797 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000798 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100799
800#if !defined(MBEDTLS_RSA_NO_CRT)
801 /* Temporaries holding the results mod p resp. mod q. */
802 mbedtls_mpi TP, TQ;
803
804 /* Temporaries holding the blinded exponents for
805 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000806 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100807
808 /* Pointers to actual exponents to be used - either the unblinded
809 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000810 mbedtls_mpi *DP = &ctx->DP;
811 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100812#else
813 /* Temporary holding the blinded exponent (if used). */
814 mbedtls_mpi D_blind;
815
816 /* Pointer to actual exponent to be used - either the unblinded
817 * or the blinded one, depending on the presence of a PRNG. */
818 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100819#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100820
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100821 /* Temporaries holding the initial input and the double
822 * checked result; should be the same in the end. */
823 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
Hanno Beckerebd2c022017-10-12 10:54:53 +0100825 if( rsa_check_context( ctx, 1 /* private key checks */,
826 f_rng != NULL /* blinding y/n */ ) != 0 )
827 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100828 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100829 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100830
Hanno Becker06811ce2017-05-03 15:10:34 +0100831#if defined(MBEDTLS_THREADING_C)
832 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
833 return( ret );
834#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000835
Hanno Becker06811ce2017-05-03 15:10:34 +0100836 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100837 mbedtls_mpi_init( &T );
838
839 mbedtls_mpi_init( &P1 );
840 mbedtls_mpi_init( &Q1 );
841 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000842
Janos Follathf9203b42017-03-22 15:13:15 +0000843 if( f_rng != NULL )
844 {
Janos Follathe81102e2017-03-22 13:38:28 +0000845#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000846 mbedtls_mpi_init( &D_blind );
847#else
848 mbedtls_mpi_init( &DP_blind );
849 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000850#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000851 }
Janos Follathe81102e2017-03-22 13:38:28 +0000852
Hanno Becker06811ce2017-05-03 15:10:34 +0100853#if !defined(MBEDTLS_RSA_NO_CRT)
854 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200855#endif
856
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100857 mbedtls_mpi_init( &I );
858 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100859
860 /* End of MPI initialization */
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
863 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200865 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
866 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 }
868
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100869 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100870
Paul Bakkerf451bac2013-08-30 15:37:02 +0200871 if( f_rng != NULL )
872 {
873 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200874 * Blinding
875 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200876 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200877 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
878 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000880
Janos Follathe81102e2017-03-22 13:38:28 +0000881 /*
882 * Exponent blinding
883 */
884 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
885 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
886
Janos Follathf9203b42017-03-22 15:13:15 +0000887#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000888 /*
889 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
890 */
891 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
892 f_rng, p_rng ) );
893 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
894 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
895 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
896
897 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000898#else
899 /*
900 * DP_blind = ( P - 1 ) * R + DP
901 */
902 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
903 f_rng, p_rng ) );
904 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
905 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
906 &ctx->DP ) );
907
908 DP = &DP_blind;
909
910 /*
911 * DQ_blind = ( Q - 1 ) * R + DQ
912 */
913 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
914 f_rng, p_rng ) );
915 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
916 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
917 &ctx->DQ ) );
918
919 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000920#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200921 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000924 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100925#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200926 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000927 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100929 * TP = input ^ dP mod P
930 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100932
933 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
934 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
936 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100937 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100939 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
940 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
941 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000942
943 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100944 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000945 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100946 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
947 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200949
Paul Bakkerf451bac2013-08-30 15:37:02 +0200950 if( f_rng != NULL )
951 {
952 /*
953 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200954 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200955 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200956 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200958 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Hanno Becker2dec5e82017-10-03 07:49:52 +0100960 /* Verify the result to prevent glitching attacks. */
961 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
962 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100963 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +0100964 {
Hanno Becker06811ce2017-05-03 15:10:34 +0100965 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
966 goto cleanup;
967 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100968
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
972cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200974 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
975 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200976#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200977
Hanno Becker06811ce2017-05-03 15:10:34 +0100978 mbedtls_mpi_free( &P1 );
979 mbedtls_mpi_free( &Q1 );
980 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000981
982 if( f_rng != NULL )
983 {
Janos Follathe81102e2017-03-22 13:38:28 +0000984#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000985 mbedtls_mpi_free( &D_blind );
986#else
987 mbedtls_mpi_free( &DP_blind );
988 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000989#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000990 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000991
Hanno Becker06811ce2017-05-03 15:10:34 +0100992 mbedtls_mpi_free( &T );
993
994#if !defined(MBEDTLS_RSA_NO_CRT)
995 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
996#endif
997
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100998 mbedtls_mpi_free( &C );
999 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001000
Paul Bakker5121ce52009-01-03 21:22:43 +00001001 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
1004 return( 0 );
1005}
1006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001008/**
1009 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1010 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001011 * \param dst buffer to mask
1012 * \param dlen length of destination buffer
1013 * \param src source of the mask generation
1014 * \param slen length of the source buffer
1015 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001016 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001017static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001019{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001021 unsigned char counter[4];
1022 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001023 unsigned int hlen;
1024 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001025 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001028 memset( counter, 0, 4 );
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001031
Simon Butcher02037452016-03-01 21:19:12 +00001032 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001033 p = dst;
1034
1035 while( dlen > 0 )
1036 {
1037 use_len = hlen;
1038 if( dlen < hlen )
1039 use_len = dlen;
1040
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001041 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1042 goto exit;
1043 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1044 goto exit;
1045 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1046 goto exit;
1047 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1048 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001049
1050 for( i = 0; i < use_len; ++i )
1051 *p++ ^= mask[i];
1052
1053 counter[3]++;
1054
1055 dlen -= use_len;
1056 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001057
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001058exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001059 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001060
1061 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001066/*
1067 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001070 int (*f_rng)(void *, unsigned char *, size_t),
1071 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001072 int mode,
1073 const unsigned char *label, size_t label_len,
1074 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001075 const unsigned char *input,
1076 unsigned char *output )
1077{
1078 size_t olen;
1079 int ret;
1080 unsigned char *p = output;
1081 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 const mbedtls_md_info_t *md_info;
1083 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1086 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001087
1088 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001092 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001094
1095 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001097
Simon Butcher02037452016-03-01 21:19:12 +00001098 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001099 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001101
1102 memset( output, 0, olen );
1103
1104 *p++ = 0;
1105
Simon Butcher02037452016-03-01 21:19:12 +00001106 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001107 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001109
1110 p += hlen;
1111
Simon Butcher02037452016-03-01 21:19:12 +00001112 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001113 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1114 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001115 p += hlen;
1116 p += olen - 2 * hlen - 2 - ilen;
1117 *p++ = 1;
1118 memcpy( p, input, ilen );
1119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001121 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001122 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001123
Simon Butcher02037452016-03-01 21:19:12 +00001124 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001125 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1126 &md_ctx ) ) != 0 )
1127 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001128
Simon Butcher02037452016-03-01 21:19:12 +00001129 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001130 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1131 &md_ctx ) ) != 0 )
1132 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001133
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001134exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001136
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001137 if( ret != 0 )
1138 return( ret );
1139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 return( ( mode == MBEDTLS_RSA_PUBLIC )
1141 ? mbedtls_rsa_public( ctx, output, output )
1142 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001143}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001147/*
1148 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001151 int (*f_rng)(void *, unsigned char *, size_t),
1152 void *p_rng,
1153 int mode, size_t ilen,
1154 const unsigned char *input,
1155 unsigned char *output )
1156{
1157 size_t nb_pad, olen;
1158 int ret;
1159 unsigned char *p = output;
1160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1162 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001163
Janos Follath1ed9f992016-03-18 11:45:44 +00001164 // We don't check p_rng because it won't be dereferenced here
1165 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001167
1168 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001169
Simon Butcher02037452016-03-01 21:19:12 +00001170 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001171 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001173
1174 nb_pad = olen - 3 - ilen;
1175
1176 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001180
1181 while( nb_pad-- > 0 )
1182 {
1183 int rng_dl = 100;
1184
1185 do {
1186 ret = f_rng( p_rng, p, 1 );
1187 } while( *p == 0 && --rng_dl && ret == 0 );
1188
Simon Butcher02037452016-03-01 21:19:12 +00001189 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001190 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001192
1193 p++;
1194 }
1195 }
1196 else
1197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001199
1200 while( nb_pad-- > 0 )
1201 *p++ = 0xFF;
1202 }
1203
1204 *p++ = 0;
1205 memcpy( p, input, ilen );
1206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 return( ( mode == MBEDTLS_RSA_PUBLIC )
1208 ? mbedtls_rsa_public( ctx, output, output )
1209 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001210}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001212
Paul Bakker5121ce52009-01-03 21:22:43 +00001213/*
1214 * Add the message padding, then do an RSA operation
1215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001217 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001218 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001219 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001220 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 unsigned char *output )
1222{
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 switch( ctx->padding )
1224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#if defined(MBEDTLS_PKCS1_V15)
1226 case MBEDTLS_RSA_PKCS_V15:
1227 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001228 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001229#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#if defined(MBEDTLS_PKCS1_V21)
1232 case MBEDTLS_RSA_PKCS_V21:
1233 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001234 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001235#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001236
1237 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001240}
1241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001243/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001244 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001247 int (*f_rng)(void *, unsigned char *, size_t),
1248 void *p_rng,
1249 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001250 const unsigned char *label, size_t label_len,
1251 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001252 const unsigned char *input,
1253 unsigned char *output,
1254 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001255{
Paul Bakker23986e52011-04-24 08:57:21 +00001256 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001257 size_t ilen, i, pad_len;
1258 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1260 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001261 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 const mbedtls_md_info_t *md_info;
1263 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001264
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001265 /*
1266 * Parameters sanity checks
1267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1269 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001270
1271 ilen = ctx->len;
1272
Paul Bakker27fdf462011-06-09 13:55:13 +00001273 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001277 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001279
Janos Follathc17cda12016-02-11 11:08:18 +00001280 hlen = mbedtls_md_get_size( md_info );
1281
1282 // checking for integer underflow
1283 if( 2 * hlen + 2 > ilen )
1284 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1285
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001286 /*
1287 * RSA operation
1288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1290 ? mbedtls_rsa_public( ctx, input, buf )
1291 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
1293 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001294 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001295
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001296 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001297 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001300 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1301 {
1302 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001303 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001304 }
1305
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001306 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001307 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1308 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001309 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001310 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1311 &md_ctx ) ) != 0 )
1312 {
1313 mbedtls_md_free( &md_ctx );
1314 goto cleanup;
1315 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001318
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001319 /* Generate lHash */
1320 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1321 goto cleanup;
1322
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001323 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001324 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001325 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001327 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001329 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001330
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001331 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001332
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001333 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001334 for( i = 0; i < hlen; i++ )
1335 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001336
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001337 /* Get zero-padding len, but always read till end of buffer
1338 * (minus one, for the 01 byte) */
1339 pad_len = 0;
1340 pad_done = 0;
1341 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1342 {
1343 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001344 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001345 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001346
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001347 p += pad_len;
1348 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001349
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001350 /*
1351 * The only information "leaked" is whether the padding was correct or not
1352 * (eg, no data is copied if it was not correct). This meets the
1353 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1354 * the different error conditions.
1355 */
1356 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001357 {
1358 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1359 goto cleanup;
1360 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001361
Paul Bakker66d5d072014-06-17 16:39:18 +02001362 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001363 {
1364 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1365 goto cleanup;
1366 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001367
1368 *olen = ilen - (p - buf);
1369 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001370 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001371
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001372cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001373 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1374 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001375
1376 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001381/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1382 *
1383 * \param value The value to analyze.
Gilles Peskine331d80e2018-10-04 18:32:29 +02001384 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001385 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001386static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001387{
1388 /* MSVC has a warning about unary minus on unsigned, but this is
1389 * well-defined and precisely what we want to do here */
1390#if defined(_MSC_VER)
1391#pragma warning( push )
1392#pragma warning( disable : 4146 )
1393#endif
1394 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1395#if defined(_MSC_VER)
1396#pragma warning( pop )
1397#endif
1398}
1399
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001400/** Check whether a size is out of bounds, without branches.
1401 *
1402 * This is equivalent to `size > max`, but is likely to be compiled to
1403 * to code using bitwise operation rather than a branch.
1404 *
1405 * \param size Size to check.
1406 * \param max Maximum desired value for \p size.
1407 * \return \c 0 if `size <= max`.
1408 * \return \c 1 if `size > max`.
1409 */
1410static unsigned size_greater_than( size_t size, size_t max )
1411{
1412 /* Return the sign bit (1 for negative) of (max - size). */
1413 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1414}
1415
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001416/** Choose between two integer values, without branches.
1417 *
Gilles Peskine331d80e2018-10-04 18:32:29 +02001418 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1419 * to code using bitwise operation rather than a branch.
1420 *
1421 * \param cond Condition to test.
1422 * \param if1 Value to use if \p cond is nonzero.
1423 * \param if0 Value to use if \p cond is zero.
1424 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001425 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001426static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001427{
Gilles Peskine331d80e2018-10-04 18:32:29 +02001428 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001429 return( ( mask & if1 ) | (~mask & if0 ) );
1430}
1431
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001432/** Shift some data towards the left inside a buffer without leaking
1433 * the length of the data through side channels.
1434 *
1435 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1436 * ```
1437 * memmove(start, start + offset, total - offset);
1438 * memset(start + offset, 0, total - offset);
1439 * ```
1440 * but it strives to use a memory access pattern (and thus total timing)
1441 * that does not depend on \p offset. This timing independence comes at
1442 * the expense of performance.
1443 *
1444 * \param start Pointer to the start of the buffer.
1445 * \param total Total size of the buffer.
1446 * \param offset Offset from which to copy \p total - \p offset bytes.
1447 */
1448static void mem_move_to_left( void *start,
1449 size_t total,
1450 size_t offset )
1451{
1452 volatile unsigned char *buf = start;
1453 size_t i, n;
1454 if( total == 0 )
1455 return;
1456 for( i = 0; i < total; i++ )
1457 {
1458 unsigned no_op = size_greater_than( total - offset, i );
1459 /* The first `total - offset` passes are a no-op. The last
1460 * `offset` passes shift the data one byte to the left and
1461 * zero out the last byte. */
1462 for( n = 0; n < total - 1; n++ )
1463 buf[n] = if_int( no_op, buf[n], buf[n+1] );
1464 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1465 }
1466}
1467
Paul Bakkerb3869132013-02-28 17:21:01 +01001468/*
1469 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001472 int (*f_rng)(void *, unsigned char *, size_t),
1473 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001474 int mode, size_t *olen,
1475 const unsigned char *input,
1476 unsigned char *output,
Gilles Peskine5908dd42018-10-02 22:43:06 +02001477 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001478{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001479 int ret;
Gilles Peskine5908dd42018-10-02 22:43:06 +02001480 size_t ilen = ctx->len;
Gilles Peskine5908dd42018-10-02 22:43:06 +02001481 size_t i;
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001482 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1483 ilen - 11 :
1484 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine85a74422018-10-05 14:50:21 +02001486 /* The following variables take sensitive values: their value must
1487 * not leak into the observable behavior of the function other than
1488 * the designated outputs (output, olen, return value). Otherwise
1489 * this would open the execution of the function to
1490 * side-channel-based variants of the Bleichenbacher padding oracle
1491 * attack. Potential side channels include overall timing, memory
1492 * access patterns (especially visible to an adversary who has access
1493 * to a shared memory cache), and branches (especially visible to
1494 * an adversary who has access to a shared code cache or to a shared
1495 * branch predictor). */
1496 size_t pad_count = 0;
1497 unsigned bad = 0;
1498 unsigned char pad_done = 0;
1499 size_t plaintext_size = 0;
1500 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1503 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001504
Paul Bakkerb3869132013-02-28 17:21:01 +01001505 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1509 ? mbedtls_rsa_public( ctx, input, buf )
1510 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001511
1512 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001513 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001514
Gilles Peskine40b57f42018-10-05 15:06:12 +02001515 /* Check and get padding length in constant time and constant
1516 * memory trace. The first byte must be 0. */
1517 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001521 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1522 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001523 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001524
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001525 /* Get padding len, but always read till end of buffer
1526 * (minus one, for the 00 byte) */
Gilles Peskine85a74422018-10-05 14:50:21 +02001527 for( i = 2; i < ilen - 1; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001528 {
Gilles Peskine85a74422018-10-05 14:50:21 +02001529 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001530 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001531 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001532 }
1533 else
1534 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001535 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1536 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001537 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001538
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001539 /* Get padding len, but always read till end of buffer
1540 * (minus one, for the 00 byte) */
Gilles Peskine85a74422018-10-05 14:50:21 +02001541 for( i = 2; i < ilen - 1; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001542 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001543 pad_done |= if_int( buf[i], 0, 1 );
1544 pad_count += if_int( pad_done, 0, 1 );
1545 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001546 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 }
1548
Gilles Peskine40b57f42018-10-05 15:06:12 +02001549 /* If pad_done is still zero, there's no data, only unfinished padding. */
1550 bad |= if_int( pad_done, 0, 1 );
1551
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001552 /* There must be at least 8 bytes of padding. */
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001553 bad |= size_greater_than( 8, pad_count );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001554
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001555 /* If the padding is valid, set plaintext_size to the number of
1556 * remaining bytes after stripping the padding. If the padding
1557 * is invalid, avoid leaking this fact through the size of the
1558 * output: use the maximum message size that fits in the output
1559 * buffer. Do it without branches to avoid leaking the padding
1560 * validity through timing. RSA keys are small enough that all the
1561 * size_t values involved fit in unsigned int. */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001562 plaintext_size = if_int( bad,
1563 (unsigned) plaintext_max_size,
Gilles Peskine85a74422018-10-05 14:50:21 +02001564 (unsigned) ( ilen - pad_count - 3 ) );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001565
Gilles Peskine9265ff42018-10-04 19:13:43 +02001566 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001567 * buffer and to 1 otherwise. */
1568 output_too_large = size_greater_than( plaintext_size,
1569 plaintext_max_size );
Paul Bakker060c5682009-01-12 21:48:39 +00001570
Gilles Peskine9265ff42018-10-04 19:13:43 +02001571 /* Set ret without branches to avoid timing attacks. Return:
1572 * - INVALID_PADDING if the padding is bad (bad != 0).
1573 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1574 * plaintext does not fit in the output buffer.
1575 * - 0 if the padding is correct. */
1576 ret = - if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1577 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1578 0 ) );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001579
Gilles Peskine9265ff42018-10-04 19:13:43 +02001580 /* If the padding is bad or the plaintext is too large, zero the
1581 * data that we're about to copy to the output buffer.
1582 * We need to copy the same amount of data
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001583 * from the same buffer whether the padding is good or not to
1584 * avoid leaking the padding validity through overall timing or
1585 * through memory or cache access patterns. */
Gilles Peskine40b57f42018-10-05 15:06:12 +02001586 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001587 for( i = 11; i < ilen; i++ )
Gilles Peskine40b57f42018-10-05 15:06:12 +02001588 buf[i] &= ~bad;
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001589
Gilles Peskine9265ff42018-10-04 19:13:43 +02001590 /* If the plaintext is too large, truncate it to the buffer size.
1591 * Copy anyway to avoid revealing the length through timing, because
1592 * revealing the length is as bad as revealing the padding validity
1593 * for a Bleichenbacher attack. */
1594 plaintext_size = if_int( output_too_large,
1595 (unsigned) plaintext_max_size,
1596 (unsigned) plaintext_size );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001597
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001598 /* Move the plaintext to the leftmost position where it can start in
1599 * the working buffer, i.e. make it start plaintext_max_size from
1600 * the end of the buffer. Do this with a memory access trace that
1601 * does not depend on the plaintext size. After this move, the
1602 * starting location of the plaintext is no longer sensitive
1603 * information. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001604 mem_move_to_left( buf + ilen - plaintext_max_size,
1605 plaintext_max_size,
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001606 plaintext_max_size - plaintext_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001607
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001608 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskine9265ff42018-10-04 19:13:43 +02001609 * into the output buffer. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001610 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001611
1612 /* Report the amount of data we copied to the output buffer. In case
1613 * of errors (bad padding or output too large), the value of *olen
1614 * when this function returns is not specified. Making it equivalent
1615 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001616 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001617
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001618cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001619 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001620
1621 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001622}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001624
1625/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001626 * Do an RSA operation, then remove the message padding
1627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001629 int (*f_rng)(void *, unsigned char *, size_t),
1630 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001631 int mode, size_t *olen,
1632 const unsigned char *input,
1633 unsigned char *output,
1634 size_t output_max_len)
1635{
1636 switch( ctx->padding )
1637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_PKCS1_V15)
1639 case MBEDTLS_RSA_PKCS_V15:
1640 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001641 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001642#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#if defined(MBEDTLS_PKCS1_V21)
1645 case MBEDTLS_RSA_PKCS_V21:
1646 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001647 olen, input, output,
1648 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001649#endif
1650
1651 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001653 }
1654}
1655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001657/*
1658 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001661 int (*f_rng)(void *, unsigned char *, size_t),
1662 void *p_rng,
1663 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001665 unsigned int hashlen,
1666 const unsigned char *hash,
1667 unsigned char *sig )
1668{
1669 size_t olen;
1670 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001672 unsigned int slen, hlen, offset = 0;
1673 int ret;
1674 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 const mbedtls_md_info_t *md_info;
1676 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1679 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001680
1681 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001683
1684 olen = ctx->len;
1685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001687 {
Simon Butcher02037452016-03-01 21:19:12 +00001688 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001690 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001694 }
1695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001697 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001701 slen = hlen;
1702
1703 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001705
1706 memset( sig, 0, olen );
1707
Simon Butcher02037452016-03-01 21:19:12 +00001708 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001709 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001711
Simon Butcher02037452016-03-01 21:19:12 +00001712 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001713 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001714 p += olen - hlen * 2 - 2;
1715 *p++ = 0x01;
1716 memcpy( p, salt, slen );
1717 p += slen;
1718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001720 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001721 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001722
Simon Butcher02037452016-03-01 21:19:12 +00001723 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001724 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1725 goto exit;
1726 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1727 goto exit;
1728 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1729 goto exit;
1730 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1731 goto exit;
1732 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1733 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001734
Simon Butcher02037452016-03-01 21:19:12 +00001735 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001736 if( msb % 8 == 0 )
1737 offset = 1;
1738
Simon Butcher02037452016-03-01 21:19:12 +00001739 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001740 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1741 &md_ctx ) ) != 0 )
1742 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001743
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001744 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001745 sig[0] &= 0xFF >> ( olen * 8 - msb );
1746
1747 p += hlen;
1748 *p++ = 0xBC;
1749
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001750 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001751
1752exit:
1753 mbedtls_md_free( &md_ctx );
1754
1755 if( ret != 0 )
1756 return( ret );
1757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 return( ( mode == MBEDTLS_RSA_PUBLIC )
1759 ? mbedtls_rsa_public( ctx, sig, sig )
1760 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001761}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001765/*
1766 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1767 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001768
1769/* Construct a PKCS v1.5 encoding of a hashed message
1770 *
1771 * This is used both for signature generation and verification.
1772 *
1773 * Parameters:
1774 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001775 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001776 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001777 * - hash: Buffer containing the hashed message or the raw data.
1778 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001779 * - dst: Buffer to hold the encoded message.
1780 *
1781 * Assumptions:
1782 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1783 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001784 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001785 *
1786 */
1787static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1788 unsigned int hashlen,
1789 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001790 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001791 unsigned char *dst )
1792{
1793 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001794 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001795 unsigned char *p = dst;
1796 const char *oid = NULL;
1797
1798 /* Are we signing hashed or raw data? */
1799 if( md_alg != MBEDTLS_MD_NONE )
1800 {
1801 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1802 if( md_info == NULL )
1803 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1804
1805 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1806 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1807
1808 hashlen = mbedtls_md_get_size( md_info );
1809
1810 /* Double-check that 8 + hashlen + oid_size can be used as a
1811 * 1-byte ASN.1 length encoding and that there's no overflow. */
1812 if( 8 + hashlen + oid_size >= 0x80 ||
1813 10 + hashlen < hashlen ||
1814 10 + hashlen + oid_size < 10 + hashlen )
1815 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1816
1817 /*
1818 * Static bounds check:
1819 * - Need 10 bytes for five tag-length pairs.
1820 * (Insist on 1-byte length encodings to protect against variants of
1821 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1822 * - Need hashlen bytes for hash
1823 * - Need oid_size bytes for hash alg OID.
1824 */
1825 if( nb_pad < 10 + hashlen + oid_size )
1826 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1827 nb_pad -= 10 + hashlen + oid_size;
1828 }
1829 else
1830 {
1831 if( nb_pad < hashlen )
1832 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1833
1834 nb_pad -= hashlen;
1835 }
1836
Hanno Becker2b2f8982017-09-27 17:10:03 +01001837 /* Need space for signature header and padding delimiter (3 bytes),
1838 * and 8 bytes for the minimal padding */
1839 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001840 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1841 nb_pad -= 3;
1842
1843 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001844 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001845
1846 /* Write signature header and padding */
1847 *p++ = 0;
1848 *p++ = MBEDTLS_RSA_SIGN;
1849 memset( p, 0xFF, nb_pad );
1850 p += nb_pad;
1851 *p++ = 0;
1852
1853 /* Are we signing raw data? */
1854 if( md_alg == MBEDTLS_MD_NONE )
1855 {
1856 memcpy( p, hash, hashlen );
1857 return( 0 );
1858 }
1859
1860 /* Signing hashed data, add corresponding ASN.1 structure
1861 *
1862 * DigestInfo ::= SEQUENCE {
1863 * digestAlgorithm DigestAlgorithmIdentifier,
1864 * digest Digest }
1865 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1866 * Digest ::= OCTET STRING
1867 *
1868 * Schematic:
1869 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1870 * TAG-NULL + LEN [ NULL ] ]
1871 * TAG-OCTET + LEN [ HASH ] ]
1872 */
1873 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001874 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001875 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001876 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001877 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001878 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001879 memcpy( p, oid, oid_size );
1880 p += oid_size;
1881 *p++ = MBEDTLS_ASN1_NULL;
1882 *p++ = 0x00;
1883 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001884 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001885 memcpy( p, hash, hashlen );
1886 p += hashlen;
1887
1888 /* Just a sanity-check, should be automatic
1889 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001890 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001891 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001892 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001893 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1894 }
1895
1896 return( 0 );
1897}
1898
Paul Bakkerb3869132013-02-28 17:21:01 +01001899/*
1900 * Do an RSA operation to sign the message digest
1901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001903 int (*f_rng)(void *, unsigned char *, size_t),
1904 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001905 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001907 unsigned int hashlen,
1908 const unsigned char *hash,
1909 unsigned char *sig )
1910{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001911 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001912 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1915 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001916
Hanno Beckerfdf38032017-09-06 12:35:55 +01001917 /*
1918 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1919 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001920
Hanno Beckerfdf38032017-09-06 12:35:55 +01001921 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1922 ctx->len, sig ) ) != 0 )
1923 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001924
1925 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001926 * Call respective RSA primitive
1927 */
1928
1929 if( mode == MBEDTLS_RSA_PUBLIC )
1930 {
1931 /* Skip verification on a public key operation */
1932 return( mbedtls_rsa_public( ctx, sig, sig ) );
1933 }
1934
1935 /* Private key operation
1936 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001937 * In order to prevent Lenstra's attack, make the signature in a
1938 * temporary buffer and check it before returning it.
1939 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001940
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001941 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001942 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001943 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1944
Hanno Beckerfdf38032017-09-06 12:35:55 +01001945 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001946 if( verif == NULL )
1947 {
1948 mbedtls_free( sig_try );
1949 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1950 }
1951
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001952 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1953 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1954
Hanno Becker171a8f12017-09-06 12:32:16 +01001955 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001956 {
1957 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1958 goto cleanup;
1959 }
1960
1961 memcpy( sig, sig_try, ctx->len );
1962
1963cleanup:
1964 mbedtls_free( sig_try );
1965 mbedtls_free( verif );
1966
1967 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001968}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001970
1971/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 * Do an RSA operation to sign the message digest
1973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001975 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001976 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001979 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001980 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001981 unsigned char *sig )
1982{
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 switch( ctx->padding )
1984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_PKCS1_V15)
1986 case MBEDTLS_RSA_PKCS_V15:
1987 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001988 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001989#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991#if defined(MBEDTLS_PKCS1_V21)
1992 case MBEDTLS_RSA_PKCS_V21:
1993 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001994 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001995#endif
1996
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002000}
2001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002003/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002004 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002007 int (*f_rng)(void *, unsigned char *, size_t),
2008 void *p_rng,
2009 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002011 unsigned int hashlen,
2012 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002014 int expected_salt_len,
2015 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002016{
Paul Bakker23986e52011-04-24 08:57:21 +00002017 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002018 size_t siglen;
2019 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002020 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002022 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002023 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002024 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 const mbedtls_md_info_t *md_info;
2026 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002027 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2030 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002031
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 siglen = ctx->len;
2033
Paul Bakker27fdf462011-06-09 13:55:13 +00002034 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2038 ? mbedtls_rsa_public( ctx, sig, buf )
2039 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002040
2041 if( ret != 0 )
2042 return( ret );
2043
2044 p = buf;
2045
Paul Bakkerb3869132013-02-28 17:21:01 +01002046 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 {
Simon Butcher02037452016-03-01 21:19:12 +00002051 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002053 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002057 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002060 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002064
Paul Bakkerb3869132013-02-28 17:21:01 +01002065 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002066
Simon Butcher02037452016-03-01 21:19:12 +00002067 /*
2068 * Note: EMSA-PSS verification is over the length of N - 1 bits
2069 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002070 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002071
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002072 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2073 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2074
Simon Butcher02037452016-03-01 21:19:12 +00002075 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002076 if( msb % 8 == 0 )
2077 {
2078 p++;
2079 siglen -= 1;
2080 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002081
Gilles Peskine139108a2017-10-18 19:03:42 +02002082 if( siglen < hlen + 2 )
2083 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2084 hash_start = p + siglen - hlen - 1;
2085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002087 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002088 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002089
Jaeden Amero66954e12018-01-25 16:05:54 +00002090 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2091 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002092 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002093
Paul Bakkerb3869132013-02-28 17:21:01 +01002094 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002095
Gilles Peskine6a54b022017-10-17 19:02:13 +02002096 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002097 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002098
Gilles Peskine91048a32017-10-19 17:46:14 +02002099 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002100 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002101 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2102 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002103 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002104
Gilles Peskine6a54b022017-10-17 19:02:13 +02002105 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002108 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002109 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002110 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2111 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002112 }
2113
Simon Butcher02037452016-03-01 21:19:12 +00002114 /*
2115 * Generate H = Hash( M' )
2116 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002117 ret = mbedtls_md_starts( &md_ctx );
2118 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002119 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002120 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2121 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002122 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002123 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2124 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002125 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002126 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2127 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002128 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002129 ret = mbedtls_md_finish( &md_ctx, result );
2130 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002131 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002132
Jaeden Amero66954e12018-01-25 16:05:54 +00002133 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002134 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002135 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002136 goto exit;
2137 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002138
2139exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002141
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002142 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002143}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002144
2145/*
2146 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002149 int (*f_rng)(void *, unsigned char *, size_t),
2150 void *p_rng,
2151 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002153 unsigned int hashlen,
2154 const unsigned char *hash,
2155 const unsigned char *sig )
2156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
2158 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002159 : md_alg;
2160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002162 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002164 sig ) );
2165
2166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002170/*
2171 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002174 int (*f_rng)(void *, unsigned char *, size_t),
2175 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002176 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002178 unsigned int hashlen,
2179 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002180 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002181{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002182 int ret = 0;
2183 const size_t sig_len = ctx->len;
2184 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2187 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002188
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002189 /*
2190 * Prepare expected PKCS1 v1.5 encoding of hash.
2191 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002192
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002193 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2194 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2195 {
2196 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2197 goto cleanup;
2198 }
2199
2200 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2201 encoded_expected ) ) != 0 )
2202 goto cleanup;
2203
2204 /*
2205 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2206 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002209 ? mbedtls_rsa_public( ctx, sig, encoded )
2210 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002211 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002212 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002213
Simon Butcher02037452016-03-01 21:19:12 +00002214 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002215 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002216 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002217
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002218 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2219 sig_len ) ) != 0 )
2220 {
2221 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2222 goto cleanup;
2223 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002224
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002225cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002226
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002227 if( encoded != NULL )
2228 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002229 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002230 mbedtls_free( encoded );
2231 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002232
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002233 if( encoded_expected != NULL )
2234 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002235 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002236 mbedtls_free( encoded_expected );
2237 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002238
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002239 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002242
2243/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002244 * Do an RSA operation and check the message digest
2245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002247 int (*f_rng)(void *, unsigned char *, size_t),
2248 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002249 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002251 unsigned int hashlen,
2252 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002253 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002254{
2255 switch( ctx->padding )
2256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#if defined(MBEDTLS_PKCS1_V15)
2258 case MBEDTLS_RSA_PKCS_V15:
2259 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002260 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002261#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263#if defined(MBEDTLS_PKCS1_V21)
2264 case MBEDTLS_RSA_PKCS_V21:
2265 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002266 hashlen, hash, sig );
2267#endif
2268
2269 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002271 }
2272}
2273
2274/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002275 * Copy the components of an RSA key
2276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002278{
2279 int ret;
2280
2281 dst->ver = src->ver;
2282 dst->len = src->len;
2283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2285 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2288 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2289 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002290
2291#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2293 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2294 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2296 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002297#endif
2298
2299 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2302 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002303
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002304 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002305 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002306
2307cleanup:
2308 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002310
2311 return( ret );
2312}
2313
2314/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002315 * Free the components of an RSA key
2316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002320 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2321 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002323
Hanno Becker33c30a02017-08-23 07:00:22 +01002324#if !defined(MBEDTLS_RSA_NO_CRT)
2325 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2326 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2327 mbedtls_mpi_free( &ctx->DP );
2328#endif /* MBEDTLS_RSA_NO_CRT */
2329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#if defined(MBEDTLS_THREADING_C)
2331 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002332#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002333}
2334
Hanno Beckerab377312017-08-23 16:24:51 +01002335#endif /* !MBEDTLS_RSA_ALT */
2336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002338
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002339#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002340
2341/*
2342 * Example RSA-1024 keypair, for test purposes
2343 */
2344#define KEY_LEN 128
2345
2346#define RSA_N "9292758453063D803DD603D5E777D788" \
2347 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2348 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2349 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2350 "93A89813FBF3C4F8066D2D800F7C38A8" \
2351 "1AE31942917403FF4946B0A83D3D3E05" \
2352 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2353 "5E94BB77B07507233A0BC7BAC8F90F79"
2354
2355#define RSA_E "10001"
2356
2357#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2358 "66CA472BC44D253102F8B4A9D3BFA750" \
2359 "91386C0077937FE33FA3252D28855837" \
2360 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2361 "DF79C5CE07EE72C7F123142198164234" \
2362 "CABB724CF78B8173B9F880FC86322407" \
2363 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2364 "071513A1E85B5DFA031F21ECAE91A34D"
2365
2366#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2367 "2C01CAD19EA484A87EA4377637E75500" \
2368 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2369 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2370
2371#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2372 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2373 "910E4168387E3C30AA1E00C339A79508" \
2374 "8452DD96A9A5EA5D9DCA68DA636032AF"
2375
Paul Bakker5121ce52009-01-03 21:22:43 +00002376#define PT_LEN 24
2377#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2378 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002381static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002382{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002383#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002384 size_t i;
2385
Paul Bakker545570e2010-07-18 09:00:25 +00002386 if( rng_state != NULL )
2387 rng_state = NULL;
2388
Paul Bakkera3d195c2011-11-27 21:07:34 +00002389 for( i = 0; i < len; ++i )
2390 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002391#else
2392 if( rng_state != NULL )
2393 rng_state = NULL;
2394
2395 arc4random_buf( output, len );
2396#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002397
Paul Bakkera3d195c2011-11-27 21:07:34 +00002398 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002401
Paul Bakker5121ce52009-01-03 21:22:43 +00002402/*
2403 * Checkup routine
2404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002406{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002407 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002409 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 unsigned char rsa_plaintext[PT_LEN];
2412 unsigned char rsa_decrypted[PT_LEN];
2413 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002415 unsigned char sha1sum[20];
2416#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
Hanno Becker3a701162017-08-22 13:52:43 +01002418 mbedtls_mpi K;
2419
2420 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
Hanno Becker3a701162017-08-22 13:52:43 +01002423 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2424 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2425 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2426 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2427 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2428 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2429 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2430 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2431 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2432 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2433
Hanno Becker7f25f852017-10-10 16:56:22 +01002434 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002435
2436 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2440 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 {
2442 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
Hanno Becker5bc87292017-05-03 15:09:31 +01002445 ret = 1;
2446 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
2448
2449 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
2452 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2453
Hanno Becker98838b02017-10-02 13:16:10 +01002454 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2455 PT_LEN, rsa_plaintext,
2456 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 {
2458 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
Hanno Becker5bc87292017-05-03 15:09:31 +01002461 ret = 1;
2462 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 }
2464
2465 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002467
Hanno Becker98838b02017-10-02 13:16:10 +01002468 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2469 &len, rsa_ciphertext, rsa_decrypted,
2470 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 {
2472 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
Hanno Becker5bc87292017-05-03 15:09:31 +01002475 ret = 1;
2476 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 }
2478
2479 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2480 {
2481 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Hanno Becker5bc87292017-05-03 15:09:31 +01002484 ret = 1;
2485 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 }
2487
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002488 if( verbose != 0 )
2489 mbedtls_printf( "passed\n" );
2490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002493 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002494
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002495 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002496 {
2497 if( verbose != 0 )
2498 mbedtls_printf( "failed\n" );
2499
2500 return( 1 );
2501 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Hanno Becker98838b02017-10-02 13:16:10 +01002503 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2504 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2505 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 {
2507 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Hanno Becker5bc87292017-05-03 15:09:31 +01002510 ret = 1;
2511 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 }
2513
2514 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
Hanno Becker98838b02017-10-02 13:16:10 +01002517 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2518 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2519 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 {
2521 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Hanno Becker5bc87292017-05-03 15:09:31 +01002524 ret = 1;
2525 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 }
2527
2528 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002529 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002532 if( verbose != 0 )
2533 mbedtls_printf( "\n" );
2534
Paul Bakker3d8fb632014-04-17 12:42:41 +02002535cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002536 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 mbedtls_rsa_free( &rsa );
2538#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002539 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002541 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542}
2543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546#endif /* MBEDTLS_RSA_C */