blob: 1f53708b2246124a8999b7cc343da1c736415986 [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;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500252 int have_N, have_P, have_Q, have_D, have_E;
253#if !defined(MBEDTLS_RSA_NO_CRT)
254 int have_DP, have_DQ, have_QP;
255#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500256 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100257
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500258 RSA_VALIDATE_RET( ctx != NULL );
259
260 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
261 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
262 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
263 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
264 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500265
266#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500267 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
268 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
269 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500270#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100271
Hanno Becker617c1ae2017-08-23 14:11:24 +0100272 /*
273 * Check whether provided parameters are enough
274 * to deduce all others. The following incomplete
275 * parameter sets for private keys are supported:
276 *
277 * (1) P, Q missing.
278 * (2) D and potentially N missing.
279 *
280 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100281
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 n_missing = have_P && have_Q && have_D && have_E;
283 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
284 d_missing = have_P && have_Q && !have_D && have_E;
285 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100286
287 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100289
Hanno Becker617c1ae2017-08-23 14:11:24 +0100290 if( !is_priv && !is_pub )
291 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
292
293 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100294 * Step 1: Deduce N if P, Q are provided.
295 */
296
297 if( !have_N && have_P && have_Q )
298 {
299 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
300 &ctx->Q ) ) != 0 )
301 {
302 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
303 }
304
305 ctx->len = mbedtls_mpi_size( &ctx->N );
306 }
307
308 /*
309 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100310 */
311
312 if( pq_missing )
313 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100314 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100315 &ctx->P, &ctx->Q );
316 if( ret != 0 )
317 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
318
319 }
320 else if( d_missing )
321 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100322 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
323 &ctx->Q,
324 &ctx->E,
325 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100326 {
327 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
328 }
329 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100330
Hanno Becker617c1ae2017-08-23 14:11:24 +0100331 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100332 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100333 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100334 */
335
Hanno Becker23344b52017-08-23 07:43:27 +0100336#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500337 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100338 {
339 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
340 &ctx->DP, &ctx->DQ, &ctx->QP );
341 if( ret != 0 )
342 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
343 }
Hanno Becker23344b52017-08-23 07:43:27 +0100344#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100345
346 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100347 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100348 */
349
Hanno Beckerebd2c022017-10-12 10:54:53 +0100350 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100351}
352
Hanno Becker617c1ae2017-08-23 14:11:24 +0100353int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
354 unsigned char *N, size_t N_len,
355 unsigned char *P, size_t P_len,
356 unsigned char *Q, size_t Q_len,
357 unsigned char *D, size_t D_len,
358 unsigned char *E, size_t E_len )
359{
360 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500361 int is_priv;
362 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100363
364 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500365 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100366 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
367 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
368 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
369 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
370 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
371
372 if( !is_priv )
373 {
374 /* If we're trying to export private parameters for a public key,
375 * something must be wrong. */
376 if( P != NULL || Q != NULL || D != NULL )
377 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
378
379 }
380
381 if( N != NULL )
382 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
383
384 if( P != NULL )
385 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
386
387 if( Q != NULL )
388 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
389
390 if( D != NULL )
391 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
392
393 if( E != NULL )
394 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100395
396cleanup:
397
398 return( ret );
399}
400
Hanno Becker617c1ae2017-08-23 14:11:24 +0100401int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
402 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
403 mbedtls_mpi *D, mbedtls_mpi *E )
404{
Janos Follath24eed8d2019-11-22 13:21:35 +0000405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500406 int is_priv;
407 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100408
409 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500410 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100411 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
412 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
413 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
414 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
415 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
416
417 if( !is_priv )
418 {
419 /* If we're trying to export private parameters for a public key,
420 * something must be wrong. */
421 if( P != NULL || Q != NULL || D != NULL )
422 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
423
424 }
425
426 /* Export all requested core parameters. */
427
428 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
429 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
430 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
431 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
432 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
433 {
434 return( ret );
435 }
436
437 return( 0 );
438}
439
440/*
441 * Export CRT parameters
442 * This must also be implemented if CRT is not used, for being able to
443 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
444 * can be used in this case.
445 */
446int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
447 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
448{
Janos Follath24eed8d2019-11-22 13:21:35 +0000449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500450 int is_priv;
451 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100452
453 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500454 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100455 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
456 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
457 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
458 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
459 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
460
461 if( !is_priv )
462 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
463
Hanno Beckerdc95c892017-08-23 06:57:02 +0100464#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100465 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100466 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
467 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
468 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
469 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100470 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100471 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100472#else
473 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
474 DP, DQ, QP ) ) != 0 )
475 {
476 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
477 }
478#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100479
480 return( 0 );
481}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100482
Paul Bakker5121ce52009-01-03 21:22:43 +0000483/*
484 * Initialize an RSA context
485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000488 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000489{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500490 RSA_VALIDATE( ctx != NULL );
491 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
492 padding == MBEDTLS_RSA_PKCS_V21 );
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#if defined(MBEDTLS_THREADING_C)
499 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200500#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000501}
502
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100503/*
504 * Set padding for an existing RSA context
505 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500506void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
507 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100508{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500509 RSA_VALIDATE( ctx != NULL );
510 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
511 padding == MBEDTLS_RSA_PKCS_V21 );
512
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100513 ctx->padding = padding;
514 ctx->hash_id = hash_id;
515}
516
Hanno Becker617c1ae2017-08-23 14:11:24 +0100517/*
518 * Get length in bytes of RSA modulus
519 */
520
521size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
522{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100523 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100524}
525
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529/*
530 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800531 *
532 * This generation method follows the RSA key pair generation procedure of
533 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000536 int (*f_rng)(void *, unsigned char *, size_t),
537 void *p_rng,
538 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000539{
Janos Follath24eed8d2019-11-22 13:21:35 +0000540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800541 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100542 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500543 RSA_VALIDATE_RET( ctx != NULL );
544 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500546 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
Janos Follathef441782016-09-21 13:18:12 +0100547 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
548
Janos Follathb8fc1b02018-09-03 15:37:01 +0100549 /*
550 * If the modulus is 1024 bit long or shorter, then the security strength of
551 * the RSA algorithm is less than or equal to 80 bits and therefore an error
552 * rate of 2^-80 is sufficient.
553 */
554 if( nbits > 1024 )
555 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
556
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100557 mbedtls_mpi_init( &H );
558 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800559 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
561 /*
562 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800563 * 1. |P-Q| > 2^( nbits / 2 - 100 )
564 * 2. GCD( E, (P-1)*(Q-1) ) == 1
565 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 do
570 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100571 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
572 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Janos Follathb8fc1b02018-09-03 15:37:01 +0100574 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
575 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800577 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
578 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
579 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 continue;
581
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800582 /* not required by any standards, but some users rely on the fact that P > Q */
583 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100584 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100585
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100586 /* Temporarily replace P,Q by P-1, Q-1 */
587 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
588 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
589 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800590
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800591 /* 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 +0200592 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800593 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
594 continue;
595
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800596 /* 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 -0800597 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
598 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
599 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
600
601 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
602 continue;
603
604 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800606 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100608 /* Restore P,Q */
609 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
610 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
611
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800612 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
613
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100614 ctx->len = mbedtls_mpi_size( &ctx->N );
615
Jethro Beekman97f95c92018-02-13 15:50:36 -0800616#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 * DP = D mod (P - 1)
619 * DQ = D mod (Q - 1)
620 * QP = Q^-1 mod P
621 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100622 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
623 &ctx->DP, &ctx->DQ, &ctx->QP ) );
624#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
Hanno Becker83aad1f2017-08-23 06:45:10 +0100626 /* Double-check */
627 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
629cleanup:
630
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100631 mbedtls_mpi_free( &H );
632 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800633 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
635 if( ret != 0 )
636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_rsa_free( ctx );
638 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 }
640
Paul Bakker48377d92013-08-30 12:06:24 +0200641 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642}
643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
646/*
647 * Check a public RSA key
648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000650{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500651 RSA_VALIDATE_RET( ctx != NULL );
652
Hanno Beckerebd2c022017-10-12 10:54:53 +0100653 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000655
Hanno Becker3a760a12018-01-05 08:14:49 +0000656 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100659 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Hanno Becker705fc682017-10-10 17:57:02 +0100661 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
662 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100666 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
668 return( 0 );
669}
670
671/*
Hanno Becker705fc682017-10-10 17:57:02 +0100672 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000675{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500676 RSA_VALIDATE_RET( ctx != NULL );
677
Hanno Becker705fc682017-10-10 17:57:02 +0100678 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100679 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 {
Hanno Becker98838b02017-10-02 13:16:10 +0100681 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 }
Paul Bakker48377d92013-08-30 12:06:24 +0200683
Hanno Becker98838b02017-10-02 13:16:10 +0100684 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100685 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100687 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000689
Hanno Beckerb269a852017-08-25 08:03:21 +0100690#if !defined(MBEDTLS_RSA_NO_CRT)
691 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
692 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
693 {
694 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
695 }
696#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000697
698 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000699}
700
701/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100702 * Check if contexts holding a public and private key match
703 */
Hanno Becker98838b02017-10-02 13:16:10 +0100704int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
705 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100706{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500707 RSA_VALIDATE_RET( pub != NULL );
708 RSA_VALIDATE_RET( prv != NULL );
709
Hanno Becker98838b02017-10-02 13:16:10 +0100710 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100714 }
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
717 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100720 }
721
722 return( 0 );
723}
724
725/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 * Do an RSA public key operation
727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000729 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000730 unsigned char *output )
731{
Janos Follath24eed8d2019-11-22 13:21:35 +0000732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000733 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500735 RSA_VALIDATE_RET( ctx != NULL );
736 RSA_VALIDATE_RET( input != NULL );
737 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000738
Hanno Beckerebd2c022017-10-12 10:54:53 +0100739 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100740 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200744#if defined(MBEDTLS_THREADING_C)
745 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
746 return( ret );
747#endif
748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000752 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200753 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
754 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000755 }
756
757 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
759 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
761cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200763 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
764 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100765#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
769 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
772 return( 0 );
773}
774
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200775/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200776 * Generate or update blinding values, see section 10 of:
777 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200778 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200779 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200780 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200781static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200782 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
783{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200784 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200785 mbedtls_mpi R;
786
787 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200788
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200789 if( ctx->Vf.p != NULL )
790 {
791 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
793 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
794 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
795 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200796
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200797 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200798 }
799
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200800 /* Unblinding value: Vf = random number, invertible mod N */
801 do {
802 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200803 {
804 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
805 goto cleanup;
806 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200809
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200810 /* Compute the Vf^1 as R * (R Vf)^-1 to avoid leaks from inv_mod.
811 * There's a negligible but non-zero probability that R is not
812 * invertible mod N, in that case we'd just loop one more time,
813 * just as if Vf itself wasn't invertible - no need to distinguish. */
814 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
815 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
816 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
817
818 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
819 if( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
820 continue;
821 if( ret != 0 )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200822 goto cleanup;
823
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200824 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
825 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
826 } while( 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200827
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200828 /* Blinding value: Vi = Vf^(-e) mod N
829 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 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 +0200831
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200832
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200833cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200834 mbedtls_mpi_free( &R );
835
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200836 return( ret );
837}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200838
Paul Bakker5121ce52009-01-03 21:22:43 +0000839/*
Janos Follathe81102e2017-03-22 13:38:28 +0000840 * Exponent blinding supposed to prevent side-channel attacks using multiple
841 * traces of measurements to recover the RSA key. The more collisions are there,
842 * the more bits of the key can be recovered. See [3].
843 *
844 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
845 * observations on avarage.
846 *
847 * For example with 28 byte blinding to achieve 2 collisions the adversary has
848 * to make 2^112 observations on avarage.
849 *
850 * (With the currently (as of 2017 April) known best algorithms breaking 2048
851 * bit RSA requires approximately as much time as trying out 2^112 random keys.
852 * Thus in this sense with 28 byte blinding the security is not reduced by
853 * side-channel attacks like the one in [3])
854 *
855 * This countermeasure does not help if the key recovery is possible with a
856 * single trace.
857 */
858#define RSA_EXPONENT_BLINDING 28
859
860/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 * Do an RSA private key operation
862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200864 int (*f_rng)(void *, unsigned char *, size_t),
865 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000866 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 unsigned char *output )
868{
Janos Follath24eed8d2019-11-22 13:21:35 +0000869 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000870 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100871
872 /* Temporary holding the result */
873 mbedtls_mpi T;
874
875 /* Temporaries holding P-1, Q-1 and the
876 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000877 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100878
879#if !defined(MBEDTLS_RSA_NO_CRT)
880 /* Temporaries holding the results mod p resp. mod q. */
881 mbedtls_mpi TP, TQ;
882
883 /* Temporaries holding the blinded exponents for
884 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000885 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100886
887 /* Pointers to actual exponents to be used - either the unblinded
888 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000889 mbedtls_mpi *DP = &ctx->DP;
890 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100891#else
892 /* Temporary holding the blinded exponent (if used). */
893 mbedtls_mpi D_blind;
894
895 /* Pointer to actual exponent to be used - either the unblinded
896 * or the blinded one, depending on the presence of a PRNG. */
897 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100898#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100899
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100900 /* Temporaries holding the initial input and the double
901 * checked result; should be the same in the end. */
902 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000903
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500904 RSA_VALIDATE_RET( ctx != NULL );
905 RSA_VALIDATE_RET( input != NULL );
906 RSA_VALIDATE_RET( output != NULL );
907
Hanno Beckerebd2c022017-10-12 10:54:53 +0100908 if( rsa_check_context( ctx, 1 /* private key checks */,
909 f_rng != NULL /* blinding y/n */ ) != 0 )
910 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100911 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100912 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100913
Hanno Becker06811ce2017-05-03 15:10:34 +0100914#if defined(MBEDTLS_THREADING_C)
915 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
916 return( ret );
917#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000918
Hanno Becker06811ce2017-05-03 15:10:34 +0100919 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100920 mbedtls_mpi_init( &T );
921
922 mbedtls_mpi_init( &P1 );
923 mbedtls_mpi_init( &Q1 );
924 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000925
Janos Follathf9203b42017-03-22 15:13:15 +0000926 if( f_rng != NULL )
927 {
Janos Follathe81102e2017-03-22 13:38:28 +0000928#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000929 mbedtls_mpi_init( &D_blind );
930#else
931 mbedtls_mpi_init( &DP_blind );
932 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000933#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000934 }
Janos Follathe81102e2017-03-22 13:38:28 +0000935
Hanno Becker06811ce2017-05-03 15:10:34 +0100936#if !defined(MBEDTLS_RSA_NO_CRT)
937 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200938#endif
939
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100940 mbedtls_mpi_init( &I );
941 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100942
943 /* End of MPI initialization */
944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
946 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200948 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
949 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 }
951
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100952 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100953
Paul Bakkerf451bac2013-08-30 15:37:02 +0200954 if( f_rng != NULL )
955 {
956 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200957 * Blinding
958 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200959 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200960 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
961 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000963
Janos Follathe81102e2017-03-22 13:38:28 +0000964 /*
965 * Exponent blinding
966 */
967 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
968 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
969
Janos Follathf9203b42017-03-22 15:13:15 +0000970#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000971 /*
972 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
973 */
974 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
975 f_rng, p_rng ) );
976 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
977 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
978 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
979
980 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000981#else
982 /*
983 * DP_blind = ( P - 1 ) * R + DP
984 */
985 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
986 f_rng, p_rng ) );
987 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
988 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
989 &ctx->DP ) );
990
991 DP = &DP_blind;
992
993 /*
994 * DQ_blind = ( Q - 1 ) * R + DQ
995 */
996 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
997 f_rng, p_rng ) );
998 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
999 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1000 &ctx->DQ ) );
1001
1002 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001003#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001004 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001007 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001008#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001009 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001010 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001012 * TP = input ^ dP mod P
1013 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001015
1016 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1017 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
1019 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001020 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001021 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001022 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1023 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1024 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
1026 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001027 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001029 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1030 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001032
Paul Bakkerf451bac2013-08-30 15:37:02 +02001033 if( f_rng != NULL )
1034 {
1035 /*
1036 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001037 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001038 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001039 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001041 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
Hanno Becker2dec5e82017-10-03 07:49:52 +01001043 /* Verify the result to prevent glitching attacks. */
1044 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1045 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001046 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001047 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001048 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1049 goto cleanup;
1050 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001051
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
1055cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001057 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1058 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001059#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001060
Hanno Becker06811ce2017-05-03 15:10:34 +01001061 mbedtls_mpi_free( &P1 );
1062 mbedtls_mpi_free( &Q1 );
1063 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001064
1065 if( f_rng != NULL )
1066 {
Janos Follathe81102e2017-03-22 13:38:28 +00001067#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001068 mbedtls_mpi_free( &D_blind );
1069#else
1070 mbedtls_mpi_free( &DP_blind );
1071 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001072#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001073 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Hanno Becker06811ce2017-05-03 15:10:34 +01001075 mbedtls_mpi_free( &T );
1076
1077#if !defined(MBEDTLS_RSA_NO_CRT)
1078 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1079#endif
1080
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001081 mbedtls_mpi_free( &C );
1082 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001083
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
1087 return( 0 );
1088}
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001091/**
1092 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1093 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001094 * \param dst buffer to mask
1095 * \param dlen length of destination buffer
1096 * \param src source of the mask generation
1097 * \param slen length of the source buffer
1098 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001099 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001100static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104 unsigned char counter[4];
1105 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001106 unsigned int hlen;
1107 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001108 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001111 memset( counter, 0, 4 );
1112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114
Simon Butcher02037452016-03-01 21:19:12 +00001115 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001116 p = dst;
1117
1118 while( dlen > 0 )
1119 {
1120 use_len = hlen;
1121 if( dlen < hlen )
1122 use_len = dlen;
1123
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001124 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1125 goto exit;
1126 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1127 goto exit;
1128 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1129 goto exit;
1130 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1131 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001132
1133 for( i = 0; i < use_len; ++i )
1134 *p++ ^= mask[i];
1135
1136 counter[3]++;
1137
1138 dlen -= use_len;
1139 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001140
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001141exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001142 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001143
1144 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001149/*
1150 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001153 int (*f_rng)(void *, unsigned char *, size_t),
1154 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001155 int mode,
1156 const unsigned char *label, size_t label_len,
1157 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 const unsigned char *input,
1159 unsigned char *output )
1160{
1161 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001163 unsigned char *p = output;
1164 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 const mbedtls_md_info_t *md_info;
1166 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001167
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001168 RSA_VALIDATE_RET( ctx != NULL );
1169 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1170 mode == MBEDTLS_RSA_PUBLIC );
1171 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001172 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001173 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1176 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001177
1178 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001182 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001184
1185 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001187
Simon Butcher02037452016-03-01 21:19:12 +00001188 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001189 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001191
1192 memset( output, 0, olen );
1193
1194 *p++ = 0;
1195
Simon Butcher02037452016-03-01 21:19:12 +00001196 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001197 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001199
1200 p += hlen;
1201
Simon Butcher02037452016-03-01 21:19:12 +00001202 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001203 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1204 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001205 p += hlen;
1206 p += olen - 2 * hlen - 2 - ilen;
1207 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001208 if( ilen != 0 )
1209 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001212 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001213 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001214
Simon Butcher02037452016-03-01 21:19:12 +00001215 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001216 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1217 &md_ctx ) ) != 0 )
1218 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001219
Simon Butcher02037452016-03-01 21:19:12 +00001220 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001221 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1222 &md_ctx ) ) != 0 )
1223 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001224
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001225exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001227
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001228 if( ret != 0 )
1229 return( ret );
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 return( ( mode == MBEDTLS_RSA_PUBLIC )
1232 ? mbedtls_rsa_public( ctx, output, output )
1233 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001238/*
1239 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001242 int (*f_rng)(void *, unsigned char *, size_t),
1243 void *p_rng,
1244 int mode, size_t ilen,
1245 const unsigned char *input,
1246 unsigned char *output )
1247{
1248 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001250 unsigned char *p = output;
1251
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001252 RSA_VALIDATE_RET( ctx != NULL );
1253 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1254 mode == MBEDTLS_RSA_PUBLIC );
1255 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001256 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001257
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001258 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001260
1261 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001262
Simon Butcher02037452016-03-01 21:19:12 +00001263 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001264 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001266
1267 nb_pad = olen - 3 - ilen;
1268
1269 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001271 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001272 if( f_rng == NULL )
1273 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001276
1277 while( nb_pad-- > 0 )
1278 {
1279 int rng_dl = 100;
1280
1281 do {
1282 ret = f_rng( p_rng, p, 1 );
1283 } while( *p == 0 && --rng_dl && ret == 0 );
1284
Simon Butcher02037452016-03-01 21:19:12 +00001285 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001286 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001288
1289 p++;
1290 }
1291 }
1292 else
1293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001295
1296 while( nb_pad-- > 0 )
1297 *p++ = 0xFF;
1298 }
1299
1300 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001301 if( ilen != 0 )
1302 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 return( ( mode == MBEDTLS_RSA_PUBLIC )
1305 ? mbedtls_rsa_public( ctx, output, output )
1306 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001309
Paul Bakker5121ce52009-01-03 21:22:43 +00001310/*
1311 * Add the message padding, then do an RSA operation
1312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001314 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001315 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001316 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001317 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 unsigned char *output )
1319{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001320 RSA_VALIDATE_RET( ctx != NULL );
1321 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1322 mode == MBEDTLS_RSA_PUBLIC );
1323 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001324 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001325
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 switch( ctx->padding )
1327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_PKCS1_V15)
1329 case MBEDTLS_RSA_PKCS_V15:
1330 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001331 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001332#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#if defined(MBEDTLS_PKCS1_V21)
1335 case MBEDTLS_RSA_PKCS_V21:
1336 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001337 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001338#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
1340 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001343}
1344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001346/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001347 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001350 int (*f_rng)(void *, unsigned char *, size_t),
1351 void *p_rng,
1352 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001353 const unsigned char *label, size_t label_len,
1354 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001355 const unsigned char *input,
1356 unsigned char *output,
1357 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001358{
Janos Follath24eed8d2019-11-22 13:21:35 +00001359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001360 size_t ilen, i, pad_len;
1361 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1363 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001364 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 const mbedtls_md_info_t *md_info;
1366 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001367
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001368 RSA_VALIDATE_RET( ctx != NULL );
1369 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1370 mode == MBEDTLS_RSA_PUBLIC );
1371 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1372 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1373 RSA_VALIDATE_RET( input != NULL );
1374 RSA_VALIDATE_RET( olen != NULL );
1375
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001376 /*
1377 * Parameters sanity checks
1378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1380 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
1382 ilen = ctx->len;
1383
Paul Bakker27fdf462011-06-09 13:55:13 +00001384 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001388 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001390
Janos Follathc17cda12016-02-11 11:08:18 +00001391 hlen = mbedtls_md_get_size( md_info );
1392
1393 // checking for integer underflow
1394 if( 2 * hlen + 2 > ilen )
1395 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1396
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001397 /*
1398 * RSA operation
1399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1401 ? mbedtls_rsa_public( ctx, input, buf )
1402 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
1404 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001405 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001407 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001408 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001411 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1412 {
1413 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001414 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001415 }
1416
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001417 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001418 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1419 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001420 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001421 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1422 &md_ctx ) ) != 0 )
1423 {
1424 mbedtls_md_free( &md_ctx );
1425 goto cleanup;
1426 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001429
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001430 /* Generate lHash */
1431 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1432 goto cleanup;
1433
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001434 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001435 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001436 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001438 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001439
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001440 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001441
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001442 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001443
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001444 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001445 for( i = 0; i < hlen; i++ )
1446 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001447
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001448 /* Get zero-padding len, but always read till end of buffer
1449 * (minus one, for the 01 byte) */
1450 pad_len = 0;
1451 pad_done = 0;
1452 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1453 {
1454 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001455 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001456 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001457
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001458 p += pad_len;
1459 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001460
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001461 /*
1462 * The only information "leaked" is whether the padding was correct or not
1463 * (eg, no data is copied if it was not correct). This meets the
1464 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1465 * the different error conditions.
1466 */
1467 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001468 {
1469 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1470 goto cleanup;
1471 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001472
Paul Bakker66d5d072014-06-17 16:39:18 +02001473 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001474 {
1475 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1476 goto cleanup;
1477 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001478
1479 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001480 if( *olen != 0 )
1481 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001482 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001483
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001484cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001485 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1486 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001487
1488 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001493/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1494 *
1495 * \param value The value to analyze.
1496 * \return Zero if \p value is zero, otherwise all-bits-one.
1497 */
1498static unsigned all_or_nothing_int( unsigned value )
1499{
1500 /* MSVC has a warning about unary minus on unsigned, but this is
1501 * well-defined and precisely what we want to do here */
1502#if defined(_MSC_VER)
1503#pragma warning( push )
1504#pragma warning( disable : 4146 )
1505#endif
1506 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1507#if defined(_MSC_VER)
1508#pragma warning( pop )
1509#endif
1510}
1511
1512/** Check whether a size is out of bounds, without branches.
1513 *
1514 * This is equivalent to `size > max`, but is likely to be compiled to
1515 * to code using bitwise operation rather than a branch.
1516 *
1517 * \param size Size to check.
1518 * \param max Maximum desired value for \p size.
1519 * \return \c 0 if `size <= max`.
1520 * \return \c 1 if `size > max`.
1521 */
1522static unsigned size_greater_than( size_t size, size_t max )
1523{
1524 /* Return the sign bit (1 for negative) of (max - size). */
1525 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1526}
1527
1528/** Choose between two integer values, without branches.
1529 *
1530 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1531 * to code using bitwise operation rather than a branch.
1532 *
1533 * \param cond Condition to test.
1534 * \param if1 Value to use if \p cond is nonzero.
1535 * \param if0 Value to use if \p cond is zero.
1536 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1537 */
1538static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
1539{
1540 unsigned mask = all_or_nothing_int( cond );
1541 return( ( mask & if1 ) | (~mask & if0 ) );
1542}
1543
1544/** Shift some data towards the left inside a buffer without leaking
1545 * the length of the data through side channels.
1546 *
1547 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1548 * ```
1549 * memmove(start, start + offset, total - offset);
1550 * memset(start + offset, 0, total - offset);
1551 * ```
1552 * but it strives to use a memory access pattern (and thus total timing)
1553 * that does not depend on \p offset. This timing independence comes at
1554 * the expense of performance.
1555 *
1556 * \param start Pointer to the start of the buffer.
1557 * \param total Total size of the buffer.
1558 * \param offset Offset from which to copy \p total - \p offset bytes.
1559 */
1560static void mem_move_to_left( void *start,
1561 size_t total,
1562 size_t offset )
1563{
1564 volatile unsigned char *buf = start;
1565 size_t i, n;
1566 if( total == 0 )
1567 return;
1568 for( i = 0; i < total; i++ )
1569 {
1570 unsigned no_op = size_greater_than( total - offset, i );
1571 /* The first `total - offset` passes are a no-op. The last
1572 * `offset` passes shift the data one byte to the left and
1573 * zero out the last byte. */
1574 for( n = 0; n < total - 1; n++ )
1575 {
1576 unsigned char current = buf[n];
1577 unsigned char next = buf[n+1];
1578 buf[n] = if_int( no_op, current, next );
1579 }
1580 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1581 }
1582}
1583
Paul Bakkerb3869132013-02-28 17:21:01 +01001584/*
1585 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001588 int (*f_rng)(void *, unsigned char *, size_t),
1589 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001590 int mode, size_t *olen,
1591 const unsigned char *input,
1592 unsigned char *output,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001593 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001594{
Janos Follath24eed8d2019-11-22 13:21:35 +00001595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001596 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001598 /* The following variables take sensitive values: their value must
1599 * not leak into the observable behavior of the function other than
1600 * the designated outputs (output, olen, return value). Otherwise
1601 * this would open the execution of the function to
1602 * side-channel-based variants of the Bleichenbacher padding oracle
1603 * attack. Potential side channels include overall timing, memory
1604 * access patterns (especially visible to an adversary who has access
1605 * to a shared memory cache), and branches (especially visible to
1606 * an adversary who has access to a shared code cache or to a shared
1607 * branch predictor). */
1608 size_t pad_count = 0;
1609 unsigned bad = 0;
1610 unsigned char pad_done = 0;
1611 size_t plaintext_size = 0;
1612 unsigned output_too_large;
1613
1614 RSA_VALIDATE_RET( ctx != NULL );
1615 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1616 mode == MBEDTLS_RSA_PUBLIC );
1617 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1618 RSA_VALIDATE_RET( input != NULL );
1619 RSA_VALIDATE_RET( olen != NULL );
1620
1621 ilen = ctx->len;
1622 plaintext_max_size = ( output_max_len > ilen - 11 ?
1623 ilen - 11 :
1624 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1627 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001628
Paul Bakkerb3869132013-02-28 17:21:01 +01001629 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1633 ? mbedtls_rsa_public( ctx, input, buf )
1634 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001635
1636 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001637 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001638
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001639 /* Check and get padding length in constant time and constant
1640 * memory trace. The first byte must be 0. */
1641 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001644 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001645 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1646 * where PS must be at least 8 nonzero bytes. */
1647 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001648
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001649 /* Read the whole buffer. Set pad_done to nonzero if we find
1650 * the 0x00 byte and remember the padding length in pad_count. */
1651 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001652 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001653 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001654 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001655 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001656 }
1657 else
1658 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001659 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1660 * where PS must be at least 8 bytes with the value 0xFF. */
1661 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001662
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001663 /* Read the whole buffer. Set pad_done to nonzero if we find
1664 * the 0x00 byte and remember the padding length in pad_count.
1665 * If there's a non-0xff byte in the padding, the padding is bad. */
1666 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001667 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001668 pad_done |= if_int( buf[i], 0, 1 );
1669 pad_count += if_int( pad_done, 0, 1 );
1670 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001671 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001672 }
1673
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001674 /* If pad_done is still zero, there's no data, only unfinished padding. */
1675 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001676
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001677 /* There must be at least 8 bytes of padding. */
1678 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001679
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001680 /* If the padding is valid, set plaintext_size to the number of
1681 * remaining bytes after stripping the padding. If the padding
1682 * is invalid, avoid leaking this fact through the size of the
1683 * output: use the maximum message size that fits in the output
1684 * buffer. Do it without branches to avoid leaking the padding
1685 * validity through timing. RSA keys are small enough that all the
1686 * size_t values involved fit in unsigned int. */
1687 plaintext_size = if_int( bad,
1688 (unsigned) plaintext_max_size,
1689 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001690
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001691 /* Set output_too_large to 0 if the plaintext fits in the output
1692 * buffer and to 1 otherwise. */
1693 output_too_large = size_greater_than( plaintext_size,
1694 plaintext_max_size );
1695
1696 /* Set ret without branches to avoid timing attacks. Return:
1697 * - INVALID_PADDING if the padding is bad (bad != 0).
1698 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1699 * plaintext does not fit in the output buffer.
1700 * - 0 if the padding is correct. */
1701 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1702 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1703 0 ) );
1704
1705 /* If the padding is bad or the plaintext is too large, zero the
1706 * data that we're about to copy to the output buffer.
1707 * We need to copy the same amount of data
1708 * from the same buffer whether the padding is good or not to
1709 * avoid leaking the padding validity through overall timing or
1710 * through memory or cache access patterns. */
1711 bad = all_or_nothing_int( bad | output_too_large );
1712 for( i = 11; i < ilen; i++ )
1713 buf[i] &= ~bad;
1714
1715 /* If the plaintext is too large, truncate it to the buffer size.
1716 * Copy anyway to avoid revealing the length through timing, because
1717 * revealing the length is as bad as revealing the padding validity
1718 * for a Bleichenbacher attack. */
1719 plaintext_size = if_int( output_too_large,
1720 (unsigned) plaintext_max_size,
1721 (unsigned) plaintext_size );
1722
1723 /* Move the plaintext to the leftmost position where it can start in
1724 * the working buffer, i.e. make it start plaintext_max_size from
1725 * the end of the buffer. Do this with a memory access trace that
1726 * does not depend on the plaintext size. After this move, the
1727 * starting location of the plaintext is no longer sensitive
1728 * information. */
1729 mem_move_to_left( buf + ilen - plaintext_max_size,
1730 plaintext_max_size,
1731 plaintext_max_size - plaintext_size );
1732
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001733 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1734 * buffer. If output_max_len is 0, then output may be an invalid pointer
1735 * and the result of memcpy() would be undefined; prevent undefined
1736 * behavior making sure to depend only on output_max_len (the size of the
1737 * user-provided output buffer), which is independent from plaintext
1738 * length, validity of padding, success of the decryption, and other
1739 * secrets. */
1740 if( output_max_len != 0 )
1741 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001742
1743 /* Report the amount of data we copied to the output buffer. In case
1744 * of errors (bad padding or output too large), the value of *olen
1745 * when this function returns is not specified. Making it equivalent
1746 * to the good case limits the risks of leaking the padding validity. */
1747 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001749cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001750 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001751
1752 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
1756/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001757 * Do an RSA operation, then remove the message padding
1758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001760 int (*f_rng)(void *, unsigned char *, size_t),
1761 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001762 int mode, size_t *olen,
1763 const unsigned char *input,
1764 unsigned char *output,
1765 size_t output_max_len)
1766{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001767 RSA_VALIDATE_RET( ctx != NULL );
1768 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1769 mode == MBEDTLS_RSA_PUBLIC );
1770 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1771 RSA_VALIDATE_RET( input != NULL );
1772 RSA_VALIDATE_RET( olen != NULL );
1773
Paul Bakkerb3869132013-02-28 17:21:01 +01001774 switch( ctx->padding )
1775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#if defined(MBEDTLS_PKCS1_V15)
1777 case MBEDTLS_RSA_PKCS_V15:
1778 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001779 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001780#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782#if defined(MBEDTLS_PKCS1_V21)
1783 case MBEDTLS_RSA_PKCS_V21:
1784 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001785 olen, input, output,
1786 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001787#endif
1788
1789 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001791 }
1792}
1793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001795/*
1796 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001799 int (*f_rng)(void *, unsigned char *, size_t),
1800 void *p_rng,
1801 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001803 unsigned int hashlen,
1804 const unsigned char *hash,
1805 unsigned char *sig )
1806{
1807 size_t olen;
1808 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001810 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001812 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 const mbedtls_md_info_t *md_info;
1814 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001815 RSA_VALIDATE_RET( ctx != NULL );
1816 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1817 mode == MBEDTLS_RSA_PUBLIC );
1818 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1819 hashlen == 0 ) ||
1820 hash != NULL );
1821 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1824 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001825
1826 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001828
1829 olen = ctx->len;
1830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001832 {
Simon Butcher02037452016-03-01 21:19:12 +00001833 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001835 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001839 }
1840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001842 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001846
Jaeden Amero3725bb22018-09-07 19:12:36 +01001847 /* Calculate the largest possible salt length. Normally this is the hash
1848 * length, which is the maximum length the salt can have. If there is not
1849 * enough room, use the maximum salt length that fits. The constraint is
1850 * that the hash length plus the salt length plus 2 bytes must be at most
1851 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1852 * (PKCS#1 v2.2) §9.1.1 step 3. */
1853 min_slen = hlen - 2;
1854 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001856 else if( olen >= hlen + hlen + 2 )
1857 slen = hlen;
1858 else
1859 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001860
1861 memset( sig, 0, olen );
1862
Simon Butcher02037452016-03-01 21:19:12 +00001863 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001864 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001866
Simon Butcher02037452016-03-01 21:19:12 +00001867 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001868 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001869 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001870 *p++ = 0x01;
1871 memcpy( p, salt, slen );
1872 p += slen;
1873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001875 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001876 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001877
Simon Butcher02037452016-03-01 21:19:12 +00001878 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001879 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1880 goto exit;
1881 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1882 goto exit;
1883 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1884 goto exit;
1885 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1886 goto exit;
1887 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1888 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001889
Simon Butcher02037452016-03-01 21:19:12 +00001890 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001891 if( msb % 8 == 0 )
1892 offset = 1;
1893
Simon Butcher02037452016-03-01 21:19:12 +00001894 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001895 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1896 &md_ctx ) ) != 0 )
1897 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001898
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001899 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001900 sig[0] &= 0xFF >> ( olen * 8 - msb );
1901
1902 p += hlen;
1903 *p++ = 0xBC;
1904
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001905 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001906
1907exit:
1908 mbedtls_md_free( &md_ctx );
1909
1910 if( ret != 0 )
1911 return( ret );
1912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 return( ( mode == MBEDTLS_RSA_PUBLIC )
1914 ? mbedtls_rsa_public( ctx, sig, sig )
1915 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001920/*
1921 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1922 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001923
1924/* Construct a PKCS v1.5 encoding of a hashed message
1925 *
1926 * This is used both for signature generation and verification.
1927 *
1928 * Parameters:
1929 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001930 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001931 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001932 * - hash: Buffer containing the hashed message or the raw data.
1933 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001934 * - dst: Buffer to hold the encoded message.
1935 *
1936 * Assumptions:
1937 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1938 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001939 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001940 *
1941 */
1942static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1943 unsigned int hashlen,
1944 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001945 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001946 unsigned char *dst )
1947{
1948 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001949 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001950 unsigned char *p = dst;
1951 const char *oid = NULL;
1952
1953 /* Are we signing hashed or raw data? */
1954 if( md_alg != MBEDTLS_MD_NONE )
1955 {
1956 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1957 if( md_info == NULL )
1958 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1959
1960 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1961 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1962
1963 hashlen = mbedtls_md_get_size( md_info );
1964
1965 /* Double-check that 8 + hashlen + oid_size can be used as a
1966 * 1-byte ASN.1 length encoding and that there's no overflow. */
1967 if( 8 + hashlen + oid_size >= 0x80 ||
1968 10 + hashlen < hashlen ||
1969 10 + hashlen + oid_size < 10 + hashlen )
1970 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1971
1972 /*
1973 * Static bounds check:
1974 * - Need 10 bytes for five tag-length pairs.
1975 * (Insist on 1-byte length encodings to protect against variants of
1976 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1977 * - Need hashlen bytes for hash
1978 * - Need oid_size bytes for hash alg OID.
1979 */
1980 if( nb_pad < 10 + hashlen + oid_size )
1981 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1982 nb_pad -= 10 + hashlen + oid_size;
1983 }
1984 else
1985 {
1986 if( nb_pad < hashlen )
1987 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1988
1989 nb_pad -= hashlen;
1990 }
1991
Hanno Becker2b2f8982017-09-27 17:10:03 +01001992 /* Need space for signature header and padding delimiter (3 bytes),
1993 * and 8 bytes for the minimal padding */
1994 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001995 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1996 nb_pad -= 3;
1997
1998 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001999 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002000
2001 /* Write signature header and padding */
2002 *p++ = 0;
2003 *p++ = MBEDTLS_RSA_SIGN;
2004 memset( p, 0xFF, nb_pad );
2005 p += nb_pad;
2006 *p++ = 0;
2007
2008 /* Are we signing raw data? */
2009 if( md_alg == MBEDTLS_MD_NONE )
2010 {
2011 memcpy( p, hash, hashlen );
2012 return( 0 );
2013 }
2014
2015 /* Signing hashed data, add corresponding ASN.1 structure
2016 *
2017 * DigestInfo ::= SEQUENCE {
2018 * digestAlgorithm DigestAlgorithmIdentifier,
2019 * digest Digest }
2020 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2021 * Digest ::= OCTET STRING
2022 *
2023 * Schematic:
2024 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2025 * TAG-NULL + LEN [ NULL ] ]
2026 * TAG-OCTET + LEN [ HASH ] ]
2027 */
2028 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002029 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002030 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002031 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002032 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002033 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002034 memcpy( p, oid, oid_size );
2035 p += oid_size;
2036 *p++ = MBEDTLS_ASN1_NULL;
2037 *p++ = 0x00;
2038 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002039 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002040 memcpy( p, hash, hashlen );
2041 p += hashlen;
2042
2043 /* Just a sanity-check, should be automatic
2044 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002045 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002046 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002047 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002048 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2049 }
2050
2051 return( 0 );
2052}
2053
Paul Bakkerb3869132013-02-28 17:21:01 +01002054/*
2055 * Do an RSA operation to sign the message digest
2056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002058 int (*f_rng)(void *, unsigned char *, size_t),
2059 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002060 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002062 unsigned int hashlen,
2063 const unsigned char *hash,
2064 unsigned char *sig )
2065{
Janos Follath24eed8d2019-11-22 13:21:35 +00002066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002067 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002068
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002069 RSA_VALIDATE_RET( ctx != NULL );
2070 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2071 mode == MBEDTLS_RSA_PUBLIC );
2072 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2073 hashlen == 0 ) ||
2074 hash != NULL );
2075 RSA_VALIDATE_RET( sig != NULL );
2076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2078 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002079
Hanno Beckerfdf38032017-09-06 12:35:55 +01002080 /*
2081 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2082 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002083
Hanno Beckerfdf38032017-09-06 12:35:55 +01002084 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2085 ctx->len, sig ) ) != 0 )
2086 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002087
2088 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002089 * Call respective RSA primitive
2090 */
2091
2092 if( mode == MBEDTLS_RSA_PUBLIC )
2093 {
2094 /* Skip verification on a public key operation */
2095 return( mbedtls_rsa_public( ctx, sig, sig ) );
2096 }
2097
2098 /* Private key operation
2099 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002100 * In order to prevent Lenstra's attack, make the signature in a
2101 * temporary buffer and check it before returning it.
2102 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002103
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002104 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002105 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002106 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2107
Hanno Beckerfdf38032017-09-06 12:35:55 +01002108 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002109 if( verif == NULL )
2110 {
2111 mbedtls_free( sig_try );
2112 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2113 }
2114
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002115 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2116 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2117
Hanno Becker171a8f12017-09-06 12:32:16 +01002118 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002119 {
2120 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2121 goto cleanup;
2122 }
2123
2124 memcpy( sig, sig_try, ctx->len );
2125
2126cleanup:
2127 mbedtls_free( sig_try );
2128 mbedtls_free( verif );
2129
2130 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002133
2134/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 * Do an RSA operation to sign the message digest
2136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002138 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002139 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002142 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002143 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 unsigned char *sig )
2145{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002146 RSA_VALIDATE_RET( ctx != NULL );
2147 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2148 mode == MBEDTLS_RSA_PUBLIC );
2149 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2150 hashlen == 0 ) ||
2151 hash != NULL );
2152 RSA_VALIDATE_RET( sig != NULL );
2153
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 switch( ctx->padding )
2155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156#if defined(MBEDTLS_PKCS1_V15)
2157 case MBEDTLS_RSA_PKCS_V15:
2158 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002159 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002160#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#if defined(MBEDTLS_PKCS1_V21)
2163 case MBEDTLS_RSA_PKCS_V21:
2164 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002165 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002166#endif
2167
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002171}
2172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002174/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002175 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002178 int (*f_rng)(void *, unsigned char *, size_t),
2179 void *p_rng,
2180 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002182 unsigned int hashlen,
2183 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002185 int expected_salt_len,
2186 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002187{
Janos Follath24eed8d2019-11-22 13:21:35 +00002188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002189 size_t siglen;
2190 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002191 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002193 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002194 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002195 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 const mbedtls_md_info_t *md_info;
2197 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002198 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002199
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002200 RSA_VALIDATE_RET( ctx != NULL );
2201 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2202 mode == MBEDTLS_RSA_PUBLIC );
2203 RSA_VALIDATE_RET( sig != NULL );
2204 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2205 hashlen == 0 ) ||
2206 hash != NULL );
2207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002210
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 siglen = ctx->len;
2212
Paul Bakker27fdf462011-06-09 13:55:13 +00002213 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2217 ? mbedtls_rsa_public( ctx, sig, buf )
2218 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002219
2220 if( ret != 0 )
2221 return( ret );
2222
2223 p = buf;
2224
Paul Bakkerb3869132013-02-28 17:21:01 +01002225 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 {
Simon Butcher02037452016-03-01 21:19:12 +00002230 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002232 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002236 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002239 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002243
Paul Bakkerb3869132013-02-28 17:21:01 +01002244 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002245
Simon Butcher02037452016-03-01 21:19:12 +00002246 /*
2247 * Note: EMSA-PSS verification is over the length of N - 1 bits
2248 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002249 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002250
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002251 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2252 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2253
Simon Butcher02037452016-03-01 21:19:12 +00002254 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002255 if( msb % 8 == 0 )
2256 {
2257 p++;
2258 siglen -= 1;
2259 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002260
Gilles Peskine139108a2017-10-18 19:03:42 +02002261 if( siglen < hlen + 2 )
2262 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2263 hash_start = p + siglen - hlen - 1;
2264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002266 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002267 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002268
Jaeden Amero66954e12018-01-25 16:05:54 +00002269 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2270 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002271 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002272
Paul Bakkerb3869132013-02-28 17:21:01 +01002273 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002274
Gilles Peskine6a54b022017-10-17 19:02:13 +02002275 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002276 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002277
Gilles Peskine91048a32017-10-19 17:46:14 +02002278 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002279 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002280 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2281 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002282 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002283
Gilles Peskine6a54b022017-10-17 19:02:13 +02002284 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002287 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002288 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002289 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2290 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002291 }
2292
Simon Butcher02037452016-03-01 21:19:12 +00002293 /*
2294 * Generate H = Hash( M' )
2295 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002296 ret = mbedtls_md_starts( &md_ctx );
2297 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002298 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002299 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2300 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002301 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002302 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2303 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002304 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002305 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2306 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002307 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002308 ret = mbedtls_md_finish( &md_ctx, result );
2309 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002310 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002311
Jaeden Amero66954e12018-01-25 16:05:54 +00002312 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002313 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002314 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002315 goto exit;
2316 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002317
2318exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002320
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002321 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002322}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002323
2324/*
2325 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002328 int (*f_rng)(void *, unsigned char *, size_t),
2329 void *p_rng,
2330 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002332 unsigned int hashlen,
2333 const unsigned char *hash,
2334 const unsigned char *sig )
2335{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002336 mbedtls_md_type_t mgf1_hash_id;
2337 RSA_VALIDATE_RET( ctx != NULL );
2338 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2339 mode == MBEDTLS_RSA_PUBLIC );
2340 RSA_VALIDATE_RET( sig != NULL );
2341 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2342 hashlen == 0 ) ||
2343 hash != NULL );
2344
2345 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002347 : md_alg;
2348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002350 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002352 sig ) );
2353
2354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002358/*
2359 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002362 int (*f_rng)(void *, unsigned char *, size_t),
2363 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002364 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002366 unsigned int hashlen,
2367 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002368 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002369{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002370 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002371 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002372 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002373
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002374 RSA_VALIDATE_RET( ctx != NULL );
2375 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2376 mode == MBEDTLS_RSA_PUBLIC );
2377 RSA_VALIDATE_RET( sig != NULL );
2378 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2379 hashlen == 0 ) ||
2380 hash != NULL );
2381
2382 sig_len = ctx->len;
2383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2385 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002386
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002387 /*
2388 * Prepare expected PKCS1 v1.5 encoding of hash.
2389 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002390
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002391 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2392 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2393 {
2394 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2395 goto cleanup;
2396 }
2397
2398 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2399 encoded_expected ) ) != 0 )
2400 goto cleanup;
2401
2402 /*
2403 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2404 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002407 ? mbedtls_rsa_public( ctx, sig, encoded )
2408 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002409 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002410 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002411
Simon Butcher02037452016-03-01 21:19:12 +00002412 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002413 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002414 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002415
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002416 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2417 sig_len ) ) != 0 )
2418 {
2419 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2420 goto cleanup;
2421 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002422
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002423cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002424
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002425 if( encoded != NULL )
2426 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002427 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002428 mbedtls_free( encoded );
2429 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002430
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002431 if( encoded_expected != NULL )
2432 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002433 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002434 mbedtls_free( encoded_expected );
2435 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002436
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002437 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
2441/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002442 * Do an RSA operation and check the message digest
2443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002445 int (*f_rng)(void *, unsigned char *, size_t),
2446 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002447 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002449 unsigned int hashlen,
2450 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002451 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002452{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002453 RSA_VALIDATE_RET( ctx != NULL );
2454 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2455 mode == MBEDTLS_RSA_PUBLIC );
2456 RSA_VALIDATE_RET( sig != NULL );
2457 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2458 hashlen == 0 ) ||
2459 hash != NULL );
2460
Paul Bakkerb3869132013-02-28 17:21:01 +01002461 switch( ctx->padding )
2462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463#if defined(MBEDTLS_PKCS1_V15)
2464 case MBEDTLS_RSA_PKCS_V15:
2465 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002466 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002467#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469#if defined(MBEDTLS_PKCS1_V21)
2470 case MBEDTLS_RSA_PKCS_V21:
2471 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002472 hashlen, hash, sig );
2473#endif
2474
2475 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002477 }
2478}
2479
2480/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002481 * Copy the components of an RSA key
2482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002484{
Janos Follath24eed8d2019-11-22 13:21:35 +00002485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002486 RSA_VALIDATE_RET( dst != NULL );
2487 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002488
2489 dst->ver = src->ver;
2490 dst->len = src->len;
2491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2493 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2496 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2497 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002498
2499#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2501 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2502 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2504 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002505#endif
2506
2507 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2510 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002511
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002512 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002513 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002514
2515cleanup:
2516 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002518
2519 return( ret );
2520}
2521
2522/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 * Free the components of an RSA key
2524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002526{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002527 if( ctx == NULL )
2528 return;
2529
2530 mbedtls_mpi_free( &ctx->Vi );
2531 mbedtls_mpi_free( &ctx->Vf );
2532 mbedtls_mpi_free( &ctx->RN );
2533 mbedtls_mpi_free( &ctx->D );
2534 mbedtls_mpi_free( &ctx->Q );
2535 mbedtls_mpi_free( &ctx->P );
2536 mbedtls_mpi_free( &ctx->E );
2537 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002538
Hanno Becker33c30a02017-08-23 07:00:22 +01002539#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002540 mbedtls_mpi_free( &ctx->RQ );
2541 mbedtls_mpi_free( &ctx->RP );
2542 mbedtls_mpi_free( &ctx->QP );
2543 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002544 mbedtls_mpi_free( &ctx->DP );
2545#endif /* MBEDTLS_RSA_NO_CRT */
2546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547#if defined(MBEDTLS_THREADING_C)
2548 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002549#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002550}
2551
Hanno Beckerab377312017-08-23 16:24:51 +01002552#endif /* !MBEDTLS_RSA_ALT */
2553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002556#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
2558/*
2559 * Example RSA-1024 keypair, for test purposes
2560 */
2561#define KEY_LEN 128
2562
2563#define RSA_N "9292758453063D803DD603D5E777D788" \
2564 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2565 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2566 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2567 "93A89813FBF3C4F8066D2D800F7C38A8" \
2568 "1AE31942917403FF4946B0A83D3D3E05" \
2569 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2570 "5E94BB77B07507233A0BC7BAC8F90F79"
2571
2572#define RSA_E "10001"
2573
2574#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2575 "66CA472BC44D253102F8B4A9D3BFA750" \
2576 "91386C0077937FE33FA3252D28855837" \
2577 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2578 "DF79C5CE07EE72C7F123142198164234" \
2579 "CABB724CF78B8173B9F880FC86322407" \
2580 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2581 "071513A1E85B5DFA031F21ECAE91A34D"
2582
2583#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2584 "2C01CAD19EA484A87EA4377637E75500" \
2585 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2586 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2587
2588#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2589 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2590 "910E4168387E3C30AA1E00C339A79508" \
2591 "8452DD96A9A5EA5D9DCA68DA636032AF"
2592
Paul Bakker5121ce52009-01-03 21:22:43 +00002593#define PT_LEN 24
2594#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2595 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002598static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002599{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002600#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002601 size_t i;
2602
Paul Bakker545570e2010-07-18 09:00:25 +00002603 if( rng_state != NULL )
2604 rng_state = NULL;
2605
Paul Bakkera3d195c2011-11-27 21:07:34 +00002606 for( i = 0; i < len; ++i )
2607 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002608#else
2609 if( rng_state != NULL )
2610 rng_state = NULL;
2611
2612 arc4random_buf( output, len );
2613#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002614
Paul Bakkera3d195c2011-11-27 21:07:34 +00002615 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002616}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002618
Paul Bakker5121ce52009-01-03 21:22:43 +00002619/*
2620 * Checkup routine
2621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002623{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002624 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002626 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 unsigned char rsa_plaintext[PT_LEN];
2629 unsigned char rsa_decrypted[PT_LEN];
2630 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002632 unsigned char sha1sum[20];
2633#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
Hanno Becker3a701162017-08-22 13:52:43 +01002635 mbedtls_mpi K;
2636
2637 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
Hanno Becker3a701162017-08-22 13:52:43 +01002640 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2641 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2642 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2643 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2644 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2645 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2646 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2647 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2648 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2649 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2650
Hanno Becker7f25f852017-10-10 16:56:22 +01002651 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
2653 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2657 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002658 {
2659 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Hanno Becker5bc87292017-05-03 15:09:31 +01002662 ret = 1;
2663 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 }
2665
2666 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
2669 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2670
Hanno Becker98838b02017-10-02 13:16:10 +01002671 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2672 PT_LEN, rsa_plaintext,
2673 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 {
2675 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002677
Hanno Becker5bc87292017-05-03 15:09:31 +01002678 ret = 1;
2679 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 }
2681
2682 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Hanno Becker98838b02017-10-02 13:16:10 +01002685 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2686 &len, rsa_ciphertext, rsa_decrypted,
2687 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 {
2689 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Hanno Becker5bc87292017-05-03 15:09:31 +01002692 ret = 1;
2693 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 }
2695
2696 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2697 {
2698 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Hanno Becker5bc87292017-05-03 15:09:31 +01002701 ret = 1;
2702 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 }
2704
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002705 if( verbose != 0 )
2706 mbedtls_printf( "passed\n" );
2707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002710 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002712 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002713 {
2714 if( verbose != 0 )
2715 mbedtls_printf( "failed\n" );
2716
2717 return( 1 );
2718 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
Hanno Becker98838b02017-10-02 13:16:10 +01002720 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2721 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2722 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 {
2724 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726
Hanno Becker5bc87292017-05-03 15:09:31 +01002727 ret = 1;
2728 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 }
2730
2731 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
Hanno Becker98838b02017-10-02 13:16:10 +01002734 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2735 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2736 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 {
2738 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Hanno Becker5bc87292017-05-03 15:09:31 +01002741 ret = 1;
2742 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 }
2744
2745 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002746 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002749 if( verbose != 0 )
2750 mbedtls_printf( "\n" );
2751
Paul Bakker3d8fb632014-04-17 12:42:41 +02002752cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002753 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 mbedtls_rsa_free( &rsa );
2755#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002756 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002758 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759}
2760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763#endif /* MBEDTLS_RSA_C */