blob: a32d4e8c5989447ab9dad9599a643020c3b68a5e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 Bakker5121ce52009-01-03 21:22:43 +000018 */
Hanno Becker74716312017-10-02 10:00:37 +010019
Paul Bakker5121ce52009-01-03 21:22:43 +000020/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000021 * The following sources were referenced in the design of this implementation
22 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000023 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000024 * [1] A method for obtaining digital signatures and public-key cryptosystems
25 * R Rivest, A Shamir, and L Adleman
26 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
27 *
28 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
29 * Menezes, van Oorschot and Vanstone
30 *
Janos Follathe81102e2017-03-22 13:38:28 +000031 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
32 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
33 * Stefan Mangard
34 * https://arxiv.org/abs/1702.08719v2
35 *
Paul Bakker5121ce52009-01-03 21:22:43 +000036 */
37
Gilles Peskinedb09ef62020-06-03 01:43:33 +020038#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +010043#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000046#include "mbedtls/error.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000047
Rich Evans00ab4702015-02-06 13:43:58 +000048#include <string.h>
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
gufe44c2620da2020-08-03 17:56:50 +020054#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010060#else
Rich Evans00ab4702015-02-06 13:43:58 +000061#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020063#define mbedtls_calloc calloc
64#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010065#endif
66
Hanno Beckera565f542017-10-11 11:00:19 +010067#if !defined(MBEDTLS_RSA_ALT)
68
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050069/* Parameter validation macros */
70#define RSA_VALIDATE_RET( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
72#define RSA_VALIDATE( cond ) \
73 MBEDTLS_INTERNAL_VALIDATE( cond )
74
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010075#if defined(MBEDTLS_PKCS1_V15)
Hanno Becker171a8f12017-09-06 12:32:16 +010076/* constant-time buffer comparison */
77static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n )
78{
79 size_t i;
80 const unsigned char *A = (const unsigned char *) a;
81 const unsigned char *B = (const unsigned char *) b;
82 unsigned char diff = 0;
83
84 for( i = 0; i < n; i++ )
85 diff |= A[i] ^ B[i];
86
87 return( diff );
88}
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +010089#endif /* MBEDTLS_PKCS1_V15 */
Hanno Becker171a8f12017-09-06 12:32:16 +010090
Hanno Becker617c1ae2017-08-23 14:11:24 +010091int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
92 const mbedtls_mpi *N,
93 const mbedtls_mpi *P, const mbedtls_mpi *Q,
94 const mbedtls_mpi *D, const mbedtls_mpi *E )
95{
Janos Follath24eed8d2019-11-22 13:21:35 +000096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +010098
99 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
100 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
101 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
102 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
103 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
104 {
Chris Jones713e4e72021-01-11 12:31:27 +0000105 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100106 }
107
108 if( N != NULL )
109 ctx->len = mbedtls_mpi_size( &ctx->N );
110
111 return( 0 );
112}
113
114int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100115 unsigned char const *N, size_t N_len,
116 unsigned char const *P, size_t P_len,
117 unsigned char const *Q, size_t Q_len,
118 unsigned char const *D, size_t D_len,
119 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100120{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000121 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500122 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100123
124 if( N != NULL )
125 {
126 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
127 ctx->len = mbedtls_mpi_size( &ctx->N );
128 }
129
130 if( P != NULL )
131 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
132
133 if( Q != NULL )
134 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
135
136 if( D != NULL )
137 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
138
139 if( E != NULL )
140 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
141
142cleanup:
143
144 if( ret != 0 )
Chris Jones713e4e72021-01-11 12:31:27 +0000145 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100146
147 return( 0 );
148}
149
Hanno Becker705fc682017-10-10 17:57:02 +0100150/*
151 * Checks whether the context fields are set in such a way
152 * that the RSA primitives will be able to execute without error.
153 * It does *not* make guarantees for consistency of the parameters.
154 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100155static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
156 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100157{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100158#if !defined(MBEDTLS_RSA_NO_CRT)
159 /* blinding_needed is only used for NO_CRT to decide whether
160 * P,Q need to be present or not. */
161 ((void) blinding_needed);
162#endif
163
Hanno Becker3a760a12018-01-05 08:14:49 +0000164 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
165 ctx->len > MBEDTLS_MPI_MAX_SIZE )
166 {
Hanno Becker705fc682017-10-10 17:57:02 +0100167 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000168 }
Hanno Becker705fc682017-10-10 17:57:02 +0100169
170 /*
171 * 1. Modular exponentiation needs positive, odd moduli.
172 */
173
174 /* Modular exponentiation wrt. N is always used for
175 * RSA public key operations. */
176 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
177 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
178 {
179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
180 }
181
182#if !defined(MBEDTLS_RSA_NO_CRT)
183 /* Modular exponentiation for P and Q is only
184 * used for private key operations and if CRT
185 * is used. */
186 if( is_priv &&
187 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
188 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
189 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
190 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
191 {
192 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
193 }
194#endif /* !MBEDTLS_RSA_NO_CRT */
195
196 /*
197 * 2. Exponents must be positive
198 */
199
200 /* Always need E for public key operations */
201 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
202 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
203
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100204#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100205 /* For private key operations, use D or DP & DQ
206 * as (unblinded) exponents. */
207 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
208 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
209#else
210 if( is_priv &&
211 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
212 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
213 {
214 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
215 }
216#endif /* MBEDTLS_RSA_NO_CRT */
217
218 /* Blinding shouldn't make exponents negative either,
219 * so check that P, Q >= 1 if that hasn't yet been
220 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100221#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100222 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100223 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
224 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
225 {
226 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
227 }
228#endif
229
230 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100231 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100232#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100233 if( is_priv &&
234 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
235 {
236 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
237 }
238#endif
239
240 return( 0 );
241}
242
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100243int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100244{
245 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500246 int have_N, have_P, have_Q, have_D, have_E;
247#if !defined(MBEDTLS_RSA_NO_CRT)
248 int have_DP, have_DQ, have_QP;
249#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500250 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100251
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500252 RSA_VALIDATE_RET( ctx != NULL );
253
254 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
255 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
256 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
257 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
258 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500259
260#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500261 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
262 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
263 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500264#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100265
Hanno Becker617c1ae2017-08-23 14:11:24 +0100266 /*
267 * Check whether provided parameters are enough
268 * to deduce all others. The following incomplete
269 * parameter sets for private keys are supported:
270 *
271 * (1) P, Q missing.
272 * (2) D and potentially N missing.
273 *
274 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100275
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500276 n_missing = have_P && have_Q && have_D && have_E;
277 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
278 d_missing = have_P && have_Q && !have_D && have_E;
279 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100280
281 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100283
Hanno Becker617c1ae2017-08-23 14:11:24 +0100284 if( !is_priv && !is_pub )
285 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
286
287 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100288 * Step 1: Deduce N if P, Q are provided.
289 */
290
291 if( !have_N && have_P && have_Q )
292 {
293 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
294 &ctx->Q ) ) != 0 )
295 {
Chris Jones713e4e72021-01-11 12:31:27 +0000296 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100297 }
298
299 ctx->len = mbedtls_mpi_size( &ctx->N );
300 }
301
302 /*
303 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100304 */
305
306 if( pq_missing )
307 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100308 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100309 &ctx->P, &ctx->Q );
310 if( ret != 0 )
Chris Jones713e4e72021-01-11 12:31:27 +0000311 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100312
313 }
314 else if( d_missing )
315 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100316 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
317 &ctx->Q,
318 &ctx->E,
319 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100320 {
Chris Jones713e4e72021-01-11 12:31:27 +0000321 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100322 }
323 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324
Hanno Becker617c1ae2017-08-23 14:11:24 +0100325 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100326 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100327 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100328 */
329
Hanno Becker23344b52017-08-23 07:43:27 +0100330#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500331 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100332 {
333 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
334 &ctx->DP, &ctx->DQ, &ctx->QP );
335 if( ret != 0 )
Chris Jones713e4e72021-01-11 12:31:27 +0000336 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100337 }
Hanno Becker23344b52017-08-23 07:43:27 +0100338#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100339
340 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100341 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100342 */
343
Hanno Beckerebd2c022017-10-12 10:54:53 +0100344 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100345}
346
Hanno Becker617c1ae2017-08-23 14:11:24 +0100347int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
348 unsigned char *N, size_t N_len,
349 unsigned char *P, size_t P_len,
350 unsigned char *Q, size_t Q_len,
351 unsigned char *D, size_t D_len,
352 unsigned char *E, size_t E_len )
353{
354 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500355 int is_priv;
356 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100357
358 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500359 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100360 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
361 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
362 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
363 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
364 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
365
366 if( !is_priv )
367 {
368 /* If we're trying to export private parameters for a public key,
369 * something must be wrong. */
370 if( P != NULL || Q != NULL || D != NULL )
371 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
372
373 }
374
375 if( N != NULL )
376 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
377
378 if( P != NULL )
379 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
380
381 if( Q != NULL )
382 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
383
384 if( D != NULL )
385 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
386
387 if( E != NULL )
388 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100389
390cleanup:
391
392 return( ret );
393}
394
Hanno Becker617c1ae2017-08-23 14:11:24 +0100395int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
396 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
397 mbedtls_mpi *D, mbedtls_mpi *E )
398{
Janos Follath24eed8d2019-11-22 13:21:35 +0000399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500400 int is_priv;
401 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100402
403 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500404 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100405 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
406 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
407 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
408 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
409 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
410
411 if( !is_priv )
412 {
413 /* If we're trying to export private parameters for a public key,
414 * something must be wrong. */
415 if( P != NULL || Q != NULL || D != NULL )
416 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
417
418 }
419
420 /* Export all requested core parameters. */
421
422 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
423 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
424 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
425 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
426 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
427 {
428 return( ret );
429 }
430
431 return( 0 );
432}
433
434/*
435 * Export CRT parameters
436 * This must also be implemented if CRT is not used, for being able to
437 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
438 * can be used in this case.
439 */
440int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
441 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
442{
Janos Follath24eed8d2019-11-22 13:21:35 +0000443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500444 int is_priv;
445 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100446
447 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500448 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100449 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
450 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
451 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
452 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
453 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
454
455 if( !is_priv )
456 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
457
Hanno Beckerdc95c892017-08-23 06:57:02 +0100458#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100459 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100460 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
461 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
462 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
463 {
Chris Jones713e4e72021-01-11 12:31:27 +0000464 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100465 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100466#else
467 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
468 DP, DQ, QP ) ) != 0 )
469 {
Chris Jones713e4e72021-01-11 12:31:27 +0000470 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100471 }
472#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100473
474 return( 0 );
475}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100476
Paul Bakker5121ce52009-01-03 21:22:43 +0000477/*
478 * Initialize an RSA context
479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000482 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000483{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500484 RSA_VALIDATE( ctx != NULL );
485 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
486 padding == MBEDTLS_RSA_PKCS_V21 );
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100493 /* Set ctx->ver to nonzero to indicate that the mutex has been
494 * initialized and will need to be freed. */
495 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000498}
499
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100500/*
501 * Set padding for an existing RSA context
502 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500503void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
504 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100505{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500506 RSA_VALIDATE( ctx != NULL );
507 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
508 padding == MBEDTLS_RSA_PKCS_V21 );
509
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100510 ctx->padding = padding;
511 ctx->hash_id = hash_id;
512}
513
Hanno Becker617c1ae2017-08-23 14:11:24 +0100514/*
515 * Get length in bytes of RSA modulus
516 */
517
518size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
519{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100520 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100521}
522
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526/*
527 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800528 *
529 * This generation method follows the RSA key pair generation procedure of
530 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000533 int (*f_rng)(void *, unsigned char *, size_t),
534 void *p_rng,
535 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000536{
Janos Follath24eed8d2019-11-22 13:21:35 +0000537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800538 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100539 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500540 RSA_VALIDATE_RET( ctx != NULL );
541 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
Janos Follathb8fc1b02018-09-03 15:37:01 +0100543 /*
544 * If the modulus is 1024 bit long or shorter, then the security strength of
545 * the RSA algorithm is less than or equal to 80 bits and therefore an error
546 * rate of 2^-80 is sufficient.
547 */
548 if( nbits > 1024 )
549 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
550
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100551 mbedtls_mpi_init( &H );
552 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800553 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100555 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
556 {
557 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
558 goto cleanup;
559 }
560
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 /*
562 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800563 * 1. |P-Q| > 2^( nbits / 2 - 100 )
564 * 2. GCD( E, (P-1)*(Q-1) ) == 1
565 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 do
570 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100571 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
572 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Janos Follathb8fc1b02018-09-03 15:37:01 +0100574 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
575 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800577 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
578 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
579 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 continue;
581
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800582 /* not required by any standards, but some users rely on the fact that P > Q */
583 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100584 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100585
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100586 /* Temporarily replace P,Q by P-1, Q-1 */
587 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
588 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
589 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800590
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800591 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800593 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
594 continue;
595
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800596 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Jethro Beekman97f95c92018-02-13 15:50:36 -0800597 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
598 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
599 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
600
601 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
602 continue;
603
604 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800606 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100608 /* Restore P,Q */
609 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
610 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
611
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800612 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
613
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100614 ctx->len = mbedtls_mpi_size( &ctx->N );
615
Jethro Beekman97f95c92018-02-13 15:50:36 -0800616#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 * DP = D mod (P - 1)
619 * DQ = D mod (Q - 1)
620 * QP = Q^-1 mod P
621 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100622 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
623 &ctx->DP, &ctx->DQ, &ctx->QP ) );
624#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
Hanno Becker83aad1f2017-08-23 06:45:10 +0100626 /* Double-check */
627 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
629cleanup:
630
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100631 mbedtls_mpi_free( &H );
632 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800633 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
635 if( ret != 0 )
636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_rsa_free( ctx );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100638 if( ( -ret & ~0x7f ) == 0 )
Chris Jones713e4e72021-01-11 12:31:27 +0000639 ret = MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100640 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 }
642
Paul Bakker48377d92013-08-30 12:06:24 +0200643 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644}
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000647
648/*
649 * Check a public RSA key
650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000652{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500653 RSA_VALIDATE_RET( ctx != NULL );
654
Hanno Beckerebd2c022017-10-12 10:54:53 +0100655 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000657
Hanno Becker3a760a12018-01-05 08:14:49 +0000658 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100661 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000662
Hanno Becker705fc682017-10-10 17:57:02 +0100663 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
664 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100668 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000669
670 return( 0 );
671}
672
673/*
Hanno Becker705fc682017-10-10 17:57:02 +0100674 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000677{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500678 RSA_VALIDATE_RET( ctx != NULL );
679
Hanno Becker705fc682017-10-10 17:57:02 +0100680 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100681 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 {
Hanno Becker98838b02017-10-02 13:16:10 +0100683 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684 }
Paul Bakker48377d92013-08-30 12:06:24 +0200685
Hanno Becker98838b02017-10-02 13:16:10 +0100686 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100687 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100689 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000691
Hanno Beckerb269a852017-08-25 08:03:21 +0100692#if !defined(MBEDTLS_RSA_NO_CRT)
693 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
694 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
695 {
696 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
697 }
698#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000699
700 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000701}
702
703/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100704 * Check if contexts holding a public and private key match
705 */
Hanno Becker98838b02017-10-02 13:16:10 +0100706int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
707 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500709 RSA_VALIDATE_RET( pub != NULL );
710 RSA_VALIDATE_RET( prv != NULL );
711
Hanno Becker98838b02017-10-02 13:16:10 +0100712 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_rsa_check_privkey( prv ) != 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
719 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100722 }
723
724 return( 0 );
725}
726
727/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000728 * Do an RSA public key operation
729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000731 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 unsigned char *output )
733{
Janos Follath24eed8d2019-11-22 13:21:35 +0000734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000735 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500737 RSA_VALIDATE_RET( ctx != NULL );
738 RSA_VALIDATE_RET( input != NULL );
739 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
Hanno Beckerebd2c022017-10-12 10:54:53 +0100741 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100742 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200746#if defined(MBEDTLS_THREADING_C)
747 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
748 return( ret );
749#endif
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200755 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
756 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000757 }
758
759 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
761 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
763cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200765 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
766 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100767#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
771 if( ret != 0 )
Chris Jones713e4e72021-01-11 12:31:27 +0000772 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
774 return( 0 );
775}
776
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200777/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200778 * Generate or update blinding values, see section 10 of:
779 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200780 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200781 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200782 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200783static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200784 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
785{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200786 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200787 mbedtls_mpi R;
788
789 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200790
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200791 if( ctx->Vf.p != NULL )
792 {
793 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
795 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
796 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
797 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200798
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200799 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200800 }
801
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200802 /* Unblinding value: Vf = random number, invertible mod N */
803 do {
804 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200805 {
806 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
807 goto cleanup;
808 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200811
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200812 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200813 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
814 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
815 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
816
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200817 /* At this point, Vi is invertible mod N if and only if both Vf and R
818 * are invertible mod N. If one of them isn't, we don't need to know
819 * which one, we just loop and choose new values for both of them.
820 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200821 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500822 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200823 goto cleanup;
824
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500825 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
826
827 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
828 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
829 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200830
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200831 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200832 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 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 +0200834
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200835
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200836cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200837 mbedtls_mpi_free( &R );
838
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200839 return( ret );
840}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200841
Paul Bakker5121ce52009-01-03 21:22:43 +0000842/*
Janos Follathe81102e2017-03-22 13:38:28 +0000843 * Exponent blinding supposed to prevent side-channel attacks using multiple
844 * traces of measurements to recover the RSA key. The more collisions are there,
845 * the more bits of the key can be recovered. See [3].
846 *
847 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
848 * observations on avarage.
849 *
850 * For example with 28 byte blinding to achieve 2 collisions the adversary has
851 * to make 2^112 observations on avarage.
852 *
853 * (With the currently (as of 2017 April) known best algorithms breaking 2048
854 * bit RSA requires approximately as much time as trying out 2^112 random keys.
855 * Thus in this sense with 28 byte blinding the security is not reduced by
856 * side-channel attacks like the one in [3])
857 *
858 * This countermeasure does not help if the key recovery is possible with a
859 * single trace.
860 */
861#define RSA_EXPONENT_BLINDING 28
862
863/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 * Do an RSA private key operation
865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200867 int (*f_rng)(void *, unsigned char *, size_t),
868 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000869 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000870 unsigned char *output )
871{
Janos Follath24eed8d2019-11-22 13:21:35 +0000872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000873 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100874
875 /* Temporary holding the result */
876 mbedtls_mpi T;
877
878 /* Temporaries holding P-1, Q-1 and the
879 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000880 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100881
882#if !defined(MBEDTLS_RSA_NO_CRT)
883 /* Temporaries holding the results mod p resp. mod q. */
884 mbedtls_mpi TP, TQ;
885
886 /* Temporaries holding the blinded exponents for
887 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000888 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100889
890 /* Pointers to actual exponents to be used - either the unblinded
891 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000892 mbedtls_mpi *DP = &ctx->DP;
893 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100894#else
895 /* Temporary holding the blinded exponent (if used). */
896 mbedtls_mpi D_blind;
897
898 /* Pointer to actual exponent to be used - either the unblinded
899 * or the blinded one, depending on the presence of a PRNG. */
900 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100901#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100902
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100903 /* Temporaries holding the initial input and the double
904 * checked result; should be the same in the end. */
905 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000906
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500907 RSA_VALIDATE_RET( ctx != NULL );
908 RSA_VALIDATE_RET( input != NULL );
909 RSA_VALIDATE_RET( output != NULL );
910
Hanno Beckerebd2c022017-10-12 10:54:53 +0100911 if( rsa_check_context( ctx, 1 /* private key checks */,
912 f_rng != NULL /* blinding y/n */ ) != 0 )
913 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100914 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100915 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100916
Hanno Becker06811ce2017-05-03 15:10:34 +0100917#if defined(MBEDTLS_THREADING_C)
918 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
919 return( ret );
920#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000921
Hanno Becker06811ce2017-05-03 15:10:34 +0100922 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100923 mbedtls_mpi_init( &T );
924
925 mbedtls_mpi_init( &P1 );
926 mbedtls_mpi_init( &Q1 );
927 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000928
Janos Follathf9203b42017-03-22 15:13:15 +0000929 if( f_rng != NULL )
930 {
Janos Follathe81102e2017-03-22 13:38:28 +0000931#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000932 mbedtls_mpi_init( &D_blind );
933#else
934 mbedtls_mpi_init( &DP_blind );
935 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000936#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000937 }
Janos Follathe81102e2017-03-22 13:38:28 +0000938
Hanno Becker06811ce2017-05-03 15:10:34 +0100939#if !defined(MBEDTLS_RSA_NO_CRT)
940 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200941#endif
942
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100943 mbedtls_mpi_init( &I );
944 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100945
946 /* End of MPI initialization */
947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
949 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200951 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
952 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000953 }
954
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100955 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100956
Paul Bakkerf451bac2013-08-30 15:37:02 +0200957 if( f_rng != NULL )
958 {
959 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200960 * Blinding
961 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200962 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200963 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
964 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000966
Janos Follathe81102e2017-03-22 13:38:28 +0000967 /*
968 * Exponent blinding
969 */
970 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
971 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
972
Janos Follathf9203b42017-03-22 15:13:15 +0000973#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000974 /*
975 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
976 */
977 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
978 f_rng, p_rng ) );
979 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
980 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
981 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
982
983 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000984#else
985 /*
986 * DP_blind = ( P - 1 ) * R + DP
987 */
988 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
989 f_rng, p_rng ) );
990 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
991 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
992 &ctx->DP ) );
993
994 DP = &DP_blind;
995
996 /*
997 * DQ_blind = ( Q - 1 ) * R + DQ
998 */
999 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
1000 f_rng, p_rng ) );
1001 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
1002 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
1003 &ctx->DQ ) );
1004
1005 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001006#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +02001007 }
Paul Bakkeraab30c12013-08-30 11:00:25 +02001008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +00001010 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001011#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001012 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001013 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001015 * TP = input ^ dP mod P
1016 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001017 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001018
1019 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1020 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
1022 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001023 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001024 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001025 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1026 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1027 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
1029 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001030 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001032 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1033 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001035
Paul Bakkerf451bac2013-08-30 15:37:02 +02001036 if( f_rng != NULL )
1037 {
1038 /*
1039 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001040 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001041 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001042 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001044 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
Hanno Becker2dec5e82017-10-03 07:49:52 +01001046 /* Verify the result to prevent glitching attacks. */
1047 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1048 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001049 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001050 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001051 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1052 goto cleanup;
1053 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001054
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
1058cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001060 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1061 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001062#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001063
Hanno Becker06811ce2017-05-03 15:10:34 +01001064 mbedtls_mpi_free( &P1 );
1065 mbedtls_mpi_free( &Q1 );
1066 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001067
1068 if( f_rng != NULL )
1069 {
Janos Follathe81102e2017-03-22 13:38:28 +00001070#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001071 mbedtls_mpi_free( &D_blind );
1072#else
1073 mbedtls_mpi_free( &DP_blind );
1074 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001075#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001076 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
Hanno Becker06811ce2017-05-03 15:10:34 +01001078 mbedtls_mpi_free( &T );
1079
1080#if !defined(MBEDTLS_RSA_NO_CRT)
1081 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1082#endif
1083
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001084 mbedtls_mpi_free( &C );
1085 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001086
Gilles Peskineae3741e2020-11-25 00:10:31 +01001087 if( ret != 0 && ret >= -0x007f )
Chris Jones96ae73b2021-01-08 17:04:59 +00001088 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089
Gilles Peskineae3741e2020-11-25 00:10:31 +01001090 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001091}
1092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001094/**
1095 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1096 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001097 * \param dst buffer to mask
1098 * \param dlen length of destination buffer
1099 * \param src source of the mask generation
1100 * \param slen length of the source buffer
1101 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001102 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001103static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001105{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001107 unsigned char counter[4];
1108 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001109 unsigned int hlen;
1110 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001111 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114 memset( counter, 0, 4 );
1115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001117
Simon Butcher02037452016-03-01 21:19:12 +00001118 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001119 p = dst;
1120
1121 while( dlen > 0 )
1122 {
1123 use_len = hlen;
1124 if( dlen < hlen )
1125 use_len = dlen;
1126
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001127 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1128 goto exit;
1129 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1130 goto exit;
1131 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1132 goto exit;
1133 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1134 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001135
1136 for( i = 0; i < use_len; ++i )
1137 *p++ ^= mask[i];
1138
1139 counter[3]++;
1140
1141 dlen -= use_len;
1142 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001143
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001144exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001145 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001146
1147 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001152/*
1153 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001156 int (*f_rng)(void *, unsigned char *, size_t),
1157 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001158 int mode,
1159 const unsigned char *label, size_t label_len,
1160 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001161 const unsigned char *input,
1162 unsigned char *output )
1163{
1164 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001165 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001166 unsigned char *p = output;
1167 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 const mbedtls_md_info_t *md_info;
1169 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001170
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001171 RSA_VALIDATE_RET( ctx != NULL );
1172 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1173 mode == MBEDTLS_RSA_PUBLIC );
1174 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001175 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001176 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001180
1181 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001185 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001187
1188 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001190
Simon Butcher02037452016-03-01 21:19:12 +00001191 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001192 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001194
1195 memset( output, 0, olen );
1196
1197 *p++ = 0;
1198
Simon Butcher02037452016-03-01 21:19:12 +00001199 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001200 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jones713e4e72021-01-11 12:31:27 +00001201 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001202
1203 p += hlen;
1204
Simon Butcher02037452016-03-01 21:19:12 +00001205 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001206 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1207 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001208 p += hlen;
1209 p += olen - 2 * hlen - 2 - ilen;
1210 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001211 if( ilen != 0 )
1212 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001215 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001216 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001217
Simon Butcher02037452016-03-01 21:19:12 +00001218 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001219 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1220 &md_ctx ) ) != 0 )
1221 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001222
Simon Butcher02037452016-03-01 21:19:12 +00001223 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001224 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1225 &md_ctx ) ) != 0 )
1226 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001227
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001228exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001230
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001231 if( ret != 0 )
1232 return( ret );
1233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 return( ( mode == MBEDTLS_RSA_PUBLIC )
1235 ? mbedtls_rsa_public( ctx, output, output )
1236 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001237}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001241/*
1242 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001245 int (*f_rng)(void *, unsigned char *, size_t),
1246 void *p_rng,
1247 int mode, size_t ilen,
1248 const unsigned char *input,
1249 unsigned char *output )
1250{
1251 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001253 unsigned char *p = output;
1254
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001255 RSA_VALIDATE_RET( ctx != NULL );
1256 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1257 mode == MBEDTLS_RSA_PUBLIC );
1258 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001259 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001260
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001261 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001263
1264 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001265
Simon Butcher02037452016-03-01 21:19:12 +00001266 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001267 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001269
1270 nb_pad = olen - 3 - ilen;
1271
1272 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001274 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001275 if( f_rng == NULL )
1276 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001279
1280 while( nb_pad-- > 0 )
1281 {
1282 int rng_dl = 100;
1283
1284 do {
1285 ret = f_rng( p_rng, p, 1 );
1286 } while( *p == 0 && --rng_dl && ret == 0 );
1287
Simon Butcher02037452016-03-01 21:19:12 +00001288 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001289 if( rng_dl == 0 || ret != 0 )
Chris Jones713e4e72021-01-11 12:31:27 +00001290 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001291
1292 p++;
1293 }
1294 }
1295 else
1296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001298
1299 while( nb_pad-- > 0 )
1300 *p++ = 0xFF;
1301 }
1302
1303 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001304 if( ilen != 0 )
1305 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 return( ( mode == MBEDTLS_RSA_PUBLIC )
1308 ? mbedtls_rsa_public( ctx, output, output )
1309 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001310}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001312
Paul Bakker5121ce52009-01-03 21:22:43 +00001313/*
1314 * Add the message padding, then do an RSA operation
1315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001317 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001318 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001319 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001320 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 unsigned char *output )
1322{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001323 RSA_VALIDATE_RET( ctx != NULL );
1324 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1325 mode == MBEDTLS_RSA_PUBLIC );
1326 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001327 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001328
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 switch( ctx->padding )
1330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_PKCS1_V15)
1332 case MBEDTLS_RSA_PKCS_V15:
1333 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001334 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001335#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#if defined(MBEDTLS_PKCS1_V21)
1338 case MBEDTLS_RSA_PKCS_V21:
1339 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001340 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001341#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
1343 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001346}
1347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001349/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001350 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001353 int (*f_rng)(void *, unsigned char *, size_t),
1354 void *p_rng,
1355 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001356 const unsigned char *label, size_t label_len,
1357 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001358 const unsigned char *input,
1359 unsigned char *output,
1360 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001361{
Janos Follath24eed8d2019-11-22 13:21:35 +00001362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001363 size_t ilen, i, pad_len;
1364 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1366 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001367 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 const mbedtls_md_info_t *md_info;
1369 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001370
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001371 RSA_VALIDATE_RET( ctx != NULL );
1372 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1373 mode == MBEDTLS_RSA_PUBLIC );
1374 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1375 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1376 RSA_VALIDATE_RET( input != NULL );
1377 RSA_VALIDATE_RET( olen != NULL );
1378
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001379 /*
1380 * Parameters sanity checks
1381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1383 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
1385 ilen = ctx->len;
1386
Paul Bakker27fdf462011-06-09 13:55:13 +00001387 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001391 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001393
Janos Follathc17cda12016-02-11 11:08:18 +00001394 hlen = mbedtls_md_get_size( md_info );
1395
1396 // checking for integer underflow
1397 if( 2 * hlen + 2 > ilen )
1398 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1399
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001400 /*
1401 * RSA operation
1402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1404 ? mbedtls_rsa_public( ctx, input, buf )
1405 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
1407 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001408 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001410 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001411 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001414 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1415 {
1416 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001417 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001418 }
1419
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001420 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001421 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1422 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001423 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001424 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1425 &md_ctx ) ) != 0 )
1426 {
1427 mbedtls_md_free( &md_ctx );
1428 goto cleanup;
1429 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001432
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001433 /* Generate lHash */
1434 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1435 goto cleanup;
1436
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001437 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001438 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001439 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001441 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001443 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001444
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001445 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001446
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001447 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001448 for( i = 0; i < hlen; i++ )
1449 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001450
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001451 /* Get zero-padding len, but always read till end of buffer
1452 * (minus one, for the 01 byte) */
1453 pad_len = 0;
1454 pad_done = 0;
1455 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1456 {
1457 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001458 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001459 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001460
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001461 p += pad_len;
1462 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001463
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001464 /*
1465 * The only information "leaked" is whether the padding was correct or not
1466 * (eg, no data is copied if it was not correct). This meets the
1467 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1468 * the different error conditions.
1469 */
1470 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001471 {
1472 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1473 goto cleanup;
1474 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001475
Paul Bakker66d5d072014-06-17 16:39:18 +02001476 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001477 {
1478 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1479 goto cleanup;
1480 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001481
1482 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001483 if( *olen != 0 )
1484 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001485 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001486
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001487cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001488 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1489 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001490
1491 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001496/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
1497 *
1498 * \param value The value to analyze.
1499 * \return Zero if \p value is zero, otherwise all-bits-one.
1500 */
1501static unsigned all_or_nothing_int( unsigned value )
1502{
1503 /* MSVC has a warning about unary minus on unsigned, but this is
1504 * well-defined and precisely what we want to do here */
1505#if defined(_MSC_VER)
1506#pragma warning( push )
1507#pragma warning( disable : 4146 )
1508#endif
1509 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
1510#if defined(_MSC_VER)
1511#pragma warning( pop )
1512#endif
1513}
1514
1515/** Check whether a size is out of bounds, without branches.
1516 *
1517 * This is equivalent to `size > max`, but is likely to be compiled to
1518 * to code using bitwise operation rather than a branch.
1519 *
1520 * \param size Size to check.
1521 * \param max Maximum desired value for \p size.
1522 * \return \c 0 if `size <= max`.
1523 * \return \c 1 if `size > max`.
1524 */
1525static unsigned size_greater_than( size_t size, size_t max )
1526{
1527 /* Return the sign bit (1 for negative) of (max - size). */
1528 return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) );
1529}
1530
1531/** Choose between two integer values, without branches.
1532 *
1533 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1534 * to code using bitwise operation rather than a branch.
1535 *
1536 * \param cond Condition to test.
1537 * \param if1 Value to use if \p cond is nonzero.
1538 * \param if0 Value to use if \p cond is zero.
1539 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1540 */
1541static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
1542{
1543 unsigned mask = all_or_nothing_int( cond );
1544 return( ( mask & if1 ) | (~mask & if0 ) );
1545}
1546
1547/** Shift some data towards the left inside a buffer without leaking
1548 * the length of the data through side channels.
1549 *
1550 * `mem_move_to_left(start, total, offset)` is functionally equivalent to
1551 * ```
1552 * memmove(start, start + offset, total - offset);
1553 * memset(start + offset, 0, total - offset);
1554 * ```
1555 * but it strives to use a memory access pattern (and thus total timing)
1556 * that does not depend on \p offset. This timing independence comes at
1557 * the expense of performance.
1558 *
1559 * \param start Pointer to the start of the buffer.
1560 * \param total Total size of the buffer.
1561 * \param offset Offset from which to copy \p total - \p offset bytes.
1562 */
1563static void mem_move_to_left( void *start,
1564 size_t total,
1565 size_t offset )
1566{
1567 volatile unsigned char *buf = start;
1568 size_t i, n;
1569 if( total == 0 )
1570 return;
1571 for( i = 0; i < total; i++ )
1572 {
1573 unsigned no_op = size_greater_than( total - offset, i );
1574 /* The first `total - offset` passes are a no-op. The last
1575 * `offset` passes shift the data one byte to the left and
1576 * zero out the last byte. */
1577 for( n = 0; n < total - 1; n++ )
1578 {
1579 unsigned char current = buf[n];
1580 unsigned char next = buf[n+1];
1581 buf[n] = if_int( no_op, current, next );
1582 }
1583 buf[total-1] = if_int( no_op, buf[total-1], 0 );
1584 }
1585}
1586
Paul Bakkerb3869132013-02-28 17:21:01 +01001587/*
1588 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001591 int (*f_rng)(void *, unsigned char *, size_t),
1592 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001593 int mode, size_t *olen,
1594 const unsigned char *input,
1595 unsigned char *output,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001596 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001597{
Janos Follath24eed8d2019-11-22 13:21:35 +00001598 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001599 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001601 /* The following variables take sensitive values: their value must
1602 * not leak into the observable behavior of the function other than
1603 * the designated outputs (output, olen, return value). Otherwise
1604 * this would open the execution of the function to
1605 * side-channel-based variants of the Bleichenbacher padding oracle
1606 * attack. Potential side channels include overall timing, memory
1607 * access patterns (especially visible to an adversary who has access
1608 * to a shared memory cache), and branches (especially visible to
1609 * an adversary who has access to a shared code cache or to a shared
1610 * branch predictor). */
1611 size_t pad_count = 0;
1612 unsigned bad = 0;
1613 unsigned char pad_done = 0;
1614 size_t plaintext_size = 0;
1615 unsigned output_too_large;
1616
1617 RSA_VALIDATE_RET( ctx != NULL );
1618 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1619 mode == MBEDTLS_RSA_PUBLIC );
1620 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1621 RSA_VALIDATE_RET( input != NULL );
1622 RSA_VALIDATE_RET( olen != NULL );
1623
1624 ilen = ctx->len;
1625 plaintext_max_size = ( output_max_len > ilen - 11 ?
1626 ilen - 11 :
1627 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1630 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001631
Paul Bakkerb3869132013-02-28 17:21:01 +01001632 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1636 ? mbedtls_rsa_public( ctx, input, buf )
1637 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001638
1639 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001640 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001641
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001642 /* Check and get padding length in constant time and constant
1643 * memory trace. The first byte must be 0. */
1644 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001647 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001648 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1649 * where PS must be at least 8 nonzero bytes. */
1650 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001652 /* Read the whole buffer. Set pad_done to nonzero if we find
1653 * the 0x00 byte and remember the padding length in pad_count. */
1654 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001655 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001656 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001657 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001658 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001659 }
1660 else
1661 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001662 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1663 * where PS must be at least 8 bytes with the value 0xFF. */
1664 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001665
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001666 /* Read the whole buffer. Set pad_done to nonzero if we find
1667 * the 0x00 byte and remember the padding length in pad_count.
1668 * If there's a non-0xff byte in the padding, the padding is bad. */
1669 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001670 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001671 pad_done |= if_int( buf[i], 0, 1 );
1672 pad_count += if_int( pad_done, 0, 1 );
1673 bad |= if_int( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001674 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001675 }
1676
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001677 /* If pad_done is still zero, there's no data, only unfinished padding. */
1678 bad |= if_int( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001679
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001680 /* There must be at least 8 bytes of padding. */
1681 bad |= size_greater_than( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001682
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001683 /* If the padding is valid, set plaintext_size to the number of
1684 * remaining bytes after stripping the padding. If the padding
1685 * is invalid, avoid leaking this fact through the size of the
1686 * output: use the maximum message size that fits in the output
1687 * buffer. Do it without branches to avoid leaking the padding
1688 * validity through timing. RSA keys are small enough that all the
1689 * size_t values involved fit in unsigned int. */
1690 plaintext_size = if_int( bad,
1691 (unsigned) plaintext_max_size,
1692 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001693
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001694 /* Set output_too_large to 0 if the plaintext fits in the output
1695 * buffer and to 1 otherwise. */
1696 output_too_large = size_greater_than( plaintext_size,
1697 plaintext_max_size );
1698
1699 /* Set ret without branches to avoid timing attacks. Return:
1700 * - INVALID_PADDING if the padding is bad (bad != 0).
1701 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1702 * plaintext does not fit in the output buffer.
1703 * - 0 if the padding is correct. */
1704 ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1705 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1706 0 ) );
1707
1708 /* If the padding is bad or the plaintext is too large, zero the
1709 * data that we're about to copy to the output buffer.
1710 * We need to copy the same amount of data
1711 * from the same buffer whether the padding is good or not to
1712 * avoid leaking the padding validity through overall timing or
1713 * through memory or cache access patterns. */
1714 bad = all_or_nothing_int( bad | output_too_large );
1715 for( i = 11; i < ilen; i++ )
1716 buf[i] &= ~bad;
1717
1718 /* If the plaintext is too large, truncate it to the buffer size.
1719 * Copy anyway to avoid revealing the length through timing, because
1720 * revealing the length is as bad as revealing the padding validity
1721 * for a Bleichenbacher attack. */
1722 plaintext_size = if_int( output_too_large,
1723 (unsigned) plaintext_max_size,
1724 (unsigned) plaintext_size );
1725
1726 /* Move the plaintext to the leftmost position where it can start in
1727 * the working buffer, i.e. make it start plaintext_max_size from
1728 * the end of the buffer. Do this with a memory access trace that
1729 * does not depend on the plaintext size. After this move, the
1730 * starting location of the plaintext is no longer sensitive
1731 * information. */
1732 mem_move_to_left( buf + ilen - plaintext_max_size,
1733 plaintext_max_size,
1734 plaintext_max_size - plaintext_size );
1735
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001736 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1737 * buffer. If output_max_len is 0, then output may be an invalid pointer
1738 * and the result of memcpy() would be undefined; prevent undefined
1739 * behavior making sure to depend only on output_max_len (the size of the
1740 * user-provided output buffer), which is independent from plaintext
1741 * length, validity of padding, success of the decryption, and other
1742 * secrets. */
1743 if( output_max_len != 0 )
1744 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001745
1746 /* Report the amount of data we copied to the output buffer. In case
1747 * of errors (bad padding or output too large), the value of *olen
1748 * when this function returns is not specified. Making it equivalent
1749 * to the good case limits the risks of leaking the padding validity. */
1750 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001752cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001753 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001754
1755 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001756}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001758
1759/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001760 * Do an RSA operation, then remove the message padding
1761 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001763 int (*f_rng)(void *, unsigned char *, size_t),
1764 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001765 int mode, size_t *olen,
1766 const unsigned char *input,
1767 unsigned char *output,
1768 size_t output_max_len)
1769{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001770 RSA_VALIDATE_RET( ctx != NULL );
1771 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1772 mode == MBEDTLS_RSA_PUBLIC );
1773 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1774 RSA_VALIDATE_RET( input != NULL );
1775 RSA_VALIDATE_RET( olen != NULL );
1776
Paul Bakkerb3869132013-02-28 17:21:01 +01001777 switch( ctx->padding )
1778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779#if defined(MBEDTLS_PKCS1_V15)
1780 case MBEDTLS_RSA_PKCS_V15:
1781 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001782 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001783#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#if defined(MBEDTLS_PKCS1_V21)
1786 case MBEDTLS_RSA_PKCS_V21:
1787 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001788 olen, input, output,
1789 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001790#endif
1791
1792 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001794 }
1795}
1796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001798static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001799 int (*f_rng)(void *, unsigned char *, size_t),
1800 void *p_rng,
1801 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001803 unsigned int hashlen,
1804 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001805 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001806 unsigned char *sig )
1807{
1808 size_t olen;
1809 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001810 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001811 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001812 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001813 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 const mbedtls_md_info_t *md_info;
1815 mbedtls_md_context_t md_ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001816 RSA_VALIDATE_RET( ctx != NULL );
1817 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1818 mode == MBEDTLS_RSA_PUBLIC );
1819 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
1820 hashlen == 0 ) ||
1821 hash != NULL );
1822 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1825 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001826
1827 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001829
1830 olen = ctx->len;
1831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001833 {
Simon Butcher02037452016-03-01 21:19:12 +00001834 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001836 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001840 }
1841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001843 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001847
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001848 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1849 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001850 /* Calculate the largest possible salt length, up to the hash size.
1851 * Normally this is the hash length, which is the maximum salt length
1852 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001853 * enough room, use the maximum salt length that fits. The constraint is
1854 * that the hash length plus the salt length plus 2 bytes must be at most
1855 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1856 * (PKCS#1 v2.2) §9.1.1 step 3. */
1857 min_slen = hlen - 2;
1858 if( olen < hlen + min_slen + 2 )
1859 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1860 else if( olen >= hlen + hlen + 2 )
1861 slen = hlen;
1862 else
1863 slen = olen - hlen - 2;
1864 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001865 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001868 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001869 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001870 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001871 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001872 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001873
1874 memset( sig, 0, olen );
1875
Simon Butcher02037452016-03-01 21:19:12 +00001876 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001877 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001878 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001879 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001880
1881 /* Generate salt of length slen in place in the encoded message */
1882 salt = p;
1883 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jones713e4e72021-01-11 12:31:27 +00001884 return( MBEDTLS_ERR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001885
Paul Bakkerb3869132013-02-28 17:21:01 +01001886 p += slen;
1887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001889 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001890 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001891
Simon Butcher02037452016-03-01 21:19:12 +00001892 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001893 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1894 goto exit;
1895 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1896 goto exit;
1897 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1898 goto exit;
1899 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1900 goto exit;
1901 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1902 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001903
Simon Butcher02037452016-03-01 21:19:12 +00001904 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001905 if( msb % 8 == 0 )
1906 offset = 1;
1907
Simon Butcher02037452016-03-01 21:19:12 +00001908 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001909 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1910 &md_ctx ) ) != 0 )
1911 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001912
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001913 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001914 sig[0] &= 0xFF >> ( olen * 8 - msb );
1915
1916 p += hlen;
1917 *p++ = 0xBC;
1918
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001919exit:
1920 mbedtls_md_free( &md_ctx );
1921
1922 if( ret != 0 )
1923 return( ret );
1924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 return( ( mode == MBEDTLS_RSA_PUBLIC )
1926 ? mbedtls_rsa_public( ctx, sig, sig )
1927 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001928}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001929
1930/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001931 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1932 * the option to pass in the salt length.
1933 */
1934int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1935 int (*f_rng)(void *, unsigned char *, size_t),
1936 void *p_rng,
1937 mbedtls_md_type_t md_alg,
1938 unsigned int hashlen,
1939 const unsigned char *hash,
1940 int saltlen,
1941 unsigned char *sig )
1942{
1943 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, md_alg,
1944 hashlen, hash, saltlen, sig );
1945}
1946
1947
1948/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001949 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1950 */
1951int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1952 int (*f_rng)(void *, unsigned char *, size_t),
1953 void *p_rng,
1954 int mode,
1955 mbedtls_md_type_t md_alg,
1956 unsigned int hashlen,
1957 const unsigned char *hash,
1958 unsigned char *sig )
1959{
Cédric Meuterf3fab332020-04-25 11:30:45 +02001960 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
1961 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001962}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001966/*
1967 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1968 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001969
1970/* Construct a PKCS v1.5 encoding of a hashed message
1971 *
1972 * This is used both for signature generation and verification.
1973 *
1974 * Parameters:
1975 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001976 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001977 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001978 * - hash: Buffer containing the hashed message or the raw data.
1979 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001980 * - dst: Buffer to hold the encoded message.
1981 *
1982 * Assumptions:
1983 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1984 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001985 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001986 *
1987 */
1988static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1989 unsigned int hashlen,
1990 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001991 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001992 unsigned char *dst )
1993{
1994 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001995 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001996 unsigned char *p = dst;
1997 const char *oid = NULL;
1998
1999 /* Are we signing hashed or raw data? */
2000 if( md_alg != MBEDTLS_MD_NONE )
2001 {
2002 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
2003 if( md_info == NULL )
2004 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2005
2006 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
2007 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2008
2009 hashlen = mbedtls_md_get_size( md_info );
2010
2011 /* Double-check that 8 + hashlen + oid_size can be used as a
2012 * 1-byte ASN.1 length encoding and that there's no overflow. */
2013 if( 8 + hashlen + oid_size >= 0x80 ||
2014 10 + hashlen < hashlen ||
2015 10 + hashlen + oid_size < 10 + hashlen )
2016 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2017
2018 /*
2019 * Static bounds check:
2020 * - Need 10 bytes for five tag-length pairs.
2021 * (Insist on 1-byte length encodings to protect against variants of
2022 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2023 * - Need hashlen bytes for hash
2024 * - Need oid_size bytes for hash alg OID.
2025 */
2026 if( nb_pad < 10 + hashlen + oid_size )
2027 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2028 nb_pad -= 10 + hashlen + oid_size;
2029 }
2030 else
2031 {
2032 if( nb_pad < hashlen )
2033 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2034
2035 nb_pad -= hashlen;
2036 }
2037
Hanno Becker2b2f8982017-09-27 17:10:03 +01002038 /* Need space for signature header and padding delimiter (3 bytes),
2039 * and 8 bytes for the minimal padding */
2040 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002041 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2042 nb_pad -= 3;
2043
2044 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002045 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002046
2047 /* Write signature header and padding */
2048 *p++ = 0;
2049 *p++ = MBEDTLS_RSA_SIGN;
2050 memset( p, 0xFF, nb_pad );
2051 p += nb_pad;
2052 *p++ = 0;
2053
2054 /* Are we signing raw data? */
2055 if( md_alg == MBEDTLS_MD_NONE )
2056 {
2057 memcpy( p, hash, hashlen );
2058 return( 0 );
2059 }
2060
2061 /* Signing hashed data, add corresponding ASN.1 structure
2062 *
2063 * DigestInfo ::= SEQUENCE {
2064 * digestAlgorithm DigestAlgorithmIdentifier,
2065 * digest Digest }
2066 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2067 * Digest ::= OCTET STRING
2068 *
2069 * Schematic:
2070 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2071 * TAG-NULL + LEN [ NULL ] ]
2072 * TAG-OCTET + LEN [ HASH ] ]
2073 */
2074 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002075 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002076 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002077 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002078 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002079 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002080 memcpy( p, oid, oid_size );
2081 p += oid_size;
2082 *p++ = MBEDTLS_ASN1_NULL;
2083 *p++ = 0x00;
2084 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002085 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002086 memcpy( p, hash, hashlen );
2087 p += hashlen;
2088
2089 /* Just a sanity-check, should be automatic
2090 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002091 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002092 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002093 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002094 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2095 }
2096
2097 return( 0 );
2098}
2099
Paul Bakkerb3869132013-02-28 17:21:01 +01002100/*
2101 * Do an RSA operation to sign the message digest
2102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002104 int (*f_rng)(void *, unsigned char *, size_t),
2105 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002106 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002108 unsigned int hashlen,
2109 const unsigned char *hash,
2110 unsigned char *sig )
2111{
Janos Follath24eed8d2019-11-22 13:21:35 +00002112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002113 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002114
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002115 RSA_VALIDATE_RET( ctx != NULL );
2116 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2117 mode == MBEDTLS_RSA_PUBLIC );
2118 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2119 hashlen == 0 ) ||
2120 hash != NULL );
2121 RSA_VALIDATE_RET( sig != NULL );
2122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2124 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002125
Hanno Beckerfdf38032017-09-06 12:35:55 +01002126 /*
2127 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2128 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002129
Hanno Beckerfdf38032017-09-06 12:35:55 +01002130 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2131 ctx->len, sig ) ) != 0 )
2132 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002133
2134 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002135 * Call respective RSA primitive
2136 */
2137
2138 if( mode == MBEDTLS_RSA_PUBLIC )
2139 {
2140 /* Skip verification on a public key operation */
2141 return( mbedtls_rsa_public( ctx, sig, sig ) );
2142 }
2143
2144 /* Private key operation
2145 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002146 * In order to prevent Lenstra's attack, make the signature in a
2147 * temporary buffer and check it before returning it.
2148 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002149
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002150 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002151 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002152 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2153
Hanno Beckerfdf38032017-09-06 12:35:55 +01002154 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002155 if( verif == NULL )
2156 {
2157 mbedtls_free( sig_try );
2158 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2159 }
2160
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002161 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2162 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2163
Hanno Becker171a8f12017-09-06 12:32:16 +01002164 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002165 {
2166 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2167 goto cleanup;
2168 }
2169
2170 memcpy( sig, sig_try, ctx->len );
2171
2172cleanup:
2173 mbedtls_free( sig_try );
2174 mbedtls_free( verif );
2175
2176 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002177}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002179
2180/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 * Do an RSA operation to sign the message digest
2182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002184 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002185 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002188 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002189 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 unsigned char *sig )
2191{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002192 RSA_VALIDATE_RET( ctx != NULL );
2193 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2194 mode == MBEDTLS_RSA_PUBLIC );
2195 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2196 hashlen == 0 ) ||
2197 hash != NULL );
2198 RSA_VALIDATE_RET( sig != NULL );
2199
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 switch( ctx->padding )
2201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202#if defined(MBEDTLS_PKCS1_V15)
2203 case MBEDTLS_RSA_PKCS_V15:
2204 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002205 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002206#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208#if defined(MBEDTLS_PKCS1_V21)
2209 case MBEDTLS_RSA_PKCS_V21:
2210 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002211 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002212#endif
2213
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002217}
2218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002220/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002221 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002224 int (*f_rng)(void *, unsigned char *, size_t),
2225 void *p_rng,
2226 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002228 unsigned int hashlen,
2229 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002231 int expected_salt_len,
2232 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002233{
Janos Follath24eed8d2019-11-22 13:21:35 +00002234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002235 size_t siglen;
2236 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002237 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002239 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002240 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002241 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 const mbedtls_md_info_t *md_info;
2243 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002244 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002245
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002246 RSA_VALIDATE_RET( ctx != NULL );
2247 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2248 mode == MBEDTLS_RSA_PUBLIC );
2249 RSA_VALIDATE_RET( sig != NULL );
2250 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2251 hashlen == 0 ) ||
2252 hash != NULL );
2253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2255 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002256
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 siglen = ctx->len;
2258
Paul Bakker27fdf462011-06-09 13:55:13 +00002259 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2263 ? mbedtls_rsa_public( ctx, sig, buf )
2264 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
2266 if( ret != 0 )
2267 return( ret );
2268
2269 p = buf;
2270
Paul Bakkerb3869132013-02-28 17:21:01 +01002271 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 {
Simon Butcher02037452016-03-01 21:19:12 +00002276 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002278 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002282 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002285 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002289
Paul Bakkerb3869132013-02-28 17:21:01 +01002290 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002291
Simon Butcher02037452016-03-01 21:19:12 +00002292 /*
2293 * Note: EMSA-PSS verification is over the length of N - 1 bits
2294 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002295 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002296
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002297 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2298 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2299
Simon Butcher02037452016-03-01 21:19:12 +00002300 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002301 if( msb % 8 == 0 )
2302 {
2303 p++;
2304 siglen -= 1;
2305 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002306
Gilles Peskine139108a2017-10-18 19:03:42 +02002307 if( siglen < hlen + 2 )
2308 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2309 hash_start = p + siglen - hlen - 1;
2310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002312 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002313 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002314
Jaeden Amero66954e12018-01-25 16:05:54 +00002315 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2316 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002317 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002318
Paul Bakkerb3869132013-02-28 17:21:01 +01002319 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002320
Gilles Peskine6a54b022017-10-17 19:02:13 +02002321 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002322 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002323
Gilles Peskine91048a32017-10-19 17:46:14 +02002324 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002325 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002326 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2327 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002328 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002329
Gilles Peskine6a54b022017-10-17 19:02:13 +02002330 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002333 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002334 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002335 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2336 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002337 }
2338
Simon Butcher02037452016-03-01 21:19:12 +00002339 /*
2340 * Generate H = Hash( M' )
2341 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002342 ret = mbedtls_md_starts( &md_ctx );
2343 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002344 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002345 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2346 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002347 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002348 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2349 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002350 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002351 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2352 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002353 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002354 ret = mbedtls_md_finish( &md_ctx, result );
2355 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002356 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002357
Jaeden Amero66954e12018-01-25 16:05:54 +00002358 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002359 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002360 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002361 goto exit;
2362 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002363
2364exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002366
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002367 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002368}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002369
2370/*
2371 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002374 int (*f_rng)(void *, unsigned char *, size_t),
2375 void *p_rng,
2376 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002378 unsigned int hashlen,
2379 const unsigned char *hash,
2380 const unsigned char *sig )
2381{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002382 mbedtls_md_type_t mgf1_hash_id;
2383 RSA_VALIDATE_RET( ctx != NULL );
2384 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2385 mode == MBEDTLS_RSA_PUBLIC );
2386 RSA_VALIDATE_RET( sig != NULL );
2387 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2388 hashlen == 0 ) ||
2389 hash != NULL );
2390
2391 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002393 : md_alg;
2394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002396 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002398 sig ) );
2399
2400}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002404/*
2405 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002408 int (*f_rng)(void *, unsigned char *, size_t),
2409 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002410 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002412 unsigned int hashlen,
2413 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002414 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002415{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002416 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002417 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002418 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002419
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002420 RSA_VALIDATE_RET( ctx != NULL );
2421 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2422 mode == MBEDTLS_RSA_PUBLIC );
2423 RSA_VALIDATE_RET( sig != NULL );
2424 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2425 hashlen == 0 ) ||
2426 hash != NULL );
2427
2428 sig_len = ctx->len;
2429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2431 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002432
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002433 /*
2434 * Prepare expected PKCS1 v1.5 encoding of hash.
2435 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002436
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002437 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2438 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2439 {
2440 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2441 goto cleanup;
2442 }
2443
2444 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2445 encoded_expected ) ) != 0 )
2446 goto cleanup;
2447
2448 /*
2449 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2450 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002453 ? mbedtls_rsa_public( ctx, sig, encoded )
2454 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002455 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002456 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002457
Simon Butcher02037452016-03-01 21:19:12 +00002458 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002459 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002460 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002461
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002462 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2463 sig_len ) ) != 0 )
2464 {
2465 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2466 goto cleanup;
2467 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002468
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002469cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002470
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002471 if( encoded != NULL )
2472 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002473 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002474 mbedtls_free( encoded );
2475 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002476
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002477 if( encoded_expected != NULL )
2478 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002479 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002480 mbedtls_free( encoded_expected );
2481 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002482
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002483 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
2487/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002488 * Do an RSA operation and check the message digest
2489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002491 int (*f_rng)(void *, unsigned char *, size_t),
2492 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002493 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002495 unsigned int hashlen,
2496 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002497 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002498{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002499 RSA_VALIDATE_RET( ctx != NULL );
2500 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2501 mode == MBEDTLS_RSA_PUBLIC );
2502 RSA_VALIDATE_RET( sig != NULL );
2503 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2504 hashlen == 0 ) ||
2505 hash != NULL );
2506
Paul Bakkerb3869132013-02-28 17:21:01 +01002507 switch( ctx->padding )
2508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#if defined(MBEDTLS_PKCS1_V15)
2510 case MBEDTLS_RSA_PKCS_V15:
2511 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002512 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002513#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#if defined(MBEDTLS_PKCS1_V21)
2516 case MBEDTLS_RSA_PKCS_V21:
2517 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002518 hashlen, hash, sig );
2519#endif
2520
2521 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002523 }
2524}
2525
2526/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002527 * Copy the components of an RSA key
2528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002530{
Janos Follath24eed8d2019-11-22 13:21:35 +00002531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002532 RSA_VALIDATE_RET( dst != NULL );
2533 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002534
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002535 dst->len = src->len;
2536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2538 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2541 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2542 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002543
2544#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2546 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2547 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2549 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002550#endif
2551
2552 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2555 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002556
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002557 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002558 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002559
2560cleanup:
2561 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002563
2564 return( ret );
2565}
2566
2567/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 * Free the components of an RSA key
2569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002571{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002572 if( ctx == NULL )
2573 return;
2574
2575 mbedtls_mpi_free( &ctx->Vi );
2576 mbedtls_mpi_free( &ctx->Vf );
2577 mbedtls_mpi_free( &ctx->RN );
2578 mbedtls_mpi_free( &ctx->D );
2579 mbedtls_mpi_free( &ctx->Q );
2580 mbedtls_mpi_free( &ctx->P );
2581 mbedtls_mpi_free( &ctx->E );
2582 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002583
Hanno Becker33c30a02017-08-23 07:00:22 +01002584#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002585 mbedtls_mpi_free( &ctx->RQ );
2586 mbedtls_mpi_free( &ctx->RP );
2587 mbedtls_mpi_free( &ctx->QP );
2588 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002589 mbedtls_mpi_free( &ctx->DP );
2590#endif /* MBEDTLS_RSA_NO_CRT */
2591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002593 /* Free the mutex, but only if it hasn't been freed already. */
2594 if( ctx->ver != 0 )
2595 {
2596 mbedtls_mutex_free( &ctx->mutex );
2597 ctx->ver = 0;
2598 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002599#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002600}
2601
Hanno Beckerab377312017-08-23 16:24:51 +01002602#endif /* !MBEDTLS_RSA_ALT */
2603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002606#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
2608/*
2609 * Example RSA-1024 keypair, for test purposes
2610 */
2611#define KEY_LEN 128
2612
2613#define RSA_N "9292758453063D803DD603D5E777D788" \
2614 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2615 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2616 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2617 "93A89813FBF3C4F8066D2D800F7C38A8" \
2618 "1AE31942917403FF4946B0A83D3D3E05" \
2619 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2620 "5E94BB77B07507233A0BC7BAC8F90F79"
2621
2622#define RSA_E "10001"
2623
2624#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2625 "66CA472BC44D253102F8B4A9D3BFA750" \
2626 "91386C0077937FE33FA3252D28855837" \
2627 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2628 "DF79C5CE07EE72C7F123142198164234" \
2629 "CABB724CF78B8173B9F880FC86322407" \
2630 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2631 "071513A1E85B5DFA031F21ECAE91A34D"
2632
2633#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2634 "2C01CAD19EA484A87EA4377637E75500" \
2635 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2636 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2637
2638#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2639 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2640 "910E4168387E3C30AA1E00C339A79508" \
2641 "8452DD96A9A5EA5D9DCA68DA636032AF"
2642
Paul Bakker5121ce52009-01-03 21:22:43 +00002643#define PT_LEN 24
2644#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2645 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002648static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002649{
gufe44c2620da2020-08-03 17:56:50 +02002650#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002651 size_t i;
2652
Paul Bakker545570e2010-07-18 09:00:25 +00002653 if( rng_state != NULL )
2654 rng_state = NULL;
2655
Paul Bakkera3d195c2011-11-27 21:07:34 +00002656 for( i = 0; i < len; ++i )
2657 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002658#else
2659 if( rng_state != NULL )
2660 rng_state = NULL;
2661
2662 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002663#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002664
Paul Bakkera3d195c2011-11-27 21:07:34 +00002665 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002668
Paul Bakker5121ce52009-01-03 21:22:43 +00002669/*
2670 * Checkup routine
2671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002673{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002674 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002676 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 unsigned char rsa_plaintext[PT_LEN];
2679 unsigned char rsa_decrypted[PT_LEN];
2680 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002682 unsigned char sha1sum[20];
2683#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Hanno Becker3a701162017-08-22 13:52:43 +01002685 mbedtls_mpi K;
2686
2687 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
Hanno Becker3a701162017-08-22 13:52:43 +01002690 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2691 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2692 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2693 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2694 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2695 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2696 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2697 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2698 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2699 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2700
Hanno Becker7f25f852017-10-10 16:56:22 +01002701 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
2703 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2707 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 {
2709 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
Hanno Becker5bc87292017-05-03 15:09:31 +01002712 ret = 1;
2713 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 }
2715
2716 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
2719 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2720
Hanno Becker98838b02017-10-02 13:16:10 +01002721 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2722 PT_LEN, rsa_plaintext,
2723 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 {
2725 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
Hanno Becker5bc87292017-05-03 15:09:31 +01002728 ret = 1;
2729 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 }
2731
2732 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Hanno Becker98838b02017-10-02 13:16:10 +01002735 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2736 &len, rsa_ciphertext, rsa_decrypted,
2737 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002738 {
2739 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Hanno Becker5bc87292017-05-03 15:09:31 +01002742 ret = 1;
2743 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 }
2745
2746 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2747 {
2748 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Hanno Becker5bc87292017-05-03 15:09:31 +01002751 ret = 1;
2752 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 }
2754
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002755 if( verbose != 0 )
2756 mbedtls_printf( "passed\n" );
2757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002760 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002762 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002763 {
2764 if( verbose != 0 )
2765 mbedtls_printf( "failed\n" );
2766
2767 return( 1 );
2768 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
Hanno Becker98838b02017-10-02 13:16:10 +01002770 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2771 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2772 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 {
2774 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
Hanno Becker5bc87292017-05-03 15:09:31 +01002777 ret = 1;
2778 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 }
2780
2781 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
Hanno Becker98838b02017-10-02 13:16:10 +01002784 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2785 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2786 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 {
2788 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002790
Hanno Becker5bc87292017-05-03 15:09:31 +01002791 ret = 1;
2792 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 }
2794
2795 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002796 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002798
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002799 if( verbose != 0 )
2800 mbedtls_printf( "\n" );
2801
Paul Bakker3d8fb632014-04-17 12:42:41 +02002802cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002803 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 mbedtls_rsa_free( &rsa );
2805#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002806 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002808 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002809}
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813#endif /* MBEDTLS_RSA_C */