blob: 7075f131f88ca2311c26636291d0ddb2f530b643 [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"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000059#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000060#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010064#else
Rich Evans00ab4702015-02-06 13:43:58 +000065#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020067#define mbedtls_calloc calloc
68#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010069#endif
70
Hanno Beckera565f542017-10-11 11:00:19 +010071#if !defined(MBEDTLS_RSA_ALT)
72
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010073/* Implementation that should never be optimized out by the compiler */
74static void mbedtls_zeroize( void *v, size_t n ) {
75 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
76}
77
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010078#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +010079/* constant-time buffer comparison */
80static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
81{
82 size_t i;
83 const unsigned char *A = (const unsigned char *) a;
84 const unsigned char *B = (const unsigned char *) b;
85 unsigned char diff = 0;
86
87 for( i = 0; i < n; i++ )
88 diff |= A[i] ^ B[i];
89
90 return( diff );
91}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010092#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +010093
Hanno Becker617c1ae2017-08-23 14:11:24 +010094int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
95 const mbedtls_mpi *N,
96 const mbedtls_mpi *P, const mbedtls_mpi *Q,
97 const mbedtls_mpi *D, const mbedtls_mpi *E )
98{
99 int ret;
100
101 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
102 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
103 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
104 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
105 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
106 {
107 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
108 }
109
110 if( N != NULL )
111 ctx->len = mbedtls_mpi_size( &ctx->N );
112
113 return( 0 );
114}
115
116int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100117 unsigned char const *N, size_t N_len,
118 unsigned char const *P, size_t P_len,
119 unsigned char const *Q, size_t Q_len,
120 unsigned char const *D, size_t D_len,
121 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100122{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000123 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100124
125 if( N != NULL )
126 {
127 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
128 ctx->len = mbedtls_mpi_size( &ctx->N );
129 }
130
131 if( P != NULL )
132 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
133
134 if( Q != NULL )
135 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
136
137 if( D != NULL )
138 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
139
140 if( E != NULL )
141 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
142
143cleanup:
144
145 if( ret != 0 )
146 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
147
148 return( 0 );
149}
150
Hanno Becker705fc682017-10-10 17:57:02 +0100151/*
152 * Checks whether the context fields are set in such a way
153 * that the RSA primitives will be able to execute without error.
154 * It does *not* make guarantees for consistency of the parameters.
155 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100156static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
157 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100158{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100159#if !defined(MBEDTLS_RSA_NO_CRT)
160 /* blinding_needed is only used for NO_CRT to decide whether
161 * P,Q need to be present or not. */
162 ((void) blinding_needed);
163#endif
164
Hanno Becker3a760a12018-01-05 08:14:49 +0000165 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
166 ctx->len > MBEDTLS_MPI_MAX_SIZE )
167 {
Hanno Becker705fc682017-10-10 17:57:02 +0100168 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000169 }
Hanno Becker705fc682017-10-10 17:57:02 +0100170
171 /*
172 * 1. Modular exponentiation needs positive, odd moduli.
173 */
174
175 /* Modular exponentiation wrt. N is always used for
176 * RSA public key operations. */
177 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
178 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
179 {
180 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
181 }
182
183#if !defined(MBEDTLS_RSA_NO_CRT)
184 /* Modular exponentiation for P and Q is only
185 * used for private key operations and if CRT
186 * is used. */
187 if( is_priv &&
188 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
189 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
190 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
191 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
192 {
193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
194 }
195#endif /* !MBEDTLS_RSA_NO_CRT */
196
197 /*
198 * 2. Exponents must be positive
199 */
200
201 /* Always need E for public key operations */
202 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
203 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
204
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100205#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100206 /* For private key operations, use D or DP & DQ
207 * as (unblinded) exponents. */
208 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
210#else
211 if( is_priv &&
212 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
213 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
214 {
215 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
216 }
217#endif /* MBEDTLS_RSA_NO_CRT */
218
219 /* Blinding shouldn't make exponents negative either,
220 * so check that P, Q >= 1 if that hasn't yet been
221 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100222#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100223 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100224 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
225 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
226 {
227 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
228 }
229#endif
230
231 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100232 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100233#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100234 if( is_priv &&
235 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
236 {
237 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
238 }
239#endif
240
241 return( 0 );
242}
243
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100244int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100245{
246 int ret = 0;
247
Hanno Becker617c1ae2017-08-23 14:11:24 +0100248 const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
249 const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
250 const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
251 const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
252 const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100253
Hanno Becker617c1ae2017-08-23 14:11:24 +0100254 /*
255 * Check whether provided parameters are enough
256 * to deduce all others. The following incomplete
257 * parameter sets for private keys are supported:
258 *
259 * (1) P, Q missing.
260 * (2) D and potentially N missing.
261 *
262 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100263
Hanno Becker2cca6f32017-09-29 11:46:40 +0100264 const int n_missing = have_P && have_Q && have_D && have_E;
265 const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
266 const int d_missing = have_P && have_Q && !have_D && have_E;
267 const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
268
269 /* These three alternatives are mutually exclusive */
270 const int is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100271
Hanno Becker617c1ae2017-08-23 14:11:24 +0100272 if( !is_priv && !is_pub )
273 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
274
275 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100276 * Step 1: Deduce N if P, Q are provided.
277 */
278
279 if( !have_N && have_P && have_Q )
280 {
281 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
282 &ctx->Q ) ) != 0 )
283 {
284 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
285 }
286
287 ctx->len = mbedtls_mpi_size( &ctx->N );
288 }
289
290 /*
291 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100292 */
293
294 if( pq_missing )
295 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100296 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100297 &ctx->P, &ctx->Q );
298 if( ret != 0 )
299 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
300
301 }
302 else if( d_missing )
303 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100304 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
305 &ctx->Q,
306 &ctx->E,
307 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100308 {
309 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
310 }
311 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100312
Hanno Becker617c1ae2017-08-23 14:11:24 +0100313 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100314 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100315 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100316 */
317
Hanno Becker23344b52017-08-23 07:43:27 +0100318#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100319 if( is_priv )
320 {
321 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
322 &ctx->DP, &ctx->DQ, &ctx->QP );
323 if( ret != 0 )
324 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
325 }
Hanno Becker23344b52017-08-23 07:43:27 +0100326#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327
328 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100329 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100330 */
331
Hanno Beckerebd2c022017-10-12 10:54:53 +0100332 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100333}
334
Hanno Becker617c1ae2017-08-23 14:11:24 +0100335int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
336 unsigned char *N, size_t N_len,
337 unsigned char *P, size_t P_len,
338 unsigned char *Q, size_t Q_len,
339 unsigned char *D, size_t D_len,
340 unsigned char *E, size_t E_len )
341{
342 int ret = 0;
343
344 /* Check if key is private or public */
345 const int is_priv =
346 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
347 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
348 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
349 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
350 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
351
352 if( !is_priv )
353 {
354 /* If we're trying to export private parameters for a public key,
355 * something must be wrong. */
356 if( P != NULL || Q != NULL || D != NULL )
357 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
358
359 }
360
361 if( N != NULL )
362 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
363
364 if( P != NULL )
365 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
366
367 if( Q != NULL )
368 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
369
370 if( D != NULL )
371 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
372
373 if( E != NULL )
374 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100375
376cleanup:
377
378 return( ret );
379}
380
Hanno Becker617c1ae2017-08-23 14:11:24 +0100381int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
382 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
383 mbedtls_mpi *D, mbedtls_mpi *E )
384{
385 int ret;
386
387 /* Check if key is private or public */
388 int is_priv =
389 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
390 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
391 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
392 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
393 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
394
395 if( !is_priv )
396 {
397 /* If we're trying to export private parameters for a public key,
398 * something must be wrong. */
399 if( P != NULL || Q != NULL || D != NULL )
400 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
401
402 }
403
404 /* Export all requested core parameters. */
405
406 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
407 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
408 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
409 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
410 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
411 {
412 return( ret );
413 }
414
415 return( 0 );
416}
417
418/*
419 * Export CRT parameters
420 * This must also be implemented if CRT is not used, for being able to
421 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
422 * can be used in this case.
423 */
424int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
425 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
426{
427 int ret;
428
429 /* Check if key is private or public */
430 int is_priv =
431 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
432 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
433 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
434 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
435 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
436
437 if( !is_priv )
438 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
439
Hanno Beckerdc95c892017-08-23 06:57:02 +0100440#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100441 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100442 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
443 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
444 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
445 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100446 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100447 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100448#else
449 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
450 DP, DQ, QP ) ) != 0 )
451 {
452 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
453 }
454#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100455
456 return( 0 );
457}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100458
Paul Bakker5121ce52009-01-03 21:22:43 +0000459/*
460 * Initialize an RSA context
461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000464 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000465{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#if defined(MBEDTLS_THREADING_C)
471 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200472#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000473}
474
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100475/*
476 * Set padding for an existing RSA context
477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100479{
480 ctx->padding = padding;
481 ctx->hash_id = hash_id;
482}
483
Hanno Becker617c1ae2017-08-23 14:11:24 +0100484/*
485 * Get length in bytes of RSA modulus
486 */
487
488size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
489{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100490 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100491}
492
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496/*
497 * Generate an RSA keypair
498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000500 int (*f_rng)(void *, unsigned char *, size_t),
501 void *p_rng,
502 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000503{
504 int ret;
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100505 mbedtls_mpi H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
Paul Bakker21eb2802010-08-16 11:10:02 +0000507 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
Janos Follathef441782016-09-21 13:18:12 +0100510 if( nbits % 2 )
511 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
512
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100513 mbedtls_mpi_init( &H );
514 mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
516 /*
517 * find primes P and Q with Q < P so that:
518 * GCD( E, (P-1)*(Q-1) ) == 1
519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522 do
523 {
Janos Follath10c575b2016-02-23 14:42:48 +0000524 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100525 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
Janos Follathef441782016-09-21 13:18:12 +0100527 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100528 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 continue;
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200534 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 continue;
536
Janos Follathef441782016-09-21 13:18:12 +0100537 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100538 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100539
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100540 /* Temporarily replace P,Q by P-1, Q-1 */
541 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
542 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
543 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100548 /* Restore P,Q */
549 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
550 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
551
552 ctx->len = mbedtls_mpi_size( &ctx->N );
553
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 /*
555 * D = E^-1 mod ((P-1)*(Q-1))
556 * DP = D mod (P - 1)
557 * DQ = D mod (Q - 1)
558 * QP = Q^-1 mod P
559 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100561 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) );
562
563#if !defined(MBEDTLS_RSA_NO_CRT)
564 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
565 &ctx->DP, &ctx->DQ, &ctx->QP ) );
566#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Hanno Becker83aad1f2017-08-23 06:45:10 +0100568 /* Double-check */
569 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571cleanup:
572
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100573 mbedtls_mpi_free( &H );
574 mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
576 if( ret != 0 )
577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_rsa_free( ctx );
579 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 }
581
Paul Bakker48377d92013-08-30 12:06:24 +0200582 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583}
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587/*
588 * Check a public RSA key
589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000591{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100592 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000594
Hanno Becker3a760a12018-01-05 08:14:49 +0000595 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100598 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
Hanno Becker705fc682017-10-10 17:57:02 +0100600 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
601 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100605 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
607 return( 0 );
608}
609
610/*
Hanno Becker705fc682017-10-10 17:57:02 +0100611 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000614{
Hanno Becker705fc682017-10-10 17:57:02 +0100615 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100616 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 {
Hanno Becker98838b02017-10-02 13:16:10 +0100618 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 }
Paul Bakker48377d92013-08-30 12:06:24 +0200620
Hanno Becker98838b02017-10-02 13:16:10 +0100621 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100622 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100624 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000626
Hanno Beckerb269a852017-08-25 08:03:21 +0100627#if !defined(MBEDTLS_RSA_NO_CRT)
628 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
629 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
630 {
631 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
632 }
633#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000634
635 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636}
637
638/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100639 * Check if contexts holding a public and private key match
640 */
Hanno Becker98838b02017-10-02 13:16:10 +0100641int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
642 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100643{
Hanno Becker98838b02017-10-02 13:16:10 +0100644 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100648 }
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
651 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100654 }
655
656 return( 0 );
657}
658
659/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 * Do an RSA public key operation
661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000663 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 unsigned char *output )
665{
Paul Bakker23986e52011-04-24 08:57:21 +0000666 int ret;
667 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000669
Hanno Beckerebd2c022017-10-12 10:54:53 +0100670 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100671 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200675#if defined(MBEDTLS_THREADING_C)
676 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
677 return( ret );
678#endif
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200684 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
685 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 }
687
688 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
690 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000691
692cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200694 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
695 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100696#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
700 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
703 return( 0 );
704}
705
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200706/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200707 * Generate or update blinding values, see section 10 of:
708 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200709 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200710 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200711 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200712static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200713 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
714{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200715 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200716
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200717 if( ctx->Vf.p != NULL )
718 {
719 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
721 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
722 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
723 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200724
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200725 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200726 }
727
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200728 /* Unblinding value: Vf = random number, invertible mod N */
729 do {
730 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
734 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
735 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200736
737 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
739 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 +0200740
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200741
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200742cleanup:
743 return( ret );
744}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200745
Paul Bakker5121ce52009-01-03 21:22:43 +0000746/*
Janos Follathe81102e2017-03-22 13:38:28 +0000747 * Exponent blinding supposed to prevent side-channel attacks using multiple
748 * traces of measurements to recover the RSA key. The more collisions are there,
749 * the more bits of the key can be recovered. See [3].
750 *
751 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
752 * observations on avarage.
753 *
754 * For example with 28 byte blinding to achieve 2 collisions the adversary has
755 * to make 2^112 observations on avarage.
756 *
757 * (With the currently (as of 2017 April) known best algorithms breaking 2048
758 * bit RSA requires approximately as much time as trying out 2^112 random keys.
759 * Thus in this sense with 28 byte blinding the security is not reduced by
760 * side-channel attacks like the one in [3])
761 *
762 * This countermeasure does not help if the key recovery is possible with a
763 * single trace.
764 */
765#define RSA_EXPONENT_BLINDING 28
766
767/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000768 * Do an RSA private key operation
769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200771 int (*f_rng)(void *, unsigned char *, size_t),
772 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000773 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 unsigned char *output )
775{
Paul Bakker23986e52011-04-24 08:57:21 +0000776 int ret;
777 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +0000779 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +0000780#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000781 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000782 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +0000783#else
784 mbedtls_mpi DP_blind, DQ_blind;
785 mbedtls_mpi *DP = &ctx->DP;
786 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +0000787#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Hanno Beckerebd2c022017-10-12 10:54:53 +0100789 if( rsa_check_context( ctx, 1 /* private key checks */,
790 f_rng != NULL /* blinding y/n */ ) != 0 )
791 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100792 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100793 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000796 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
797
Janos Follathf9203b42017-03-22 15:13:15 +0000798 if( f_rng != NULL )
799 {
Janos Follathe81102e2017-03-22 13:38:28 +0000800#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000801 mbedtls_mpi_init( &D_blind );
802#else
803 mbedtls_mpi_init( &DP_blind );
804 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000805#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000806 }
Janos Follathe81102e2017-03-22 13:38:28 +0000807
Paul Bakker5121ce52009-01-03 21:22:43 +0000808
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200809#if defined(MBEDTLS_THREADING_C)
810 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
811 return( ret );
812#endif
813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
815 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200817 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
818 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000819 }
820
Paul Bakkerf451bac2013-08-30 15:37:02 +0200821 if( f_rng != NULL )
822 {
823 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200824 * Blinding
825 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200826 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200827 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
828 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000830
Janos Follathe81102e2017-03-22 13:38:28 +0000831 /*
832 * Exponent blinding
833 */
834 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
835 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
836
Janos Follathf9203b42017-03-22 15:13:15 +0000837#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000838 /*
839 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
840 */
841 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
842 f_rng, p_rng ) );
843 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
844 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
845 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
846
847 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000848#else
849 /*
850 * DP_blind = ( P - 1 ) * R + DP
851 */
852 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
853 f_rng, p_rng ) );
854 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
855 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
856 &ctx->DP ) );
857
858 DP = &DP_blind;
859
860 /*
861 * DQ_blind = ( Q - 1 ) * R + DQ
862 */
863 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
864 f_rng, p_rng ) );
865 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
866 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
867 &ctx->DQ ) );
868
869 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000870#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200871 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000874 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100875#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200876 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000877 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000878 *
879 * T1 = input ^ dP mod P
880 * T2 = input ^ dQ mod Q
881 */
Janos Follathf9203b42017-03-22 15:13:15 +0000882 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
883 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000884
885 /*
886 * T = (T1 - T2) * (Q^-1 mod P) mod P
887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
889 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
890 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
892 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200893 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
896 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
897#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200898
Paul Bakkerf451bac2013-08-30 15:37:02 +0200899 if( f_rng != NULL )
900 {
901 /*
902 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200903 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200904 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200905 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200907 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
909 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
912cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200914 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
915 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200916#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000919 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
920
921 if( f_rng != NULL )
922 {
Janos Follathe81102e2017-03-22 13:38:28 +0000923#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000924 mbedtls_mpi_free( &D_blind );
925#else
926 mbedtls_mpi_free( &DP_blind );
927 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000928#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000929 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
931 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
934 return( 0 );
935}
936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000938/**
939 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
940 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000941 * \param dst buffer to mask
942 * \param dlen length of destination buffer
943 * \param src source of the mask generation
944 * \param slen length of the source buffer
945 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000946 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100947static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000949{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000951 unsigned char counter[4];
952 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000953 unsigned int hlen;
954 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +0100955 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000958 memset( counter, 0, 4 );
959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000961
Simon Butcher02037452016-03-01 21:19:12 +0000962 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000963 p = dst;
964
965 while( dlen > 0 )
966 {
967 use_len = hlen;
968 if( dlen < hlen )
969 use_len = dlen;
970
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100971 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
972 goto exit;
973 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
974 goto exit;
975 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
976 goto exit;
977 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
978 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000979
980 for( i = 0; i < use_len; ++i )
981 *p++ ^= mask[i];
982
983 counter[3]++;
984
985 dlen -= use_len;
986 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200987
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100988exit:
Gilles Peskine18ac7162017-05-05 19:24:06 +0200989 mbedtls_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100990
991 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100996/*
997 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001000 int (*f_rng)(void *, unsigned char *, size_t),
1001 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001002 int mode,
1003 const unsigned char *label, size_t label_len,
1004 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001005 const unsigned char *input,
1006 unsigned char *output )
1007{
1008 size_t olen;
1009 int ret;
1010 unsigned char *p = output;
1011 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 const mbedtls_md_info_t *md_info;
1013 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1016 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001017
1018 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001022 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001024
1025 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001027
Simon Butcher02037452016-03-01 21:19:12 +00001028 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001029 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001031
1032 memset( output, 0, olen );
1033
1034 *p++ = 0;
1035
Simon Butcher02037452016-03-01 21:19:12 +00001036 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001037 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001039
1040 p += hlen;
1041
Simon Butcher02037452016-03-01 21:19:12 +00001042 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001043 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1044 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001045 p += hlen;
1046 p += olen - 2 * hlen - 2 - ilen;
1047 *p++ = 1;
1048 memcpy( p, input, ilen );
1049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001051 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001052 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001053
Simon Butcher02037452016-03-01 21:19:12 +00001054 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001055 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1056 &md_ctx ) ) != 0 )
1057 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001058
Simon Butcher02037452016-03-01 21:19:12 +00001059 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001060 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1061 &md_ctx ) ) != 0 )
1062 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001063
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001064exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001066
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001067 if( ret != 0 )
1068 return( ret );
1069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 return( ( mode == MBEDTLS_RSA_PUBLIC )
1071 ? mbedtls_rsa_public( ctx, output, output )
1072 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001073}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001077/*
1078 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001081 int (*f_rng)(void *, unsigned char *, size_t),
1082 void *p_rng,
1083 int mode, size_t ilen,
1084 const unsigned char *input,
1085 unsigned char *output )
1086{
1087 size_t nb_pad, olen;
1088 int ret;
1089 unsigned char *p = output;
1090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1092 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001093
Janos Follath1ed9f992016-03-18 11:45:44 +00001094 // We don't check p_rng because it won't be dereferenced here
1095 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001097
1098 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001099
Simon Butcher02037452016-03-01 21:19:12 +00001100 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001101 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001103
1104 nb_pad = olen - 3 - ilen;
1105
1106 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001110
1111 while( nb_pad-- > 0 )
1112 {
1113 int rng_dl = 100;
1114
1115 do {
1116 ret = f_rng( p_rng, p, 1 );
1117 } while( *p == 0 && --rng_dl && ret == 0 );
1118
Simon Butcher02037452016-03-01 21:19:12 +00001119 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001120 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001122
1123 p++;
1124 }
1125 }
1126 else
1127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001129
1130 while( nb_pad-- > 0 )
1131 *p++ = 0xFF;
1132 }
1133
1134 *p++ = 0;
1135 memcpy( p, input, ilen );
1136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 return( ( mode == MBEDTLS_RSA_PUBLIC )
1138 ? mbedtls_rsa_public( ctx, output, output )
1139 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001142
Paul Bakker5121ce52009-01-03 21:22:43 +00001143/*
1144 * Add the message padding, then do an RSA operation
1145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001147 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001148 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001149 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001150 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 unsigned char *output )
1152{
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 switch( ctx->padding )
1154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_PKCS1_V15)
1156 case MBEDTLS_RSA_PKCS_V15:
1157 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001159#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_PKCS1_V21)
1162 case MBEDTLS_RSA_PKCS_V21:
1163 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001164 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001165#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
1167 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001170}
1171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001173/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001174 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001177 int (*f_rng)(void *, unsigned char *, size_t),
1178 void *p_rng,
1179 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001180 const unsigned char *label, size_t label_len,
1181 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001182 const unsigned char *input,
1183 unsigned char *output,
1184 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001185{
Paul Bakker23986e52011-04-24 08:57:21 +00001186 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001187 size_t ilen, i, pad_len;
1188 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1190 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001191 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 const mbedtls_md_info_t *md_info;
1193 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001194
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001195 /*
1196 * Parameters sanity checks
1197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1199 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
1201 ilen = ctx->len;
1202
Paul Bakker27fdf462011-06-09 13:55:13 +00001203 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001207 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001209
Janos Follathc17cda12016-02-11 11:08:18 +00001210 hlen = mbedtls_md_get_size( md_info );
1211
1212 // checking for integer underflow
1213 if( 2 * hlen + 2 > ilen )
1214 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1215
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001216 /*
1217 * RSA operation
1218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1220 ? mbedtls_rsa_public( ctx, input, buf )
1221 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001222
1223 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001224 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001226 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001227 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001230 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1231 {
1232 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001233 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001234 }
1235
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001236 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001237 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1238 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001239 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001240 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1241 &md_ctx ) ) != 0 )
1242 {
1243 mbedtls_md_free( &md_ctx );
1244 goto cleanup;
1245 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001248
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001249 /* Generate lHash */
1250 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1251 goto cleanup;
1252
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001253 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001254 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001255 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001257 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001258
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001259 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001260
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001261 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001262
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001263 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001264 for( i = 0; i < hlen; i++ )
1265 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001266
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001267 /* Get zero-padding len, but always read till end of buffer
1268 * (minus one, for the 01 byte) */
1269 pad_len = 0;
1270 pad_done = 0;
1271 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1272 {
1273 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001274 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001275 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001276
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001277 p += pad_len;
1278 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001279
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001280 /*
1281 * The only information "leaked" is whether the padding was correct or not
1282 * (eg, no data is copied if it was not correct). This meets the
1283 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1284 * the different error conditions.
1285 */
1286 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001287 {
1288 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1289 goto cleanup;
1290 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001291
Paul Bakker66d5d072014-06-17 16:39:18 +02001292 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001293 {
1294 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1295 goto cleanup;
1296 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001297
1298 *olen = ilen - (p - buf);
1299 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001300 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001301
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001302cleanup:
1303 mbedtls_zeroize( buf, sizeof( buf ) );
1304 mbedtls_zeroize( lhash, sizeof( lhash ) );
1305
1306 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001311/*
1312 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001315 int (*f_rng)(void *, unsigned char *, size_t),
1316 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001317 int mode, size_t *olen,
1318 const unsigned char *input,
1319 unsigned char *output,
1320 size_t output_max_len)
1321{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001322 int ret;
1323 size_t ilen, pad_count = 0, i;
1324 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1328 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001329
1330 ilen = ctx->len;
1331
1332 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1336 ? mbedtls_rsa_public( ctx, input, buf )
1337 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001338
1339 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001340 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001341
1342 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001343 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001344
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001345 /*
1346 * Check and get padding len in "constant-time"
1347 */
1348 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001349
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001350 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001354
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001355 /* Get padding len, but always read till end of buffer
1356 * (minus one, for the 00 byte) */
1357 for( i = 0; i < ilen - 3; i++ )
1358 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001359 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1360 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001361 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001362
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001363 p += pad_count;
1364 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001365 }
1366 else
1367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001369
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001370 /* Get padding len, but always read till end of buffer
1371 * (minus one, for the 00 byte) */
1372 for( i = 0; i < ilen - 3; i++ )
1373 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001374 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001375 pad_count += ( pad_done == 0 );
1376 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001377
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001378 p += pad_count;
1379 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 }
1381
Janos Follathc69fa502016-02-12 13:30:09 +00001382 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001383
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001384 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001385 {
1386 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1387 goto cleanup;
1388 }
Paul Bakker8804f692013-02-28 18:06:26 +01001389
Paul Bakker66d5d072014-06-17 16:39:18 +02001390 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001391 {
1392 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1393 goto cleanup;
1394 }
Paul Bakker060c5682009-01-12 21:48:39 +00001395
Paul Bakker27fdf462011-06-09 13:55:13 +00001396 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001398 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001400cleanup:
1401 mbedtls_zeroize( buf, sizeof( buf ) );
1402
1403 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
1407/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001408 * Do an RSA operation, then remove the message padding
1409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001411 int (*f_rng)(void *, unsigned char *, size_t),
1412 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001413 int mode, size_t *olen,
1414 const unsigned char *input,
1415 unsigned char *output,
1416 size_t output_max_len)
1417{
1418 switch( ctx->padding )
1419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if defined(MBEDTLS_PKCS1_V15)
1421 case MBEDTLS_RSA_PKCS_V15:
1422 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001423 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001424#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_PKCS1_V21)
1427 case MBEDTLS_RSA_PKCS_V21:
1428 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001429 olen, input, output,
1430 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001431#endif
1432
1433 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001435 }
1436}
1437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001439/*
1440 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001443 int (*f_rng)(void *, unsigned char *, size_t),
1444 void *p_rng,
1445 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001447 unsigned int hashlen,
1448 const unsigned char *hash,
1449 unsigned char *sig )
1450{
1451 size_t olen;
1452 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001454 unsigned int slen, hlen, offset = 0;
1455 int ret;
1456 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 const mbedtls_md_info_t *md_info;
1458 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1461 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001462
1463 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001465
1466 olen = ctx->len;
1467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001469 {
Simon Butcher02037452016-03-01 21:19:12 +00001470 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001472 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001476 }
1477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001479 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001483 slen = hlen;
1484
1485 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001487
1488 memset( sig, 0, olen );
1489
Simon Butcher02037452016-03-01 21:19:12 +00001490 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001491 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001493
Simon Butcher02037452016-03-01 21:19:12 +00001494 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001495 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001496 p += olen - hlen * 2 - 2;
1497 *p++ = 0x01;
1498 memcpy( p, salt, slen );
1499 p += slen;
1500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001502 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001503 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001504
Simon Butcher02037452016-03-01 21:19:12 +00001505 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001506 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1507 goto exit;
1508 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1509 goto exit;
1510 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1511 goto exit;
1512 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1513 goto exit;
1514 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1515 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001516
Simon Butcher02037452016-03-01 21:19:12 +00001517 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001518 if( msb % 8 == 0 )
1519 offset = 1;
1520
Simon Butcher02037452016-03-01 21:19:12 +00001521 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001522 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1523 &md_ctx ) ) != 0 )
1524 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001525
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001526 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001527 sig[0] &= 0xFF >> ( olen * 8 - msb );
1528
1529 p += hlen;
1530 *p++ = 0xBC;
1531
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001532 mbedtls_zeroize( salt, sizeof( salt ) );
1533
1534exit:
1535 mbedtls_md_free( &md_ctx );
1536
1537 if( ret != 0 )
1538 return( ret );
1539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 return( ( mode == MBEDTLS_RSA_PUBLIC )
1541 ? mbedtls_rsa_public( ctx, sig, sig )
1542 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001547/*
1548 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1549 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001550
1551/* Construct a PKCS v1.5 encoding of a hashed message
1552 *
1553 * This is used both for signature generation and verification.
1554 *
1555 * Parameters:
1556 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001557 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001558 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001559 * - hash: Buffer containing the hashed message or the raw data.
1560 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001561 * - dst: Buffer to hold the encoded message.
1562 *
1563 * Assumptions:
1564 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1565 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001566 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001567 *
1568 */
1569static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1570 unsigned int hashlen,
1571 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001572 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001573 unsigned char *dst )
1574{
1575 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001576 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001577 unsigned char *p = dst;
1578 const char *oid = NULL;
1579
1580 /* Are we signing hashed or raw data? */
1581 if( md_alg != MBEDTLS_MD_NONE )
1582 {
1583 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1584 if( md_info == NULL )
1585 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1586
1587 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1588 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1589
1590 hashlen = mbedtls_md_get_size( md_info );
1591
1592 /* Double-check that 8 + hashlen + oid_size can be used as a
1593 * 1-byte ASN.1 length encoding and that there's no overflow. */
1594 if( 8 + hashlen + oid_size >= 0x80 ||
1595 10 + hashlen < hashlen ||
1596 10 + hashlen + oid_size < 10 + hashlen )
1597 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1598
1599 /*
1600 * Static bounds check:
1601 * - Need 10 bytes for five tag-length pairs.
1602 * (Insist on 1-byte length encodings to protect against variants of
1603 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1604 * - Need hashlen bytes for hash
1605 * - Need oid_size bytes for hash alg OID.
1606 */
1607 if( nb_pad < 10 + hashlen + oid_size )
1608 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1609 nb_pad -= 10 + hashlen + oid_size;
1610 }
1611 else
1612 {
1613 if( nb_pad < hashlen )
1614 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1615
1616 nb_pad -= hashlen;
1617 }
1618
Hanno Becker2b2f8982017-09-27 17:10:03 +01001619 /* Need space for signature header and padding delimiter (3 bytes),
1620 * and 8 bytes for the minimal padding */
1621 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001622 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1623 nb_pad -= 3;
1624
1625 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001626 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001627
1628 /* Write signature header and padding */
1629 *p++ = 0;
1630 *p++ = MBEDTLS_RSA_SIGN;
1631 memset( p, 0xFF, nb_pad );
1632 p += nb_pad;
1633 *p++ = 0;
1634
1635 /* Are we signing raw data? */
1636 if( md_alg == MBEDTLS_MD_NONE )
1637 {
1638 memcpy( p, hash, hashlen );
1639 return( 0 );
1640 }
1641
1642 /* Signing hashed data, add corresponding ASN.1 structure
1643 *
1644 * DigestInfo ::= SEQUENCE {
1645 * digestAlgorithm DigestAlgorithmIdentifier,
1646 * digest Digest }
1647 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1648 * Digest ::= OCTET STRING
1649 *
1650 * Schematic:
1651 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1652 * TAG-NULL + LEN [ NULL ] ]
1653 * TAG-OCTET + LEN [ HASH ] ]
1654 */
1655 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001656 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001657 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001658 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001659 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001660 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001661 memcpy( p, oid, oid_size );
1662 p += oid_size;
1663 *p++ = MBEDTLS_ASN1_NULL;
1664 *p++ = 0x00;
1665 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001666 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001667 memcpy( p, hash, hashlen );
1668 p += hashlen;
1669
1670 /* Just a sanity-check, should be automatic
1671 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01001672 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001673 {
Hanno Beckere58d38c2017-09-27 17:09:00 +01001674 mbedtls_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001675 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1676 }
1677
1678 return( 0 );
1679}
1680
Paul Bakkerb3869132013-02-28 17:21:01 +01001681/*
1682 * Do an RSA operation to sign the message digest
1683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001685 int (*f_rng)(void *, unsigned char *, size_t),
1686 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001687 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001689 unsigned int hashlen,
1690 const unsigned char *hash,
1691 unsigned char *sig )
1692{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001693 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001694 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1697 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001698
Hanno Beckerfdf38032017-09-06 12:35:55 +01001699 /*
1700 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1701 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001702
Hanno Beckerfdf38032017-09-06 12:35:55 +01001703 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
1704 ctx->len, sig ) ) != 0 )
1705 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001706
1707 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01001708 * Call respective RSA primitive
1709 */
1710
1711 if( mode == MBEDTLS_RSA_PUBLIC )
1712 {
1713 /* Skip verification on a public key operation */
1714 return( mbedtls_rsa_public( ctx, sig, sig ) );
1715 }
1716
1717 /* Private key operation
1718 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001719 * In order to prevent Lenstra's attack, make the signature in a
1720 * temporary buffer and check it before returning it.
1721 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001722
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001723 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001724 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001725 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1726
Hanno Beckerfdf38032017-09-06 12:35:55 +01001727 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001728 if( verif == NULL )
1729 {
1730 mbedtls_free( sig_try );
1731 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1732 }
1733
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001734 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1735 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1736
Hanno Becker171a8f12017-09-06 12:32:16 +01001737 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001738 {
1739 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1740 goto cleanup;
1741 }
1742
1743 memcpy( sig, sig_try, ctx->len );
1744
1745cleanup:
1746 mbedtls_free( sig_try );
1747 mbedtls_free( verif );
1748
1749 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001750}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001752
1753/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001754 * Do an RSA operation to sign the message digest
1755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001757 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001758 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001759 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001761 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001762 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 unsigned char *sig )
1764{
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 switch( ctx->padding )
1766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#if defined(MBEDTLS_PKCS1_V15)
1768 case MBEDTLS_RSA_PKCS_V15:
1769 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001770 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001771#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_PKCS1_V21)
1774 case MBEDTLS_RSA_PKCS_V21:
1775 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001776 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001777#endif
1778
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001782}
1783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001785/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001786 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001789 int (*f_rng)(void *, unsigned char *, size_t),
1790 void *p_rng,
1791 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001793 unsigned int hashlen,
1794 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001796 int expected_salt_len,
1797 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001798{
Paul Bakker23986e52011-04-24 08:57:21 +00001799 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001800 size_t siglen;
1801 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001802 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001804 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001805 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001806 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 const mbedtls_md_info_t *md_info;
1808 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001809 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1812 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001813
Paul Bakker5121ce52009-01-03 21:22:43 +00001814 siglen = ctx->len;
1815
Paul Bakker27fdf462011-06-09 13:55:13 +00001816 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1820 ? mbedtls_rsa_public( ctx, sig, buf )
1821 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
1823 if( ret != 0 )
1824 return( ret );
1825
1826 p = buf;
1827
Paul Bakkerb3869132013-02-28 17:21:01 +01001828 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001832 {
Simon Butcher02037452016-03-01 21:19:12 +00001833 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001835 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001839 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001842 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001846
Paul Bakkerb3869132013-02-28 17:21:01 +01001847 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001848
Simon Butcher02037452016-03-01 21:19:12 +00001849 /*
1850 * Note: EMSA-PSS verification is over the length of N - 1 bits
1851 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001852 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001853
Gilles Peskineb00b0da2017-10-19 15:23:49 +02001854 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1855 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1856
Simon Butcher02037452016-03-01 21:19:12 +00001857 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001858 if( msb % 8 == 0 )
1859 {
1860 p++;
1861 siglen -= 1;
1862 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001863
Gilles Peskine139108a2017-10-18 19:03:42 +02001864 if( siglen < hlen + 2 )
1865 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1866 hash_start = p + siglen - hlen - 1;
1867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001869 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001870 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001871
Jaeden Amero66954e12018-01-25 16:05:54 +00001872 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
1873 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001874 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01001875
Paul Bakkerb3869132013-02-28 17:21:01 +01001876 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001877
Gilles Peskine6a54b022017-10-17 19:02:13 +02001878 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001879 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001880
Gilles Peskine91048a32017-10-19 17:46:14 +02001881 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001882 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001883 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1884 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001885 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001886
Gilles Peskine6a54b022017-10-17 19:02:13 +02001887 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02001890 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001891 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001892 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1893 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001894 }
1895
Simon Butcher02037452016-03-01 21:19:12 +00001896 /*
1897 * Generate H = Hash( M' )
1898 */
Jaeden Amero66954e12018-01-25 16:05:54 +00001899 ret = mbedtls_md_starts( &md_ctx );
1900 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001901 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00001902 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
1903 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001904 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00001905 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
1906 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001907 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00001908 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
1909 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001910 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00001911 ret = mbedtls_md_finish( &md_ctx, result );
1912 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001913 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00001914
Jaeden Amero66954e12018-01-25 16:05:54 +00001915 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01001916 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001917 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01001918 goto exit;
1919 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001920
1921exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001923
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001924 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001925}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001926
1927/*
1928 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001931 int (*f_rng)(void *, unsigned char *, size_t),
1932 void *p_rng,
1933 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001935 unsigned int hashlen,
1936 const unsigned char *hash,
1937 const unsigned char *sig )
1938{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1940 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001941 : md_alg;
1942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001944 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001946 sig ) );
1947
1948}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001952/*
1953 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1954 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001956 int (*f_rng)(void *, unsigned char *, size_t),
1957 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001958 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001960 unsigned int hashlen,
1961 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001962 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001963{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001964 int ret = 0;
1965 const size_t sig_len = ctx->len;
1966 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1969 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001970
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001971 /*
1972 * Prepare expected PKCS1 v1.5 encoding of hash.
1973 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001974
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001975 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
1976 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
1977 {
1978 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1979 goto cleanup;
1980 }
1981
1982 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
1983 encoded_expected ) ) != 0 )
1984 goto cleanup;
1985
1986 /*
1987 * Apply RSA primitive to get what should be PKCS1 encoded hash.
1988 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001991 ? mbedtls_rsa_public( ctx, sig, encoded )
1992 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01001993 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001994 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001995
Simon Butcher02037452016-03-01 21:19:12 +00001996 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01001997 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00001998 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001999
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002000 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2001 sig_len ) ) != 0 )
2002 {
2003 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2004 goto cleanup;
2005 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002006
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002007cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002008
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002009 if( encoded != NULL )
2010 {
2011 mbedtls_zeroize( encoded, sig_len );
2012 mbedtls_free( encoded );
2013 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002014
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002015 if( encoded_expected != NULL )
2016 {
2017 mbedtls_zeroize( encoded_expected, sig_len );
2018 mbedtls_free( encoded_expected );
2019 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002020
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002021 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002024
2025/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002026 * Do an RSA operation and check the message digest
2027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002029 int (*f_rng)(void *, unsigned char *, size_t),
2030 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002031 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002033 unsigned int hashlen,
2034 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002035 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002036{
2037 switch( ctx->padding )
2038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#if defined(MBEDTLS_PKCS1_V15)
2040 case MBEDTLS_RSA_PKCS_V15:
2041 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002042 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002043#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045#if defined(MBEDTLS_PKCS1_V21)
2046 case MBEDTLS_RSA_PKCS_V21:
2047 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002048 hashlen, hash, sig );
2049#endif
2050
2051 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002053 }
2054}
2055
2056/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002057 * Copy the components of an RSA key
2058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002060{
2061 int ret;
2062
2063 dst->ver = src->ver;
2064 dst->len = src->len;
2065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2067 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2070 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2071 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002072
2073#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2075 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2076 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2078 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002079#endif
2080
2081 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2084 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002085
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002086 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002087 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002088
2089cleanup:
2090 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002092
2093 return( ret );
2094}
2095
2096/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 * Free the components of an RSA key
2098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002102 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2103 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002105
Hanno Becker33c30a02017-08-23 07:00:22 +01002106#if !defined(MBEDTLS_RSA_NO_CRT)
2107 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2108 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2109 mbedtls_mpi_free( &ctx->DP );
2110#endif /* MBEDTLS_RSA_NO_CRT */
2111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112#if defined(MBEDTLS_THREADING_C)
2113 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002114#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002115}
2116
Hanno Beckerab377312017-08-23 16:24:51 +01002117#endif /* !MBEDTLS_RSA_ALT */
2118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002120
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002121#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
2123/*
2124 * Example RSA-1024 keypair, for test purposes
2125 */
2126#define KEY_LEN 128
2127
2128#define RSA_N "9292758453063D803DD603D5E777D788" \
2129 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2130 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2131 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2132 "93A89813FBF3C4F8066D2D800F7C38A8" \
2133 "1AE31942917403FF4946B0A83D3D3E05" \
2134 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2135 "5E94BB77B07507233A0BC7BAC8F90F79"
2136
2137#define RSA_E "10001"
2138
2139#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2140 "66CA472BC44D253102F8B4A9D3BFA750" \
2141 "91386C0077937FE33FA3252D28855837" \
2142 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2143 "DF79C5CE07EE72C7F123142198164234" \
2144 "CABB724CF78B8173B9F880FC86322407" \
2145 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2146 "071513A1E85B5DFA031F21ECAE91A34D"
2147
2148#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2149 "2C01CAD19EA484A87EA4377637E75500" \
2150 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2151 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2152
2153#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2154 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2155 "910E4168387E3C30AA1E00C339A79508" \
2156 "8452DD96A9A5EA5D9DCA68DA636032AF"
2157
Paul Bakker5121ce52009-01-03 21:22:43 +00002158#define PT_LEN 24
2159#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2160 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002163static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002164{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002165#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002166 size_t i;
2167
Paul Bakker545570e2010-07-18 09:00:25 +00002168 if( rng_state != NULL )
2169 rng_state = NULL;
2170
Paul Bakkera3d195c2011-11-27 21:07:34 +00002171 for( i = 0; i < len; ++i )
2172 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002173#else
2174 if( rng_state != NULL )
2175 rng_state = NULL;
2176
2177 arc4random_buf( output, len );
2178#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002179
Paul Bakkera3d195c2011-11-27 21:07:34 +00002180 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002181}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002183
Paul Bakker5121ce52009-01-03 21:22:43 +00002184/*
2185 * Checkup routine
2186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002188{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002189 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002191 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 unsigned char rsa_plaintext[PT_LEN];
2194 unsigned char rsa_decrypted[PT_LEN];
2195 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002197 unsigned char sha1sum[20];
2198#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002199
Hanno Becker3a701162017-08-22 13:52:43 +01002200 mbedtls_mpi K;
2201
2202 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
Hanno Becker3a701162017-08-22 13:52:43 +01002205 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2206 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2207 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2208 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2209 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2210 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2211 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2212 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2213 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2214 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2215
Hanno Becker7f25f852017-10-10 16:56:22 +01002216 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002217
2218 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2222 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 {
2224 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002226
2227 return( 1 );
2228 }
2229
2230 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232
2233 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2234
Hanno Becker98838b02017-10-02 13:16:10 +01002235 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2236 PT_LEN, rsa_plaintext,
2237 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 {
2239 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002241
2242 return( 1 );
2243 }
2244
2245 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002247
Hanno Becker98838b02017-10-02 13:16:10 +01002248 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2249 &len, rsa_ciphertext, rsa_decrypted,
2250 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 {
2252 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
2255 return( 1 );
2256 }
2257
2258 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2259 {
2260 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002262
2263 return( 1 );
2264 }
2265
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002266 if( verbose != 0 )
2267 mbedtls_printf( "passed\n" );
2268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002271 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002273 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002274 {
2275 if( verbose != 0 )
2276 mbedtls_printf( "failed\n" );
2277
2278 return( 1 );
2279 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
Hanno Becker98838b02017-10-02 13:16:10 +01002281 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2282 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2283 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 {
2285 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002287
2288 return( 1 );
2289 }
2290
2291 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002293
Hanno Becker98838b02017-10-02 13:16:10 +01002294 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2295 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2296 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002297 {
2298 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
2301 return( 1 );
2302 }
2303
2304 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002305 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002307
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002308 if( verbose != 0 )
2309 mbedtls_printf( "\n" );
2310
Paul Bakker3d8fb632014-04-17 12:42:41 +02002311cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002312 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 mbedtls_rsa_free( &rsa );
2314#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002315 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002317 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002318}
2319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322#endif /* MBEDTLS_RSA_C */