blob: 3c2f31438cc2ff8df7523acbd103233d811a0a17 [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"
Janos Follath24eed8d2019-11-22 13:21:35 +000052#include "mbedtls/error.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000053
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <string.h>
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000061#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000062#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010066#else
Rich Evans00ab4702015-02-06 13:43:58 +000067#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020069#define mbedtls_calloc calloc
70#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010071#endif
72
Hanno Beckera565f542017-10-11 11:00:19 +010073#if !defined(MBEDTLS_RSA_ALT)
74
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050075/* Parameter validation macros */
76#define RSA_VALIDATE_RET( cond ) \
77 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
78#define RSA_VALIDATE( cond ) \
79 MBEDTLS_INTERNAL_VALIDATE( cond )
80
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010081#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +010082/* constant-time buffer comparison */
83static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
84{
85 size_t i;
86 const unsigned char *A = (const unsigned char *) a;
87 const unsigned char *B = (const unsigned char *) b;
88 unsigned char diff = 0;
89
90 for( i = 0; i < n; i++ )
91 diff |= A[i] ^ B[i];
92
93 return( diff );
94}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010095#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +010096
Hanno Becker617c1ae2017-08-23 14:11:24 +010097int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
98 const mbedtls_mpi *N,
99 const mbedtls_mpi *P, const mbedtls_mpi *Q,
100 const mbedtls_mpi *D, const mbedtls_mpi *E )
101{
Janos Follath24eed8d2019-11-22 13:21:35 +0000102 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500103 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100104
105 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
106 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
107 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
108 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
109 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
110 {
111 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
112 }
113
114 if( N != NULL )
115 ctx->len = mbedtls_mpi_size( &ctx->N );
116
117 return( 0 );
118}
119
120int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100121 unsigned char const *N, size_t N_len,
122 unsigned char const *P, size_t P_len,
123 unsigned char const *Q, size_t Q_len,
124 unsigned char const *D, size_t D_len,
125 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100126{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000127 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500128 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100129
130 if( N != NULL )
131 {
132 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
133 ctx->len = mbedtls_mpi_size( &ctx->N );
134 }
135
136 if( P != NULL )
137 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
138
139 if( Q != NULL )
140 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
141
142 if( D != NULL )
143 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
144
145 if( E != NULL )
146 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
147
148cleanup:
149
150 if( ret != 0 )
151 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
152
153 return( 0 );
154}
155
Hanno Becker705fc682017-10-10 17:57:02 +0100156/*
157 * Checks whether the context fields are set in such a way
158 * that the RSA primitives will be able to execute without error.
159 * It does *not* make guarantees for consistency of the parameters.
160 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100161static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
162 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100163{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100164#if !defined(MBEDTLS_RSA_NO_CRT)
165 /* blinding_needed is only used for NO_CRT to decide whether
166 * P,Q need to be present or not. */
167 ((void) blinding_needed);
168#endif
169
Hanno Becker3a760a12018-01-05 08:14:49 +0000170 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
171 ctx->len > MBEDTLS_MPI_MAX_SIZE )
172 {
Hanno Becker705fc682017-10-10 17:57:02 +0100173 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000174 }
Hanno Becker705fc682017-10-10 17:57:02 +0100175
176 /*
177 * 1. Modular exponentiation needs positive, odd moduli.
178 */
179
180 /* Modular exponentiation wrt. N is always used for
181 * RSA public key operations. */
182 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
183 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
184 {
185 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
186 }
187
188#if !defined(MBEDTLS_RSA_NO_CRT)
189 /* Modular exponentiation for P and Q is only
190 * used for private key operations and if CRT
191 * is used. */
192 if( is_priv &&
193 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
194 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
195 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
196 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
197 {
198 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
199 }
200#endif /* !MBEDTLS_RSA_NO_CRT */
201
202 /*
203 * 2. Exponents must be positive
204 */
205
206 /* Always need E for public key operations */
207 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
208 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
209
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100210#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100211 /* For private key operations, use D or DP & DQ
212 * as (unblinded) exponents. */
213 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
214 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
215#else
216 if( is_priv &&
217 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
218 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
219 {
220 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
221 }
222#endif /* MBEDTLS_RSA_NO_CRT */
223
224 /* Blinding shouldn't make exponents negative either,
225 * so check that P, Q >= 1 if that hasn't yet been
226 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100227#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100228 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100229 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
230 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
231 {
232 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
233 }
234#endif
235
236 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100237 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100238#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100239 if( is_priv &&
240 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
241 {
242 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
243 }
244#endif
245
246 return( 0 );
247}
248
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100249int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100250{
251 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500252 int have_N, have_P, have_Q, have_D, have_E;
253 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100254
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 RSA_VALIDATE_RET( ctx != NULL );
256
257 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
258 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
259 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
260 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
261 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100262
Hanno Becker617c1ae2017-08-23 14:11:24 +0100263 /*
264 * Check whether provided parameters are enough
265 * to deduce all others. The following incomplete
266 * parameter sets for private keys are supported:
267 *
268 * (1) P, Q missing.
269 * (2) D and potentially N missing.
270 *
271 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100272
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500273 n_missing = have_P && have_Q && have_D && have_E;
274 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
275 d_missing = have_P && have_Q && !have_D && have_E;
276 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100277
278 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500279 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100280
Hanno Becker617c1ae2017-08-23 14:11:24 +0100281 if( !is_priv && !is_pub )
282 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
283
284 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100285 * Step 1: Deduce N if P, Q are provided.
286 */
287
288 if( !have_N && have_P && have_Q )
289 {
290 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
291 &ctx->Q ) ) != 0 )
292 {
293 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
294 }
295
296 ctx->len = mbedtls_mpi_size( &ctx->N );
297 }
298
299 /*
300 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100301 */
302
303 if( pq_missing )
304 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100305 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100306 &ctx->P, &ctx->Q );
307 if( ret != 0 )
308 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
309
310 }
311 else if( d_missing )
312 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100313 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
314 &ctx->Q,
315 &ctx->E,
316 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100317 {
318 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
319 }
320 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100321
Hanno Becker617c1ae2017-08-23 14:11:24 +0100322 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100323 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100324 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100325 */
326
Hanno Becker23344b52017-08-23 07:43:27 +0100327#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100328 if( is_priv )
329 {
330 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
331 &ctx->DP, &ctx->DQ, &ctx->QP );
332 if( ret != 0 )
333 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
334 }
Hanno Becker23344b52017-08-23 07:43:27 +0100335#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100336
337 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100338 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100339 */
340
Hanno Beckerebd2c022017-10-12 10:54:53 +0100341 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100342}
343
Hanno Becker617c1ae2017-08-23 14:11:24 +0100344int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
345 unsigned char *N, size_t N_len,
346 unsigned char *P, size_t P_len,
347 unsigned char *Q, size_t Q_len,
348 unsigned char *D, size_t D_len,
349 unsigned char *E, size_t E_len )
350{
351 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500352 int is_priv;
353 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100354
355 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500356 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100357 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
358 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
359 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
360 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
361 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
362
363 if( !is_priv )
364 {
365 /* If we're trying to export private parameters for a public key,
366 * something must be wrong. */
367 if( P != NULL || Q != NULL || D != NULL )
368 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
369
370 }
371
372 if( N != NULL )
373 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
374
375 if( P != NULL )
376 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
377
378 if( Q != NULL )
379 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
380
381 if( D != NULL )
382 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
383
384 if( E != NULL )
385 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100386
387cleanup:
388
389 return( ret );
390}
391
Hanno Becker617c1ae2017-08-23 14:11:24 +0100392int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
393 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
394 mbedtls_mpi *D, mbedtls_mpi *E )
395{
Janos Follath24eed8d2019-11-22 13:21:35 +0000396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500397 int is_priv;
398 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100399
400 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500401 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100402 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
403 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
404 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
405 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
406 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
407
408 if( !is_priv )
409 {
410 /* If we're trying to export private parameters for a public key,
411 * something must be wrong. */
412 if( P != NULL || Q != NULL || D != NULL )
413 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
414
415 }
416
417 /* Export all requested core parameters. */
418
419 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
420 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
421 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
422 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
423 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
424 {
425 return( ret );
426 }
427
428 return( 0 );
429}
430
431/*
432 * Export CRT parameters
433 * This must also be implemented if CRT is not used, for being able to
434 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
435 * can be used in this case.
436 */
437int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
438 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
439{
Janos Follath24eed8d2019-11-22 13:21:35 +0000440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500441 int is_priv;
442 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100443
444 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500445 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100446 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
447 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
448 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
449 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
450 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
451
452 if( !is_priv )
453 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
454
Hanno Beckerdc95c892017-08-23 06:57:02 +0100455#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100456 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100457 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
458 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
459 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
460 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100461 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100462 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100463#else
464 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
465 DP, DQ, QP ) ) != 0 )
466 {
467 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
468 }
469#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100470
471 return( 0 );
472}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100473
Paul Bakker5121ce52009-01-03 21:22:43 +0000474/*
475 * Initialize an RSA context
476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000479 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000480{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500481 RSA_VALIDATE( ctx != NULL );
482 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
483 padding == MBEDTLS_RSA_PKCS_V21 );
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#if defined(MBEDTLS_THREADING_C)
490 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200491#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000492}
493
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100494/*
495 * Set padding for an existing RSA context
496 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500497void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
498 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100499{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500500 RSA_VALIDATE( ctx != NULL );
501 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
502 padding == MBEDTLS_RSA_PKCS_V21 );
503
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100504 ctx->padding = padding;
505 ctx->hash_id = hash_id;
506}
507
Hanno Becker617c1ae2017-08-23 14:11:24 +0100508/*
509 * Get length in bytes of RSA modulus
510 */
511
512size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
513{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100514 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100515}
516
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520/*
521 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800522 *
523 * This generation method follows the RSA key pair generation procedure of
524 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000527 int (*f_rng)(void *, unsigned char *, size_t),
528 void *p_rng,
529 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000530{
Janos Follath24eed8d2019-11-22 13:21:35 +0000531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800532 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100533 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500534 RSA_VALIDATE_RET( ctx != NULL );
535 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500537 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
Janos Follathef441782016-09-21 13:18:12 +0100538 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
539
Janos Follathb8fc1b02018-09-03 15:37:01 +0100540 /*
541 * If the modulus is 1024 bit long or shorter, then the security strength of
542 * the RSA algorithm is less than or equal to 80 bits and therefore an error
543 * rate of 2^-80 is sufficient.
544 */
545 if( nbits > 1024 )
546 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
547
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100548 mbedtls_mpi_init( &H );
549 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800550 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
552 /*
553 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800554 * 1. |P-Q| > 2^( nbits / 2 - 100 )
555 * 2. GCD( E, (P-1)*(Q-1) ) == 1
556 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 do
561 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100562 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
563 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
Janos Follathb8fc1b02018-09-03 15:37:01 +0100565 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
566 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800568 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
569 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
570 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 continue;
572
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800573 /* not required by any standards, but some users rely on the fact that P > Q */
574 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100575 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100576
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100577 /* Temporarily replace P,Q by P-1, Q-1 */
578 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
579 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
580 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800581
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800582 /* 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 +0200583 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800584 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
585 continue;
586
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800587 /* 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 -0800588 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
589 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
590 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
591
592 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
593 continue;
594
595 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800597 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100599 /* Restore P,Q */
600 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
601 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
602
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800603 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
604
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100605 ctx->len = mbedtls_mpi_size( &ctx->N );
606
Jethro Beekman97f95c92018-02-13 15:50:36 -0800607#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 * DP = D mod (P - 1)
610 * DQ = D mod (Q - 1)
611 * QP = Q^-1 mod P
612 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100613 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
614 &ctx->DP, &ctx->DQ, &ctx->QP ) );
615#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
Hanno Becker83aad1f2017-08-23 06:45:10 +0100617 /* Double-check */
618 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620cleanup:
621
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100622 mbedtls_mpi_free( &H );
623 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800624 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626 if( ret != 0 )
627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_rsa_free( ctx );
629 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 }
631
Paul Bakker48377d92013-08-30 12:06:24 +0200632 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633}
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
637/*
638 * Check a public RSA key
639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000641{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500642 RSA_VALIDATE_RET( ctx != NULL );
643
Hanno Beckerebd2c022017-10-12 10:54:53 +0100644 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000646
Hanno Becker3a760a12018-01-05 08:14:49 +0000647 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100650 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Hanno Becker705fc682017-10-10 17:57:02 +0100652 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
653 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100657 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000658
659 return( 0 );
660}
661
662/*
Hanno Becker705fc682017-10-10 17:57:02 +0100663 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000666{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500667 RSA_VALIDATE_RET( ctx != NULL );
668
Hanno Becker705fc682017-10-10 17:57:02 +0100669 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100670 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 {
Hanno Becker98838b02017-10-02 13:16:10 +0100672 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 }
Paul Bakker48377d92013-08-30 12:06:24 +0200674
Hanno Becker98838b02017-10-02 13:16:10 +0100675 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100676 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100678 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000680
Hanno Beckerb269a852017-08-25 08:03:21 +0100681#if !defined(MBEDTLS_RSA_NO_CRT)
682 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
683 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
684 {
685 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
686 }
687#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000688
689 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000690}
691
692/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100693 * Check if contexts holding a public and private key match
694 */
Hanno Becker98838b02017-10-02 13:16:10 +0100695int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
696 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100697{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500698 RSA_VALIDATE_RET( pub != NULL );
699 RSA_VALIDATE_RET( prv != NULL );
700
Hanno Becker98838b02017-10-02 13:16:10 +0100701 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100705 }
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
708 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100711 }
712
713 return( 0 );
714}
715
716/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 * Do an RSA public key operation
718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000720 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 unsigned char *output )
722{
Janos Follath24eed8d2019-11-22 13:21:35 +0000723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000724 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500726 RSA_VALIDATE_RET( ctx != NULL );
727 RSA_VALIDATE_RET( input != NULL );
728 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Hanno Beckerebd2c022017-10-12 10:54:53 +0100730 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100731 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200735#if defined(MBEDTLS_THREADING_C)
736 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
737 return( ret );
738#endif
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000743 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200744 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
745 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000746 }
747
748 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
750 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
752cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200754 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
755 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100756#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
760 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
763 return( 0 );
764}
765
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200766/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200767 * Generate or update blinding values, see section 10 of:
768 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200769 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200770 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200771 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200772static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200773 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
774{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200775 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200776
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200777 if( ctx->Vf.p != NULL )
778 {
779 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
781 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
782 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
783 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200784
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200785 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200786 }
787
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200788 /* Unblinding value: Vf = random number, invertible mod N */
789 do {
790 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
794 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
795 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200796
797 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
799 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 +0200800
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200801
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200802cleanup:
803 return( ret );
804}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200805
Paul Bakker5121ce52009-01-03 21:22:43 +0000806/*
Janos Follathe81102e2017-03-22 13:38:28 +0000807 * Exponent blinding supposed to prevent side-channel attacks using multiple
808 * traces of measurements to recover the RSA key. The more collisions are there,
809 * the more bits of the key can be recovered. See [3].
810 *
811 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
812 * observations on avarage.
813 *
814 * For example with 28 byte blinding to achieve 2 collisions the adversary has
815 * to make 2^112 observations on avarage.
816 *
817 * (With the currently (as of 2017 April) known best algorithms breaking 2048
818 * bit RSA requires approximately as much time as trying out 2^112 random keys.
819 * Thus in this sense with 28 byte blinding the security is not reduced by
820 * side-channel attacks like the one in [3])
821 *
822 * This countermeasure does not help if the key recovery is possible with a
823 * single trace.
824 */
825#define RSA_EXPONENT_BLINDING 28
826
827/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000828 * Do an RSA private key operation
829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200831 int (*f_rng)(void *, unsigned char *, size_t),
832 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000833 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000834 unsigned char *output )
835{
Janos Follath24eed8d2019-11-22 13:21:35 +0000836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000837 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100838
839 /* Temporary holding the result */
840 mbedtls_mpi T;
841
842 /* Temporaries holding P-1, Q-1 and the
843 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000844 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100845
846#if !defined(MBEDTLS_RSA_NO_CRT)
847 /* Temporaries holding the results mod p resp. mod q. */
848 mbedtls_mpi TP, TQ;
849
850 /* Temporaries holding the blinded exponents for
851 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000852 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100853
854 /* Pointers to actual exponents to be used - either the unblinded
855 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000856 mbedtls_mpi *DP = &ctx->DP;
857 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100858#else
859 /* Temporary holding the blinded exponent (if used). */
860 mbedtls_mpi D_blind;
861
862 /* Pointer to actual exponent to be used - either the unblinded
863 * or the blinded one, depending on the presence of a PRNG. */
864 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100865#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100866
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100867 /* Temporaries holding the initial input and the double
868 * checked result; should be the same in the end. */
869 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000870
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500871 RSA_VALIDATE_RET( ctx != NULL );
872 RSA_VALIDATE_RET( input != NULL );
873 RSA_VALIDATE_RET( output != NULL );
874
Hanno Beckerebd2c022017-10-12 10:54:53 +0100875 if( rsa_check_context( ctx, 1 /* private key checks */,
876 f_rng != NULL /* blinding y/n */ ) != 0 )
877 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100878 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100879 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100880
Hanno Becker06811ce2017-05-03 15:10:34 +0100881#if defined(MBEDTLS_THREADING_C)
882 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
883 return( ret );
884#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000885
Hanno Becker06811ce2017-05-03 15:10:34 +0100886 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100887 mbedtls_mpi_init( &T );
888
889 mbedtls_mpi_init( &P1 );
890 mbedtls_mpi_init( &Q1 );
891 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000892
Janos Follathf9203b42017-03-22 15:13:15 +0000893 if( f_rng != NULL )
894 {
Janos Follathe81102e2017-03-22 13:38:28 +0000895#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000896 mbedtls_mpi_init( &D_blind );
897#else
898 mbedtls_mpi_init( &DP_blind );
899 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000900#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000901 }
Janos Follathe81102e2017-03-22 13:38:28 +0000902
Hanno Becker06811ce2017-05-03 15:10:34 +0100903#if !defined(MBEDTLS_RSA_NO_CRT)
904 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200905#endif
906
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100907 mbedtls_mpi_init( &I );
908 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100909
910 /* End of MPI initialization */
911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
913 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200915 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
916 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 }
918
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100919 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100920
Paul Bakkerf451bac2013-08-30 15:37:02 +0200921 if( f_rng != NULL )
922 {
923 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200924 * Blinding
925 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200926 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200927 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
928 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000930
Janos Follathe81102e2017-03-22 13:38:28 +0000931 /*
932 * Exponent blinding
933 */
934 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
935 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
936
Janos Follathf9203b42017-03-22 15:13:15 +0000937#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000938 /*
939 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
940 */
941 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
942 f_rng, p_rng ) );
943 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
944 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
945 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
946
947 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000948#else
949 /*
950 * DP_blind = ( P - 1 ) * R + DP
951 */
952 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
953 f_rng, p_rng ) );
954 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
955 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
956 &ctx->DP ) );
957
958 DP = &DP_blind;
959
960 /*
961 * DQ_blind = ( Q - 1 ) * R + DQ
962 */
963 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
964 f_rng, p_rng ) );
965 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
966 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
967 &ctx->DQ ) );
968
969 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000970#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200971 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000974 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100975#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200976 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000977 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100979 * TP = input ^ dP mod P
980 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000981 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100982
983 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
984 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000985
986 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100987 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100989 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
990 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
991 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
993 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100994 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100996 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
997 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200999
Paul Bakkerf451bac2013-08-30 15:37:02 +02001000 if( f_rng != NULL )
1001 {
1002 /*
1003 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001004 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001005 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001006 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001008 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001009
Hanno Becker2dec5e82017-10-03 07:49:52 +01001010 /* Verify the result to prevent glitching attacks. */
1011 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1012 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001013 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001014 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001015 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1016 goto cleanup;
1017 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001018
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
1022cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001024 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1025 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001026#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001027
Hanno Becker06811ce2017-05-03 15:10:34 +01001028 mbedtls_mpi_free( &P1 );
1029 mbedtls_mpi_free( &Q1 );
1030 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001031
1032 if( f_rng != NULL )
1033 {
Janos Follathe81102e2017-03-22 13:38:28 +00001034#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001035 mbedtls_mpi_free( &D_blind );
1036#else
1037 mbedtls_mpi_free( &DP_blind );
1038 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001039#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001040 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Hanno Becker06811ce2017-05-03 15:10:34 +01001042 mbedtls_mpi_free( &T );
1043
1044#if !defined(MBEDTLS_RSA_NO_CRT)
1045 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1046#endif
1047
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001048 mbedtls_mpi_free( &C );
1049 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001050
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
1054 return( 0 );
1055}
1056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001058/**
1059 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1060 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001061 * \param dst buffer to mask
1062 * \param dlen length of destination buffer
1063 * \param src source of the mask generation
1064 * \param slen length of the source buffer
1065 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001066 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001067static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001069{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001071 unsigned char counter[4];
1072 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001073 unsigned int hlen;
1074 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001075 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001078 memset( counter, 0, 4 );
1079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001081
Simon Butcher02037452016-03-01 21:19:12 +00001082 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001083 p = dst;
1084
1085 while( dlen > 0 )
1086 {
1087 use_len = hlen;
1088 if( dlen < hlen )
1089 use_len = dlen;
1090
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001091 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1092 goto exit;
1093 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1094 goto exit;
1095 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1096 goto exit;
1097 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1098 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001099
1100 for( i = 0; i < use_len; ++i )
1101 *p++ ^= mask[i];
1102
1103 counter[3]++;
1104
1105 dlen -= use_len;
1106 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001107
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001108exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001109 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001110
1111 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001116/*
1117 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001120 int (*f_rng)(void *, unsigned char *, size_t),
1121 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001122 int mode,
1123 const unsigned char *label, size_t label_len,
1124 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001125 const unsigned char *input,
1126 unsigned char *output )
1127{
1128 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001130 unsigned char *p = output;
1131 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 const mbedtls_md_info_t *md_info;
1133 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001134
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001135 RSA_VALIDATE_RET( ctx != NULL );
1136 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1137 mode == MBEDTLS_RSA_PUBLIC );
1138 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001139 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001140 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1143 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001144
1145 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001149 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001151
1152 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001154
Simon Butcher02037452016-03-01 21:19:12 +00001155 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001156 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001158
1159 memset( output, 0, olen );
1160
1161 *p++ = 0;
1162
Simon Butcher02037452016-03-01 21:19:12 +00001163 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001164 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001166
1167 p += hlen;
1168
Simon Butcher02037452016-03-01 21:19:12 +00001169 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001170 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1171 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001172 p += hlen;
1173 p += olen - 2 * hlen - 2 - ilen;
1174 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001175 if( ilen != 0 )
1176 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001179 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001180 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001181
Simon Butcher02037452016-03-01 21:19:12 +00001182 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001183 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1184 &md_ctx ) ) != 0 )
1185 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001186
Simon Butcher02037452016-03-01 21:19:12 +00001187 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001188 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1189 &md_ctx ) ) != 0 )
1190 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001191
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001192exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001194
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001195 if( ret != 0 )
1196 return( ret );
1197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 return( ( mode == MBEDTLS_RSA_PUBLIC )
1199 ? mbedtls_rsa_public( ctx, output, output )
1200 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001205/*
1206 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001209 int (*f_rng)(void *, unsigned char *, size_t),
1210 void *p_rng,
1211 int mode, size_t ilen,
1212 const unsigned char *input,
1213 unsigned char *output )
1214{
1215 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001217 unsigned char *p = output;
1218
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001219 RSA_VALIDATE_RET( ctx != NULL );
1220 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1221 mode == MBEDTLS_RSA_PUBLIC );
1222 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001223 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001224
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001225 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001227
1228 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001229
Simon Butcher02037452016-03-01 21:19:12 +00001230 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001231 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001233
1234 nb_pad = olen - 3 - ilen;
1235
1236 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001238 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001239 if( f_rng == NULL )
1240 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001243
1244 while( nb_pad-- > 0 )
1245 {
1246 int rng_dl = 100;
1247
1248 do {
1249 ret = f_rng( p_rng, p, 1 );
1250 } while( *p == 0 && --rng_dl && ret == 0 );
1251
Simon Butcher02037452016-03-01 21:19:12 +00001252 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001253 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001255
1256 p++;
1257 }
1258 }
1259 else
1260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001262
1263 while( nb_pad-- > 0 )
1264 *p++ = 0xFF;
1265 }
1266
1267 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001268 if( ilen != 0 )
1269 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 return( ( mode == MBEDTLS_RSA_PUBLIC )
1272 ? mbedtls_rsa_public( ctx, output, output )
1273 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001274}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001276
Paul Bakker5121ce52009-01-03 21:22:43 +00001277/*
1278 * Add the message padding, then do an RSA operation
1279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001281 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001282 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001283 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001284 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 unsigned char *output )
1286{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001287 RSA_VALIDATE_RET( ctx != NULL );
1288 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1289 mode == MBEDTLS_RSA_PUBLIC );
1290 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001291 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001292
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 switch( ctx->padding )
1294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#if defined(MBEDTLS_PKCS1_V15)
1296 case MBEDTLS_RSA_PKCS_V15:
1297 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001298 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001299#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_PKCS1_V21)
1302 case MBEDTLS_RSA_PKCS_V21:
1303 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001304 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001305#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
1307 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001310}
1311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001313/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001314 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001317 int (*f_rng)(void *, unsigned char *, size_t),
1318 void *p_rng,
1319 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001320 const unsigned char *label, size_t label_len,
1321 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001322 const unsigned char *input,
1323 unsigned char *output,
1324 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001325{
Janos Follath24eed8d2019-11-22 13:21:35 +00001326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001327 size_t ilen, i, pad_len;
1328 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1330 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001331 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 const mbedtls_md_info_t *md_info;
1333 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001334
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001335 RSA_VALIDATE_RET( ctx != NULL );
1336 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1337 mode == MBEDTLS_RSA_PUBLIC );
1338 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1339 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1340 RSA_VALIDATE_RET( input != NULL );
1341 RSA_VALIDATE_RET( olen != NULL );
1342
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001343 /*
1344 * Parameters sanity checks
1345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1347 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
1349 ilen = ctx->len;
1350
Paul Bakker27fdf462011-06-09 13:55:13 +00001351 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001355 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001357
Janos Follathc17cda12016-02-11 11:08:18 +00001358 hlen = mbedtls_md_get_size( md_info );
1359
1360 // checking for integer underflow
1361 if( 2 * hlen + 2 > ilen )
1362 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1363
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001364 /*
1365 * RSA operation
1366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1368 ? mbedtls_rsa_public( ctx, input, buf )
1369 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
1371 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001372 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001374 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001375 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001378 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1379 {
1380 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001381 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001382 }
1383
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001384 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001385 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1386 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001387 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001388 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1389 &md_ctx ) ) != 0 )
1390 {
1391 mbedtls_md_free( &md_ctx );
1392 goto cleanup;
1393 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001396
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001397 /* Generate lHash */
1398 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1399 goto cleanup;
1400
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001401 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001402 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001403 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001405 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001407 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001408
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001409 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001410
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001411 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001412 for( i = 0; i < hlen; i++ )
1413 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001414
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001415 /* Get zero-padding len, but always read till end of buffer
1416 * (minus one, for the 01 byte) */
1417 pad_len = 0;
1418 pad_done = 0;
1419 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1420 {
1421 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001422 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001423 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001424
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001425 p += pad_len;
1426 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001427
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001428 /*
1429 * The only information "leaked" is whether the padding was correct or not
1430 * (eg, no data is copied if it was not correct). This meets the
1431 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1432 * the different error conditions.
1433 */
1434 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001435 {
1436 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1437 goto cleanup;
1438 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001439
Paul Bakker66d5d072014-06-17 16:39:18 +02001440 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001441 {
1442 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1443 goto cleanup;
1444 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
1446 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001447 if( *olen != 0 )
1448 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001449 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001450
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001451cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001452 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1453 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001454
1455 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001460/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1461 *
1462 * \param value The value to analyze.
1463 * \return Zero if \p value is zero, otherwise all-bits-one.
1464 */
1465static unsigned all_or_nothing_int( unsigned value )
1466{
1467 /* MSVC has a warning about unary minus on unsigned, but this is
1468 * well-defined and precisely what we want to do here */
1469#if defined(_MSC_VER)
1470#pragma warning( push )
1471#pragma warning( disable : 4146 )
1472#endif
1473 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1474#if defined(_MSC_VER)
1475#pragma warning( pop )
1476#endif
1477}
1478
1479/** Check whether a size is out of bounds, without branches.
1480 *
1481 * This is equivalent to `size > max`, but is likely to be compiled to
1482 * to code using bitwise operation rather than a branch.
1483 *
1484 * \param size Size to check.
1485 * \param max Maximum desired value for \p size.
1486 * \return \c 0 if `size <= max`.
1487 * \return \c 1 if `size > max`.
1488 */
1489static unsigned size_greater_than( size_t size, size_t max )
1490{
1491 /* Return the sign bit (1 for negative) of (max - size). */
1492 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1493}
1494
1495/** Choose between two integer values, without branches.
1496 *
1497 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1498 * to code using bitwise operation rather than a branch.
1499 *
1500 * \param cond Condition to test.
1501 * \param if1 Value to use if \p cond is nonzero.
1502 * \param if0 Value to use if \p cond is zero.
1503 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1504 */
1505static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
1506{
1507 unsigned mask = all_or_nothing_int( cond );
1508 return( ( mask & if1 ) | (~mask & if0 ) );
1509}
1510
1511/** Shift some data towards the left inside a buffer without leaking
1512 * the length of the data through side channels.
1513 *
1514 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1515 * ```
1516 * memmove(start, start + offset, total - offset);
1517 * memset(start + offset, 0, total - offset);
1518 * ```
1519 * but it strives to use a memory access pattern (and thus total timing)
1520 * that does not depend on \p offset. This timing independence comes at
1521 * the expense of performance.
1522 *
1523 * \param start Pointer to the start of the buffer.
1524 * \param total Total size of the buffer.
1525 * \param offset Offset from which to copy \p total - \p offset bytes.
1526 */
1527static void mem_move_to_left( void *start,
1528 size_t total,
1529 size_t offset )
1530{
1531 volatile unsigned char *buf = start;
1532 size_t i, n;
1533 if( total == 0 )
1534 return;
1535 for( i = 0; i < total; i++ )
1536 {
1537 unsigned no_op = size_greater_than( total - offset, i );
1538 /* The first `total - offset` passes are a no-op. The last
1539 * `offset` passes shift the data one byte to the left and
1540 * zero out the last byte. */
1541 for( n = 0; n < total - 1; n++ )
1542 {
1543 unsigned char current = buf[n];
1544 unsigned char next = buf[n+1];
1545 buf[n] = if_int( no_op, current, next );
1546 }
1547 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1548 }
1549}
1550
Paul Bakkerb3869132013-02-28 17:21:01 +01001551/*
1552 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001555 int (*f_rng)(void *, unsigned char *, size_t),
1556 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001557 int mode, size_t *olen,
1558 const unsigned char *input,
1559 unsigned char *output,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001560 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001561{
Janos Follath24eed8d2019-11-22 13:21:35 +00001562 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001563 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001565 /* The following variables take sensitive values: their value must
1566 * not leak into the observable behavior of the function other than
1567 * the designated outputs (output, olen, return value). Otherwise
1568 * this would open the execution of the function to
1569 * side-channel-based variants of the Bleichenbacher padding oracle
1570 * attack. Potential side channels include overall timing, memory
1571 * access patterns (especially visible to an adversary who has access
1572 * to a shared memory cache), and branches (especially visible to
1573 * an adversary who has access to a shared code cache or to a shared
1574 * branch predictor). */
1575 size_t pad_count = 0;
1576 unsigned bad = 0;
1577 unsigned char pad_done = 0;
1578 size_t plaintext_size = 0;
1579 unsigned output_too_large;
1580
1581 RSA_VALIDATE_RET( ctx != NULL );
1582 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1583 mode == MBEDTLS_RSA_PUBLIC );
1584 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1585 RSA_VALIDATE_RET( input != NULL );
1586 RSA_VALIDATE_RET( olen != NULL );
1587
1588 ilen = ctx->len;
1589 plaintext_max_size = ( output_max_len > ilen - 11 ?
1590 ilen - 11 :
1591 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1594 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001595
Paul Bakkerb3869132013-02-28 17:21:01 +01001596 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1600 ? mbedtls_rsa_public( ctx, input, buf )
1601 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001602
1603 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001604 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001605
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001606 /* Check and get padding length in constant time and constant
1607 * memory trace. The first byte must be 0. */
1608 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001612 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1613 * where PS must be at least 8 nonzero bytes. */
1614 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001616 /* Read the whole buffer. Set pad_done to nonzero if we find
1617 * the 0x00 byte and remember the padding length in pad_count. */
1618 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001619 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001620 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001621 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001622 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001623 }
1624 else
1625 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001626 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1627 * where PS must be at least 8 bytes with the value 0xFF. */
1628 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001629
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001630 /* Read the whole buffer. Set pad_done to nonzero if we find
1631 * the 0x00 byte and remember the padding length in pad_count.
1632 * If there's a non-0xff byte in the padding, the padding is bad. */
1633 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001634 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001635 pad_done |= if_int( buf[i], 0, 1 );
1636 pad_count += if_int( pad_done, 0, 1 );
1637 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001638 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 }
1640
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001641 /* If pad_done is still zero, there's no data, only unfinished padding. */
1642 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001643
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001644 /* There must be at least 8 bytes of padding. */
1645 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001646
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001647 /* If the padding is valid, set plaintext_size to the number of
1648 * remaining bytes after stripping the padding. If the padding
1649 * is invalid, avoid leaking this fact through the size of the
1650 * output: use the maximum message size that fits in the output
1651 * buffer. Do it without branches to avoid leaking the padding
1652 * validity through timing. RSA keys are small enough that all the
1653 * size_t values involved fit in unsigned int. */
1654 plaintext_size = if_int( bad,
1655 (unsigned) plaintext_max_size,
1656 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001657
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001658 /* Set output_too_large to 0 if the plaintext fits in the output
1659 * buffer and to 1 otherwise. */
1660 output_too_large = size_greater_than( plaintext_size,
1661 plaintext_max_size );
1662
1663 /* Set ret without branches to avoid timing attacks. Return:
1664 * - INVALID_PADDING if the padding is bad (bad != 0).
1665 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1666 * plaintext does not fit in the output buffer.
1667 * - 0 if the padding is correct. */
1668 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1669 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1670 0 ) );
1671
1672 /* If the padding is bad or the plaintext is too large, zero the
1673 * data that we're about to copy to the output buffer.
1674 * We need to copy the same amount of data
1675 * from the same buffer whether the padding is good or not to
1676 * avoid leaking the padding validity through overall timing or
1677 * through memory or cache access patterns. */
1678 bad = all_or_nothing_int( bad | output_too_large );
1679 for( i = 11; i < ilen; i++ )
1680 buf[i] &= ~bad;
1681
1682 /* If the plaintext is too large, truncate it to the buffer size.
1683 * Copy anyway to avoid revealing the length through timing, because
1684 * revealing the length is as bad as revealing the padding validity
1685 * for a Bleichenbacher attack. */
1686 plaintext_size = if_int( output_too_large,
1687 (unsigned) plaintext_max_size,
1688 (unsigned) plaintext_size );
1689
1690 /* Move the plaintext to the leftmost position where it can start in
1691 * the working buffer, i.e. make it start plaintext_max_size from
1692 * the end of the buffer. Do this with a memory access trace that
1693 * does not depend on the plaintext size. After this move, the
1694 * starting location of the plaintext is no longer sensitive
1695 * information. */
1696 mem_move_to_left( buf + ilen - plaintext_max_size,
1697 plaintext_max_size,
1698 plaintext_max_size - plaintext_size );
1699
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001700 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1701 * buffer. If output_max_len is 0, then output may be an invalid pointer
1702 * and the result of memcpy() would be undefined; prevent undefined
1703 * behavior making sure to depend only on output_max_len (the size of the
1704 * user-provided output buffer), which is independent from plaintext
1705 * length, validity of padding, success of the decryption, and other
1706 * secrets. */
1707 if( output_max_len != 0 )
1708 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001709
1710 /* Report the amount of data we copied to the output buffer. In case
1711 * of errors (bad padding or output too large), the value of *olen
1712 * when this function returns is not specified. Making it equivalent
1713 * to the good case limits the risks of leaking the padding validity. */
1714 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001716cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001717 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001718
1719 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001720}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
1723/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001724 * Do an RSA operation, then remove the message padding
1725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001727 int (*f_rng)(void *, unsigned char *, size_t),
1728 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001729 int mode, size_t *olen,
1730 const unsigned char *input,
1731 unsigned char *output,
1732 size_t output_max_len)
1733{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001734 RSA_VALIDATE_RET( ctx != NULL );
1735 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1736 mode == MBEDTLS_RSA_PUBLIC );
1737 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1738 RSA_VALIDATE_RET( input != NULL );
1739 RSA_VALIDATE_RET( olen != NULL );
1740
Paul Bakkerb3869132013-02-28 17:21:01 +01001741 switch( ctx->padding )
1742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#if defined(MBEDTLS_PKCS1_V15)
1744 case MBEDTLS_RSA_PKCS_V15:
1745 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001746 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001747#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#if defined(MBEDTLS_PKCS1_V21)
1750 case MBEDTLS_RSA_PKCS_V21:
1751 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001752 olen, input, output,
1753 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001754#endif
1755
1756 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001758 }
1759}
1760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001762/*
1763 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001766 int (*f_rng)(void *, unsigned char *, size_t),
1767 void *p_rng,
1768 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001770 unsigned int hashlen,
1771 const unsigned char *hash,
1772 unsigned char *sig )
1773{
1774 size_t olen;
1775 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001777 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001779 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 const mbedtls_md_info_t *md_info;
1781 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001782 RSA_VALIDATE_RET( ctx != NULL );
1783 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1784 mode == MBEDTLS_RSA_PUBLIC );
1785 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1786 hashlen == 0 ) ||
1787 hash != NULL );
1788 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1791 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001792
1793 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001795
1796 olen = ctx->len;
1797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001799 {
Simon Butcher02037452016-03-01 21:19:12 +00001800 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001802 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001806 }
1807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001809 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001813
Jaeden Amero3725bb22018-09-07 19:12:36 +01001814 /* Calculate the largest possible salt length. Normally this is the hash
1815 * length, which is the maximum length the salt can have. If there is not
1816 * enough room, use the maximum salt length that fits. The constraint is
1817 * that the hash length plus the salt length plus 2 bytes must be at most
1818 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1819 * (PKCS#1 v2.2) §9.1.1 step 3. */
1820 min_slen = hlen - 2;
1821 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001823 else if( olen >= hlen + hlen + 2 )
1824 slen = hlen;
1825 else
1826 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001827
1828 memset( sig, 0, olen );
1829
Simon Butcher02037452016-03-01 21:19:12 +00001830 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001831 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001833
Simon Butcher02037452016-03-01 21:19:12 +00001834 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001835 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001836 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001837 *p++ = 0x01;
1838 memcpy( p, salt, slen );
1839 p += slen;
1840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001842 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001843 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001844
Simon Butcher02037452016-03-01 21:19:12 +00001845 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001846 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1847 goto exit;
1848 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1849 goto exit;
1850 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1851 goto exit;
1852 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1853 goto exit;
1854 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1855 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001856
Simon Butcher02037452016-03-01 21:19:12 +00001857 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001858 if( msb % 8 == 0 )
1859 offset = 1;
1860
Simon Butcher02037452016-03-01 21:19:12 +00001861 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001862 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1863 &md_ctx ) ) != 0 )
1864 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001865
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001866 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001867 sig[0] &= 0xFF >> ( olen * 8 - msb );
1868
1869 p += hlen;
1870 *p++ = 0xBC;
1871
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001872 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001873
1874exit:
1875 mbedtls_md_free( &md_ctx );
1876
1877 if( ret != 0 )
1878 return( ret );
1879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 return( ( mode == MBEDTLS_RSA_PUBLIC )
1881 ? mbedtls_rsa_public( ctx, sig, sig )
1882 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001883}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001887/*
1888 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1889 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001890
1891/* Construct a PKCS v1.5 encoding of a hashed message
1892 *
1893 * This is used both for signature generation and verification.
1894 *
1895 * Parameters:
1896 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001897 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001898 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001899 * - hash: Buffer containing the hashed message or the raw data.
1900 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001901 * - dst: Buffer to hold the encoded message.
1902 *
1903 * Assumptions:
1904 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1905 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001906 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001907 *
1908 */
1909static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1910 unsigned int hashlen,
1911 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001912 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001913 unsigned char *dst )
1914{
1915 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001916 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001917 unsigned char *p = dst;
1918 const char *oid = NULL;
1919
1920 /* Are we signing hashed or raw data? */
1921 if( md_alg != MBEDTLS_MD_NONE )
1922 {
1923 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1924 if( md_info == NULL )
1925 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1926
1927 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1928 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1929
1930 hashlen = mbedtls_md_get_size( md_info );
1931
1932 /* Double-check that 8 + hashlen + oid_size can be used as a
1933 * 1-byte ASN.1 length encoding and that there's no overflow. */
1934 if( 8 + hashlen + oid_size >= 0x80 ||
1935 10 + hashlen < hashlen ||
1936 10 + hashlen + oid_size < 10 + hashlen )
1937 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1938
1939 /*
1940 * Static bounds check:
1941 * - Need 10 bytes for five tag-length pairs.
1942 * (Insist on 1-byte length encodings to protect against variants of
1943 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1944 * - Need hashlen bytes for hash
1945 * - Need oid_size bytes for hash alg OID.
1946 */
1947 if( nb_pad < 10 + hashlen + oid_size )
1948 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1949 nb_pad -= 10 + hashlen + oid_size;
1950 }
1951 else
1952 {
1953 if( nb_pad < hashlen )
1954 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1955
1956 nb_pad -= hashlen;
1957 }
1958
Hanno Becker2b2f8982017-09-27 17:10:03 +01001959 /* Need space for signature header and padding delimiter (3 bytes),
1960 * and 8 bytes for the minimal padding */
1961 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001962 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1963 nb_pad -= 3;
1964
1965 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001966 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001967
1968 /* Write signature header and padding */
1969 *p++ = 0;
1970 *p++ = MBEDTLS_RSA_SIGN;
1971 memset( p, 0xFF, nb_pad );
1972 p += nb_pad;
1973 *p++ = 0;
1974
1975 /* Are we signing raw data? */
1976 if( md_alg == MBEDTLS_MD_NONE )
1977 {
1978 memcpy( p, hash, hashlen );
1979 return( 0 );
1980 }
1981
1982 /* Signing hashed data, add corresponding ASN.1 structure
1983 *
1984 * DigestInfo ::= SEQUENCE {
1985 * digestAlgorithm DigestAlgorithmIdentifier,
1986 * digest Digest }
1987 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1988 * Digest ::= OCTET STRING
1989 *
1990 * Schematic:
1991 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1992 * TAG-NULL + LEN [ NULL ] ]
1993 * TAG-OCTET + LEN [ HASH ] ]
1994 */
1995 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001996 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001997 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00001998 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01001999 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002000 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002001 memcpy( p, oid, oid_size );
2002 p += oid_size;
2003 *p++ = MBEDTLS_ASN1_NULL;
2004 *p++ = 0x00;
2005 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002006 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002007 memcpy( p, hash, hashlen );
2008 p += hashlen;
2009
2010 /* Just a sanity-check, should be automatic
2011 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002012 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002013 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002014 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002015 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2016 }
2017
2018 return( 0 );
2019}
2020
Paul Bakkerb3869132013-02-28 17:21:01 +01002021/*
2022 * Do an RSA operation to sign the message digest
2023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002025 int (*f_rng)(void *, unsigned char *, size_t),
2026 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002027 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002029 unsigned int hashlen,
2030 const unsigned char *hash,
2031 unsigned char *sig )
2032{
Janos Follath24eed8d2019-11-22 13:21:35 +00002033 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002034 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002035
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002036 RSA_VALIDATE_RET( ctx != NULL );
2037 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2038 mode == MBEDTLS_RSA_PUBLIC );
2039 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2040 hashlen == 0 ) ||
2041 hash != NULL );
2042 RSA_VALIDATE_RET( sig != NULL );
2043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2045 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002046
Hanno Beckerfdf38032017-09-06 12:35:55 +01002047 /*
2048 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2049 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002050
Hanno Beckerfdf38032017-09-06 12:35:55 +01002051 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2052 ctx->len, sig ) ) != 0 )
2053 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002054
2055 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002056 * Call respective RSA primitive
2057 */
2058
2059 if( mode == MBEDTLS_RSA_PUBLIC )
2060 {
2061 /* Skip verification on a public key operation */
2062 return( mbedtls_rsa_public( ctx, sig, sig ) );
2063 }
2064
2065 /* Private key operation
2066 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002067 * In order to prevent Lenstra's attack, make the signature in a
2068 * temporary buffer and check it before returning it.
2069 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002070
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002071 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002072 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002073 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2074
Hanno Beckerfdf38032017-09-06 12:35:55 +01002075 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002076 if( verif == NULL )
2077 {
2078 mbedtls_free( sig_try );
2079 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2080 }
2081
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002082 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2083 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2084
Hanno Becker171a8f12017-09-06 12:32:16 +01002085 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002086 {
2087 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2088 goto cleanup;
2089 }
2090
2091 memcpy( sig, sig_try, ctx->len );
2092
2093cleanup:
2094 mbedtls_free( sig_try );
2095 mbedtls_free( verif );
2096
2097 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002100
2101/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 * Do an RSA operation to sign the message digest
2103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002105 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002106 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002109 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002110 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 unsigned char *sig )
2112{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002113 RSA_VALIDATE_RET( ctx != NULL );
2114 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2115 mode == MBEDTLS_RSA_PUBLIC );
2116 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2117 hashlen == 0 ) ||
2118 hash != NULL );
2119 RSA_VALIDATE_RET( sig != NULL );
2120
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 switch( ctx->padding )
2122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123#if defined(MBEDTLS_PKCS1_V15)
2124 case MBEDTLS_RSA_PKCS_V15:
2125 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002126 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129#if defined(MBEDTLS_PKCS1_V21)
2130 case MBEDTLS_RSA_PKCS_V21:
2131 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002132 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002133#endif
2134
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002138}
2139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002141/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002142 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002145 int (*f_rng)(void *, unsigned char *, size_t),
2146 void *p_rng,
2147 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002149 unsigned int hashlen,
2150 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002152 int expected_salt_len,
2153 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002154{
Janos Follath24eed8d2019-11-22 13:21:35 +00002155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002156 size_t siglen;
2157 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002158 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002160 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002161 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002162 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 const mbedtls_md_info_t *md_info;
2164 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002165 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002166
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002167 RSA_VALIDATE_RET( ctx != NULL );
2168 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2169 mode == MBEDTLS_RSA_PUBLIC );
2170 RSA_VALIDATE_RET( sig != NULL );
2171 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2172 hashlen == 0 ) ||
2173 hash != NULL );
2174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002177
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 siglen = ctx->len;
2179
Paul Bakker27fdf462011-06-09 13:55:13 +00002180 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2184 ? mbedtls_rsa_public( ctx, sig, buf )
2185 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
2187 if( ret != 0 )
2188 return( ret );
2189
2190 p = buf;
2191
Paul Bakkerb3869132013-02-28 17:21:01 +01002192 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 {
Simon Butcher02037452016-03-01 21:19:12 +00002197 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002199 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002203 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002206 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002210
Paul Bakkerb3869132013-02-28 17:21:01 +01002211 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002212
Simon Butcher02037452016-03-01 21:19:12 +00002213 /*
2214 * Note: EMSA-PSS verification is over the length of N - 1 bits
2215 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002216 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002217
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002218 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2219 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2220
Simon Butcher02037452016-03-01 21:19:12 +00002221 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002222 if( msb % 8 == 0 )
2223 {
2224 p++;
2225 siglen -= 1;
2226 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002227
Gilles Peskine139108a2017-10-18 19:03:42 +02002228 if( siglen < hlen + 2 )
2229 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2230 hash_start = p + siglen - hlen - 1;
2231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002233 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002234 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002235
Jaeden Amero66954e12018-01-25 16:05:54 +00002236 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2237 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002238 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002239
Paul Bakkerb3869132013-02-28 17:21:01 +01002240 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002241
Gilles Peskine6a54b022017-10-17 19:02:13 +02002242 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002243 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002244
Gilles Peskine91048a32017-10-19 17:46:14 +02002245 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002246 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002247 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2248 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002249 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002250
Gilles Peskine6a54b022017-10-17 19:02:13 +02002251 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002254 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002255 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002256 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2257 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002258 }
2259
Simon Butcher02037452016-03-01 21:19:12 +00002260 /*
2261 * Generate H = Hash( M' )
2262 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002263 ret = mbedtls_md_starts( &md_ctx );
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_update( &md_ctx, zeros, 8 );
2267 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002268 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002269 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2270 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002271 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002272 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2273 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002274 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002275 ret = mbedtls_md_finish( &md_ctx, result );
2276 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002277 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002278
Jaeden Amero66954e12018-01-25 16:05:54 +00002279 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002280 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002281 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002282 goto exit;
2283 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002284
2285exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002287
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002288 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002289}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002290
2291/*
2292 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002295 int (*f_rng)(void *, unsigned char *, size_t),
2296 void *p_rng,
2297 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002299 unsigned int hashlen,
2300 const unsigned char *hash,
2301 const unsigned char *sig )
2302{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002303 mbedtls_md_type_t mgf1_hash_id;
2304 RSA_VALIDATE_RET( ctx != NULL );
2305 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2306 mode == MBEDTLS_RSA_PUBLIC );
2307 RSA_VALIDATE_RET( sig != NULL );
2308 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2309 hashlen == 0 ) ||
2310 hash != NULL );
2311
2312 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002314 : md_alg;
2315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002317 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002319 sig ) );
2320
2321}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002325/*
2326 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002329 int (*f_rng)(void *, unsigned char *, size_t),
2330 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002331 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002333 unsigned int hashlen,
2334 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002335 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002336{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002337 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002338 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002339 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002340
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002341 RSA_VALIDATE_RET( ctx != NULL );
2342 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2343 mode == MBEDTLS_RSA_PUBLIC );
2344 RSA_VALIDATE_RET( sig != NULL );
2345 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2346 hashlen == 0 ) ||
2347 hash != NULL );
2348
2349 sig_len = ctx->len;
2350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2352 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002353
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002354 /*
2355 * Prepare expected PKCS1 v1.5 encoding of hash.
2356 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002357
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002358 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2359 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2360 {
2361 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2362 goto cleanup;
2363 }
2364
2365 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2366 encoded_expected ) ) != 0 )
2367 goto cleanup;
2368
2369 /*
2370 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2371 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002374 ? mbedtls_rsa_public( ctx, sig, encoded )
2375 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002376 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002377 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002378
Simon Butcher02037452016-03-01 21:19:12 +00002379 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002380 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002381 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002382
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002383 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2384 sig_len ) ) != 0 )
2385 {
2386 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2387 goto cleanup;
2388 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002389
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002390cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002391
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002392 if( encoded != NULL )
2393 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002394 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002395 mbedtls_free( encoded );
2396 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002397
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002398 if( encoded_expected != NULL )
2399 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002400 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002401 mbedtls_free( encoded_expected );
2402 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002403
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002404 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002405}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002407
2408/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002409 * Do an RSA operation and check the message digest
2410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002412 int (*f_rng)(void *, unsigned char *, size_t),
2413 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002414 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002416 unsigned int hashlen,
2417 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002418 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002419{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002420 RSA_VALIDATE_RET( ctx != NULL );
2421 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2422 mode == MBEDTLS_RSA_PUBLIC );
2423 RSA_VALIDATE_RET( sig != NULL );
2424 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2425 hashlen == 0 ) ||
2426 hash != NULL );
2427
Paul Bakkerb3869132013-02-28 17:21:01 +01002428 switch( ctx->padding )
2429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430#if defined(MBEDTLS_PKCS1_V15)
2431 case MBEDTLS_RSA_PKCS_V15:
2432 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002433 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002434#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#if defined(MBEDTLS_PKCS1_V21)
2437 case MBEDTLS_RSA_PKCS_V21:
2438 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002439 hashlen, hash, sig );
2440#endif
2441
2442 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002444 }
2445}
2446
2447/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002448 * Copy the components of an RSA key
2449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002451{
Janos Follath24eed8d2019-11-22 13:21:35 +00002452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002453 RSA_VALIDATE_RET( dst != NULL );
2454 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002455
2456 dst->ver = src->ver;
2457 dst->len = src->len;
2458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2460 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2463 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2464 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002465
2466#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2468 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2469 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2471 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002472#endif
2473
2474 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2477 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002478
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002479 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002480 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002481
2482cleanup:
2483 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002485
2486 return( ret );
2487}
2488
2489/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 * Free the components of an RSA key
2491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002493{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002494 if( ctx == NULL )
2495 return;
2496
2497 mbedtls_mpi_free( &ctx->Vi );
2498 mbedtls_mpi_free( &ctx->Vf );
2499 mbedtls_mpi_free( &ctx->RN );
2500 mbedtls_mpi_free( &ctx->D );
2501 mbedtls_mpi_free( &ctx->Q );
2502 mbedtls_mpi_free( &ctx->P );
2503 mbedtls_mpi_free( &ctx->E );
2504 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002505
Hanno Becker33c30a02017-08-23 07:00:22 +01002506#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002507 mbedtls_mpi_free( &ctx->RQ );
2508 mbedtls_mpi_free( &ctx->RP );
2509 mbedtls_mpi_free( &ctx->QP );
2510 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002511 mbedtls_mpi_free( &ctx->DP );
2512#endif /* MBEDTLS_RSA_NO_CRT */
2513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#if defined(MBEDTLS_THREADING_C)
2515 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002516#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002517}
2518
Hanno Beckerab377312017-08-23 16:24:51 +01002519#endif /* !MBEDTLS_RSA_ALT */
2520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002523#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
2525/*
2526 * Example RSA-1024 keypair, for test purposes
2527 */
2528#define KEY_LEN 128
2529
2530#define RSA_N "9292758453063D803DD603D5E777D788" \
2531 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2532 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2533 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2534 "93A89813FBF3C4F8066D2D800F7C38A8" \
2535 "1AE31942917403FF4946B0A83D3D3E05" \
2536 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2537 "5E94BB77B07507233A0BC7BAC8F90F79"
2538
2539#define RSA_E "10001"
2540
2541#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2542 "66CA472BC44D253102F8B4A9D3BFA750" \
2543 "91386C0077937FE33FA3252D28855837" \
2544 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2545 "DF79C5CE07EE72C7F123142198164234" \
2546 "CABB724CF78B8173B9F880FC86322407" \
2547 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2548 "071513A1E85B5DFA031F21ECAE91A34D"
2549
2550#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2551 "2C01CAD19EA484A87EA4377637E75500" \
2552 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2553 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2554
2555#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2556 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2557 "910E4168387E3C30AA1E00C339A79508" \
2558 "8452DD96A9A5EA5D9DCA68DA636032AF"
2559
Paul Bakker5121ce52009-01-03 21:22:43 +00002560#define PT_LEN 24
2561#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2562 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002565static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002566{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002567#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002568 size_t i;
2569
Paul Bakker545570e2010-07-18 09:00:25 +00002570 if( rng_state != NULL )
2571 rng_state = NULL;
2572
Paul Bakkera3d195c2011-11-27 21:07:34 +00002573 for( i = 0; i < len; ++i )
2574 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002575#else
2576 if( rng_state != NULL )
2577 rng_state = NULL;
2578
2579 arc4random_buf( output, len );
2580#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002581
Paul Bakkera3d195c2011-11-27 21:07:34 +00002582 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002585
Paul Bakker5121ce52009-01-03 21:22:43 +00002586/*
2587 * Checkup routine
2588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002590{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002591 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002593 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 unsigned char rsa_plaintext[PT_LEN];
2596 unsigned char rsa_decrypted[PT_LEN];
2597 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002599 unsigned char sha1sum[20];
2600#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Hanno Becker3a701162017-08-22 13:52:43 +01002602 mbedtls_mpi K;
2603
2604 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
Hanno Becker3a701162017-08-22 13:52:43 +01002607 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2608 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2609 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2610 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2611 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2612 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2613 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2614 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2615 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2616 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2617
Hanno Becker7f25f852017-10-10 16:56:22 +01002618 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
2620 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2624 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 {
2626 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Hanno Becker5bc87292017-05-03 15:09:31 +01002629 ret = 1;
2630 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002631 }
2632
2633 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
2636 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2637
Hanno Becker98838b02017-10-02 13:16:10 +01002638 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2639 PT_LEN, rsa_plaintext,
2640 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 {
2642 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
Hanno Becker5bc87292017-05-03 15:09:31 +01002645 ret = 1;
2646 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 }
2648
2649 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Hanno Becker98838b02017-10-02 13:16:10 +01002652 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2653 &len, rsa_ciphertext, rsa_decrypted,
2654 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 {
2656 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Hanno Becker5bc87292017-05-03 15:09:31 +01002659 ret = 1;
2660 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 }
2662
2663 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2664 {
2665 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
Hanno Becker5bc87292017-05-03 15:09:31 +01002668 ret = 1;
2669 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 }
2671
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002672 if( verbose != 0 )
2673 mbedtls_printf( "passed\n" );
2674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002677 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002679 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002680 {
2681 if( verbose != 0 )
2682 mbedtls_printf( "failed\n" );
2683
2684 return( 1 );
2685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Hanno Becker98838b02017-10-02 13:16:10 +01002687 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2688 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2689 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 {
2691 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Hanno Becker5bc87292017-05-03 15:09:31 +01002694 ret = 1;
2695 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 }
2697
2698 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Hanno Becker98838b02017-10-02 13:16:10 +01002701 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2702 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2703 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 {
2705 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Hanno Becker5bc87292017-05-03 15:09:31 +01002708 ret = 1;
2709 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 }
2711
2712 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002713 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002716 if( verbose != 0 )
2717 mbedtls_printf( "\n" );
2718
Paul Bakker3d8fb632014-04-17 12:42:41 +02002719cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002720 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 mbedtls_rsa_free( &rsa );
2722#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002723 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002725 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726}
2727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730#endif /* MBEDTLS_RSA_C */