blob: 603db092c7122a0eba703f8c211694df0b1331c8 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Hanno Becker74716312017-10-02 10:00:37 +010021
Paul Bakker5121ce52009-01-03 21:22:43 +000022/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000023 * The following sources were referenced in the design of this implementation
24 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000025 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000026 * [1] A method for obtaining digital signatures and public-key cryptosystems
27 * R Rivest, A Shamir, and L Adleman
28 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
29 *
30 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
31 * Menezes, van Oorschot and Vanstone
32 *
Janos Follathe81102e2017-03-22 13:38:28 +000033 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
34 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
35 * Stefan Mangard
36 * https://arxiv.org/abs/1702.08719v2
37 *
Paul Bakker5121ce52009-01-03 21:22:43 +000038 */
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020042#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010049#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050051#include "mbedtls/platform_util.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000052
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <string.h>
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000061#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010065#else
Rich Evans00ab4702015-02-06 13:43:58 +000066#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020068#define mbedtls_calloc calloc
69#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010070#endif
71
Hanno Beckera565f542017-10-11 11:00:19 +010072#if !defined(MBEDTLS_RSA_ALT)
73
Hanno Beckerddeeed72018-12-13 18:07:00 +000074/* Parameter validation macros */
75#define RSA_VALIDATE_RET( cond ) \
76 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
77#define RSA_VALIDATE( cond ) \
78 MBEDTLS_INTERNAL_VALIDATE( cond )
79
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010080#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +010081/* constant-time buffer comparison */
82static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
83{
84 size_t i;
85 const unsigned char *A = (const unsigned char *) a;
86 const unsigned char *B = (const unsigned char *) b;
87 unsigned char diff = 0;
88
89 for( i = 0; i < n; i++ )
90 diff |= A[i] ^ B[i];
91
92 return( diff );
93}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010094#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +010095
Hanno Becker617c1ae2017-08-23 14:11:24 +010096int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
97 const mbedtls_mpi *N,
98 const mbedtls_mpi *P, const mbedtls_mpi *Q,
99 const mbedtls_mpi *D, const mbedtls_mpi *E )
100{
101 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000102 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100103
104 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
105 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
106 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
107 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
108 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
109 {
110 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
111 }
112
113 if( N != NULL )
114 ctx->len = mbedtls_mpi_size( &ctx->N );
115
116 return( 0 );
117}
118
119int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100120 unsigned char const *N, size_t N_len,
121 unsigned char const *P, size_t P_len,
122 unsigned char const *Q, size_t Q_len,
123 unsigned char const *D, size_t D_len,
124 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100125{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000126 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000127 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100128
129 if( N != NULL )
130 {
131 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
132 ctx->len = mbedtls_mpi_size( &ctx->N );
133 }
134
135 if( P != NULL )
136 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
137
138 if( Q != NULL )
139 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
140
141 if( D != NULL )
142 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
143
144 if( E != NULL )
145 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
146
147cleanup:
148
149 if( ret != 0 )
150 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
151
152 return( 0 );
153}
154
Hanno Becker705fc682017-10-10 17:57:02 +0100155/*
156 * Checks whether the context fields are set in such a way
157 * that the RSA primitives will be able to execute without error.
158 * It does *not* make guarantees for consistency of the parameters.
159 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100160static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
161 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100162{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100163#if !defined(MBEDTLS_RSA_NO_CRT)
164 /* blinding_needed is only used for NO_CRT to decide whether
165 * P,Q need to be present or not. */
166 ((void) blinding_needed);
167#endif
168
Hanno Becker3a760a12018-01-05 08:14:49 +0000169 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
170 ctx->len > MBEDTLS_MPI_MAX_SIZE )
171 {
Hanno Becker705fc682017-10-10 17:57:02 +0100172 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000173 }
Hanno Becker705fc682017-10-10 17:57:02 +0100174
175 /*
176 * 1. Modular exponentiation needs positive, odd moduli.
177 */
178
179 /* Modular exponentiation wrt. N is always used for
180 * RSA public key operations. */
181 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
182 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
183 {
184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
185 }
186
187#if !defined(MBEDTLS_RSA_NO_CRT)
188 /* Modular exponentiation for P and Q is only
189 * used for private key operations and if CRT
190 * is used. */
191 if( is_priv &&
192 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
193 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
194 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
195 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
196 {
197 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
198 }
199#endif /* !MBEDTLS_RSA_NO_CRT */
200
201 /*
202 * 2. Exponents must be positive
203 */
204
205 /* Always need E for public key operations */
206 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
208
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100209#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100210 /* For private key operations, use D or DP & DQ
211 * as (unblinded) exponents. */
212 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
213 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
214#else
215 if( is_priv &&
216 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
217 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
218 {
219 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
220 }
221#endif /* MBEDTLS_RSA_NO_CRT */
222
223 /* Blinding shouldn't make exponents negative either,
224 * so check that P, Q >= 1 if that hasn't yet been
225 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100226#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100227 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100228 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
229 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
230 {
231 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
232 }
233#endif
234
235 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100236 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100237#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100238 if( is_priv &&
239 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
240 {
241 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
242 }
243#endif
244
245 return( 0 );
246}
247
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100248int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100249{
250 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000251 int have_N, have_P, have_Q, have_D, have_E;
252 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100253
Hanno Beckerddeeed72018-12-13 18:07:00 +0000254 RSA_VALIDATE_RET( ctx != NULL );
255
256 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
257 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
258 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
259 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
260 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100261
Hanno Becker617c1ae2017-08-23 14:11:24 +0100262 /*
263 * Check whether provided parameters are enough
264 * to deduce all others. The following incomplete
265 * parameter sets for private keys are supported:
266 *
267 * (1) P, Q missing.
268 * (2) D and potentially N missing.
269 *
270 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100271
Hanno Beckerddeeed72018-12-13 18:07:00 +0000272 n_missing = have_P && have_Q && have_D && have_E;
273 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
274 d_missing = have_P && have_Q && !have_D && have_E;
275 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100276
277 /* These three alternatives are mutually exclusive */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000278 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100279
Hanno Becker617c1ae2017-08-23 14:11:24 +0100280 if( !is_priv && !is_pub )
281 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
282
283 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100284 * Step 1: Deduce N if P, Q are provided.
285 */
286
287 if( !have_N && have_P && have_Q )
288 {
289 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
290 &ctx->Q ) ) != 0 )
291 {
292 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
293 }
294
295 ctx->len = mbedtls_mpi_size( &ctx->N );
296 }
297
298 /*
299 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100300 */
301
302 if( pq_missing )
303 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100304 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100305 &ctx->P, &ctx->Q );
306 if( ret != 0 )
307 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
308
309 }
310 else if( d_missing )
311 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100312 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
313 &ctx->Q,
314 &ctx->E,
315 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100316 {
317 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
318 }
319 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100320
Hanno Becker617c1ae2017-08-23 14:11:24 +0100321 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100322 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100323 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324 */
325
Hanno Becker23344b52017-08-23 07:43:27 +0100326#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327 if( is_priv )
328 {
329 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
330 &ctx->DP, &ctx->DQ, &ctx->QP );
331 if( ret != 0 )
332 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
333 }
Hanno Becker23344b52017-08-23 07:43:27 +0100334#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100335
336 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100337 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100338 */
339
Hanno Beckerebd2c022017-10-12 10:54:53 +0100340 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100341}
342
Hanno Becker617c1ae2017-08-23 14:11:24 +0100343int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
344 unsigned char *N, size_t N_len,
345 unsigned char *P, size_t P_len,
346 unsigned char *Q, size_t Q_len,
347 unsigned char *D, size_t D_len,
348 unsigned char *E, size_t E_len )
349{
350 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000351 int is_priv;
352 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100353
354 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000355 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100356 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
357 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
358 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
359 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
360 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
361
362 if( !is_priv )
363 {
364 /* If we're trying to export private parameters for a public key,
365 * something must be wrong. */
366 if( P != NULL || Q != NULL || D != NULL )
367 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
368
369 }
370
371 if( N != NULL )
372 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
373
374 if( P != NULL )
375 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
376
377 if( Q != NULL )
378 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
379
380 if( D != NULL )
381 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
382
383 if( E != NULL )
384 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100385
386cleanup:
387
388 return( ret );
389}
390
Hanno Becker617c1ae2017-08-23 14:11:24 +0100391int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
392 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
393 mbedtls_mpi *D, mbedtls_mpi *E )
394{
395 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000396 int is_priv;
397 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100398
399 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000400 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100401 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
402 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
403 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
404 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
405 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
406
407 if( !is_priv )
408 {
409 /* If we're trying to export private parameters for a public key,
410 * something must be wrong. */
411 if( P != NULL || Q != NULL || D != NULL )
412 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
413
414 }
415
416 /* Export all requested core parameters. */
417
418 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
419 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
420 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
421 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
422 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
423 {
424 return( ret );
425 }
426
427 return( 0 );
428}
429
430/*
431 * Export CRT parameters
432 * This must also be implemented if CRT is not used, for being able to
433 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
434 * can be used in this case.
435 */
436int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
437 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
438{
439 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000440 int is_priv;
441 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100442
443 /* Check if key is private or public */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000444 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100445 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
446 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
447 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
448 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
449 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
450
451 if( !is_priv )
452 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
453
Hanno Beckerdc95c892017-08-23 06:57:02 +0100454#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100455 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100456 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
457 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
458 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
459 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100460 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100461 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100462#else
463 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
464 DP, DQ, QP ) ) != 0 )
465 {
466 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
467 }
468#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100469
470 return( 0 );
471}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100472
Paul Bakker5121ce52009-01-03 21:22:43 +0000473/*
474 * Initialize an RSA context
475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000478 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000479{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000480 RSA_VALIDATE( ctx != NULL );
481 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
482 padding == MBEDTLS_RSA_PKCS_V21 );
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488#if defined(MBEDTLS_THREADING_C)
489 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200490#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000491}
492
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100493/*
494 * Set padding for an existing RSA context
495 */
Hanno Beckerddeeed72018-12-13 18:07:00 +0000496void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
497 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100498{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000499 RSA_VALIDATE( ctx != NULL );
500 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
501 padding == MBEDTLS_RSA_PKCS_V21 );
502
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100503 ctx->padding = padding;
504 ctx->hash_id = hash_id;
505}
506
Hanno Becker617c1ae2017-08-23 14:11:24 +0100507/*
508 * Get length in bytes of RSA modulus
509 */
510
511size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
512{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100513 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100514}
515
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
519/*
520 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800521 *
522 * This generation method follows the RSA key pair generation procedure of
523 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000526 int (*f_rng)(void *, unsigned char *, size_t),
527 void *p_rng,
528 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000529{
530 int ret;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800531 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100532 int prime_quality = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000533 RSA_VALIDATE_RET( ctx != NULL );
534 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Hanno Beckerddeeed72018-12-13 18:07:00 +0000536 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
Janos Follathef441782016-09-21 13:18:12 +0100537 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
538
Janos Follathb8fc1b02018-09-03 15:37:01 +0100539 /*
540 * If the modulus is 1024 bit long or shorter, then the security strength of
541 * the RSA algorithm is less than or equal to 80 bits and therefore an error
542 * rate of 2^-80 is sufficient.
543 */
544 if( nbits > 1024 )
545 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
546
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100547 mbedtls_mpi_init( &H );
548 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800549 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
551 /*
552 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800553 * 1. |P-Q| > 2^( nbits / 2 - 100 )
554 * 2. GCD( E, (P-1)*(Q-1) ) == 1
555 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 do
560 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100561 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
562 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
Janos Follathb8fc1b02018-09-03 15:37:01 +0100564 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
565 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800567 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
568 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
569 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 continue;
571
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800572 /* not required by any standards, but some users rely on the fact that P > Q */
573 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100574 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100575
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100576 /* Temporarily replace P,Q by P-1, Q-1 */
577 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
578 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
579 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800580
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800581 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800583 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
584 continue;
585
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800586 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Jethro Beekman97f95c92018-02-13 15:50:36 -0800587 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
588 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
589 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
590
591 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
592 continue;
593
594 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800596 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100598 /* Restore P,Q */
599 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
600 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
601
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800602 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
603
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100604 ctx->len = mbedtls_mpi_size( &ctx->N );
605
Jethro Beekman97f95c92018-02-13 15:50:36 -0800606#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 * DP = D mod (P - 1)
609 * DQ = D mod (Q - 1)
610 * QP = Q^-1 mod P
611 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100612 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
613 &ctx->DP, &ctx->DQ, &ctx->QP ) );
614#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Hanno Becker83aad1f2017-08-23 06:45:10 +0100616 /* Double-check */
617 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
619cleanup:
620
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100621 mbedtls_mpi_free( &H );
622 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800623 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
625 if( ret != 0 )
626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_rsa_free( ctx );
628 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 }
630
Paul Bakker48377d92013-08-30 12:06:24 +0200631 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000632}
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636/*
637 * Check a public RSA key
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000640{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000641 RSA_VALIDATE_RET( ctx != NULL );
642
Hanno Beckerebd2c022017-10-12 10:54:53 +0100643 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000645
Hanno Becker3a760a12018-01-05 08:14:49 +0000646 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100649 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
Hanno Becker705fc682017-10-10 17:57:02 +0100651 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
652 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100656 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
658 return( 0 );
659}
660
661/*
Hanno Becker705fc682017-10-10 17:57:02 +0100662 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000665{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000666 RSA_VALIDATE_RET( ctx != NULL );
667
Hanno Becker705fc682017-10-10 17:57:02 +0100668 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100669 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 {
Hanno Becker98838b02017-10-02 13:16:10 +0100671 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 }
Paul Bakker48377d92013-08-30 12:06:24 +0200673
Hanno Becker98838b02017-10-02 13:16:10 +0100674 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100675 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100677 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000679
Hanno Beckerb269a852017-08-25 08:03:21 +0100680#if !defined(MBEDTLS_RSA_NO_CRT)
681 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
682 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
683 {
684 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
685 }
686#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000687
688 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000689}
690
691/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100692 * Check if contexts holding a public and private key match
693 */
Hanno Becker98838b02017-10-02 13:16:10 +0100694int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
695 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696{
Hanno Beckerddeeed72018-12-13 18:07:00 +0000697 RSA_VALIDATE_RET( pub != NULL );
698 RSA_VALIDATE_RET( prv != NULL );
699
Hanno Becker98838b02017-10-02 13:16:10 +0100700 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100704 }
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
707 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100710 }
711
712 return( 0 );
713}
714
715/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000716 * Do an RSA public key operation
717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000719 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 unsigned char *output )
721{
Paul Bakker23986e52011-04-24 08:57:21 +0000722 int ret;
723 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_mpi T;
Hanno Beckerddeeed72018-12-13 18:07:00 +0000725 RSA_VALIDATE_RET( ctx != NULL );
726 RSA_VALIDATE_RET( input != NULL );
727 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
Hanno Beckerebd2c022017-10-12 10:54:53 +0100729 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100730 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200734#if defined(MBEDTLS_THREADING_C)
735 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
736 return( ret );
737#endif
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200743 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
744 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000745 }
746
747 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
749 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
751cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200753 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
754 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100755#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
759 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
762 return( 0 );
763}
764
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200765/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200766 * Generate or update blinding values, see section 10 of:
767 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200768 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200769 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200770 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200771static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200772 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
773{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200774 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200775
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200776 if( ctx->Vf.p != NULL )
777 {
778 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
780 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
781 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
782 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200783
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200784 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200785 }
786
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200787 /* Unblinding value: Vf = random number, invertible mod N */
788 do {
789 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
793 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
794 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200795
796 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
798 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 +0200799
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200800
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200801cleanup:
802 return( ret );
803}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200804
Paul Bakker5121ce52009-01-03 21:22:43 +0000805/*
Janos Follathe81102e2017-03-22 13:38:28 +0000806 * Exponent blinding supposed to prevent side-channel attacks using multiple
807 * traces of measurements to recover the RSA key. The more collisions are there,
808 * the more bits of the key can be recovered. See [3].
809 *
810 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
811 * observations on avarage.
812 *
813 * For example with 28 byte blinding to achieve 2 collisions the adversary has
814 * to make 2^112 observations on avarage.
815 *
816 * (With the currently (as of 2017 April) known best algorithms breaking 2048
817 * bit RSA requires approximately as much time as trying out 2^112 random keys.
818 * Thus in this sense with 28 byte blinding the security is not reduced by
819 * side-channel attacks like the one in [3])
820 *
821 * This countermeasure does not help if the key recovery is possible with a
822 * single trace.
823 */
824#define RSA_EXPONENT_BLINDING 28
825
826/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000827 * Do an RSA private key operation
828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200830 int (*f_rng)(void *, unsigned char *, size_t),
831 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000832 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 unsigned char *output )
834{
Paul Bakker23986e52011-04-24 08:57:21 +0000835 int ret;
836 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100837
838 /* Temporary holding the result */
839 mbedtls_mpi T;
840
841 /* Temporaries holding P-1, Q-1 and the
842 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000843 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100844
845#if !defined(MBEDTLS_RSA_NO_CRT)
846 /* Temporaries holding the results mod p resp. mod q. */
847 mbedtls_mpi TP, TQ;
848
849 /* Temporaries holding the blinded exponents for
850 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000851 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100852
853 /* Pointers to actual exponents to be used - either the unblinded
854 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000855 mbedtls_mpi *DP = &ctx->DP;
856 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100857#else
858 /* Temporary holding the blinded exponent (if used). */
859 mbedtls_mpi D_blind;
860
861 /* Pointer to actual exponent to be used - either the unblinded
862 * or the blinded one, depending on the presence of a PRNG. */
863 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100864#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100865
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100866 /* Temporaries holding the initial input and the double
867 * checked result; should be the same in the end. */
868 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000869
Hanno Beckerddeeed72018-12-13 18:07:00 +0000870 RSA_VALIDATE_RET( ctx != NULL );
871 RSA_VALIDATE_RET( input != NULL );
872 RSA_VALIDATE_RET( output != NULL );
873
Hanno Beckerebd2c022017-10-12 10:54:53 +0100874 if( rsa_check_context( ctx, 1 /* private key checks */,
875 f_rng != NULL /* blinding y/n */ ) != 0 )
876 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100877 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100878 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100879
Hanno Becker06811ce2017-05-03 15:10:34 +0100880#if defined(MBEDTLS_THREADING_C)
881 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
882 return( ret );
883#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000884
Hanno Becker06811ce2017-05-03 15:10:34 +0100885 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100886 mbedtls_mpi_init( &T );
887
888 mbedtls_mpi_init( &P1 );
889 mbedtls_mpi_init( &Q1 );
890 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000891
Janos Follathf9203b42017-03-22 15:13:15 +0000892 if( f_rng != NULL )
893 {
Janos Follathe81102e2017-03-22 13:38:28 +0000894#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000895 mbedtls_mpi_init( &D_blind );
896#else
897 mbedtls_mpi_init( &DP_blind );
898 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000899#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000900 }
Janos Follathe81102e2017-03-22 13:38:28 +0000901
Hanno Becker06811ce2017-05-03 15:10:34 +0100902#if !defined(MBEDTLS_RSA_NO_CRT)
903 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200904#endif
905
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100906 mbedtls_mpi_init( &I );
907 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100908
909 /* End of MPI initialization */
910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
912 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000913 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200914 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
915 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000916 }
917
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100918 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100919
Paul Bakkerf451bac2013-08-30 15:37:02 +0200920 if( f_rng != NULL )
921 {
922 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200923 * Blinding
924 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200925 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200926 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
927 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000929
Janos Follathe81102e2017-03-22 13:38:28 +0000930 /*
931 * Exponent blinding
932 */
933 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
934 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
935
Janos Follathf9203b42017-03-22 15:13:15 +0000936#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000937 /*
938 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
939 */
940 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
941 f_rng, p_rng ) );
942 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
943 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
944 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
945
946 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000947#else
948 /*
949 * DP_blind = ( P - 1 ) * R + DP
950 */
951 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
952 f_rng, p_rng ) );
953 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
954 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
955 &ctx->DP ) );
956
957 DP = &DP_blind;
958
959 /*
960 * DQ_blind = ( Q - 1 ) * R + DQ
961 */
962 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
963 f_rng, p_rng ) );
964 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
965 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
966 &ctx->DQ ) );
967
968 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000969#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200970 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000973 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100974#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200975 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000976 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000977 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100978 * TP = input ^ dP mod P
979 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100981
982 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
983 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
985 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100986 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000987 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100988 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
989 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
990 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000991
992 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100993 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100995 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
996 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200998
Paul Bakkerf451bac2013-08-30 15:37:02 +0200999 if( f_rng != NULL )
1000 {
1001 /*
1002 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001003 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001004 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001005 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001007 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
Hanno Becker2dec5e82017-10-03 07:49:52 +01001009 /* Verify the result to prevent glitching attacks. */
1010 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1011 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001012 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001013 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001014 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1015 goto cleanup;
1016 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001017
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
1021cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001023 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1024 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001025#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001026
Hanno Becker06811ce2017-05-03 15:10:34 +01001027 mbedtls_mpi_free( &P1 );
1028 mbedtls_mpi_free( &Q1 );
1029 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001030
1031 if( f_rng != NULL )
1032 {
Janos Follathe81102e2017-03-22 13:38:28 +00001033#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001034 mbedtls_mpi_free( &D_blind );
1035#else
1036 mbedtls_mpi_free( &DP_blind );
1037 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001038#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Hanno Becker06811ce2017-05-03 15:10:34 +01001041 mbedtls_mpi_free( &T );
1042
1043#if !defined(MBEDTLS_RSA_NO_CRT)
1044 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1045#endif
1046
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001047 mbedtls_mpi_free( &C );
1048 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001049
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
1053 return( 0 );
1054}
1055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001057/**
1058 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1059 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001060 * \param dst buffer to mask
1061 * \param dlen length of destination buffer
1062 * \param src source of the mask generation
1063 * \param slen length of the source buffer
1064 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001065 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001066static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001070 unsigned char counter[4];
1071 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001072 unsigned int hlen;
1073 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001074 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001077 memset( counter, 0, 4 );
1078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001080
Simon Butcher02037452016-03-01 21:19:12 +00001081 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001082 p = dst;
1083
1084 while( dlen > 0 )
1085 {
1086 use_len = hlen;
1087 if( dlen < hlen )
1088 use_len = dlen;
1089
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001090 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1091 goto exit;
1092 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1093 goto exit;
1094 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1095 goto exit;
1096 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1097 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001098
1099 for( i = 0; i < use_len; ++i )
1100 *p++ ^= mask[i];
1101
1102 counter[3]++;
1103
1104 dlen -= use_len;
1105 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001106
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001107exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001108 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001109
1110 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001115/*
1116 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001119 int (*f_rng)(void *, unsigned char *, size_t),
1120 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001121 int mode,
1122 const unsigned char *label, size_t label_len,
1123 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001124 const unsigned char *input,
1125 unsigned char *output )
1126{
1127 size_t olen;
1128 int ret;
1129 unsigned char *p = output;
1130 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 const mbedtls_md_info_t *md_info;
1132 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001133
Hanno Beckerddeeed72018-12-13 18:07:00 +00001134 RSA_VALIDATE_RET( ctx != NULL );
1135 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1136 mode == MBEDTLS_RSA_PUBLIC );
1137 RSA_VALIDATE_RET( output != NULL );
1138 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
1139 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1142 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001143
1144 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001148 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001150
1151 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001153
Simon Butcher02037452016-03-01 21:19:12 +00001154 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001155 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001157
1158 memset( output, 0, olen );
1159
1160 *p++ = 0;
1161
Simon Butcher02037452016-03-01 21:19:12 +00001162 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001163 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001165
1166 p += hlen;
1167
Simon Butcher02037452016-03-01 21:19:12 +00001168 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001169 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1170 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001171 p += hlen;
1172 p += olen - 2 * hlen - 2 - ilen;
1173 *p++ = 1;
1174 memcpy( p, input, ilen );
1175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001177 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001178 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001179
Simon Butcher02037452016-03-01 21:19:12 +00001180 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001181 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1182 &md_ctx ) ) != 0 )
1183 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001184
Simon Butcher02037452016-03-01 21:19:12 +00001185 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001186 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1187 &md_ctx ) ) != 0 )
1188 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001189
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001190exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001192
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001193 if( ret != 0 )
1194 return( ret );
1195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 return( ( mode == MBEDTLS_RSA_PUBLIC )
1197 ? mbedtls_rsa_public( ctx, output, output )
1198 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001199}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001203/*
1204 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001207 int (*f_rng)(void *, unsigned char *, size_t),
1208 void *p_rng,
1209 int mode, size_t ilen,
1210 const unsigned char *input,
1211 unsigned char *output )
1212{
1213 size_t nb_pad, olen;
1214 int ret;
1215 unsigned char *p = output;
1216
Hanno Beckerddeeed72018-12-13 18:07:00 +00001217 RSA_VALIDATE_RET( ctx != NULL );
1218 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1219 mode == MBEDTLS_RSA_PUBLIC );
1220 RSA_VALIDATE_RET( output != NULL );
1221 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
1222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1224 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001225
Janos Follath1ed9f992016-03-18 11:45:44 +00001226 // We don't check p_rng because it won't be dereferenced here
1227 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001229
1230 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001231
Simon Butcher02037452016-03-01 21:19:12 +00001232 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001233 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001235
1236 nb_pad = olen - 3 - ilen;
1237
1238 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001242
1243 while( nb_pad-- > 0 )
1244 {
1245 int rng_dl = 100;
1246
1247 do {
1248 ret = f_rng( p_rng, p, 1 );
1249 } while( *p == 0 && --rng_dl && ret == 0 );
1250
Simon Butcher02037452016-03-01 21:19:12 +00001251 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001252 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001254
1255 p++;
1256 }
1257 }
1258 else
1259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001261
1262 while( nb_pad-- > 0 )
1263 *p++ = 0xFF;
1264 }
1265
1266 *p++ = 0;
1267 memcpy( p, input, ilen );
1268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 return( ( mode == MBEDTLS_RSA_PUBLIC )
1270 ? mbedtls_rsa_public( ctx, output, output )
1271 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001274
Paul Bakker5121ce52009-01-03 21:22:43 +00001275/*
1276 * Add the message padding, then do an RSA operation
1277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001279 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001280 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001281 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001282 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 unsigned char *output )
1284{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001285 RSA_VALIDATE_RET( ctx != NULL );
1286 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1287 mode == MBEDTLS_RSA_PUBLIC );
1288 RSA_VALIDATE_RET( output != NULL );
1289 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
1290
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 switch( ctx->padding )
1292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#if defined(MBEDTLS_PKCS1_V15)
1294 case MBEDTLS_RSA_PKCS_V15:
1295 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001296 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001297#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#if defined(MBEDTLS_PKCS1_V21)
1300 case MBEDTLS_RSA_PKCS_V21:
1301 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001302 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001303#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001304
1305 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001308}
1309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001311/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001312 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314int mbedtls_rsa_rsaes_oaep_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,
1317 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001318 const unsigned char *label, size_t label_len,
1319 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001320 const unsigned char *input,
1321 unsigned char *output,
1322 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001323{
Paul Bakker23986e52011-04-24 08:57:21 +00001324 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001325 size_t ilen, i, pad_len;
1326 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1328 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001329 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 const mbedtls_md_info_t *md_info;
1331 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001332
Hanno Beckerddeeed72018-12-13 18:07:00 +00001333 RSA_VALIDATE_RET( ctx != NULL );
1334 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1335 mode == MBEDTLS_RSA_PUBLIC );
1336 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1337 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1338 RSA_VALIDATE_RET( input != NULL );
1339 RSA_VALIDATE_RET( olen != NULL );
1340
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001341 /*
1342 * Parameters sanity checks
1343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1345 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
1347 ilen = ctx->len;
1348
Paul Bakker27fdf462011-06-09 13:55:13 +00001349 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001353 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001355
Janos Follathc17cda12016-02-11 11:08:18 +00001356 hlen = mbedtls_md_get_size( md_info );
1357
1358 // checking for integer underflow
1359 if( 2 * hlen + 2 > ilen )
1360 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1361
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001362 /*
1363 * RSA operation
1364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1366 ? mbedtls_rsa_public( ctx, input, buf )
1367 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001368
1369 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001370 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001372 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001373 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001376 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1377 {
1378 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001379 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001380 }
1381
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001382 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001383 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1384 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001385 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001386 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1387 &md_ctx ) ) != 0 )
1388 {
1389 mbedtls_md_free( &md_ctx );
1390 goto cleanup;
1391 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001394
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001395 /* Generate lHash */
1396 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1397 goto cleanup;
1398
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001399 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001400 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001401 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001403 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001405 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001406
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001407 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001408
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001409 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001410 for( i = 0; i < hlen; i++ )
1411 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001412
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001413 /* Get zero-padding len, but always read till end of buffer
1414 * (minus one, for the 01 byte) */
1415 pad_len = 0;
1416 pad_done = 0;
1417 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1418 {
1419 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001420 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001421 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001422
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001423 p += pad_len;
1424 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001425
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001426 /*
1427 * The only information "leaked" is whether the padding was correct or not
1428 * (eg, no data is copied if it was not correct). This meets the
1429 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1430 * the different error conditions.
1431 */
1432 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001433 {
1434 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1435 goto cleanup;
1436 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001437
Paul Bakker66d5d072014-06-17 16:39:18 +02001438 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001439 {
1440 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1441 goto cleanup;
1442 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001443
1444 *olen = ilen - (p - buf);
1445 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001446 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001447
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001448cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001449 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1450 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001451
1452 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001457/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1458 *
1459 * \param value The value to analyze.
Gilles Peskine331d80e2018-10-04 18:32:29 +02001460 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001461 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001462static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001463{
1464 /* MSVC has a warning about unary minus on unsigned, but this is
1465 * well-defined and precisely what we want to do here */
1466#if defined(_MSC_VER)
1467#pragma warning( push )
1468#pragma warning( disable : 4146 )
1469#endif
1470 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1471#if defined(_MSC_VER)
1472#pragma warning( pop )
1473#endif
1474}
1475
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001476/** Check whether a size is out of bounds, without branches.
1477 *
1478 * This is equivalent to `size > max`, but is likely to be compiled to
1479 * to code using bitwise operation rather than a branch.
1480 *
1481 * \param size Size to check.
1482 * \param max Maximum desired value for \p size.
1483 * \return \c 0 if `size <= max`.
1484 * \return \c 1 if `size > max`.
1485 */
1486static unsigned size_greater_than( size_t size, size_t max )
1487{
1488 /* Return the sign bit (1 for negative) of (max - size). */
1489 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1490}
1491
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001492/** Choose between two integer values, without branches.
1493 *
Gilles Peskine331d80e2018-10-04 18:32:29 +02001494 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1495 * to code using bitwise operation rather than a branch.
1496 *
1497 * \param cond Condition to test.
1498 * \param if1 Value to use if \p cond is nonzero.
1499 * \param if0 Value to use if \p cond is zero.
1500 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001501 */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001502static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001503{
Gilles Peskine331d80e2018-10-04 18:32:29 +02001504 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001505 return( ( mask & if1 ) | (~mask & if0 ) );
1506}
1507
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001508/** Shift some data towards the left inside a buffer without leaking
1509 * the length of the data through side channels.
1510 *
1511 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1512 * ```
1513 * memmove(start, start + offset, total - offset);
1514 * memset(start + offset, 0, total - offset);
1515 * ```
1516 * but it strives to use a memory access pattern (and thus total timing)
1517 * that does not depend on \p offset. This timing independence comes at
1518 * the expense of performance.
1519 *
1520 * \param start Pointer to the start of the buffer.
1521 * \param total Total size of the buffer.
1522 * \param offset Offset from which to copy \p total - \p offset bytes.
1523 */
1524static void mem_move_to_left( void *start,
1525 size_t total,
1526 size_t offset )
1527{
1528 volatile unsigned char *buf = start;
1529 size_t i, n;
1530 if( total == 0 )
1531 return;
1532 for( i = 0; i < total; i++ )
1533 {
1534 unsigned no_op = size_greater_than( total - offset, i );
1535 /* The first `total - offset` passes are a no-op. The last
1536 * `offset` passes shift the data one byte to the left and
1537 * zero out the last byte. */
1538 for( n = 0; n < total - 1; n++ )
Gilles Peskine9b430702018-10-12 19:15:34 +02001539 {
1540 unsigned char current = buf[n];
1541 unsigned char next = buf[n+1];
1542 buf[n] = if_int( no_op, current, next );
1543 }
Gilles Peskinea1af5c82018-10-04 21:18:30 +02001544 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1545 }
1546}
1547
Paul Bakkerb3869132013-02-28 17:21:01 +01001548/*
1549 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001552 int (*f_rng)(void *, unsigned char *, size_t),
1553 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001554 int mode, size_t *olen,
1555 const unsigned char *input,
1556 unsigned char *output,
Gilles Peskine5908dd42018-10-02 22:43:06 +02001557 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001558{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001559 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001560 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskine85a74422018-10-05 14:50:21 +02001562 /* The following variables take sensitive values: their value must
1563 * not leak into the observable behavior of the function other than
1564 * the designated outputs (output, olen, return value). Otherwise
1565 * this would open the execution of the function to
1566 * side-channel-based variants of the Bleichenbacher padding oracle
1567 * attack. Potential side channels include overall timing, memory
1568 * access patterns (especially visible to an adversary who has access
1569 * to a shared memory cache), and branches (especially visible to
1570 * an adversary who has access to a shared code cache or to a shared
1571 * branch predictor). */
1572 size_t pad_count = 0;
1573 unsigned bad = 0;
1574 unsigned char pad_done = 0;
1575 size_t plaintext_size = 0;
1576 unsigned output_too_large;
Paul Bakkerb3869132013-02-28 17:21:01 +01001577
Hanno Beckerddeeed72018-12-13 18:07:00 +00001578 RSA_VALIDATE_RET( ctx != NULL );
1579 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1580 mode == MBEDTLS_RSA_PUBLIC );
1581 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1582 RSA_VALIDATE_RET( input != NULL );
1583 RSA_VALIDATE_RET( olen != NULL );
1584
1585 ilen = ctx->len;
1586 plaintext_max_size = ( output_max_len > ilen - 11 ?
1587 ilen - 11 :
1588 output_max_len );
1589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1591 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001592
Paul Bakkerb3869132013-02-28 17:21:01 +01001593 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1597 ? mbedtls_rsa_public( ctx, input, buf )
1598 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001599
1600 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001601 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001602
Gilles Peskine40b57f42018-10-05 15:06:12 +02001603 /* Check and get padding length in constant time and constant
1604 * memory trace. The first byte must be 0. */
1605 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001609 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1610 * where PS must be at least 8 nonzero bytes. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001611 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001612
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001613 /* Read the whole buffer. Set pad_done to nonzero if we find
1614 * the 0x00 byte and remember the padding length in pad_count. */
1615 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001616 {
Gilles Peskine85a74422018-10-05 14:50:21 +02001617 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001618 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001619 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001620 }
1621 else
1622 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001623 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1624 * where PS must be at least 8 bytes with the value 0xFF. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001625 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001626
Gilles Peskineec2a5fd2018-10-05 18:11:27 +02001627 /* Read the whole buffer. Set pad_done to nonzero if we find
1628 * the 0x00 byte and remember the padding length in pad_count.
1629 * If there's a non-0xff byte in the padding, the padding is bad. */
1630 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001631 {
Gilles Peskine40b57f42018-10-05 15:06:12 +02001632 pad_done |= if_int( buf[i], 0, 1 );
1633 pad_count += if_int( pad_done, 0, 1 );
1634 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001635 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001636 }
1637
Gilles Peskine40b57f42018-10-05 15:06:12 +02001638 /* If pad_done is still zero, there's no data, only unfinished padding. */
1639 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001640
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001641 /* There must be at least 8 bytes of padding. */
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001642 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001643
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001644 /* If the padding is valid, set plaintext_size to the number of
1645 * remaining bytes after stripping the padding. If the padding
1646 * is invalid, avoid leaking this fact through the size of the
1647 * output: use the maximum message size that fits in the output
1648 * buffer. Do it without branches to avoid leaking the padding
1649 * validity through timing. RSA keys are small enough that all the
1650 * size_t values involved fit in unsigned int. */
Gilles Peskine331d80e2018-10-04 18:32:29 +02001651 plaintext_size = if_int( bad,
1652 (unsigned) plaintext_max_size,
Gilles Peskine85a74422018-10-05 14:50:21 +02001653 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001654
Gilles Peskine9265ff42018-10-04 19:13:43 +02001655 /* Set output_too_large to 0 if the plaintext fits in the output
Gilles Peskine8c9440a2018-10-04 21:24:21 +02001656 * buffer and to 1 otherwise. */
1657 output_too_large = size_greater_than( plaintext_size,
1658 plaintext_max_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Gilles Peskine9265ff42018-10-04 19:13:43 +02001660 /* Set ret without branches to avoid timing attacks. Return:
1661 * - INVALID_PADDING if the padding is bad (bad != 0).
1662 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1663 * plaintext does not fit in the output buffer.
1664 * - 0 if the padding is correct. */
Gilles Peskine48992472018-10-12 19:19:12 +02001665 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1666 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1667 0 ) );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001668
Gilles Peskine9265ff42018-10-04 19:13:43 +02001669 /* If the padding is bad or the plaintext is too large, zero the
1670 * data that we're about to copy to the output buffer.
1671 * We need to copy the same amount of data
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001672 * from the same buffer whether the padding is good or not to
1673 * avoid leaking the padding validity through overall timing or
1674 * through memory or cache access patterns. */
Gilles Peskine40b57f42018-10-05 15:06:12 +02001675 bad = all_or_nothing_int( bad | output_too_large );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001676 for( i = 11; i < ilen; i++ )
Gilles Peskine40b57f42018-10-05 15:06:12 +02001677 buf[i] &= ~bad;
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001678
Gilles Peskine9265ff42018-10-04 19:13:43 +02001679 /* If the plaintext is too large, truncate it to the buffer size.
1680 * Copy anyway to avoid revealing the length through timing, because
1681 * revealing the length is as bad as revealing the padding validity
1682 * for a Bleichenbacher attack. */
1683 plaintext_size = if_int( output_too_large,
1684 (unsigned) plaintext_max_size,
1685 (unsigned) plaintext_size );
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001686
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001687 /* Move the plaintext to the leftmost position where it can start in
1688 * the working buffer, i.e. make it start plaintext_max_size from
1689 * the end of the buffer. Do this with a memory access trace that
1690 * does not depend on the plaintext size. After this move, the
1691 * starting location of the plaintext is no longer sensitive
1692 * information. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001693 mem_move_to_left( buf + ilen - plaintext_max_size,
1694 plaintext_max_size,
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001695 plaintext_max_size - plaintext_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001696
Gilles Peskineeeedabe2018-10-04 22:45:13 +02001697 /* Finally copy the decrypted plaintext plus trailing zeros
Gilles Peskine9265ff42018-10-04 19:13:43 +02001698 * into the output buffer. */
Gilles Peskine85a74422018-10-05 14:50:21 +02001699 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Gilles Peskine9265ff42018-10-04 19:13:43 +02001700
1701 /* Report the amount of data we copied to the output buffer. In case
1702 * of errors (bad padding or output too large), the value of *olen
1703 * when this function returns is not specified. Making it equivalent
1704 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinee2a10de2018-10-02 22:44:41 +02001705 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001706
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001707cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001708 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001709
1710 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001713
1714/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001715 * Do an RSA operation, then remove the message padding
1716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001718 int (*f_rng)(void *, unsigned char *, size_t),
1719 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001720 int mode, size_t *olen,
1721 const unsigned char *input,
1722 unsigned char *output,
1723 size_t output_max_len)
1724{
Hanno Beckerddeeed72018-12-13 18:07:00 +00001725 RSA_VALIDATE_RET( ctx != NULL );
1726 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1727 mode == MBEDTLS_RSA_PUBLIC );
1728 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1729 RSA_VALIDATE_RET( input != NULL );
1730 RSA_VALIDATE_RET( olen != NULL );
1731
Paul Bakkerb3869132013-02-28 17:21:01 +01001732 switch( ctx->padding )
1733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#if defined(MBEDTLS_PKCS1_V15)
1735 case MBEDTLS_RSA_PKCS_V15:
1736 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001737 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001738#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740#if defined(MBEDTLS_PKCS1_V21)
1741 case MBEDTLS_RSA_PKCS_V21:
1742 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001743 olen, input, output,
1744 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001745#endif
1746
1747 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001749 }
1750}
1751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001753/*
1754 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001757 int (*f_rng)(void *, unsigned char *, size_t),
1758 void *p_rng,
1759 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001761 unsigned int hashlen,
1762 const unsigned char *hash,
1763 unsigned char *sig )
1764{
1765 size_t olen;
1766 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001768 size_t slen, min_slen, hlen, offset = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001769 int ret;
1770 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 const mbedtls_md_info_t *md_info;
1772 mbedtls_md_context_t md_ctx;
Hanno Beckerddeeed72018-12-13 18:07:00 +00001773 RSA_VALIDATE_RET( ctx != NULL );
1774 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1775 mode == MBEDTLS_RSA_PUBLIC );
1776 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1777 hashlen == 0 ) ||
1778 hash != NULL );
1779 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1782 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001783
1784 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001786
1787 olen = ctx->len;
1788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001790 {
Simon Butcher02037452016-03-01 21:19:12 +00001791 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001793 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001797 }
1798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001800 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001804
Jaeden Amero3725bb22018-09-07 19:12:36 +01001805 /* Calculate the largest possible salt length. Normally this is the hash
1806 * length, which is the maximum length the salt can have. If there is not
1807 * enough room, use the maximum salt length that fits. The constraint is
1808 * that the hash length plus the salt length plus 2 bytes must be at most
1809 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1810 * (PKCS#1 v2.2) §9.1.1 step 3. */
1811 min_slen = hlen - 2;
1812 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001814 else if( olen >= hlen + hlen + 2 )
1815 slen = hlen;
1816 else
1817 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001818
1819 memset( sig, 0, olen );
1820
Simon Butcher02037452016-03-01 21:19:12 +00001821 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001822 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001824
Simon Butcher02037452016-03-01 21:19:12 +00001825 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001826 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001827 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001828 *p++ = 0x01;
1829 memcpy( p, salt, slen );
1830 p += slen;
1831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001833 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001834 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001835
Simon Butcher02037452016-03-01 21:19:12 +00001836 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001837 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1838 goto exit;
1839 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1840 goto exit;
1841 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1842 goto exit;
1843 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1844 goto exit;
1845 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1846 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001847
Simon Butcher02037452016-03-01 21:19:12 +00001848 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001849 if( msb % 8 == 0 )
1850 offset = 1;
1851
Simon Butcher02037452016-03-01 21:19:12 +00001852 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001853 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1854 &md_ctx ) ) != 0 )
1855 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001856
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001857 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001858 sig[0] &= 0xFF >> ( olen * 8 - msb );
1859
1860 p += hlen;
1861 *p++ = 0xBC;
1862
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001863 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001864
1865exit:
1866 mbedtls_md_free( &md_ctx );
1867
1868 if( ret != 0 )
1869 return( ret );
1870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 return( ( mode == MBEDTLS_RSA_PUBLIC )
1872 ? mbedtls_rsa_public( ctx, sig, sig )
1873 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001874}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001878/*
1879 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1880 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001881
1882/* Construct a PKCS v1.5 encoding of a hashed message
1883 *
1884 * This is used both for signature generation and verification.
1885 *
1886 * Parameters:
1887 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001888 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001889 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001890 * - hash: Buffer containing the hashed message or the raw data.
1891 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001892 * - dst: Buffer to hold the encoded message.
1893 *
1894 * Assumptions:
1895 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1896 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001897 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001898 *
1899 */
1900static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1901 unsigned int hashlen,
1902 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001903 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001904 unsigned char *dst )
1905{
1906 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001907 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001908 unsigned char *p = dst;
1909 const char *oid = NULL;
1910
1911 /* Are we signing hashed or raw data? */
1912 if( md_alg != MBEDTLS_MD_NONE )
1913 {
1914 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1915 if( md_info == NULL )
1916 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1917
1918 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1919 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1920
1921 hashlen = mbedtls_md_get_size( md_info );
1922
1923 /* Double-check that 8 + hashlen + oid_size can be used as a
1924 * 1-byte ASN.1 length encoding and that there's no overflow. */
1925 if( 8 + hashlen + oid_size >= 0x80 ||
1926 10 + hashlen < hashlen ||
1927 10 + hashlen + oid_size < 10 + hashlen )
1928 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1929
1930 /*
1931 * Static bounds check:
1932 * - Need 10 bytes for five tag-length pairs.
1933 * (Insist on 1-byte length encodings to protect against variants of
1934 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1935 * - Need hashlen bytes for hash
1936 * - Need oid_size bytes for hash alg OID.
1937 */
1938 if( nb_pad < 10 + hashlen + oid_size )
1939 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1940 nb_pad -= 10 + hashlen + oid_size;
1941 }
1942 else
1943 {
1944 if( nb_pad < hashlen )
1945 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1946
1947 nb_pad -= hashlen;
1948 }
1949
Hanno Becker2b2f8982017-09-27 17:10:03 +01001950 /* Need space for signature header and padding delimiter (3 bytes),
1951 * and 8 bytes for the minimal padding */
1952 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001953 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1954 nb_pad -= 3;
1955
1956 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001957 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001958
1959 /* Write signature header and padding */
1960 *p++ = 0;
1961 *p++ = MBEDTLS_RSA_SIGN;
1962 memset( p, 0xFF, nb_pad );
1963 p += nb_pad;
1964 *p++ = 0;
1965
1966 /* Are we signing raw data? */
1967 if( md_alg == MBEDTLS_MD_NONE )
1968 {
1969 memcpy( p, hash, hashlen );
1970 return( 0 );
1971 }
1972
1973 /* Signing hashed data, add corresponding ASN.1 structure
1974 *
1975 * DigestInfo ::= SEQUENCE {
1976 * digestAlgorithm DigestAlgorithmIdentifier,
1977 * digest Digest }
1978 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1979 * Digest ::= OCTET STRING
1980 *
1981 * Schematic:
1982 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1983 * TAG-NULL + LEN [ NULL ] ]
1984 * TAG-OCTET + LEN [ HASH ] ]
1985 */
1986 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001987 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001988 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001989 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001990 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001991 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001992 memcpy( p, oid, oid_size );
1993 p += oid_size;
1994 *p++ = MBEDTLS_ASN1_NULL;
1995 *p++ = 0x00;
1996 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001997 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001998 memcpy( p, hash, hashlen );
1999 p += hashlen;
2000
2001 /* Just a sanity-check, should be automatic
2002 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002003 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002004 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002005 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002006 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2007 }
2008
2009 return( 0 );
2010}
2011
Paul Bakkerb3869132013-02-28 17:21:01 +01002012/*
2013 * Do an RSA operation to sign the message digest
2014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002016 int (*f_rng)(void *, unsigned char *, size_t),
2017 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002018 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002020 unsigned int hashlen,
2021 const unsigned char *hash,
2022 unsigned char *sig )
2023{
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002024 int ret;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002025 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002026
Hanno Beckerddeeed72018-12-13 18:07:00 +00002027 RSA_VALIDATE_RET( ctx != NULL );
2028 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2029 mode == MBEDTLS_RSA_PUBLIC );
2030 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2031 hashlen == 0 ) ||
2032 hash != NULL );
2033 RSA_VALIDATE_RET( sig != NULL );
2034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2036 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002037
Hanno Beckerfdf38032017-09-06 12:35:55 +01002038 /*
2039 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2040 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002041
Hanno Beckerfdf38032017-09-06 12:35:55 +01002042 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2043 ctx->len, sig ) ) != 0 )
2044 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002045
2046 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002047 * Call respective RSA primitive
2048 */
2049
2050 if( mode == MBEDTLS_RSA_PUBLIC )
2051 {
2052 /* Skip verification on a public key operation */
2053 return( mbedtls_rsa_public( ctx, sig, sig ) );
2054 }
2055
2056 /* Private key operation
2057 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002058 * In order to prevent Lenstra's attack, make the signature in a
2059 * temporary buffer and check it before returning it.
2060 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002061
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002062 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002063 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002064 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2065
Hanno Beckerfdf38032017-09-06 12:35:55 +01002066 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002067 if( verif == NULL )
2068 {
2069 mbedtls_free( sig_try );
2070 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2071 }
2072
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002073 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2074 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2075
Hanno Becker171a8f12017-09-06 12:32:16 +01002076 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002077 {
2078 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2079 goto cleanup;
2080 }
2081
2082 memcpy( sig, sig_try, ctx->len );
2083
2084cleanup:
2085 mbedtls_free( sig_try );
2086 mbedtls_free( verif );
2087
2088 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002089}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002091
2092/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 * Do an RSA operation to sign the message digest
2094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002096 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002097 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002100 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002101 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 unsigned char *sig )
2103{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002104 RSA_VALIDATE_RET( ctx != NULL );
2105 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2106 mode == MBEDTLS_RSA_PUBLIC );
2107 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2108 hashlen == 0 ) ||
2109 hash != NULL );
2110 RSA_VALIDATE_RET( sig != NULL );
2111
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 switch( ctx->padding )
2113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114#if defined(MBEDTLS_PKCS1_V15)
2115 case MBEDTLS_RSA_PKCS_V15:
2116 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002117 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002118#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120#if defined(MBEDTLS_PKCS1_V21)
2121 case MBEDTLS_RSA_PKCS_V21:
2122 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002123 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002124#endif
2125
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002129}
2130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002132/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002133 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002136 int (*f_rng)(void *, unsigned char *, size_t),
2137 void *p_rng,
2138 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002140 unsigned int hashlen,
2141 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002143 int expected_salt_len,
2144 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002145{
Paul Bakker23986e52011-04-24 08:57:21 +00002146 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002147 size_t siglen;
2148 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002149 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002151 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002152 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002153 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 const mbedtls_md_info_t *md_info;
2155 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002156 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002157
Hanno Beckerddeeed72018-12-13 18:07:00 +00002158 RSA_VALIDATE_RET( ctx != NULL );
2159 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2160 mode == MBEDTLS_RSA_PUBLIC );
2161 RSA_VALIDATE_RET( sig != NULL );
2162 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2163 hashlen == 0 ) ||
2164 hash != NULL );
2165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2167 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002168
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 siglen = ctx->len;
2170
Paul Bakker27fdf462011-06-09 13:55:13 +00002171 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2175 ? mbedtls_rsa_public( ctx, sig, buf )
2176 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
2178 if( ret != 0 )
2179 return( ret );
2180
2181 p = buf;
2182
Paul Bakkerb3869132013-02-28 17:21:01 +01002183 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 {
Simon Butcher02037452016-03-01 21:19:12 +00002188 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002190 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002194 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002197 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002201
Paul Bakkerb3869132013-02-28 17:21:01 +01002202 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002203
Simon Butcher02037452016-03-01 21:19:12 +00002204 /*
2205 * Note: EMSA-PSS verification is over the length of N - 1 bits
2206 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002207 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002208
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002209 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2210 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2211
Simon Butcher02037452016-03-01 21:19:12 +00002212 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002213 if( msb % 8 == 0 )
2214 {
2215 p++;
2216 siglen -= 1;
2217 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002218
Gilles Peskine139108a2017-10-18 19:03:42 +02002219 if( siglen < hlen + 2 )
2220 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2221 hash_start = p + siglen - hlen - 1;
2222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002224 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002225 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002226
Jaeden Amero66954e12018-01-25 16:05:54 +00002227 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2228 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002229 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002230
Paul Bakkerb3869132013-02-28 17:21:01 +01002231 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002232
Gilles Peskine6a54b022017-10-17 19:02:13 +02002233 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002234 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002235
Gilles Peskine91048a32017-10-19 17:46:14 +02002236 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002237 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002238 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2239 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002240 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002241
Gilles Peskine6a54b022017-10-17 19:02:13 +02002242 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002245 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002246 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002247 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2248 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002249 }
2250
Simon Butcher02037452016-03-01 21:19:12 +00002251 /*
2252 * Generate H = Hash( M' )
2253 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002254 ret = mbedtls_md_starts( &md_ctx );
2255 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002256 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002257 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2258 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002259 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002260 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2261 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002262 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002263 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2264 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002265 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002266 ret = mbedtls_md_finish( &md_ctx, result );
2267 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002268 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002269
Jaeden Amero66954e12018-01-25 16:05:54 +00002270 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002271 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002272 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002273 goto exit;
2274 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002275
2276exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002278
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002279 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002280}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002281
2282/*
2283 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002286 int (*f_rng)(void *, unsigned char *, size_t),
2287 void *p_rng,
2288 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002290 unsigned int hashlen,
2291 const unsigned char *hash,
2292 const unsigned char *sig )
2293{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002294 mbedtls_md_type_t mgf1_hash_id;
2295 RSA_VALIDATE_RET( ctx != NULL );
2296 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2297 mode == MBEDTLS_RSA_PUBLIC );
2298 RSA_VALIDATE_RET( sig != NULL );
2299 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2300 hashlen == 0 ) ||
2301 hash != NULL );
2302
2303 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002305 : md_alg;
2306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002308 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002310 sig ) );
2311
2312}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002316/*
2317 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002320 int (*f_rng)(void *, unsigned char *, size_t),
2321 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002322 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002324 unsigned int hashlen,
2325 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002326 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002327{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002328 int ret = 0;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002329 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002330 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002331
Hanno Beckerddeeed72018-12-13 18:07:00 +00002332 RSA_VALIDATE_RET( ctx != NULL );
2333 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2334 mode == MBEDTLS_RSA_PUBLIC );
2335 RSA_VALIDATE_RET( sig != NULL );
2336 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2337 hashlen == 0 ) ||
2338 hash != NULL );
2339
2340 sig_len = ctx->len;
2341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2343 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002344
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002345 /*
2346 * Prepare expected PKCS1 v1.5 encoding of hash.
2347 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002348
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002349 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2350 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2351 {
2352 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2353 goto cleanup;
2354 }
2355
2356 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2357 encoded_expected ) ) != 0 )
2358 goto cleanup;
2359
2360 /*
2361 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2362 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002365 ? mbedtls_rsa_public( ctx, sig, encoded )
2366 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002367 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002368 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002369
Simon Butcher02037452016-03-01 21:19:12 +00002370 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002371 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002372 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002373
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002374 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2375 sig_len ) ) != 0 )
2376 {
2377 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2378 goto cleanup;
2379 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002380
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002381cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002382
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002383 if( encoded != NULL )
2384 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002385 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002386 mbedtls_free( encoded );
2387 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002388
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002389 if( encoded_expected != NULL )
2390 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002391 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002392 mbedtls_free( encoded_expected );
2393 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002394
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002395 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
2399/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002400 * Do an RSA operation and check the message digest
2401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002403 int (*f_rng)(void *, unsigned char *, size_t),
2404 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002405 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002407 unsigned int hashlen,
2408 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002409 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002410{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002411 RSA_VALIDATE_RET( ctx != NULL );
2412 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2413 mode == MBEDTLS_RSA_PUBLIC );
2414 RSA_VALIDATE_RET( sig != NULL );
2415 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2416 hashlen == 0 ) ||
2417 hash != NULL );
2418
Paul Bakkerb3869132013-02-28 17:21:01 +01002419 switch( ctx->padding )
2420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421#if defined(MBEDTLS_PKCS1_V15)
2422 case MBEDTLS_RSA_PKCS_V15:
2423 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002424 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002425#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427#if defined(MBEDTLS_PKCS1_V21)
2428 case MBEDTLS_RSA_PKCS_V21:
2429 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002430 hashlen, hash, sig );
2431#endif
2432
2433 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002435 }
2436}
2437
2438/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002439 * Copy the components of an RSA key
2440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002442{
2443 int ret;
Hanno Beckerddeeed72018-12-13 18:07:00 +00002444 RSA_VALIDATE_RET( dst != NULL );
2445 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002446
2447 dst->ver = src->ver;
2448 dst->len = src->len;
2449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2451 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2454 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2455 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002456
2457#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2459 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2460 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2462 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002463#endif
2464
2465 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2468 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002469
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002470 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002471 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002472
2473cleanup:
2474 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002476
2477 return( ret );
2478}
2479
2480/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 * Free the components of an RSA key
2482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002484{
Hanno Beckerddeeed72018-12-13 18:07:00 +00002485 if( ctx == NULL )
2486 return;
2487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Hanno Becker33c30a02017-08-23 07:00:22 +01002489 mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D );
2490 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002492
Hanno Becker33c30a02017-08-23 07:00:22 +01002493#if !defined(MBEDTLS_RSA_NO_CRT)
2494 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP );
2495 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ );
2496 mbedtls_mpi_free( &ctx->DP );
2497#endif /* MBEDTLS_RSA_NO_CRT */
2498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#if defined(MBEDTLS_THREADING_C)
2500 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002501#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002502}
2503
Hanno Beckerab377312017-08-23 16:24:51 +01002504#endif /* !MBEDTLS_RSA_ALT */
2505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002508#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
2510/*
2511 * Example RSA-1024 keypair, for test purposes
2512 */
2513#define KEY_LEN 128
2514
2515#define RSA_N "9292758453063D803DD603D5E777D788" \
2516 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2517 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2518 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2519 "93A89813FBF3C4F8066D2D800F7C38A8" \
2520 "1AE31942917403FF4946B0A83D3D3E05" \
2521 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2522 "5E94BB77B07507233A0BC7BAC8F90F79"
2523
2524#define RSA_E "10001"
2525
2526#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2527 "66CA472BC44D253102F8B4A9D3BFA750" \
2528 "91386C0077937FE33FA3252D28855837" \
2529 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2530 "DF79C5CE07EE72C7F123142198164234" \
2531 "CABB724CF78B8173B9F880FC86322407" \
2532 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2533 "071513A1E85B5DFA031F21ECAE91A34D"
2534
2535#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2536 "2C01CAD19EA484A87EA4377637E75500" \
2537 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2538 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2539
2540#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2541 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2542 "910E4168387E3C30AA1E00C339A79508" \
2543 "8452DD96A9A5EA5D9DCA68DA636032AF"
2544
Paul Bakker5121ce52009-01-03 21:22:43 +00002545#define PT_LEN 24
2546#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2547 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002550static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002551{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002552#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002553 size_t i;
2554
Paul Bakker545570e2010-07-18 09:00:25 +00002555 if( rng_state != NULL )
2556 rng_state = NULL;
2557
Paul Bakkera3d195c2011-11-27 21:07:34 +00002558 for( i = 0; i < len; ++i )
2559 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002560#else
2561 if( rng_state != NULL )
2562 rng_state = NULL;
2563
2564 arc4random_buf( output, len );
2565#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002566
Paul Bakkera3d195c2011-11-27 21:07:34 +00002567 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002568}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002570
Paul Bakker5121ce52009-01-03 21:22:43 +00002571/*
2572 * Checkup routine
2573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002575{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002576 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002578 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 unsigned char rsa_plaintext[PT_LEN];
2581 unsigned char rsa_decrypted[PT_LEN];
2582 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002584 unsigned char sha1sum[20];
2585#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Hanno Becker3a701162017-08-22 13:52:43 +01002587 mbedtls_mpi K;
2588
2589 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Hanno Becker3a701162017-08-22 13:52:43 +01002592 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2593 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2594 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2595 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2596 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2597 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2598 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2599 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2600 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2601 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2602
Hanno Becker7f25f852017-10-10 16:56:22 +01002603 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
2605 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2609 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 {
2611 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002613
Hanno Becker5bc87292017-05-03 15:09:31 +01002614 ret = 1;
2615 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 }
2617
2618 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
2621 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2622
Hanno Becker98838b02017-10-02 13:16:10 +01002623 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2624 PT_LEN, rsa_plaintext,
2625 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 {
2627 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002629
Hanno Becker5bc87292017-05-03 15:09:31 +01002630 ret = 1;
2631 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 }
2633
2634 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
Hanno Becker98838b02017-10-02 13:16:10 +01002637 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2638 &len, rsa_ciphertext, rsa_decrypted,
2639 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 {
2641 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
Hanno Becker5bc87292017-05-03 15:09:31 +01002644 ret = 1;
2645 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 }
2647
2648 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2649 {
2650 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Hanno Becker5bc87292017-05-03 15:09:31 +01002653 ret = 1;
2654 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002657 if( verbose != 0 )
2658 mbedtls_printf( "passed\n" );
2659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002662 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002664 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002665 {
2666 if( verbose != 0 )
2667 mbedtls_printf( "failed\n" );
2668
2669 return( 1 );
2670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002671
Hanno Becker98838b02017-10-02 13:16:10 +01002672 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2673 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2674 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 {
2676 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Hanno Becker5bc87292017-05-03 15:09:31 +01002679 ret = 1;
2680 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 }
2682
2683 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Hanno Becker98838b02017-10-02 13:16:10 +01002686 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2687 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2688 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 {
2690 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Hanno Becker5bc87292017-05-03 15:09:31 +01002693 ret = 1;
2694 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 }
2696
2697 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002698 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002701 if( verbose != 0 )
2702 mbedtls_printf( "\n" );
2703
Paul Bakker3d8fb632014-04-17 12:42:41 +02002704cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002705 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 mbedtls_rsa_free( &rsa );
2707#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002708 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002710 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711}
2712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715#endif /* MBEDTLS_RSA_C */