blob: 7ea72cd84680f2b2ad5dd9a7e49267f3b843afe2 [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 Lloyd80cc8112020-01-22 17:34:29 -0500252 int have_N, have_P, have_Q, have_D, have_E, have_DP, have_DQ, have_QP;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500253 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100254
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 RSA_VALIDATE_RET( ctx != NULL );
256
257 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
258 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
259 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
260 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
261 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd80cc8112020-01-22 17:34:29 -0500262 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
263 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
264 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
265
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100266
Hanno Becker617c1ae2017-08-23 14:11:24 +0100267 /*
268 * Check whether provided parameters are enough
269 * to deduce all others. The following incomplete
270 * parameter sets for private keys are supported:
271 *
272 * (1) P, Q missing.
273 * (2) D and potentially N missing.
274 *
275 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100276
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500277 n_missing = have_P && have_Q && have_D && have_E;
278 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
279 d_missing = have_P && have_Q && !have_D && have_E;
280 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100281
282 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500283 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100284
Hanno Becker617c1ae2017-08-23 14:11:24 +0100285 if( !is_priv && !is_pub )
286 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
287
288 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100289 * Step 1: Deduce N if P, Q are provided.
290 */
291
292 if( !have_N && have_P && have_Q )
293 {
294 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
295 &ctx->Q ) ) != 0 )
296 {
297 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
298 }
299
300 ctx->len = mbedtls_mpi_size( &ctx->N );
301 }
302
303 /*
304 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100305 */
306
307 if( pq_missing )
308 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100309 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100310 &ctx->P, &ctx->Q );
311 if( ret != 0 )
312 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
313
314 }
315 else if( d_missing )
316 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100317 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
318 &ctx->Q,
319 &ctx->E,
320 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100321 {
322 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
323 }
324 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100325
Hanno Becker617c1ae2017-08-23 14:11:24 +0100326 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100327 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100328 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100329 */
330
Hanno Becker23344b52017-08-23 07:43:27 +0100331#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500332 if( is_priv && !(have_DP && have_DQ && have_QP))
Hanno Becker617c1ae2017-08-23 14:11:24 +0100333 {
334 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
335 &ctx->DP, &ctx->DQ, &ctx->QP );
336 if( ret != 0 )
337 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
338 }
Hanno Becker23344b52017-08-23 07:43:27 +0100339#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100340
341 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100342 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100343 */
344
Hanno Beckerebd2c022017-10-12 10:54:53 +0100345 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100346}
347
Hanno Becker617c1ae2017-08-23 14:11:24 +0100348int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
349 unsigned char *N, size_t N_len,
350 unsigned char *P, size_t P_len,
351 unsigned char *Q, size_t Q_len,
352 unsigned char *D, size_t D_len,
353 unsigned char *E, size_t E_len )
354{
355 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500356 int is_priv;
357 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100358
359 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500360 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100361 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
362 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
363 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
364 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
365 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
366
367 if( !is_priv )
368 {
369 /* If we're trying to export private parameters for a public key,
370 * something must be wrong. */
371 if( P != NULL || Q != NULL || D != NULL )
372 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
373
374 }
375
376 if( N != NULL )
377 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
378
379 if( P != NULL )
380 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
381
382 if( Q != NULL )
383 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
384
385 if( D != NULL )
386 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
387
388 if( E != NULL )
389 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100390
391cleanup:
392
393 return( ret );
394}
395
Hanno Becker617c1ae2017-08-23 14:11:24 +0100396int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
397 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
398 mbedtls_mpi *D, mbedtls_mpi *E )
399{
Janos Follath24eed8d2019-11-22 13:21:35 +0000400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500401 int is_priv;
402 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100403
404 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500405 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100406 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
407 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
408 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
409 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
410 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
411
412 if( !is_priv )
413 {
414 /* If we're trying to export private parameters for a public key,
415 * something must be wrong. */
416 if( P != NULL || Q != NULL || D != NULL )
417 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
418
419 }
420
421 /* Export all requested core parameters. */
422
423 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
424 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
425 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
426 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
427 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
428 {
429 return( ret );
430 }
431
432 return( 0 );
433}
434
435/*
436 * Export CRT parameters
437 * This must also be implemented if CRT is not used, for being able to
438 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
439 * can be used in this case.
440 */
441int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
442 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
443{
Janos Follath24eed8d2019-11-22 13:21:35 +0000444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500445 int is_priv;
446 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100447
448 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500449 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100450 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
451 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
452 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
453 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
454 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
455
456 if( !is_priv )
457 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
458
Hanno Beckerdc95c892017-08-23 06:57:02 +0100459#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100460 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100461 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
462 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
463 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
464 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100465 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100466 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100467#else
468 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
469 DP, DQ, QP ) ) != 0 )
470 {
471 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
472 }
473#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100474
475 return( 0 );
476}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100477
Paul Bakker5121ce52009-01-03 21:22:43 +0000478/*
479 * Initialize an RSA context
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000483 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000484{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500485 RSA_VALIDATE( ctx != NULL );
486 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
487 padding == MBEDTLS_RSA_PKCS_V21 );
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#if defined(MBEDTLS_THREADING_C)
494 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200495#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000496}
497
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100498/*
499 * Set padding for an existing RSA context
500 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500501void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
502 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100503{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500504 RSA_VALIDATE( ctx != NULL );
505 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
506 padding == MBEDTLS_RSA_PKCS_V21 );
507
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100508 ctx->padding = padding;
509 ctx->hash_id = hash_id;
510}
511
Hanno Becker617c1ae2017-08-23 14:11:24 +0100512/*
513 * Get length in bytes of RSA modulus
514 */
515
516size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
517{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100518 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100519}
520
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524/*
525 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800526 *
527 * This generation method follows the RSA key pair generation procedure of
528 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000531 int (*f_rng)(void *, unsigned char *, size_t),
532 void *p_rng,
533 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000534{
Janos Follath24eed8d2019-11-22 13:21:35 +0000535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800536 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100537 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500538 RSA_VALIDATE_RET( ctx != NULL );
539 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500541 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
Janos Follathef441782016-09-21 13:18:12 +0100542 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
543
Janos Follathb8fc1b02018-09-03 15:37:01 +0100544 /*
545 * If the modulus is 1024 bit long or shorter, then the security strength of
546 * the RSA algorithm is less than or equal to 80 bits and therefore an error
547 * rate of 2^-80 is sufficient.
548 */
549 if( nbits > 1024 )
550 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
551
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100552 mbedtls_mpi_init( &H );
553 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800554 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556 /*
557 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800558 * 1. |P-Q| > 2^( nbits / 2 - 100 )
559 * 2. GCD( E, (P-1)*(Q-1) ) == 1
560 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 do
565 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100566 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
567 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
Janos Follathb8fc1b02018-09-03 15:37:01 +0100569 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
570 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800572 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
573 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
574 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 continue;
576
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800577 /* not required by any standards, but some users rely on the fact that P > Q */
578 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100579 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100580
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100581 /* Temporarily replace P,Q by P-1, Q-1 */
582 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
583 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
584 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800585
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800586 /* 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 +0200587 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800588 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
589 continue;
590
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800591 /* 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 -0800592 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
593 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
594 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
595
596 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
597 continue;
598
599 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800601 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100603 /* Restore P,Q */
604 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
605 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
606
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800607 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
608
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100609 ctx->len = mbedtls_mpi_size( &ctx->N );
610
Jethro Beekman97f95c92018-02-13 15:50:36 -0800611#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 * DP = D mod (P - 1)
614 * DQ = D mod (Q - 1)
615 * QP = Q^-1 mod P
616 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100617 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
618 &ctx->DP, &ctx->DQ, &ctx->QP ) );
619#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Hanno Becker83aad1f2017-08-23 06:45:10 +0100621 /* Double-check */
622 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
624cleanup:
625
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100626 mbedtls_mpi_free( &H );
627 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800628 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630 if( ret != 0 )
631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_rsa_free( ctx );
633 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634 }
635
Paul Bakker48377d92013-08-30 12:06:24 +0200636 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000637}
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
641/*
642 * Check a public RSA key
643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000645{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500646 RSA_VALIDATE_RET( ctx != NULL );
647
Hanno Beckerebd2c022017-10-12 10:54:53 +0100648 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000650
Hanno Becker3a760a12018-01-05 08:14:49 +0000651 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100654 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Hanno Becker705fc682017-10-10 17:57:02 +0100656 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
657 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100661 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000662
663 return( 0 );
664}
665
666/*
Hanno Becker705fc682017-10-10 17:57:02 +0100667 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000670{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500671 RSA_VALIDATE_RET( ctx != NULL );
672
Hanno Becker705fc682017-10-10 17:57:02 +0100673 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100674 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 {
Hanno Becker98838b02017-10-02 13:16:10 +0100676 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 }
Paul Bakker48377d92013-08-30 12:06:24 +0200678
Hanno Becker98838b02017-10-02 13:16:10 +0100679 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100680 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100682 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000684
Hanno Beckerb269a852017-08-25 08:03:21 +0100685#if !defined(MBEDTLS_RSA_NO_CRT)
686 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
687 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
688 {
689 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
690 }
691#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000692
693 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000694}
695
696/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100697 * Check if contexts holding a public and private key match
698 */
Hanno Becker98838b02017-10-02 13:16:10 +0100699int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
700 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100701{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500702 RSA_VALIDATE_RET( pub != NULL );
703 RSA_VALIDATE_RET( prv != NULL );
704
Hanno Becker98838b02017-10-02 13:16:10 +0100705 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100709 }
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
712 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100715 }
716
717 return( 0 );
718}
719
720/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 * Do an RSA public key operation
722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000724 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 unsigned char *output )
726{
Janos Follath24eed8d2019-11-22 13:21:35 +0000727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000728 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500730 RSA_VALIDATE_RET( ctx != NULL );
731 RSA_VALIDATE_RET( input != NULL );
732 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
Hanno Beckerebd2c022017-10-12 10:54:53 +0100734 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100735 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000738
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200739#if defined(MBEDTLS_THREADING_C)
740 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
741 return( ret );
742#endif
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000747 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200748 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
749 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000750 }
751
752 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
754 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
756cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200758 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
759 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100760#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
764 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
767 return( 0 );
768}
769
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200770/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200771 * Generate or update blinding values, see section 10 of:
772 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200773 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200774 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200775 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200776static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200777 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
778{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200779 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200780
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200781 if( ctx->Vf.p != NULL )
782 {
783 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
785 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
786 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
787 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200788
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200789 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200790 }
791
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200792 /* Unblinding value: Vf = random number, invertible mod N */
793 do {
794 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
798 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
799 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200800
801 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
803 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 +0200804
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200805
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200806cleanup:
807 return( ret );
808}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200809
Paul Bakker5121ce52009-01-03 21:22:43 +0000810/*
Janos Follathe81102e2017-03-22 13:38:28 +0000811 * Exponent blinding supposed to prevent side-channel attacks using multiple
812 * traces of measurements to recover the RSA key. The more collisions are there,
813 * the more bits of the key can be recovered. See [3].
814 *
815 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
816 * observations on avarage.
817 *
818 * For example with 28 byte blinding to achieve 2 collisions the adversary has
819 * to make 2^112 observations on avarage.
820 *
821 * (With the currently (as of 2017 April) known best algorithms breaking 2048
822 * bit RSA requires approximately as much time as trying out 2^112 random keys.
823 * Thus in this sense with 28 byte blinding the security is not reduced by
824 * side-channel attacks like the one in [3])
825 *
826 * This countermeasure does not help if the key recovery is possible with a
827 * single trace.
828 */
829#define RSA_EXPONENT_BLINDING 28
830
831/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 * Do an RSA private key operation
833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200835 int (*f_rng)(void *, unsigned char *, size_t),
836 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000837 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000838 unsigned char *output )
839{
Janos Follath24eed8d2019-11-22 13:21:35 +0000840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000841 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100842
843 /* Temporary holding the result */
844 mbedtls_mpi T;
845
846 /* Temporaries holding P-1, Q-1 and the
847 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000848 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100849
850#if !defined(MBEDTLS_RSA_NO_CRT)
851 /* Temporaries holding the results mod p resp. mod q. */
852 mbedtls_mpi TP, TQ;
853
854 /* Temporaries holding the blinded exponents for
855 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000856 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100857
858 /* Pointers to actual exponents to be used - either the unblinded
859 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000860 mbedtls_mpi *DP = &ctx->DP;
861 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100862#else
863 /* Temporary holding the blinded exponent (if used). */
864 mbedtls_mpi D_blind;
865
866 /* Pointer to actual exponent to be used - either the unblinded
867 * or the blinded one, depending on the presence of a PRNG. */
868 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100869#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100870
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100871 /* Temporaries holding the initial input and the double
872 * checked result; should be the same in the end. */
873 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000874
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500875 RSA_VALIDATE_RET( ctx != NULL );
876 RSA_VALIDATE_RET( input != NULL );
877 RSA_VALIDATE_RET( output != NULL );
878
Hanno Beckerebd2c022017-10-12 10:54:53 +0100879 if( rsa_check_context( ctx, 1 /* private key checks */,
880 f_rng != NULL /* blinding y/n */ ) != 0 )
881 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100882 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100883 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100884
Hanno Becker06811ce2017-05-03 15:10:34 +0100885#if defined(MBEDTLS_THREADING_C)
886 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
887 return( ret );
888#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000889
Hanno Becker06811ce2017-05-03 15:10:34 +0100890 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100891 mbedtls_mpi_init( &T );
892
893 mbedtls_mpi_init( &P1 );
894 mbedtls_mpi_init( &Q1 );
895 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000896
Janos Follathf9203b42017-03-22 15:13:15 +0000897 if( f_rng != NULL )
898 {
Janos Follathe81102e2017-03-22 13:38:28 +0000899#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000900 mbedtls_mpi_init( &D_blind );
901#else
902 mbedtls_mpi_init( &DP_blind );
903 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000904#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000905 }
Janos Follathe81102e2017-03-22 13:38:28 +0000906
Hanno Becker06811ce2017-05-03 15:10:34 +0100907#if !defined(MBEDTLS_RSA_NO_CRT)
908 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200909#endif
910
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100911 mbedtls_mpi_init( &I );
912 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100913
914 /* End of MPI initialization */
915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
917 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000918 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200919 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
920 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000921 }
922
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100923 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100924
Paul Bakkerf451bac2013-08-30 15:37:02 +0200925 if( f_rng != NULL )
926 {
927 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200928 * Blinding
929 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200930 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200931 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
932 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000934
Janos Follathe81102e2017-03-22 13:38:28 +0000935 /*
936 * Exponent blinding
937 */
938 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
939 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
940
Janos Follathf9203b42017-03-22 15:13:15 +0000941#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000942 /*
943 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
944 */
945 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
946 f_rng, p_rng ) );
947 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
948 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
949 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
950
951 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000952#else
953 /*
954 * DP_blind = ( P - 1 ) * R + DP
955 */
956 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
957 f_rng, p_rng ) );
958 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
959 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
960 &ctx->DP ) );
961
962 DP = &DP_blind;
963
964 /*
965 * DQ_blind = ( Q - 1 ) * R + DQ
966 */
967 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
968 f_rng, p_rng ) );
969 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
970 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
971 &ctx->DQ ) );
972
973 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000974#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200975 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000978 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100979#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200980 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000981 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000982 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100983 * TP = input ^ dP mod P
984 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000985 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100986
987 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
988 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
990 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100991 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100993 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
994 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
995 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000996
997 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100998 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001000 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1001 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001003
Paul Bakkerf451bac2013-08-30 15:37:02 +02001004 if( f_rng != NULL )
1005 {
1006 /*
1007 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001008 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001009 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001010 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001012 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
Hanno Becker2dec5e82017-10-03 07:49:52 +01001014 /* Verify the result to prevent glitching attacks. */
1015 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1016 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001017 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001018 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001019 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1020 goto cleanup;
1021 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001022
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
1026cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001028 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1029 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001030#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001031
Hanno Becker06811ce2017-05-03 15:10:34 +01001032 mbedtls_mpi_free( &P1 );
1033 mbedtls_mpi_free( &Q1 );
1034 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001035
1036 if( f_rng != NULL )
1037 {
Janos Follathe81102e2017-03-22 13:38:28 +00001038#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001039 mbedtls_mpi_free( &D_blind );
1040#else
1041 mbedtls_mpi_free( &DP_blind );
1042 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001043#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001044 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
Hanno Becker06811ce2017-05-03 15:10:34 +01001046 mbedtls_mpi_free( &T );
1047
1048#if !defined(MBEDTLS_RSA_NO_CRT)
1049 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1050#endif
1051
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001052 mbedtls_mpi_free( &C );
1053 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001054
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
1058 return( 0 );
1059}
1060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001062/**
1063 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1064 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001065 * \param dst buffer to mask
1066 * \param dlen length of destination buffer
1067 * \param src source of the mask generation
1068 * \param slen length of the source buffer
1069 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001070 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001071static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001073{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001075 unsigned char counter[4];
1076 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001077 unsigned int hlen;
1078 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001079 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001082 memset( counter, 0, 4 );
1083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001085
Simon Butcher02037452016-03-01 21:19:12 +00001086 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001087 p = dst;
1088
1089 while( dlen > 0 )
1090 {
1091 use_len = hlen;
1092 if( dlen < hlen )
1093 use_len = dlen;
1094
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001095 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1096 goto exit;
1097 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1098 goto exit;
1099 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1100 goto exit;
1101 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1102 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001103
1104 for( i = 0; i < use_len; ++i )
1105 *p++ ^= mask[i];
1106
1107 counter[3]++;
1108
1109 dlen -= use_len;
1110 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001111
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001112exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001113 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001114
1115 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001116}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001120/*
1121 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001124 int (*f_rng)(void *, unsigned char *, size_t),
1125 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001126 int mode,
1127 const unsigned char *label, size_t label_len,
1128 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001129 const unsigned char *input,
1130 unsigned char *output )
1131{
1132 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001134 unsigned char *p = output;
1135 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 const mbedtls_md_info_t *md_info;
1137 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001138
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001139 RSA_VALIDATE_RET( ctx != NULL );
1140 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1141 mode == MBEDTLS_RSA_PUBLIC );
1142 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001143 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001144 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1147 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001148
1149 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001153 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001155
1156 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001158
Simon Butcher02037452016-03-01 21:19:12 +00001159 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001160 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001162
1163 memset( output, 0, olen );
1164
1165 *p++ = 0;
1166
Simon Butcher02037452016-03-01 21:19:12 +00001167 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001168 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001170
1171 p += hlen;
1172
Simon Butcher02037452016-03-01 21:19:12 +00001173 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001174 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1175 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001176 p += hlen;
1177 p += olen - 2 * hlen - 2 - ilen;
1178 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001179 if( ilen != 0 )
1180 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001183 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001184 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001185
Simon Butcher02037452016-03-01 21:19:12 +00001186 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001187 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1188 &md_ctx ) ) != 0 )
1189 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001190
Simon Butcher02037452016-03-01 21:19:12 +00001191 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001192 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1193 &md_ctx ) ) != 0 )
1194 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001195
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001196exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001198
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001199 if( ret != 0 )
1200 return( ret );
1201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 return( ( mode == MBEDTLS_RSA_PUBLIC )
1203 ? mbedtls_rsa_public( ctx, output, output )
1204 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001209/*
1210 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001213 int (*f_rng)(void *, unsigned char *, size_t),
1214 void *p_rng,
1215 int mode, size_t ilen,
1216 const unsigned char *input,
1217 unsigned char *output )
1218{
1219 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001221 unsigned char *p = output;
1222
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001223 RSA_VALIDATE_RET( ctx != NULL );
1224 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1225 mode == MBEDTLS_RSA_PUBLIC );
1226 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001227 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001228
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001229 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001231
1232 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001233
Simon Butcher02037452016-03-01 21:19:12 +00001234 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001235 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001237
1238 nb_pad = olen - 3 - ilen;
1239
1240 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001242 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001243 if( f_rng == NULL )
1244 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001247
1248 while( nb_pad-- > 0 )
1249 {
1250 int rng_dl = 100;
1251
1252 do {
1253 ret = f_rng( p_rng, p, 1 );
1254 } while( *p == 0 && --rng_dl && ret == 0 );
1255
Simon Butcher02037452016-03-01 21:19:12 +00001256 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001257 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001259
1260 p++;
1261 }
1262 }
1263 else
1264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001266
1267 while( nb_pad-- > 0 )
1268 *p++ = 0xFF;
1269 }
1270
1271 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001272 if( ilen != 0 )
1273 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 return( ( mode == MBEDTLS_RSA_PUBLIC )
1276 ? mbedtls_rsa_public( ctx, output, output )
1277 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001278}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001280
Paul Bakker5121ce52009-01-03 21:22:43 +00001281/*
1282 * Add the message padding, then do an RSA operation
1283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001285 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001286 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001287 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001288 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 unsigned char *output )
1290{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001291 RSA_VALIDATE_RET( ctx != NULL );
1292 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1293 mode == MBEDTLS_RSA_PUBLIC );
1294 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001295 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001296
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 switch( ctx->padding )
1298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#if defined(MBEDTLS_PKCS1_V15)
1300 case MBEDTLS_RSA_PKCS_V15:
1301 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001302 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001303#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#if defined(MBEDTLS_PKCS1_V21)
1306 case MBEDTLS_RSA_PKCS_V21:
1307 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001308 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001309#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
1311 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001314}
1315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001317/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001318 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001321 int (*f_rng)(void *, unsigned char *, size_t),
1322 void *p_rng,
1323 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001324 const unsigned char *label, size_t label_len,
1325 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001326 const unsigned char *input,
1327 unsigned char *output,
1328 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001329{
Janos Follath24eed8d2019-11-22 13:21:35 +00001330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001331 size_t ilen, i, pad_len;
1332 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1334 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001335 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 const mbedtls_md_info_t *md_info;
1337 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001338
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001339 RSA_VALIDATE_RET( ctx != NULL );
1340 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1341 mode == MBEDTLS_RSA_PUBLIC );
1342 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1343 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1344 RSA_VALIDATE_RET( input != NULL );
1345 RSA_VALIDATE_RET( olen != NULL );
1346
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001347 /*
1348 * Parameters sanity checks
1349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1351 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
1353 ilen = ctx->len;
1354
Paul Bakker27fdf462011-06-09 13:55:13 +00001355 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001359 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001361
Janos Follathc17cda12016-02-11 11:08:18 +00001362 hlen = mbedtls_md_get_size( md_info );
1363
1364 // checking for integer underflow
1365 if( 2 * hlen + 2 > ilen )
1366 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1367
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001368 /*
1369 * RSA operation
1370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1372 ? mbedtls_rsa_public( ctx, input, buf )
1373 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374
1375 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001376 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001378 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001379 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001382 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1383 {
1384 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001385 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001386 }
1387
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001388 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001389 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1390 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001391 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001392 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1393 &md_ctx ) ) != 0 )
1394 {
1395 mbedtls_md_free( &md_ctx );
1396 goto cleanup;
1397 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001400
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001401 /* Generate lHash */
1402 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1403 goto cleanup;
1404
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001405 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001406 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001407 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001409 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001411 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001412
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001413 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001414
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001415 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001416 for( i = 0; i < hlen; i++ )
1417 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001418
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001419 /* Get zero-padding len, but always read till end of buffer
1420 * (minus one, for the 01 byte) */
1421 pad_len = 0;
1422 pad_done = 0;
1423 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1424 {
1425 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001426 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001427 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001428
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001429 p += pad_len;
1430 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001431
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001432 /*
1433 * The only information "leaked" is whether the padding was correct or not
1434 * (eg, no data is copied if it was not correct). This meets the
1435 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1436 * the different error conditions.
1437 */
1438 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001439 {
1440 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1441 goto cleanup;
1442 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001443
Paul Bakker66d5d072014-06-17 16:39:18 +02001444 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001445 {
1446 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1447 goto cleanup;
1448 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001449
1450 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001451 if( *olen != 0 )
1452 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001453 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001454
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001455cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001456 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1457 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001458
1459 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001460}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001464/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1465 *
1466 * \param value The value to analyze.
1467 * \return Zero if \p value is zero, otherwise all-bits-one.
1468 */
1469static unsigned all_or_nothing_int( unsigned value )
1470{
1471 /* MSVC has a warning about unary minus on unsigned, but this is
1472 * well-defined and precisely what we want to do here */
1473#if defined(_MSC_VER)
1474#pragma warning( push )
1475#pragma warning( disable : 4146 )
1476#endif
1477 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1478#if defined(_MSC_VER)
1479#pragma warning( pop )
1480#endif
1481}
1482
1483/** Check whether a size is out of bounds, without branches.
1484 *
1485 * This is equivalent to `size > max`, but is likely to be compiled to
1486 * to code using bitwise operation rather than a branch.
1487 *
1488 * \param size Size to check.
1489 * \param max Maximum desired value for \p size.
1490 * \return \c 0 if `size <= max`.
1491 * \return \c 1 if `size > max`.
1492 */
1493static unsigned size_greater_than( size_t size, size_t max )
1494{
1495 /* Return the sign bit (1 for negative) of (max - size). */
1496 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1497}
1498
1499/** Choose between two integer values, without branches.
1500 *
1501 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1502 * to code using bitwise operation rather than a branch.
1503 *
1504 * \param cond Condition to test.
1505 * \param if1 Value to use if \p cond is nonzero.
1506 * \param if0 Value to use if \p cond is zero.
1507 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1508 */
1509static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
1510{
1511 unsigned mask = all_or_nothing_int( cond );
1512 return( ( mask & if1 ) | (~mask & if0 ) );
1513}
1514
1515/** Shift some data towards the left inside a buffer without leaking
1516 * the length of the data through side channels.
1517 *
1518 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1519 * ```
1520 * memmove(start, start + offset, total - offset);
1521 * memset(start + offset, 0, total - offset);
1522 * ```
1523 * but it strives to use a memory access pattern (and thus total timing)
1524 * that does not depend on \p offset. This timing independence comes at
1525 * the expense of performance.
1526 *
1527 * \param start Pointer to the start of the buffer.
1528 * \param total Total size of the buffer.
1529 * \param offset Offset from which to copy \p total - \p offset bytes.
1530 */
1531static void mem_move_to_left( void *start,
1532 size_t total,
1533 size_t offset )
1534{
1535 volatile unsigned char *buf = start;
1536 size_t i, n;
1537 if( total == 0 )
1538 return;
1539 for( i = 0; i < total; i++ )
1540 {
1541 unsigned no_op = size_greater_than( total - offset, i );
1542 /* The first `total - offset` passes are a no-op. The last
1543 * `offset` passes shift the data one byte to the left and
1544 * zero out the last byte. */
1545 for( n = 0; n < total - 1; n++ )
1546 {
1547 unsigned char current = buf[n];
1548 unsigned char next = buf[n+1];
1549 buf[n] = if_int( no_op, current, next );
1550 }
1551 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1552 }
1553}
1554
Paul Bakkerb3869132013-02-28 17:21:01 +01001555/*
1556 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001559 int (*f_rng)(void *, unsigned char *, size_t),
1560 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001561 int mode, size_t *olen,
1562 const unsigned char *input,
1563 unsigned char *output,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001564 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001565{
Janos Follath24eed8d2019-11-22 13:21:35 +00001566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001567 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001569 /* The following variables take sensitive values: their value must
1570 * not leak into the observable behavior of the function other than
1571 * the designated outputs (output, olen, return value). Otherwise
1572 * this would open the execution of the function to
1573 * side-channel-based variants of the Bleichenbacher padding oracle
1574 * attack. Potential side channels include overall timing, memory
1575 * access patterns (especially visible to an adversary who has access
1576 * to a shared memory cache), and branches (especially visible to
1577 * an adversary who has access to a shared code cache or to a shared
1578 * branch predictor). */
1579 size_t pad_count = 0;
1580 unsigned bad = 0;
1581 unsigned char pad_done = 0;
1582 size_t plaintext_size = 0;
1583 unsigned output_too_large;
1584
1585 RSA_VALIDATE_RET( ctx != NULL );
1586 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1587 mode == MBEDTLS_RSA_PUBLIC );
1588 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1589 RSA_VALIDATE_RET( input != NULL );
1590 RSA_VALIDATE_RET( olen != NULL );
1591
1592 ilen = ctx->len;
1593 plaintext_max_size = ( output_max_len > ilen - 11 ?
1594 ilen - 11 :
1595 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1598 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001599
Paul Bakkerb3869132013-02-28 17:21:01 +01001600 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1604 ? mbedtls_rsa_public( ctx, input, buf )
1605 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001606
1607 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001608 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001609
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001610 /* Check and get padding length in constant time and constant
1611 * memory trace. The first byte must be 0. */
1612 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001616 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1617 * where PS must be at least 8 nonzero bytes. */
1618 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001619
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001620 /* Read the whole buffer. Set pad_done to nonzero if we find
1621 * the 0x00 byte and remember the padding length in pad_count. */
1622 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001623 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001624 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001625 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001626 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001627 }
1628 else
1629 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001630 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1631 * where PS must be at least 8 bytes with the value 0xFF. */
1632 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001633
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001634 /* Read the whole buffer. Set pad_done to nonzero if we find
1635 * the 0x00 byte and remember the padding length in pad_count.
1636 * If there's a non-0xff byte in the padding, the padding is bad. */
1637 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001638 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001639 pad_done |= if_int( buf[i], 0, 1 );
1640 pad_count += if_int( pad_done, 0, 1 );
1641 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001642 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001643 }
1644
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001645 /* If pad_done is still zero, there's no data, only unfinished padding. */
1646 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001647
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001648 /* There must be at least 8 bytes of padding. */
1649 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001650
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001651 /* If the padding is valid, set plaintext_size to the number of
1652 * remaining bytes after stripping the padding. If the padding
1653 * is invalid, avoid leaking this fact through the size of the
1654 * output: use the maximum message size that fits in the output
1655 * buffer. Do it without branches to avoid leaking the padding
1656 * validity through timing. RSA keys are small enough that all the
1657 * size_t values involved fit in unsigned int. */
1658 plaintext_size = if_int( bad,
1659 (unsigned) plaintext_max_size,
1660 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001661
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001662 /* Set output_too_large to 0 if the plaintext fits in the output
1663 * buffer and to 1 otherwise. */
1664 output_too_large = size_greater_than( plaintext_size,
1665 plaintext_max_size );
1666
1667 /* Set ret without branches to avoid timing attacks. Return:
1668 * - INVALID_PADDING if the padding is bad (bad != 0).
1669 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1670 * plaintext does not fit in the output buffer.
1671 * - 0 if the padding is correct. */
1672 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1673 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1674 0 ) );
1675
1676 /* If the padding is bad or the plaintext is too large, zero the
1677 * data that we're about to copy to the output buffer.
1678 * We need to copy the same amount of data
1679 * from the same buffer whether the padding is good or not to
1680 * avoid leaking the padding validity through overall timing or
1681 * through memory or cache access patterns. */
1682 bad = all_or_nothing_int( bad | output_too_large );
1683 for( i = 11; i < ilen; i++ )
1684 buf[i] &= ~bad;
1685
1686 /* If the plaintext is too large, truncate it to the buffer size.
1687 * Copy anyway to avoid revealing the length through timing, because
1688 * revealing the length is as bad as revealing the padding validity
1689 * for a Bleichenbacher attack. */
1690 plaintext_size = if_int( output_too_large,
1691 (unsigned) plaintext_max_size,
1692 (unsigned) plaintext_size );
1693
1694 /* Move the plaintext to the leftmost position where it can start in
1695 * the working buffer, i.e. make it start plaintext_max_size from
1696 * the end of the buffer. Do this with a memory access trace that
1697 * does not depend on the plaintext size. After this move, the
1698 * starting location of the plaintext is no longer sensitive
1699 * information. */
1700 mem_move_to_left( buf + ilen - plaintext_max_size,
1701 plaintext_max_size,
1702 plaintext_max_size - plaintext_size );
1703
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001704 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1705 * buffer. If output_max_len is 0, then output may be an invalid pointer
1706 * and the result of memcpy() would be undefined; prevent undefined
1707 * behavior making sure to depend only on output_max_len (the size of the
1708 * user-provided output buffer), which is independent from plaintext
1709 * length, validity of padding, success of the decryption, and other
1710 * secrets. */
1711 if( output_max_len != 0 )
1712 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001713
1714 /* Report the amount of data we copied to the output buffer. In case
1715 * of errors (bad padding or output too large), the value of *olen
1716 * when this function returns is not specified. Making it equivalent
1717 * to the good case limits the risks of leaking the padding validity. */
1718 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001719
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001720cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001721 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001722
1723 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001724}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001726
1727/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001728 * Do an RSA operation, then remove the message padding
1729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001731 int (*f_rng)(void *, unsigned char *, size_t),
1732 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001733 int mode, size_t *olen,
1734 const unsigned char *input,
1735 unsigned char *output,
1736 size_t output_max_len)
1737{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001738 RSA_VALIDATE_RET( ctx != NULL );
1739 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1740 mode == MBEDTLS_RSA_PUBLIC );
1741 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1742 RSA_VALIDATE_RET( input != NULL );
1743 RSA_VALIDATE_RET( olen != NULL );
1744
Paul Bakkerb3869132013-02-28 17:21:01 +01001745 switch( ctx->padding )
1746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747#if defined(MBEDTLS_PKCS1_V15)
1748 case MBEDTLS_RSA_PKCS_V15:
1749 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001750 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001751#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#if defined(MBEDTLS_PKCS1_V21)
1754 case MBEDTLS_RSA_PKCS_V21:
1755 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001756 olen, input, output,
1757 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001758#endif
1759
1760 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001762 }
1763}
1764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001766/*
1767 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001770 int (*f_rng)(void *, unsigned char *, size_t),
1771 void *p_rng,
1772 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001774 unsigned int hashlen,
1775 const unsigned char *hash,
1776 unsigned char *sig )
1777{
1778 size_t olen;
1779 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001781 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001783 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 const mbedtls_md_info_t *md_info;
1785 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001786 RSA_VALIDATE_RET( ctx != NULL );
1787 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1788 mode == MBEDTLS_RSA_PUBLIC );
1789 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1790 hashlen == 0 ) ||
1791 hash != NULL );
1792 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1795 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001796
1797 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001799
1800 olen = ctx->len;
1801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001803 {
Simon Butcher02037452016-03-01 21:19:12 +00001804 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001806 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001810 }
1811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001813 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001817
Jaeden Amero3725bb22018-09-07 19:12:36 +01001818 /* Calculate the largest possible salt length. Normally this is the hash
1819 * length, which is the maximum length the salt can have. If there is not
1820 * enough room, use the maximum salt length that fits. The constraint is
1821 * that the hash length plus the salt length plus 2 bytes must be at most
1822 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1823 * (PKCS#1 v2.2) §9.1.1 step 3. */
1824 min_slen = hlen - 2;
1825 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001827 else if( olen >= hlen + hlen + 2 )
1828 slen = hlen;
1829 else
1830 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001831
1832 memset( sig, 0, olen );
1833
Simon Butcher02037452016-03-01 21:19:12 +00001834 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001835 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001837
Simon Butcher02037452016-03-01 21:19:12 +00001838 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001839 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001840 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001841 *p++ = 0x01;
1842 memcpy( p, salt, slen );
1843 p += slen;
1844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001846 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001847 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001848
Simon Butcher02037452016-03-01 21:19:12 +00001849 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001850 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1851 goto exit;
1852 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1853 goto exit;
1854 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1855 goto exit;
1856 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1857 goto exit;
1858 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1859 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001860
Simon Butcher02037452016-03-01 21:19:12 +00001861 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001862 if( msb % 8 == 0 )
1863 offset = 1;
1864
Simon Butcher02037452016-03-01 21:19:12 +00001865 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001866 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1867 &md_ctx ) ) != 0 )
1868 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001869
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001870 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001871 sig[0] &= 0xFF >> ( olen * 8 - msb );
1872
1873 p += hlen;
1874 *p++ = 0xBC;
1875
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001876 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001877
1878exit:
1879 mbedtls_md_free( &md_ctx );
1880
1881 if( ret != 0 )
1882 return( ret );
1883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 return( ( mode == MBEDTLS_RSA_PUBLIC )
1885 ? mbedtls_rsa_public( ctx, sig, sig )
1886 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001887}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001891/*
1892 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1893 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001894
1895/* Construct a PKCS v1.5 encoding of a hashed message
1896 *
1897 * This is used both for signature generation and verification.
1898 *
1899 * Parameters:
1900 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001901 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001902 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001903 * - hash: Buffer containing the hashed message or the raw data.
1904 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001905 * - dst: Buffer to hold the encoded message.
1906 *
1907 * Assumptions:
1908 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1909 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001910 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001911 *
1912 */
1913static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1914 unsigned int hashlen,
1915 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001916 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001917 unsigned char *dst )
1918{
1919 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001920 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001921 unsigned char *p = dst;
1922 const char *oid = NULL;
1923
1924 /* Are we signing hashed or raw data? */
1925 if( md_alg != MBEDTLS_MD_NONE )
1926 {
1927 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1928 if( md_info == NULL )
1929 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1930
1931 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1932 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1933
1934 hashlen = mbedtls_md_get_size( md_info );
1935
1936 /* Double-check that 8 + hashlen + oid_size can be used as a
1937 * 1-byte ASN.1 length encoding and that there's no overflow. */
1938 if( 8 + hashlen + oid_size >= 0x80 ||
1939 10 + hashlen < hashlen ||
1940 10 + hashlen + oid_size < 10 + hashlen )
1941 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1942
1943 /*
1944 * Static bounds check:
1945 * - Need 10 bytes for five tag-length pairs.
1946 * (Insist on 1-byte length encodings to protect against variants of
1947 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1948 * - Need hashlen bytes for hash
1949 * - Need oid_size bytes for hash alg OID.
1950 */
1951 if( nb_pad < 10 + hashlen + oid_size )
1952 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1953 nb_pad -= 10 + hashlen + oid_size;
1954 }
1955 else
1956 {
1957 if( nb_pad < hashlen )
1958 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1959
1960 nb_pad -= hashlen;
1961 }
1962
Hanno Becker2b2f8982017-09-27 17:10:03 +01001963 /* Need space for signature header and padding delimiter (3 bytes),
1964 * and 8 bytes for the minimal padding */
1965 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001966 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1967 nb_pad -= 3;
1968
1969 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001970 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001971
1972 /* Write signature header and padding */
1973 *p++ = 0;
1974 *p++ = MBEDTLS_RSA_SIGN;
1975 memset( p, 0xFF, nb_pad );
1976 p += nb_pad;
1977 *p++ = 0;
1978
1979 /* Are we signing raw data? */
1980 if( md_alg == MBEDTLS_MD_NONE )
1981 {
1982 memcpy( p, hash, hashlen );
1983 return( 0 );
1984 }
1985
1986 /* Signing hashed data, add corresponding ASN.1 structure
1987 *
1988 * DigestInfo ::= SEQUENCE {
1989 * digestAlgorithm DigestAlgorithmIdentifier,
1990 * digest Digest }
1991 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1992 * Digest ::= OCTET STRING
1993 *
1994 * Schematic:
1995 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1996 * TAG-NULL + LEN [ NULL ] ]
1997 * TAG-OCTET + LEN [ HASH ] ]
1998 */
1999 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002000 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002001 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002002 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002003 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002004 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002005 memcpy( p, oid, oid_size );
2006 p += oid_size;
2007 *p++ = MBEDTLS_ASN1_NULL;
2008 *p++ = 0x00;
2009 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002010 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002011 memcpy( p, hash, hashlen );
2012 p += hashlen;
2013
2014 /* Just a sanity-check, should be automatic
2015 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002016 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002017 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002018 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002019 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2020 }
2021
2022 return( 0 );
2023}
2024
Paul Bakkerb3869132013-02-28 17:21:01 +01002025/*
2026 * Do an RSA operation to sign the message digest
2027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002029 int (*f_rng)(void *, unsigned char *, size_t),
2030 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002031 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002033 unsigned int hashlen,
2034 const unsigned char *hash,
2035 unsigned char *sig )
2036{
Janos Follath24eed8d2019-11-22 13:21:35 +00002037 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002038 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002039
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002040 RSA_VALIDATE_RET( ctx != NULL );
2041 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2042 mode == MBEDTLS_RSA_PUBLIC );
2043 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2044 hashlen == 0 ) ||
2045 hash != NULL );
2046 RSA_VALIDATE_RET( sig != NULL );
2047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2049 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002050
Hanno Beckerfdf38032017-09-06 12:35:55 +01002051 /*
2052 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2053 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002054
Hanno Beckerfdf38032017-09-06 12:35:55 +01002055 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2056 ctx->len, sig ) ) != 0 )
2057 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002058
2059 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002060 * Call respective RSA primitive
2061 */
2062
2063 if( mode == MBEDTLS_RSA_PUBLIC )
2064 {
2065 /* Skip verification on a public key operation */
2066 return( mbedtls_rsa_public( ctx, sig, sig ) );
2067 }
2068
2069 /* Private key operation
2070 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002071 * In order to prevent Lenstra's attack, make the signature in a
2072 * temporary buffer and check it before returning it.
2073 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002074
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002075 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002076 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002077 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2078
Hanno Beckerfdf38032017-09-06 12:35:55 +01002079 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002080 if( verif == NULL )
2081 {
2082 mbedtls_free( sig_try );
2083 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2084 }
2085
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002086 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2087 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2088
Hanno Becker171a8f12017-09-06 12:32:16 +01002089 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002090 {
2091 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2092 goto cleanup;
2093 }
2094
2095 memcpy( sig, sig_try, ctx->len );
2096
2097cleanup:
2098 mbedtls_free( sig_try );
2099 mbedtls_free( verif );
2100
2101 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002104
2105/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 * Do an RSA operation to sign the message digest
2107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002109 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002110 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002113 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002114 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 unsigned char *sig )
2116{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002117 RSA_VALIDATE_RET( ctx != NULL );
2118 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2119 mode == MBEDTLS_RSA_PUBLIC );
2120 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2121 hashlen == 0 ) ||
2122 hash != NULL );
2123 RSA_VALIDATE_RET( sig != NULL );
2124
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 switch( ctx->padding )
2126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127#if defined(MBEDTLS_PKCS1_V15)
2128 case MBEDTLS_RSA_PKCS_V15:
2129 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002130 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002131#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133#if defined(MBEDTLS_PKCS1_V21)
2134 case MBEDTLS_RSA_PKCS_V21:
2135 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002136 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002137#endif
2138
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002142}
2143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002145/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002146 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002149 int (*f_rng)(void *, unsigned char *, size_t),
2150 void *p_rng,
2151 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002153 unsigned int hashlen,
2154 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002156 int expected_salt_len,
2157 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002158{
Janos Follath24eed8d2019-11-22 13:21:35 +00002159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002160 size_t siglen;
2161 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002162 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002164 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002165 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002166 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 const mbedtls_md_info_t *md_info;
2168 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002169 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002170
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002171 RSA_VALIDATE_RET( ctx != NULL );
2172 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2173 mode == MBEDTLS_RSA_PUBLIC );
2174 RSA_VALIDATE_RET( sig != NULL );
2175 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2176 hashlen == 0 ) ||
2177 hash != NULL );
2178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2180 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002181
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 siglen = ctx->len;
2183
Paul Bakker27fdf462011-06-09 13:55:13 +00002184 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2188 ? mbedtls_rsa_public( ctx, sig, buf )
2189 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
2191 if( ret != 0 )
2192 return( ret );
2193
2194 p = buf;
2195
Paul Bakkerb3869132013-02-28 17:21:01 +01002196 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 {
Simon Butcher02037452016-03-01 21:19:12 +00002201 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002203 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002207 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002210 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002214
Paul Bakkerb3869132013-02-28 17:21:01 +01002215 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002216
Simon Butcher02037452016-03-01 21:19:12 +00002217 /*
2218 * Note: EMSA-PSS verification is over the length of N - 1 bits
2219 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002220 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002221
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002222 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2223 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2224
Simon Butcher02037452016-03-01 21:19:12 +00002225 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002226 if( msb % 8 == 0 )
2227 {
2228 p++;
2229 siglen -= 1;
2230 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002231
Gilles Peskine139108a2017-10-18 19:03:42 +02002232 if( siglen < hlen + 2 )
2233 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2234 hash_start = p + siglen - hlen - 1;
2235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002237 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002238 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002239
Jaeden Amero66954e12018-01-25 16:05:54 +00002240 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2241 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002242 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002243
Paul Bakkerb3869132013-02-28 17:21:01 +01002244 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002245
Gilles Peskine6a54b022017-10-17 19:02:13 +02002246 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002247 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002248
Gilles Peskine91048a32017-10-19 17:46:14 +02002249 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002250 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002251 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2252 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002253 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002254
Gilles Peskine6a54b022017-10-17 19:02:13 +02002255 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002258 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002259 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002260 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2261 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002262 }
2263
Simon Butcher02037452016-03-01 21:19:12 +00002264 /*
2265 * Generate H = Hash( M' )
2266 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002267 ret = mbedtls_md_starts( &md_ctx );
2268 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002269 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002270 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2271 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002272 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002273 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2274 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002275 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002276 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2277 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002278 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002279 ret = mbedtls_md_finish( &md_ctx, result );
2280 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002281 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002282
Jaeden Amero66954e12018-01-25 16:05:54 +00002283 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002284 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002285 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002286 goto exit;
2287 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002288
2289exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002291
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002292 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002293}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002294
2295/*
2296 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002299 int (*f_rng)(void *, unsigned char *, size_t),
2300 void *p_rng,
2301 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002303 unsigned int hashlen,
2304 const unsigned char *hash,
2305 const unsigned char *sig )
2306{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002307 mbedtls_md_type_t mgf1_hash_id;
2308 RSA_VALIDATE_RET( ctx != NULL );
2309 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2310 mode == MBEDTLS_RSA_PUBLIC );
2311 RSA_VALIDATE_RET( sig != NULL );
2312 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2313 hashlen == 0 ) ||
2314 hash != NULL );
2315
2316 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002318 : md_alg;
2319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002321 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002323 sig ) );
2324
2325}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002329/*
2330 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002333 int (*f_rng)(void *, unsigned char *, size_t),
2334 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002335 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002337 unsigned int hashlen,
2338 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002339 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002340{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002341 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002342 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002343 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002344
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002345 RSA_VALIDATE_RET( ctx != NULL );
2346 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2347 mode == MBEDTLS_RSA_PUBLIC );
2348 RSA_VALIDATE_RET( sig != NULL );
2349 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2350 hashlen == 0 ) ||
2351 hash != NULL );
2352
2353 sig_len = ctx->len;
2354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2356 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002357
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002358 /*
2359 * Prepare expected PKCS1 v1.5 encoding of hash.
2360 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002361
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002362 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2363 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2364 {
2365 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2366 goto cleanup;
2367 }
2368
2369 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2370 encoded_expected ) ) != 0 )
2371 goto cleanup;
2372
2373 /*
2374 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2375 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002378 ? mbedtls_rsa_public( ctx, sig, encoded )
2379 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002380 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002381 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002382
Simon Butcher02037452016-03-01 21:19:12 +00002383 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002384 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002385 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002386
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002387 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2388 sig_len ) ) != 0 )
2389 {
2390 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2391 goto cleanup;
2392 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002393
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002394cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002395
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002396 if( encoded != NULL )
2397 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002398 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002399 mbedtls_free( encoded );
2400 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002401
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002402 if( encoded_expected != NULL )
2403 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002404 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002405 mbedtls_free( encoded_expected );
2406 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002407
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002408 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002411
2412/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002413 * Do an RSA operation and check the message digest
2414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002416 int (*f_rng)(void *, unsigned char *, size_t),
2417 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002418 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002420 unsigned int hashlen,
2421 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002422 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002423{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002424 RSA_VALIDATE_RET( ctx != NULL );
2425 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2426 mode == MBEDTLS_RSA_PUBLIC );
2427 RSA_VALIDATE_RET( sig != NULL );
2428 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2429 hashlen == 0 ) ||
2430 hash != NULL );
2431
Paul Bakkerb3869132013-02-28 17:21:01 +01002432 switch( ctx->padding )
2433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434#if defined(MBEDTLS_PKCS1_V15)
2435 case MBEDTLS_RSA_PKCS_V15:
2436 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002437 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002438#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440#if defined(MBEDTLS_PKCS1_V21)
2441 case MBEDTLS_RSA_PKCS_V21:
2442 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002443 hashlen, hash, sig );
2444#endif
2445
2446 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002448 }
2449}
2450
2451/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002452 * Copy the components of an RSA key
2453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002455{
Janos Follath24eed8d2019-11-22 13:21:35 +00002456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002457 RSA_VALIDATE_RET( dst != NULL );
2458 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002459
2460 dst->ver = src->ver;
2461 dst->len = src->len;
2462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2464 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2467 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2468 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002469
2470#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2472 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2473 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2475 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002476#endif
2477
2478 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2481 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002482
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002483 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002484 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002485
2486cleanup:
2487 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002489
2490 return( ret );
2491}
2492
2493/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 * Free the components of an RSA key
2495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002497{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002498 if( ctx == NULL )
2499 return;
2500
2501 mbedtls_mpi_free( &ctx->Vi );
2502 mbedtls_mpi_free( &ctx->Vf );
2503 mbedtls_mpi_free( &ctx->RN );
2504 mbedtls_mpi_free( &ctx->D );
2505 mbedtls_mpi_free( &ctx->Q );
2506 mbedtls_mpi_free( &ctx->P );
2507 mbedtls_mpi_free( &ctx->E );
2508 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002509
Hanno Becker33c30a02017-08-23 07:00:22 +01002510#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002511 mbedtls_mpi_free( &ctx->RQ );
2512 mbedtls_mpi_free( &ctx->RP );
2513 mbedtls_mpi_free( &ctx->QP );
2514 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002515 mbedtls_mpi_free( &ctx->DP );
2516#endif /* MBEDTLS_RSA_NO_CRT */
2517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#if defined(MBEDTLS_THREADING_C)
2519 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002520#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002521}
2522
Hanno Beckerab377312017-08-23 16:24:51 +01002523#endif /* !MBEDTLS_RSA_ALT */
2524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002527#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
2529/*
2530 * Example RSA-1024 keypair, for test purposes
2531 */
2532#define KEY_LEN 128
2533
2534#define RSA_N "9292758453063D803DD603D5E777D788" \
2535 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2536 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2537 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2538 "93A89813FBF3C4F8066D2D800F7C38A8" \
2539 "1AE31942917403FF4946B0A83D3D3E05" \
2540 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2541 "5E94BB77B07507233A0BC7BAC8F90F79"
2542
2543#define RSA_E "10001"
2544
2545#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2546 "66CA472BC44D253102F8B4A9D3BFA750" \
2547 "91386C0077937FE33FA3252D28855837" \
2548 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2549 "DF79C5CE07EE72C7F123142198164234" \
2550 "CABB724CF78B8173B9F880FC86322407" \
2551 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2552 "071513A1E85B5DFA031F21ECAE91A34D"
2553
2554#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2555 "2C01CAD19EA484A87EA4377637E75500" \
2556 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2557 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2558
2559#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2560 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2561 "910E4168387E3C30AA1E00C339A79508" \
2562 "8452DD96A9A5EA5D9DCA68DA636032AF"
2563
Paul Bakker5121ce52009-01-03 21:22:43 +00002564#define PT_LEN 24
2565#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2566 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002569static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002570{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002571#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002572 size_t i;
2573
Paul Bakker545570e2010-07-18 09:00:25 +00002574 if( rng_state != NULL )
2575 rng_state = NULL;
2576
Paul Bakkera3d195c2011-11-27 21:07:34 +00002577 for( i = 0; i < len; ++i )
2578 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002579#else
2580 if( rng_state != NULL )
2581 rng_state = NULL;
2582
2583 arc4random_buf( output, len );
2584#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002585
Paul Bakkera3d195c2011-11-27 21:07:34 +00002586 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002587}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002589
Paul Bakker5121ce52009-01-03 21:22:43 +00002590/*
2591 * Checkup routine
2592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002594{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002595 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002597 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 unsigned char rsa_plaintext[PT_LEN];
2600 unsigned char rsa_decrypted[PT_LEN];
2601 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002603 unsigned char sha1sum[20];
2604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Hanno Becker3a701162017-08-22 13:52:43 +01002606 mbedtls_mpi K;
2607
2608 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
Hanno Becker3a701162017-08-22 13:52:43 +01002611 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2612 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2613 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2614 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2615 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2616 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2617 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2618 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2619 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2620 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2621
Hanno Becker7f25f852017-10-10 16:56:22 +01002622 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
2624 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2628 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 {
2630 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Hanno Becker5bc87292017-05-03 15:09:31 +01002633 ret = 1;
2634 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 }
2636
2637 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
2640 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2641
Hanno Becker98838b02017-10-02 13:16:10 +01002642 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2643 PT_LEN, rsa_plaintext,
2644 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 {
2646 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Hanno Becker5bc87292017-05-03 15:09:31 +01002649 ret = 1;
2650 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 }
2652
2653 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Hanno Becker98838b02017-10-02 13:16:10 +01002656 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2657 &len, rsa_ciphertext, rsa_decrypted,
2658 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 {
2660 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
Hanno Becker5bc87292017-05-03 15:09:31 +01002663 ret = 1;
2664 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 }
2666
2667 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2668 {
2669 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002671
Hanno Becker5bc87292017-05-03 15:09:31 +01002672 ret = 1;
2673 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 }
2675
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002676 if( verbose != 0 )
2677 mbedtls_printf( "passed\n" );
2678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002681 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002683 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002684 {
2685 if( verbose != 0 )
2686 mbedtls_printf( "failed\n" );
2687
2688 return( 1 );
2689 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
Hanno Becker98838b02017-10-02 13:16:10 +01002691 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2692 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2693 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 {
2695 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
Hanno Becker5bc87292017-05-03 15:09:31 +01002698 ret = 1;
2699 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 }
2701
2702 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Hanno Becker98838b02017-10-02 13:16:10 +01002705 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2706 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2707 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 {
2709 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
Hanno Becker5bc87292017-05-03 15:09:31 +01002712 ret = 1;
2713 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 }
2715
2716 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002717 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002720 if( verbose != 0 )
2721 mbedtls_printf( "\n" );
2722
Paul Bakker3d8fb632014-04-17 12:42:41 +02002723cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002724 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 mbedtls_rsa_free( &rsa );
2726#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002727 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002729 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730}
2731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734#endif /* MBEDTLS_RSA_C */