blob: c6c5956dc0b8b2df99838b36ddd62b405f84b7a4 [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020040#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010045#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050047#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000048#include "mbedtls/error.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
gufe44c2620da2020-08-03 17:56:50 +020056#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#else
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020065#define mbedtls_calloc calloc
66#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#endif
68
Hanno Beckera565f542017-10-11 11:00:19 +010069#if !defined(MBEDTLS_RSA_ALT)
70
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050071/* Parameter validation macros */
72#define RSA_VALIDATE_RET( cond ) \
73 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
74#define RSA_VALIDATE( cond ) \
75 MBEDTLS_INTERNAL_VALIDATE( cond )
76
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010077#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +010078/* constant-time buffer comparison */
79static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
80{
81 size_t i;
82 const unsigned char *A = (const unsigned char *) a;
83 const unsigned char *B = (const unsigned char *) b;
84 unsigned char diff = 0;
85
86 for( i = 0; i < n; i++ )
87 diff |= A[i] ^ B[i];
88
89 return( diff );
90}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010091#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +010092
Hanno Becker617c1ae2017-08-23 14:11:24 +010093int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
94 const mbedtls_mpi *N,
95 const mbedtls_mpi *P, const mbedtls_mpi *Q,
96 const mbedtls_mpi *D, const mbedtls_mpi *E )
97{
Janos Follath24eed8d2019-11-22 13:21:35 +000098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050099 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100100
101 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
102 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
103 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
104 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
105 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
106 {
107 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
108 }
109
110 if( N != NULL )
111 ctx->len = mbedtls_mpi_size( &ctx->N );
112
113 return( 0 );
114}
115
116int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100117 unsigned char const *N, size_t N_len,
118 unsigned char const *P, size_t P_len,
119 unsigned char const *Q, size_t Q_len,
120 unsigned char const *D, size_t D_len,
121 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100122{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000123 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100125
126 if( N != NULL )
127 {
128 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
129 ctx->len = mbedtls_mpi_size( &ctx->N );
130 }
131
132 if( P != NULL )
133 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
134
135 if( Q != NULL )
136 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
137
138 if( D != NULL )
139 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
140
141 if( E != NULL )
142 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
143
144cleanup:
145
146 if( ret != 0 )
147 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
148
149 return( 0 );
150}
151
Hanno Becker705fc682017-10-10 17:57:02 +0100152/*
153 * Checks whether the context fields are set in such a way
154 * that the RSA primitives will be able to execute without error.
155 * It does *not* make guarantees for consistency of the parameters.
156 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100157static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
158 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100159{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100160#if !defined(MBEDTLS_RSA_NO_CRT)
161 /* blinding_needed is only used for NO_CRT to decide whether
162 * P,Q need to be present or not. */
163 ((void) blinding_needed);
164#endif
165
Hanno Becker3a760a12018-01-05 08:14:49 +0000166 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
167 ctx->len > MBEDTLS_MPI_MAX_SIZE )
168 {
Hanno Becker705fc682017-10-10 17:57:02 +0100169 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000170 }
Hanno Becker705fc682017-10-10 17:57:02 +0100171
172 /*
173 * 1. Modular exponentiation needs positive, odd moduli.
174 */
175
176 /* Modular exponentiation wrt. N is always used for
177 * RSA public key operations. */
178 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
179 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
180 {
181 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
182 }
183
184#if !defined(MBEDTLS_RSA_NO_CRT)
185 /* Modular exponentiation for P and Q is only
186 * used for private key operations and if CRT
187 * is used. */
188 if( is_priv &&
189 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
190 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
191 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
192 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
193 {
194 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
195 }
196#endif /* !MBEDTLS_RSA_NO_CRT */
197
198 /*
199 * 2. Exponents must be positive
200 */
201
202 /* Always need E for public key operations */
203 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
204 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
205
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100206#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100207 /* For private key operations, use D or DP & DQ
208 * as (unblinded) exponents. */
209 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
210 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
211#else
212 if( is_priv &&
213 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
214 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
215 {
216 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
217 }
218#endif /* MBEDTLS_RSA_NO_CRT */
219
220 /* Blinding shouldn't make exponents negative either,
221 * so check that P, Q >= 1 if that hasn't yet been
222 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100223#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100224 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100225 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
226 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
227 {
228 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
229 }
230#endif
231
232 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100233 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100234#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100235 if( is_priv &&
236 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
237 {
238 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
239 }
240#endif
241
242 return( 0 );
243}
244
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100245int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100246{
247 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500248 int have_N, have_P, have_Q, have_D, have_E;
249#if !defined(MBEDTLS_RSA_NO_CRT)
250 int have_DP, have_DQ, have_QP;
251#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500252 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100253
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500254 RSA_VALIDATE_RET( ctx != NULL );
255
256 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
257 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
258 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
259 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
260 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500261
262#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500263 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
264 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
265 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500266#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100267
Hanno Becker617c1ae2017-08-23 14:11:24 +0100268 /*
269 * Check whether provided parameters are enough
270 * to deduce all others. The following incomplete
271 * parameter sets for private keys are supported:
272 *
273 * (1) P, Q missing.
274 * (2) D and potentially N missing.
275 *
276 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100277
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500278 n_missing = have_P && have_Q && have_D && have_E;
279 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
280 d_missing = have_P && have_Q && !have_D && have_E;
281 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100282
283 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500284 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100285
Hanno Becker617c1ae2017-08-23 14:11:24 +0100286 if( !is_priv && !is_pub )
287 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
288
289 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100290 * Step 1: Deduce N if P, Q are provided.
291 */
292
293 if( !have_N && have_P && have_Q )
294 {
295 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
296 &ctx->Q ) ) != 0 )
297 {
298 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
299 }
300
301 ctx->len = mbedtls_mpi_size( &ctx->N );
302 }
303
304 /*
305 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100306 */
307
308 if( pq_missing )
309 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100310 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100311 &ctx->P, &ctx->Q );
312 if( ret != 0 )
313 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
314
315 }
316 else if( d_missing )
317 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100318 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
319 &ctx->Q,
320 &ctx->E,
321 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100322 {
323 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
324 }
325 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100326
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100328 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100329 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100330 */
331
Hanno Becker23344b52017-08-23 07:43:27 +0100332#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500333 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100334 {
335 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
336 &ctx->DP, &ctx->DQ, &ctx->QP );
337 if( ret != 0 )
338 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
339 }
Hanno Becker23344b52017-08-23 07:43:27 +0100340#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100341
342 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100343 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100344 */
345
Hanno Beckerebd2c022017-10-12 10:54:53 +0100346 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100347}
348
Hanno Becker617c1ae2017-08-23 14:11:24 +0100349int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
350 unsigned char *N, size_t N_len,
351 unsigned char *P, size_t P_len,
352 unsigned char *Q, size_t Q_len,
353 unsigned char *D, size_t D_len,
354 unsigned char *E, size_t E_len )
355{
356 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500357 int is_priv;
358 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100359
360 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500361 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100362 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
363 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
364 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
365 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
366 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
367
368 if( !is_priv )
369 {
370 /* If we're trying to export private parameters for a public key,
371 * something must be wrong. */
372 if( P != NULL || Q != NULL || D != NULL )
373 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
374
375 }
376
377 if( N != NULL )
378 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
379
380 if( P != NULL )
381 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
382
383 if( Q != NULL )
384 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
385
386 if( D != NULL )
387 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
388
389 if( E != NULL )
390 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100391
392cleanup:
393
394 return( ret );
395}
396
Hanno Becker617c1ae2017-08-23 14:11:24 +0100397int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
398 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
399 mbedtls_mpi *D, mbedtls_mpi *E )
400{
Janos Follath24eed8d2019-11-22 13:21:35 +0000401 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500402 int is_priv;
403 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100404
405 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500406 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100407 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
408 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
409 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
410 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
411 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
412
413 if( !is_priv )
414 {
415 /* If we're trying to export private parameters for a public key,
416 * something must be wrong. */
417 if( P != NULL || Q != NULL || D != NULL )
418 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
419
420 }
421
422 /* Export all requested core parameters. */
423
424 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
425 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
426 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
427 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
428 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
429 {
430 return( ret );
431 }
432
433 return( 0 );
434}
435
436/*
437 * Export CRT parameters
438 * This must also be implemented if CRT is not used, for being able to
439 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
440 * can be used in this case.
441 */
442int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
443 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
444{
Janos Follath24eed8d2019-11-22 13:21:35 +0000445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500446 int is_priv;
447 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100448
449 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500450 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100451 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
452 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
453 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
454 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
455 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
456
457 if( !is_priv )
458 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
459
Hanno Beckerdc95c892017-08-23 06:57:02 +0100460#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100461 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100462 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
463 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
464 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
465 {
Hanno Beckerdc95c892017-08-23 06:57:02 +0100466 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100467 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100468#else
469 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
470 DP, DQ, QP ) ) != 0 )
471 {
472 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
473 }
474#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100475
476 return( 0 );
477}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100478
Paul Bakker5121ce52009-01-03 21:22:43 +0000479/*
480 * Initialize an RSA context
481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000484 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000485{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500486 RSA_VALIDATE( ctx != NULL );
487 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
488 padding == MBEDTLS_RSA_PKCS_V21 );
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_THREADING_C)
495 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200496#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000497}
498
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100499/*
500 * Set padding for an existing RSA context
501 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500502void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
503 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100504{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500505 RSA_VALIDATE( ctx != NULL );
506 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
507 padding == MBEDTLS_RSA_PKCS_V21 );
508
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100509 ctx->padding = padding;
510 ctx->hash_id = hash_id;
511}
512
Hanno Becker617c1ae2017-08-23 14:11:24 +0100513/*
514 * Get length in bytes of RSA modulus
515 */
516
517size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
518{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100519 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100520}
521
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525/*
526 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800527 *
528 * This generation method follows the RSA key pair generation procedure of
529 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000532 int (*f_rng)(void *, unsigned char *, size_t),
533 void *p_rng,
534 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000535{
Janos Follath24eed8d2019-11-22 13:21:35 +0000536 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800537 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100538 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500539 RSA_VALIDATE_RET( ctx != NULL );
540 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500542 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
Janos Follathef441782016-09-21 13:18:12 +0100543 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
544
Janos Follathb8fc1b02018-09-03 15:37:01 +0100545 /*
546 * If the modulus is 1024 bit long or shorter, then the security strength of
547 * the RSA algorithm is less than or equal to 80 bits and therefore an error
548 * rate of 2^-80 is sufficient.
549 */
550 if( nbits > 1024 )
551 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
552
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100553 mbedtls_mpi_init( &H );
554 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800555 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557 /*
558 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800559 * 1. |P-Q| > 2^( nbits / 2 - 100 )
560 * 2. GCD( E, (P-1)*(Q-1) ) == 1
561 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
565 do
566 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100567 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
568 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Janos Follathb8fc1b02018-09-03 15:37:01 +0100570 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
571 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800573 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
574 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
575 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 continue;
577
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800578 /* not required by any standards, but some users rely on the fact that P > Q */
579 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100580 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100581
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100582 /* Temporarily replace P,Q by P-1, Q-1 */
583 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
584 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
585 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800586
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800587 /* 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 +0200588 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800589 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
590 continue;
591
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800592 /* 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 -0800593 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
594 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
595 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
596
597 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
598 continue;
599
600 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000601 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800602 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100604 /* Restore P,Q */
605 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
606 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
607
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800608 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
609
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100610 ctx->len = mbedtls_mpi_size( &ctx->N );
611
Jethro Beekman97f95c92018-02-13 15:50:36 -0800612#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 * DP = D mod (P - 1)
615 * DQ = D mod (Q - 1)
616 * QP = Q^-1 mod P
617 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100618 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
619 &ctx->DP, &ctx->DQ, &ctx->QP ) );
620#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Hanno Becker83aad1f2017-08-23 06:45:10 +0100622 /* Double-check */
623 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
625cleanup:
626
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100627 mbedtls_mpi_free( &H );
628 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800629 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
631 if( ret != 0 )
632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_rsa_free( ctx );
634 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 }
636
Paul Bakker48377d92013-08-30 12:06:24 +0200637 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000638}
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000641
642/*
643 * Check a public RSA key
644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000646{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500647 RSA_VALIDATE_RET( ctx != NULL );
648
Hanno Beckerebd2c022017-10-12 10:54:53 +0100649 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000651
Hanno Becker3a760a12018-01-05 08:14:49 +0000652 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100655 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Hanno Becker705fc682017-10-10 17:57:02 +0100657 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
658 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100662 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
664 return( 0 );
665}
666
667/*
Hanno Becker705fc682017-10-10 17:57:02 +0100668 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000671{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500672 RSA_VALIDATE_RET( ctx != NULL );
673
Hanno Becker705fc682017-10-10 17:57:02 +0100674 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100675 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 {
Hanno Becker98838b02017-10-02 13:16:10 +0100677 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 }
Paul Bakker48377d92013-08-30 12:06:24 +0200679
Hanno Becker98838b02017-10-02 13:16:10 +0100680 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100681 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100683 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000685
Hanno Beckerb269a852017-08-25 08:03:21 +0100686#if !defined(MBEDTLS_RSA_NO_CRT)
687 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
688 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
689 {
690 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
691 }
692#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000693
694 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000695}
696
697/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100698 * Check if contexts holding a public and private key match
699 */
Hanno Becker98838b02017-10-02 13:16:10 +0100700int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
701 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100702{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500703 RSA_VALIDATE_RET( pub != NULL );
704 RSA_VALIDATE_RET( prv != NULL );
705
Hanno Becker98838b02017-10-02 13:16:10 +0100706 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100710 }
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
713 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100716 }
717
718 return( 0 );
719}
720
721/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 * Do an RSA public key operation
723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000725 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 unsigned char *output )
727{
Janos Follath24eed8d2019-11-22 13:21:35 +0000728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000729 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500731 RSA_VALIDATE_RET( ctx != NULL );
732 RSA_VALIDATE_RET( input != NULL );
733 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Hanno Beckerebd2c022017-10-12 10:54:53 +0100735 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100736 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200740#if defined(MBEDTLS_THREADING_C)
741 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
742 return( ret );
743#endif
744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000748 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200749 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
750 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 }
752
753 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
755 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
757cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200759 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
760 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100761#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000764
765 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
768 return( 0 );
769}
770
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200771/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200772 * Generate or update blinding values, see section 10 of:
773 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200774 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200775 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200776 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200777static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200778 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
779{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200780 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200781
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200782 if( ctx->Vf.p != NULL )
783 {
784 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
786 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
787 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
788 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200789
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200790 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200791 }
792
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200793 /* Unblinding value: Vf = random number, invertible mod N */
794 do {
795 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
799 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
800 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200801
802 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
804 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 +0200805
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200806
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200807cleanup:
808 return( ret );
809}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200810
Paul Bakker5121ce52009-01-03 21:22:43 +0000811/*
Janos Follathe81102e2017-03-22 13:38:28 +0000812 * Exponent blinding supposed to prevent side-channel attacks using multiple
813 * traces of measurements to recover the RSA key. The more collisions are there,
814 * the more bits of the key can be recovered. See [3].
815 *
816 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
817 * observations on avarage.
818 *
819 * For example with 28 byte blinding to achieve 2 collisions the adversary has
820 * to make 2^112 observations on avarage.
821 *
822 * (With the currently (as of 2017 April) known best algorithms breaking 2048
823 * bit RSA requires approximately as much time as trying out 2^112 random keys.
824 * Thus in this sense with 28 byte blinding the security is not reduced by
825 * side-channel attacks like the one in [3])
826 *
827 * This countermeasure does not help if the key recovery is possible with a
828 * single trace.
829 */
830#define RSA_EXPONENT_BLINDING 28
831
832/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 * Do an RSA private key operation
834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200836 int (*f_rng)(void *, unsigned char *, size_t),
837 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000838 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 unsigned char *output )
840{
Janos Follath24eed8d2019-11-22 13:21:35 +0000841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000842 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100843
844 /* Temporary holding the result */
845 mbedtls_mpi T;
846
847 /* Temporaries holding P-1, Q-1 and the
848 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000849 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100850
851#if !defined(MBEDTLS_RSA_NO_CRT)
852 /* Temporaries holding the results mod p resp. mod q. */
853 mbedtls_mpi TP, TQ;
854
855 /* Temporaries holding the blinded exponents for
856 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000857 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100858
859 /* Pointers to actual exponents to be used - either the unblinded
860 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000861 mbedtls_mpi *DP = &ctx->DP;
862 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100863#else
864 /* Temporary holding the blinded exponent (if used). */
865 mbedtls_mpi D_blind;
866
867 /* Pointer to actual exponent to be used - either the unblinded
868 * or the blinded one, depending on the presence of a PRNG. */
869 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100870#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100871
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100872 /* Temporaries holding the initial input and the double
873 * checked result; should be the same in the end. */
874 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000875
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500876 RSA_VALIDATE_RET( ctx != NULL );
877 RSA_VALIDATE_RET( input != NULL );
878 RSA_VALIDATE_RET( output != NULL );
879
Hanno Beckerebd2c022017-10-12 10:54:53 +0100880 if( rsa_check_context( ctx, 1 /* private key checks */,
881 f_rng != NULL /* blinding y/n */ ) != 0 )
882 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100883 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100884 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100885
Hanno Becker06811ce2017-05-03 15:10:34 +0100886#if defined(MBEDTLS_THREADING_C)
887 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
888 return( ret );
889#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000890
Hanno Becker06811ce2017-05-03 15:10:34 +0100891 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100892 mbedtls_mpi_init( &T );
893
894 mbedtls_mpi_init( &P1 );
895 mbedtls_mpi_init( &Q1 );
896 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000897
Janos Follathf9203b42017-03-22 15:13:15 +0000898 if( f_rng != NULL )
899 {
Janos Follathe81102e2017-03-22 13:38:28 +0000900#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000901 mbedtls_mpi_init( &D_blind );
902#else
903 mbedtls_mpi_init( &DP_blind );
904 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000905#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000906 }
Janos Follathe81102e2017-03-22 13:38:28 +0000907
Hanno Becker06811ce2017-05-03 15:10:34 +0100908#if !defined(MBEDTLS_RSA_NO_CRT)
909 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200910#endif
911
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100912 mbedtls_mpi_init( &I );
913 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100914
915 /* End of MPI initialization */
916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
918 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200920 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
921 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000922 }
923
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100924 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100925
Paul Bakkerf451bac2013-08-30 15:37:02 +0200926 if( f_rng != NULL )
927 {
928 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200929 * Blinding
930 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200931 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200932 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
933 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000935
Janos Follathe81102e2017-03-22 13:38:28 +0000936 /*
937 * Exponent blinding
938 */
939 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
940 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
941
Janos Follathf9203b42017-03-22 15:13:15 +0000942#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000943 /*
944 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
945 */
946 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
947 f_rng, p_rng ) );
948 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
949 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
950 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
951
952 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000953#else
954 /*
955 * DP_blind = ( P - 1 ) * R + DP
956 */
957 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
958 f_rng, p_rng ) );
959 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
960 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
961 &ctx->DP ) );
962
963 DP = &DP_blind;
964
965 /*
966 * DQ_blind = ( Q - 1 ) * R + DQ
967 */
968 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
969 f_rng, p_rng ) );
970 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
971 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
972 &ctx->DQ ) );
973
974 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000975#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200976 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000979 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100980#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200981 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000982 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100984 * TP = input ^ dP mod P
985 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100987
988 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
989 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000990
991 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100992 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100994 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
995 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
996 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
998 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100999 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001001 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1002 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001004
Paul Bakkerf451bac2013-08-30 15:37:02 +02001005 if( f_rng != NULL )
1006 {
1007 /*
1008 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001009 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001010 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001011 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001013 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
Hanno Becker2dec5e82017-10-03 07:49:52 +01001015 /* Verify the result to prevent glitching attacks. */
1016 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1017 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001018 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001019 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001020 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1021 goto cleanup;
1022 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001023
Paul Bakker5121ce52009-01-03 21:22:43 +00001024 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
1027cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001029 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1030 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001031#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001032
Hanno Becker06811ce2017-05-03 15:10:34 +01001033 mbedtls_mpi_free( &P1 );
1034 mbedtls_mpi_free( &Q1 );
1035 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001036
1037 if( f_rng != NULL )
1038 {
Janos Follathe81102e2017-03-22 13:38:28 +00001039#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001040 mbedtls_mpi_free( &D_blind );
1041#else
1042 mbedtls_mpi_free( &DP_blind );
1043 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001044#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001045 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
Hanno Becker06811ce2017-05-03 15:10:34 +01001047 mbedtls_mpi_free( &T );
1048
1049#if !defined(MBEDTLS_RSA_NO_CRT)
1050 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1051#endif
1052
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001053 mbedtls_mpi_free( &C );
1054 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001055
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
1059 return( 0 );
1060}
1061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001063/**
1064 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1065 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001066 * \param dst buffer to mask
1067 * \param dlen length of destination buffer
1068 * \param src source of the mask generation
1069 * \param slen length of the source buffer
1070 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001071 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001072static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001076 unsigned char counter[4];
1077 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001078 unsigned int hlen;
1079 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001080 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001083 memset( counter, 0, 4 );
1084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001086
Simon Butcher02037452016-03-01 21:19:12 +00001087 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001088 p = dst;
1089
1090 while( dlen > 0 )
1091 {
1092 use_len = hlen;
1093 if( dlen < hlen )
1094 use_len = dlen;
1095
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001096 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1097 goto exit;
1098 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1099 goto exit;
1100 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1101 goto exit;
1102 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1103 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104
1105 for( i = 0; i < use_len; ++i )
1106 *p++ ^= mask[i];
1107
1108 counter[3]++;
1109
1110 dlen -= use_len;
1111 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001112
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001113exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001114 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001115
1116 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001117}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001121/*
1122 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001125 int (*f_rng)(void *, unsigned char *, size_t),
1126 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001127 int mode,
1128 const unsigned char *label, size_t label_len,
1129 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001130 const unsigned char *input,
1131 unsigned char *output )
1132{
1133 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001135 unsigned char *p = output;
1136 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 const mbedtls_md_info_t *md_info;
1138 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001139
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001140 RSA_VALIDATE_RET( ctx != NULL );
1141 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1142 mode == MBEDTLS_RSA_PUBLIC );
1143 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001144 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001145 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1148 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001149
1150 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001154 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001156
1157 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001159
Simon Butcher02037452016-03-01 21:19:12 +00001160 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001161 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001163
1164 memset( output, 0, olen );
1165
1166 *p++ = 0;
1167
Simon Butcher02037452016-03-01 21:19:12 +00001168 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001169 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001171
1172 p += hlen;
1173
Simon Butcher02037452016-03-01 21:19:12 +00001174 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001175 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1176 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001177 p += hlen;
1178 p += olen - 2 * hlen - 2 - ilen;
1179 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001180 if( ilen != 0 )
1181 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001184 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001185 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001186
Simon Butcher02037452016-03-01 21:19:12 +00001187 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001188 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1189 &md_ctx ) ) != 0 )
1190 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001191
Simon Butcher02037452016-03-01 21:19:12 +00001192 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001193 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1194 &md_ctx ) ) != 0 )
1195 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001196
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001197exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001199
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001200 if( ret != 0 )
1201 return( ret );
1202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 return( ( mode == MBEDTLS_RSA_PUBLIC )
1204 ? mbedtls_rsa_public( ctx, output, output )
1205 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001206}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001210/*
1211 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001214 int (*f_rng)(void *, unsigned char *, size_t),
1215 void *p_rng,
1216 int mode, size_t ilen,
1217 const unsigned char *input,
1218 unsigned char *output )
1219{
1220 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001221 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001222 unsigned char *p = output;
1223
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001224 RSA_VALIDATE_RET( ctx != NULL );
1225 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1226 mode == MBEDTLS_RSA_PUBLIC );
1227 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001228 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001229
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001230 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001232
1233 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001234
Simon Butcher02037452016-03-01 21:19:12 +00001235 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001236 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001238
1239 nb_pad = olen - 3 - ilen;
1240
1241 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001243 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001244 if( f_rng == NULL )
1245 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001248
1249 while( nb_pad-- > 0 )
1250 {
1251 int rng_dl = 100;
1252
1253 do {
1254 ret = f_rng( p_rng, p, 1 );
1255 } while( *p == 0 && --rng_dl && ret == 0 );
1256
Simon Butcher02037452016-03-01 21:19:12 +00001257 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001258 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001260
1261 p++;
1262 }
1263 }
1264 else
1265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001267
1268 while( nb_pad-- > 0 )
1269 *p++ = 0xFF;
1270 }
1271
1272 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001273 if( ilen != 0 )
1274 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 return( ( mode == MBEDTLS_RSA_PUBLIC )
1277 ? mbedtls_rsa_public( ctx, output, output )
1278 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001281
Paul Bakker5121ce52009-01-03 21:22:43 +00001282/*
1283 * Add the message padding, then do an RSA operation
1284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001286 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001287 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001288 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001289 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 unsigned char *output )
1291{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001292 RSA_VALIDATE_RET( ctx != NULL );
1293 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1294 mode == MBEDTLS_RSA_PUBLIC );
1295 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001296 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001297
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 switch( ctx->padding )
1299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_PKCS1_V15)
1301 case MBEDTLS_RSA_PKCS_V15:
1302 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001303 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001304#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_PKCS1_V21)
1307 case MBEDTLS_RSA_PKCS_V21:
1308 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001309 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001310#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001311
1312 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001315}
1316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001318/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001319 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001322 int (*f_rng)(void *, unsigned char *, size_t),
1323 void *p_rng,
1324 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001325 const unsigned char *label, size_t label_len,
1326 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001327 const unsigned char *input,
1328 unsigned char *output,
1329 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001330{
Janos Follath24eed8d2019-11-22 13:21:35 +00001331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001332 size_t ilen, i, pad_len;
1333 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1335 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001336 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 const mbedtls_md_info_t *md_info;
1338 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001339
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001340 RSA_VALIDATE_RET( ctx != NULL );
1341 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1342 mode == MBEDTLS_RSA_PUBLIC );
1343 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1344 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1345 RSA_VALIDATE_RET( input != NULL );
1346 RSA_VALIDATE_RET( olen != NULL );
1347
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001348 /*
1349 * Parameters sanity checks
1350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1352 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
1354 ilen = ctx->len;
1355
Paul Bakker27fdf462011-06-09 13:55:13 +00001356 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001360 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001362
Janos Follathc17cda12016-02-11 11:08:18 +00001363 hlen = mbedtls_md_get_size( md_info );
1364
1365 // checking for integer underflow
1366 if( 2 * hlen + 2 > ilen )
1367 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1368
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001369 /*
1370 * RSA operation
1371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1373 ? mbedtls_rsa_public( ctx, input, buf )
1374 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001375
1376 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001377 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001379 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001380 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001383 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1384 {
1385 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001386 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001387 }
1388
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001389 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001390 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1391 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001392 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001393 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1394 &md_ctx ) ) != 0 )
1395 {
1396 mbedtls_md_free( &md_ctx );
1397 goto cleanup;
1398 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001401
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001402 /* Generate lHash */
1403 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1404 goto cleanup;
1405
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001406 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001407 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001408 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001410 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001411
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001412 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001413
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001414 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001415
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001416 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001417 for( i = 0; i < hlen; i++ )
1418 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001419
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001420 /* Get zero-padding len, but always read till end of buffer
1421 * (minus one, for the 01 byte) */
1422 pad_len = 0;
1423 pad_done = 0;
1424 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1425 {
1426 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001427 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001428 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001429
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001430 p += pad_len;
1431 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001432
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001433 /*
1434 * The only information "leaked" is whether the padding was correct or not
1435 * (eg, no data is copied if it was not correct). This meets the
1436 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1437 * the different error conditions.
1438 */
1439 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001440 {
1441 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1442 goto cleanup;
1443 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001444
Paul Bakker66d5d072014-06-17 16:39:18 +02001445 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001446 {
1447 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1448 goto cleanup;
1449 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001450
1451 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001452 if( *olen != 0 )
1453 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001454 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001455
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001456cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001457 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1458 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001459
1460 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001465/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1466 *
1467 * \param value The value to analyze.
1468 * \return Zero if \p value is zero, otherwise all-bits-one.
1469 */
1470static unsigned all_or_nothing_int( unsigned value )
1471{
1472 /* MSVC has a warning about unary minus on unsigned, but this is
1473 * well-defined and precisely what we want to do here */
1474#if defined(_MSC_VER)
1475#pragma warning( push )
1476#pragma warning( disable : 4146 )
1477#endif
1478 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1479#if defined(_MSC_VER)
1480#pragma warning( pop )
1481#endif
1482}
1483
1484/** Check whether a size is out of bounds, without branches.
1485 *
1486 * This is equivalent to `size > max`, but is likely to be compiled to
1487 * to code using bitwise operation rather than a branch.
1488 *
1489 * \param size Size to check.
1490 * \param max Maximum desired value for \p size.
1491 * \return \c 0 if `size <= max`.
1492 * \return \c 1 if `size > max`.
1493 */
1494static unsigned size_greater_than( size_t size, size_t max )
1495{
1496 /* Return the sign bit (1 for negative) of (max - size). */
1497 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1498}
1499
1500/** Choose between two integer values, without branches.
1501 *
1502 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1503 * to code using bitwise operation rather than a branch.
1504 *
1505 * \param cond Condition to test.
1506 * \param if1 Value to use if \p cond is nonzero.
1507 * \param if0 Value to use if \p cond is zero.
1508 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1509 */
1510static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
1511{
1512 unsigned mask = all_or_nothing_int( cond );
1513 return( ( mask & if1 ) | (~mask & if0 ) );
1514}
1515
1516/** Shift some data towards the left inside a buffer without leaking
1517 * the length of the data through side channels.
1518 *
1519 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1520 * ```
1521 * memmove(start, start + offset, total - offset);
1522 * memset(start + offset, 0, total - offset);
1523 * ```
1524 * but it strives to use a memory access pattern (and thus total timing)
1525 * that does not depend on \p offset. This timing independence comes at
1526 * the expense of performance.
1527 *
1528 * \param start Pointer to the start of the buffer.
1529 * \param total Total size of the buffer.
1530 * \param offset Offset from which to copy \p total - \p offset bytes.
1531 */
1532static void mem_move_to_left( void *start,
1533 size_t total,
1534 size_t offset )
1535{
1536 volatile unsigned char *buf = start;
1537 size_t i, n;
1538 if( total == 0 )
1539 return;
1540 for( i = 0; i < total; i++ )
1541 {
1542 unsigned no_op = size_greater_than( total - offset, i );
1543 /* The first `total - offset` passes are a no-op. The last
1544 * `offset` passes shift the data one byte to the left and
1545 * zero out the last byte. */
1546 for( n = 0; n < total - 1; n++ )
1547 {
1548 unsigned char current = buf[n];
1549 unsigned char next = buf[n+1];
1550 buf[n] = if_int( no_op, current, next );
1551 }
1552 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1553 }
1554}
1555
Paul Bakkerb3869132013-02-28 17:21:01 +01001556/*
1557 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001560 int (*f_rng)(void *, unsigned char *, size_t),
1561 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001562 int mode, size_t *olen,
1563 const unsigned char *input,
1564 unsigned char *output,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001565 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001566{
Janos Follath24eed8d2019-11-22 13:21:35 +00001567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001568 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001570 /* The following variables take sensitive values: their value must
1571 * not leak into the observable behavior of the function other than
1572 * the designated outputs (output, olen, return value). Otherwise
1573 * this would open the execution of the function to
1574 * side-channel-based variants of the Bleichenbacher padding oracle
1575 * attack. Potential side channels include overall timing, memory
1576 * access patterns (especially visible to an adversary who has access
1577 * to a shared memory cache), and branches (especially visible to
1578 * an adversary who has access to a shared code cache or to a shared
1579 * branch predictor). */
1580 size_t pad_count = 0;
1581 unsigned bad = 0;
1582 unsigned char pad_done = 0;
1583 size_t plaintext_size = 0;
1584 unsigned output_too_large;
1585
1586 RSA_VALIDATE_RET( ctx != NULL );
1587 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1588 mode == MBEDTLS_RSA_PUBLIC );
1589 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1590 RSA_VALIDATE_RET( input != NULL );
1591 RSA_VALIDATE_RET( olen != NULL );
1592
1593 ilen = ctx->len;
1594 plaintext_max_size = ( output_max_len > ilen - 11 ?
1595 ilen - 11 :
1596 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1599 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001600
Paul Bakkerb3869132013-02-28 17:21:01 +01001601 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1605 ? mbedtls_rsa_public( ctx, input, buf )
1606 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001607
1608 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001609 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001610
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001611 /* Check and get padding length in constant time and constant
1612 * memory trace. The first byte must be 0. */
1613 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001617 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1618 * where PS must be at least 8 nonzero bytes. */
1619 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001620
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001621 /* Read the whole buffer. Set pad_done to nonzero if we find
1622 * the 0x00 byte and remember the padding length in pad_count. */
1623 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001624 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001625 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001626 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001627 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001628 }
1629 else
1630 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001631 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1632 * where PS must be at least 8 bytes with the value 0xFF. */
1633 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001634
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001635 /* Read the whole buffer. Set pad_done to nonzero if we find
1636 * the 0x00 byte and remember the padding length in pad_count.
1637 * If there's a non-0xff byte in the padding, the padding is bad. */
1638 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001639 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001640 pad_done |= if_int( buf[i], 0, 1 );
1641 pad_count += if_int( pad_done, 0, 1 );
1642 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001643 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001644 }
1645
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001646 /* If pad_done is still zero, there's no data, only unfinished padding. */
1647 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001648
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001649 /* There must be at least 8 bytes of padding. */
1650 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001651
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001652 /* If the padding is valid, set plaintext_size to the number of
1653 * remaining bytes after stripping the padding. If the padding
1654 * is invalid, avoid leaking this fact through the size of the
1655 * output: use the maximum message size that fits in the output
1656 * buffer. Do it without branches to avoid leaking the padding
1657 * validity through timing. RSA keys are small enough that all the
1658 * size_t values involved fit in unsigned int. */
1659 plaintext_size = if_int( bad,
1660 (unsigned) plaintext_max_size,
1661 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001662
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001663 /* Set output_too_large to 0 if the plaintext fits in the output
1664 * buffer and to 1 otherwise. */
1665 output_too_large = size_greater_than( plaintext_size,
1666 plaintext_max_size );
1667
1668 /* Set ret without branches to avoid timing attacks. Return:
1669 * - INVALID_PADDING if the padding is bad (bad != 0).
1670 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1671 * plaintext does not fit in the output buffer.
1672 * - 0 if the padding is correct. */
1673 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1674 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1675 0 ) );
1676
1677 /* If the padding is bad or the plaintext is too large, zero the
1678 * data that we're about to copy to the output buffer.
1679 * We need to copy the same amount of data
1680 * from the same buffer whether the padding is good or not to
1681 * avoid leaking the padding validity through overall timing or
1682 * through memory or cache access patterns. */
1683 bad = all_or_nothing_int( bad | output_too_large );
1684 for( i = 11; i < ilen; i++ )
1685 buf[i] &= ~bad;
1686
1687 /* If the plaintext is too large, truncate it to the buffer size.
1688 * Copy anyway to avoid revealing the length through timing, because
1689 * revealing the length is as bad as revealing the padding validity
1690 * for a Bleichenbacher attack. */
1691 plaintext_size = if_int( output_too_large,
1692 (unsigned) plaintext_max_size,
1693 (unsigned) plaintext_size );
1694
1695 /* Move the plaintext to the leftmost position where it can start in
1696 * the working buffer, i.e. make it start plaintext_max_size from
1697 * the end of the buffer. Do this with a memory access trace that
1698 * does not depend on the plaintext size. After this move, the
1699 * starting location of the plaintext is no longer sensitive
1700 * information. */
1701 mem_move_to_left( buf + ilen - plaintext_max_size,
1702 plaintext_max_size,
1703 plaintext_max_size - plaintext_size );
1704
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001705 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1706 * buffer. If output_max_len is 0, then output may be an invalid pointer
1707 * and the result of memcpy() would be undefined; prevent undefined
1708 * behavior making sure to depend only on output_max_len (the size of the
1709 * user-provided output buffer), which is independent from plaintext
1710 * length, validity of padding, success of the decryption, and other
1711 * secrets. */
1712 if( output_max_len != 0 )
1713 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001714
1715 /* Report the amount of data we copied to the output buffer. In case
1716 * of errors (bad padding or output too large), the value of *olen
1717 * when this function returns is not specified. Making it equivalent
1718 * to the good case limits the risks of leaking the padding validity. */
1719 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001721cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001722 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001723
1724 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001727
1728/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001729 * Do an RSA operation, then remove the message padding
1730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001732 int (*f_rng)(void *, unsigned char *, size_t),
1733 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001734 int mode, size_t *olen,
1735 const unsigned char *input,
1736 unsigned char *output,
1737 size_t output_max_len)
1738{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001739 RSA_VALIDATE_RET( ctx != NULL );
1740 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1741 mode == MBEDTLS_RSA_PUBLIC );
1742 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1743 RSA_VALIDATE_RET( input != NULL );
1744 RSA_VALIDATE_RET( olen != NULL );
1745
Paul Bakkerb3869132013-02-28 17:21:01 +01001746 switch( ctx->padding )
1747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748#if defined(MBEDTLS_PKCS1_V15)
1749 case MBEDTLS_RSA_PKCS_V15:
1750 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001751 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001752#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754#if defined(MBEDTLS_PKCS1_V21)
1755 case MBEDTLS_RSA_PKCS_V21:
1756 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001757 olen, input, output,
1758 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001759#endif
1760
1761 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001763 }
1764}
1765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001767/*
1768 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001771 int (*f_rng)(void *, unsigned char *, size_t),
1772 void *p_rng,
1773 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001775 unsigned int hashlen,
1776 const unsigned char *hash,
1777 unsigned char *sig )
1778{
1779 size_t olen;
1780 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Jaeden Amero3725bb22018-09-07 19:12:36 +01001782 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001783 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001784 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 const mbedtls_md_info_t *md_info;
1786 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001787 RSA_VALIDATE_RET( ctx != NULL );
1788 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1789 mode == MBEDTLS_RSA_PUBLIC );
1790 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1791 hashlen == 0 ) ||
1792 hash != NULL );
1793 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1796 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001797
1798 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001800
1801 olen = ctx->len;
1802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001804 {
Simon Butcher02037452016-03-01 21:19:12 +00001805 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001807 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001811 }
1812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001814 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001818
Jaeden Amero3725bb22018-09-07 19:12:36 +01001819 /* Calculate the largest possible salt length. Normally this is the hash
1820 * length, which is the maximum length the salt can have. If there is not
1821 * enough room, use the maximum salt length that fits. The constraint is
1822 * that the hash length plus the salt length plus 2 bytes must be at most
1823 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1824 * (PKCS#1 v2.2) §9.1.1 step 3. */
1825 min_slen = hlen - 2;
1826 if( olen < hlen + min_slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Jaeden Amero3725bb22018-09-07 19:12:36 +01001828 else if( olen >= hlen + hlen + 2 )
1829 slen = hlen;
1830 else
1831 slen = olen - hlen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001832
1833 memset( sig, 0, olen );
1834
Simon Butcher02037452016-03-01 21:19:12 +00001835 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001836 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001838
Simon Butcher02037452016-03-01 21:19:12 +00001839 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001840 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001841 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001842 *p++ = 0x01;
1843 memcpy( p, salt, slen );
1844 p += slen;
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001847 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001848 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001849
Simon Butcher02037452016-03-01 21:19:12 +00001850 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001851 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1852 goto exit;
1853 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1854 goto exit;
1855 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1856 goto exit;
1857 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1858 goto exit;
1859 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1860 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001861
Simon Butcher02037452016-03-01 21:19:12 +00001862 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001863 if( msb % 8 == 0 )
1864 offset = 1;
1865
Simon Butcher02037452016-03-01 21:19:12 +00001866 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001867 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1868 &md_ctx ) ) != 0 )
1869 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001870
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001871 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001872 sig[0] &= 0xFF >> ( olen * 8 - msb );
1873
1874 p += hlen;
1875 *p++ = 0xBC;
1876
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001877 mbedtls_platform_zeroize( salt, sizeof( salt ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001878
1879exit:
1880 mbedtls_md_free( &md_ctx );
1881
1882 if( ret != 0 )
1883 return( ret );
1884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 return( ( mode == MBEDTLS_RSA_PUBLIC )
1886 ? mbedtls_rsa_public( ctx, sig, sig )
1887 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001888}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001892/*
1893 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1894 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001895
1896/* Construct a PKCS v1.5 encoding of a hashed message
1897 *
1898 * This is used both for signature generation and verification.
1899 *
1900 * Parameters:
1901 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001902 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001903 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001904 * - hash: Buffer containing the hashed message or the raw data.
1905 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001906 * - dst: Buffer to hold the encoded message.
1907 *
1908 * Assumptions:
1909 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1910 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001911 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001912 *
1913 */
1914static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1915 unsigned int hashlen,
1916 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001917 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001918 unsigned char *dst )
1919{
1920 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001921 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001922 unsigned char *p = dst;
1923 const char *oid = NULL;
1924
1925 /* Are we signing hashed or raw data? */
1926 if( md_alg != MBEDTLS_MD_NONE )
1927 {
1928 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1929 if( md_info == NULL )
1930 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1931
1932 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1933 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1934
1935 hashlen = mbedtls_md_get_size( md_info );
1936
1937 /* Double-check that 8 + hashlen + oid_size can be used as a
1938 * 1-byte ASN.1 length encoding and that there's no overflow. */
1939 if( 8 + hashlen + oid_size >= 0x80 ||
1940 10 + hashlen < hashlen ||
1941 10 + hashlen + oid_size < 10 + hashlen )
1942 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1943
1944 /*
1945 * Static bounds check:
1946 * - Need 10 bytes for five tag-length pairs.
1947 * (Insist on 1-byte length encodings to protect against variants of
1948 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1949 * - Need hashlen bytes for hash
1950 * - Need oid_size bytes for hash alg OID.
1951 */
1952 if( nb_pad < 10 + hashlen + oid_size )
1953 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1954 nb_pad -= 10 + hashlen + oid_size;
1955 }
1956 else
1957 {
1958 if( nb_pad < hashlen )
1959 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1960
1961 nb_pad -= hashlen;
1962 }
1963
Hanno Becker2b2f8982017-09-27 17:10:03 +01001964 /* Need space for signature header and padding delimiter (3 bytes),
1965 * and 8 bytes for the minimal padding */
1966 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001967 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1968 nb_pad -= 3;
1969
1970 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001971 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001972
1973 /* Write signature header and padding */
1974 *p++ = 0;
1975 *p++ = MBEDTLS_RSA_SIGN;
1976 memset( p, 0xFF, nb_pad );
1977 p += nb_pad;
1978 *p++ = 0;
1979
1980 /* Are we signing raw data? */
1981 if( md_alg == MBEDTLS_MD_NONE )
1982 {
1983 memcpy( p, hash, hashlen );
1984 return( 0 );
1985 }
1986
1987 /* Signing hashed data, add corresponding ASN.1 structure
1988 *
1989 * DigestInfo ::= SEQUENCE {
1990 * digestAlgorithm DigestAlgorithmIdentifier,
1991 * digest Digest }
1992 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1993 * Digest ::= OCTET STRING
1994 *
1995 * Schematic:
1996 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1997 * TAG-NULL + LEN [ NULL ] ]
1998 * TAG-OCTET + LEN [ HASH ] ]
1999 */
2000 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002001 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002002 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002003 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002004 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002005 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002006 memcpy( p, oid, oid_size );
2007 p += oid_size;
2008 *p++ = MBEDTLS_ASN1_NULL;
2009 *p++ = 0x00;
2010 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002011 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002012 memcpy( p, hash, hashlen );
2013 p += hashlen;
2014
2015 /* Just a sanity-check, should be automatic
2016 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002017 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002018 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002019 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002020 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2021 }
2022
2023 return( 0 );
2024}
2025
Paul Bakkerb3869132013-02-28 17:21:01 +01002026/*
2027 * Do an RSA operation to sign the message digest
2028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002030 int (*f_rng)(void *, unsigned char *, size_t),
2031 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002032 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002034 unsigned int hashlen,
2035 const unsigned char *hash,
2036 unsigned char *sig )
2037{
Janos Follath24eed8d2019-11-22 13:21:35 +00002038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002039 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002040
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002041 RSA_VALIDATE_RET( ctx != NULL );
2042 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2043 mode == MBEDTLS_RSA_PUBLIC );
2044 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2045 hashlen == 0 ) ||
2046 hash != NULL );
2047 RSA_VALIDATE_RET( sig != NULL );
2048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2050 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002051
Hanno Beckerfdf38032017-09-06 12:35:55 +01002052 /*
2053 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2054 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002055
Hanno Beckerfdf38032017-09-06 12:35:55 +01002056 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2057 ctx->len, sig ) ) != 0 )
2058 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002059
2060 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002061 * Call respective RSA primitive
2062 */
2063
2064 if( mode == MBEDTLS_RSA_PUBLIC )
2065 {
2066 /* Skip verification on a public key operation */
2067 return( mbedtls_rsa_public( ctx, sig, sig ) );
2068 }
2069
2070 /* Private key operation
2071 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002072 * In order to prevent Lenstra's attack, make the signature in a
2073 * temporary buffer and check it before returning it.
2074 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002075
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002076 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002077 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002078 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2079
Hanno Beckerfdf38032017-09-06 12:35:55 +01002080 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002081 if( verif == NULL )
2082 {
2083 mbedtls_free( sig_try );
2084 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2085 }
2086
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002087 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2088 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2089
Hanno Becker171a8f12017-09-06 12:32:16 +01002090 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002091 {
2092 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2093 goto cleanup;
2094 }
2095
2096 memcpy( sig, sig_try, ctx->len );
2097
2098cleanup:
2099 mbedtls_free( sig_try );
2100 mbedtls_free( verif );
2101
2102 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002105
2106/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 * Do an RSA operation to sign the message digest
2108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002110 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002111 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002114 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002115 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 unsigned char *sig )
2117{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002118 RSA_VALIDATE_RET( ctx != NULL );
2119 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2120 mode == MBEDTLS_RSA_PUBLIC );
2121 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2122 hashlen == 0 ) ||
2123 hash != NULL );
2124 RSA_VALIDATE_RET( sig != NULL );
2125
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 switch( ctx->padding )
2127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128#if defined(MBEDTLS_PKCS1_V15)
2129 case MBEDTLS_RSA_PKCS_V15:
2130 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002131 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002132#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134#if defined(MBEDTLS_PKCS1_V21)
2135 case MBEDTLS_RSA_PKCS_V21:
2136 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002137 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002138#endif
2139
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002143}
2144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002146/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002147 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002150 int (*f_rng)(void *, unsigned char *, size_t),
2151 void *p_rng,
2152 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002154 unsigned int hashlen,
2155 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002157 int expected_salt_len,
2158 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002159{
Janos Follath24eed8d2019-11-22 13:21:35 +00002160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002161 size_t siglen;
2162 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002163 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002165 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002166 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002167 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 const mbedtls_md_info_t *md_info;
2169 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002170 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002171
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002172 RSA_VALIDATE_RET( ctx != NULL );
2173 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2174 mode == MBEDTLS_RSA_PUBLIC );
2175 RSA_VALIDATE_RET( sig != NULL );
2176 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2177 hashlen == 0 ) ||
2178 hash != NULL );
2179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2181 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002182
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 siglen = ctx->len;
2184
Paul Bakker27fdf462011-06-09 13:55:13 +00002185 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2189 ? mbedtls_rsa_public( ctx, sig, buf )
2190 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191
2192 if( ret != 0 )
2193 return( ret );
2194
2195 p = buf;
2196
Paul Bakkerb3869132013-02-28 17:21:01 +01002197 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 {
Simon Butcher02037452016-03-01 21:19:12 +00002202 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002204 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002208 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002211 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002215
Paul Bakkerb3869132013-02-28 17:21:01 +01002216 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002217
Simon Butcher02037452016-03-01 21:19:12 +00002218 /*
2219 * Note: EMSA-PSS verification is over the length of N - 1 bits
2220 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002221 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002222
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002223 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2224 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2225
Simon Butcher02037452016-03-01 21:19:12 +00002226 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002227 if( msb % 8 == 0 )
2228 {
2229 p++;
2230 siglen -= 1;
2231 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002232
Gilles Peskine139108a2017-10-18 19:03:42 +02002233 if( siglen < hlen + 2 )
2234 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2235 hash_start = p + siglen - hlen - 1;
2236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002238 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002239 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002240
Jaeden Amero66954e12018-01-25 16:05:54 +00002241 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2242 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002243 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002244
Paul Bakkerb3869132013-02-28 17:21:01 +01002245 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002246
Gilles Peskine6a54b022017-10-17 19:02:13 +02002247 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002248 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002249
Gilles Peskine91048a32017-10-19 17:46:14 +02002250 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002251 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002252 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2253 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002254 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002255
Gilles Peskine6a54b022017-10-17 19:02:13 +02002256 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002259 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002260 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002261 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2262 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002263 }
2264
Simon Butcher02037452016-03-01 21:19:12 +00002265 /*
2266 * Generate H = Hash( M' )
2267 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002268 ret = mbedtls_md_starts( &md_ctx );
2269 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002270 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002271 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2272 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002273 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002274 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2275 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002276 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002277 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2278 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002279 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002280 ret = mbedtls_md_finish( &md_ctx, result );
2281 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002282 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002283
Jaeden Amero66954e12018-01-25 16:05:54 +00002284 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002285 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002286 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002287 goto exit;
2288 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002289
2290exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002292
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002293 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002294}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002295
2296/*
2297 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002300 int (*f_rng)(void *, unsigned char *, size_t),
2301 void *p_rng,
2302 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002304 unsigned int hashlen,
2305 const unsigned char *hash,
2306 const unsigned char *sig )
2307{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002308 mbedtls_md_type_t mgf1_hash_id;
2309 RSA_VALIDATE_RET( ctx != NULL );
2310 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2311 mode == MBEDTLS_RSA_PUBLIC );
2312 RSA_VALIDATE_RET( sig != NULL );
2313 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2314 hashlen == 0 ) ||
2315 hash != NULL );
2316
2317 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002319 : md_alg;
2320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002322 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002324 sig ) );
2325
2326}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002330/*
2331 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002334 int (*f_rng)(void *, unsigned char *, size_t),
2335 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002336 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002338 unsigned int hashlen,
2339 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002340 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002341{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002342 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002343 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002344 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002345
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002346 RSA_VALIDATE_RET( ctx != NULL );
2347 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2348 mode == MBEDTLS_RSA_PUBLIC );
2349 RSA_VALIDATE_RET( sig != NULL );
2350 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2351 hashlen == 0 ) ||
2352 hash != NULL );
2353
2354 sig_len = ctx->len;
2355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2357 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002358
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002359 /*
2360 * Prepare expected PKCS1 v1.5 encoding of hash.
2361 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002362
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002363 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2364 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2365 {
2366 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2367 goto cleanup;
2368 }
2369
2370 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2371 encoded_expected ) ) != 0 )
2372 goto cleanup;
2373
2374 /*
2375 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2376 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002379 ? mbedtls_rsa_public( ctx, sig, encoded )
2380 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002381 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002382 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002383
Simon Butcher02037452016-03-01 21:19:12 +00002384 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002385 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002386 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002387
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002388 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2389 sig_len ) ) != 0 )
2390 {
2391 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2392 goto cleanup;
2393 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002394
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002395cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002396
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002397 if( encoded != NULL )
2398 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002399 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002400 mbedtls_free( encoded );
2401 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002402
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002403 if( encoded_expected != NULL )
2404 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002405 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002406 mbedtls_free( encoded_expected );
2407 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002408
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002409 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
2413/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002414 * Do an RSA operation and check the message digest
2415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002417 int (*f_rng)(void *, unsigned char *, size_t),
2418 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002419 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002421 unsigned int hashlen,
2422 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002423 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002424{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002425 RSA_VALIDATE_RET( ctx != NULL );
2426 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2427 mode == MBEDTLS_RSA_PUBLIC );
2428 RSA_VALIDATE_RET( sig != NULL );
2429 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2430 hashlen == 0 ) ||
2431 hash != NULL );
2432
Paul Bakkerb3869132013-02-28 17:21:01 +01002433 switch( ctx->padding )
2434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435#if defined(MBEDTLS_PKCS1_V15)
2436 case MBEDTLS_RSA_PKCS_V15:
2437 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002438 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002439#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#if defined(MBEDTLS_PKCS1_V21)
2442 case MBEDTLS_RSA_PKCS_V21:
2443 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002444 hashlen, hash, sig );
2445#endif
2446
2447 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002449 }
2450}
2451
2452/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002453 * Copy the components of an RSA key
2454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002456{
Janos Follath24eed8d2019-11-22 13:21:35 +00002457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002458 RSA_VALIDATE_RET( dst != NULL );
2459 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002460
2461 dst->ver = src->ver;
2462 dst->len = src->len;
2463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2465 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2468 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2469 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002470
2471#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2473 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2474 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2476 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002477#endif
2478
2479 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2482 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002483
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002484 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002485 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002486
2487cleanup:
2488 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002490
2491 return( ret );
2492}
2493
2494/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 * Free the components of an RSA key
2496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002498{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002499 if( ctx == NULL )
2500 return;
2501
2502 mbedtls_mpi_free( &ctx->Vi );
2503 mbedtls_mpi_free( &ctx->Vf );
2504 mbedtls_mpi_free( &ctx->RN );
2505 mbedtls_mpi_free( &ctx->D );
2506 mbedtls_mpi_free( &ctx->Q );
2507 mbedtls_mpi_free( &ctx->P );
2508 mbedtls_mpi_free( &ctx->E );
2509 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002510
Hanno Becker33c30a02017-08-23 07:00:22 +01002511#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002512 mbedtls_mpi_free( &ctx->RQ );
2513 mbedtls_mpi_free( &ctx->RP );
2514 mbedtls_mpi_free( &ctx->QP );
2515 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002516 mbedtls_mpi_free( &ctx->DP );
2517#endif /* MBEDTLS_RSA_NO_CRT */
2518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519#if defined(MBEDTLS_THREADING_C)
2520 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002521#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002522}
2523
Hanno Beckerab377312017-08-23 16:24:51 +01002524#endif /* !MBEDTLS_RSA_ALT */
2525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002528#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
2530/*
2531 * Example RSA-1024 keypair, for test purposes
2532 */
2533#define KEY_LEN 128
2534
2535#define RSA_N "9292758453063D803DD603D5E777D788" \
2536 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2537 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2538 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2539 "93A89813FBF3C4F8066D2D800F7C38A8" \
2540 "1AE31942917403FF4946B0A83D3D3E05" \
2541 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2542 "5E94BB77B07507233A0BC7BAC8F90F79"
2543
2544#define RSA_E "10001"
2545
2546#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2547 "66CA472BC44D253102F8B4A9D3BFA750" \
2548 "91386C0077937FE33FA3252D28855837" \
2549 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2550 "DF79C5CE07EE72C7F123142198164234" \
2551 "CABB724CF78B8173B9F880FC86322407" \
2552 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2553 "071513A1E85B5DFA031F21ECAE91A34D"
2554
2555#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2556 "2C01CAD19EA484A87EA4377637E75500" \
2557 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2558 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2559
2560#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2561 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2562 "910E4168387E3C30AA1E00C339A79508" \
2563 "8452DD96A9A5EA5D9DCA68DA636032AF"
2564
Paul Bakker5121ce52009-01-03 21:22:43 +00002565#define PT_LEN 24
2566#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2567 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002570static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002571{
gufe44c2620da2020-08-03 17:56:50 +02002572#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002573 size_t i;
2574
Paul Bakker545570e2010-07-18 09:00:25 +00002575 if( rng_state != NULL )
2576 rng_state = NULL;
2577
Paul Bakkera3d195c2011-11-27 21:07:34 +00002578 for( i = 0; i < len; ++i )
2579 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002580#else
2581 if( rng_state != NULL )
2582 rng_state = NULL;
2583
2584 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002585#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002586
Paul Bakkera3d195c2011-11-27 21:07:34 +00002587 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002588}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002590
Paul Bakker5121ce52009-01-03 21:22:43 +00002591/*
2592 * Checkup routine
2593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002595{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002596 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002598 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 unsigned char rsa_plaintext[PT_LEN];
2601 unsigned char rsa_decrypted[PT_LEN];
2602 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002604 unsigned char sha1sum[20];
2605#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
Hanno Becker3a701162017-08-22 13:52:43 +01002607 mbedtls_mpi K;
2608
2609 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Hanno Becker3a701162017-08-22 13:52:43 +01002612 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2613 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2614 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2615 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2616 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2617 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2618 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2619 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2620 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2621 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2622
Hanno Becker7f25f852017-10-10 16:56:22 +01002623 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
2625 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2629 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 {
2631 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
Hanno Becker5bc87292017-05-03 15:09:31 +01002634 ret = 1;
2635 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 }
2637
2638 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
2641 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2642
Hanno Becker98838b02017-10-02 13:16:10 +01002643 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2644 PT_LEN, rsa_plaintext,
2645 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 {
2647 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Hanno Becker5bc87292017-05-03 15:09:31 +01002650 ret = 1;
2651 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 }
2653
2654 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Hanno Becker98838b02017-10-02 13:16:10 +01002657 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2658 &len, rsa_ciphertext, rsa_decrypted,
2659 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 {
2661 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Hanno Becker5bc87292017-05-03 15:09:31 +01002664 ret = 1;
2665 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 }
2667
2668 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2669 {
2670 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Hanno Becker5bc87292017-05-03 15:09:31 +01002673 ret = 1;
2674 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 }
2676
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002677 if( verbose != 0 )
2678 mbedtls_printf( "passed\n" );
2679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002682 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002684 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002685 {
2686 if( verbose != 0 )
2687 mbedtls_printf( "failed\n" );
2688
2689 return( 1 );
2690 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Hanno Becker98838b02017-10-02 13:16:10 +01002692 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2693 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2694 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 {
2696 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Hanno Becker5bc87292017-05-03 15:09:31 +01002699 ret = 1;
2700 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 }
2702
2703 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Hanno Becker98838b02017-10-02 13:16:10 +01002706 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2707 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2708 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 {
2710 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Hanno Becker5bc87292017-05-03 15:09:31 +01002713 ret = 1;
2714 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 }
2716
2717 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002718 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002721 if( verbose != 0 )
2722 mbedtls_printf( "\n" );
2723
Paul Bakker3d8fb632014-04-17 12:42:41 +02002724cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002725 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 mbedtls_rsa_free( &rsa );
2727#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002728 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002730 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731}
2732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735#endif /* MBEDTLS_RSA_C */