blob: 26b6d3422931823cc71569c6ba258477a0d9d2f9 [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"
gabor-mezei-arm944c1072021-09-27 11:28:54 +020047#include "constant_time.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000048
Rich Evans00ab4702015-02-06 13:43:58 +000049#include <string.h>
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
gufe44c2620da2020-08-03 17:56:50 +020055#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000056#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010061#else
Rich Evans00ab4702015-02-06 13:43:58 +000062#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020064#define mbedtls_calloc calloc
65#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010066#endif
67
Hanno Beckera565f542017-10-11 11:00:19 +010068#if !defined(MBEDTLS_RSA_ALT)
69
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050070/* Parameter validation macros */
71#define RSA_VALIDATE_RET( cond ) \
72 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
73#define RSA_VALIDATE( cond ) \
74 MBEDTLS_INTERNAL_VALIDATE( cond )
75
Hanno Becker617c1ae2017-08-23 14:11:24 +010076int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
77 const mbedtls_mpi *N,
78 const mbedtls_mpi *P, const mbedtls_mpi *Q,
79 const mbedtls_mpi *D, const mbedtls_mpi *E )
80{
Janos Follath24eed8d2019-11-22 13:21:35 +000081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050082 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +010083
84 if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) ||
85 ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) ||
86 ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) ||
87 ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) ||
88 ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) )
89 {
Chris Jonesb7d02e02021-04-01 17:40:03 +010090 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +010091 }
92
93 if( N != NULL )
94 ctx->len = mbedtls_mpi_size( &ctx->N );
95
96 return( 0 );
97}
98
99int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
Hanno Becker74716312017-10-02 10:00:37 +0100100 unsigned char const *N, size_t N_len,
101 unsigned char const *P, size_t P_len,
102 unsigned char const *Q, size_t Q_len,
103 unsigned char const *D, size_t D_len,
104 unsigned char const *E, size_t E_len )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100105{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000106 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500107 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100108
109 if( N != NULL )
110 {
111 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) );
112 ctx->len = mbedtls_mpi_size( &ctx->N );
113 }
114
115 if( P != NULL )
116 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) );
117
118 if( Q != NULL )
119 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) );
120
121 if( D != NULL )
122 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) );
123
124 if( E != NULL )
125 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) );
126
127cleanup:
128
129 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100130 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100131
132 return( 0 );
133}
134
Hanno Becker705fc682017-10-10 17:57:02 +0100135/*
136 * Checks whether the context fields are set in such a way
137 * that the RSA primitives will be able to execute without error.
138 * It does *not* make guarantees for consistency of the parameters.
139 */
Hanno Beckerebd2c022017-10-12 10:54:53 +0100140static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
141 int blinding_needed )
Hanno Becker705fc682017-10-10 17:57:02 +0100142{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100143#if !defined(MBEDTLS_RSA_NO_CRT)
144 /* blinding_needed is only used for NO_CRT to decide whether
145 * P,Q need to be present or not. */
146 ((void) blinding_needed);
147#endif
148
Hanno Becker3a760a12018-01-05 08:14:49 +0000149 if( ctx->len != mbedtls_mpi_size( &ctx->N ) ||
150 ctx->len > MBEDTLS_MPI_MAX_SIZE )
151 {
Hanno Becker705fc682017-10-10 17:57:02 +0100152 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker3a760a12018-01-05 08:14:49 +0000153 }
Hanno Becker705fc682017-10-10 17:57:02 +0100154
155 /*
156 * 1. Modular exponentiation needs positive, odd moduli.
157 */
158
159 /* Modular exponentiation wrt. N is always used for
160 * RSA public key operations. */
161 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 ||
162 mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 )
163 {
164 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
165 }
166
167#if !defined(MBEDTLS_RSA_NO_CRT)
168 /* Modular exponentiation for P and Q is only
169 * used for private key operations and if CRT
170 * is used. */
171 if( is_priv &&
172 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
173 mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 ||
174 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ||
175 mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) )
176 {
177 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
178 }
179#endif /* !MBEDTLS_RSA_NO_CRT */
180
181 /*
182 * 2. Exponents must be positive
183 */
184
185 /* Always need E for public key operations */
186 if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 )
187 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
188
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100189#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100190 /* For private key operations, use D or DP & DQ
191 * as (unblinded) exponents. */
192 if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 )
193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
194#else
195 if( is_priv &&
196 ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 ||
197 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) )
198 {
199 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
200 }
201#endif /* MBEDTLS_RSA_NO_CRT */
202
203 /* Blinding shouldn't make exponents negative either,
204 * so check that P, Q >= 1 if that hasn't yet been
205 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100206#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Beckerebd2c022017-10-12 10:54:53 +0100207 if( is_priv && blinding_needed &&
Hanno Becker705fc682017-10-10 17:57:02 +0100208 ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 ||
209 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) )
210 {
211 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
212 }
213#endif
214
215 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100216 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100217#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100218 if( is_priv &&
219 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 )
220 {
221 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
222 }
223#endif
224
225 return( 0 );
226}
227
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100228int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100229{
230 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500231 int have_N, have_P, have_Q, have_D, have_E;
232#if !defined(MBEDTLS_RSA_NO_CRT)
233 int have_DP, have_DQ, have_QP;
234#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500235 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100236
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500237 RSA_VALIDATE_RET( ctx != NULL );
238
239 have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 );
240 have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 );
241 have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
242 have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
243 have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500244
245#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd80cc8112020-01-22 17:34:29 -0500246 have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
247 have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
248 have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500249#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100250
Hanno Becker617c1ae2017-08-23 14:11:24 +0100251 /*
252 * Check whether provided parameters are enough
253 * to deduce all others. The following incomplete
254 * parameter sets for private keys are supported:
255 *
256 * (1) P, Q missing.
257 * (2) D and potentially N missing.
258 *
259 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100260
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500261 n_missing = have_P && have_Q && have_D && have_E;
262 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
263 d_missing = have_P && have_Q && !have_D && have_E;
264 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100265
266 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500267 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100268
Hanno Becker617c1ae2017-08-23 14:11:24 +0100269 if( !is_priv && !is_pub )
270 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
271
272 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100273 * Step 1: Deduce N if P, Q are provided.
274 */
275
276 if( !have_N && have_P && have_Q )
277 {
278 if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P,
279 &ctx->Q ) ) != 0 )
280 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100281 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker2cca6f32017-09-29 11:46:40 +0100282 }
283
284 ctx->len = mbedtls_mpi_size( &ctx->N );
285 }
286
287 /*
288 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100289 */
290
291 if( pq_missing )
292 {
Hanno Beckerc36aab62017-10-17 09:15:06 +0100293 ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->E, &ctx->D,
Hanno Becker617c1ae2017-08-23 14:11:24 +0100294 &ctx->P, &ctx->Q );
295 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100296 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100297
298 }
299 else if( d_missing )
300 {
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100301 if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P,
302 &ctx->Q,
303 &ctx->E,
304 &ctx->D ) ) != 0 )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100305 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100306 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100307 }
308 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100309
Hanno Becker617c1ae2017-08-23 14:11:24 +0100310 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100311 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100312 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100313 */
314
Hanno Becker23344b52017-08-23 07:43:27 +0100315#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500316 if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
Hanno Becker617c1ae2017-08-23 14:11:24 +0100317 {
318 ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
319 &ctx->DP, &ctx->DQ, &ctx->QP );
320 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100321 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100322 }
Hanno Becker23344b52017-08-23 07:43:27 +0100323#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324
325 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100326 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327 */
328
Hanno Beckerebd2c022017-10-12 10:54:53 +0100329 return( rsa_check_context( ctx, is_priv, 1 ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100330}
331
Hanno Becker617c1ae2017-08-23 14:11:24 +0100332int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
333 unsigned char *N, size_t N_len,
334 unsigned char *P, size_t P_len,
335 unsigned char *Q, size_t Q_len,
336 unsigned char *D, size_t D_len,
337 unsigned char *E, size_t E_len )
338{
339 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500340 int is_priv;
341 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100342
343 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500344 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100345 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
346 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
347 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
348 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
349 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
350
351 if( !is_priv )
352 {
353 /* If we're trying to export private parameters for a public key,
354 * something must be wrong. */
355 if( P != NULL || Q != NULL || D != NULL )
356 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
357
358 }
359
360 if( N != NULL )
361 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) );
362
363 if( P != NULL )
364 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) );
365
366 if( Q != NULL )
367 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) );
368
369 if( D != NULL )
370 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) );
371
372 if( E != NULL )
373 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) );
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100374
375cleanup:
376
377 return( ret );
378}
379
Hanno Becker617c1ae2017-08-23 14:11:24 +0100380int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
381 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
382 mbedtls_mpi *D, mbedtls_mpi *E )
383{
Janos Follath24eed8d2019-11-22 13:21:35 +0000384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500385 int is_priv;
386 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100387
388 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500389 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100390 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
391 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
392 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
393 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
394 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
395
396 if( !is_priv )
397 {
398 /* If we're trying to export private parameters for a public key,
399 * something must be wrong. */
400 if( P != NULL || Q != NULL || D != NULL )
401 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
402
403 }
404
405 /* Export all requested core parameters. */
406
407 if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) ||
408 ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) ||
409 ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) ||
410 ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) ||
411 ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) )
412 {
413 return( ret );
414 }
415
416 return( 0 );
417}
418
419/*
420 * Export CRT parameters
421 * This must also be implemented if CRT is not used, for being able to
422 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
423 * can be used in this case.
424 */
425int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
426 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP )
427{
Janos Follath24eed8d2019-11-22 13:21:35 +0000428 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500429 int is_priv;
430 RSA_VALIDATE_RET( ctx != NULL );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100431
432 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500433 is_priv =
Hanno Becker617c1ae2017-08-23 14:11:24 +0100434 mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&
435 mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 &&
436 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 &&
437 mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 &&
438 mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0;
439
440 if( !is_priv )
441 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
442
Hanno Beckerdc95c892017-08-23 06:57:02 +0100443#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444 /* Export all requested blinding parameters. */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100445 if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) ||
446 ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) ||
447 ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) )
448 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100449 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100450 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100451#else
452 if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
453 DP, DQ, QP ) ) != 0 )
454 {
Chris Jonesb7d02e02021-04-01 17:40:03 +0100455 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret ) );
Hanno Beckerdc95c892017-08-23 06:57:02 +0100456 }
457#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100458
459 return( 0 );
460}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100461
Paul Bakker5121ce52009-01-03 21:22:43 +0000462/*
463 * Initialize an RSA context
464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +0000467 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +0000468{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500469 RSA_VALIDATE( ctx != NULL );
470 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
471 padding == MBEDTLS_RSA_PKCS_V21 );
472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100478 /* Set ctx->ver to nonzero to indicate that the mutex has been
479 * initialized and will need to be freed. */
480 ctx->ver = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200482#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000483}
484
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100485/*
486 * Set padding for an existing RSA context
487 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500488void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
489 int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100490{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500491 RSA_VALIDATE( ctx != NULL );
492 RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
493 padding == MBEDTLS_RSA_PKCS_V21 );
494
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100495 ctx->padding = padding;
496 ctx->hash_id = hash_id;
497}
498
Hanno Becker617c1ae2017-08-23 14:11:24 +0100499/*
500 * Get length in bytes of RSA modulus
501 */
502
503size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx )
504{
Hanno Becker2f8f06a2017-09-29 11:47:26 +0100505 return( ctx->len );
Hanno Becker617c1ae2017-08-23 14:11:24 +0100506}
507
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511/*
512 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800513 *
514 * This generation method follows the RSA key pair generation procedure of
515 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000518 int (*f_rng)(void *, unsigned char *, size_t),
519 void *p_rng,
520 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000521{
Janos Follath24eed8d2019-11-22 13:21:35 +0000522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800523 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100524 int prime_quality = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500525 RSA_VALIDATE_RET( ctx != NULL );
526 RSA_VALIDATE_RET( f_rng != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
Janos Follathb8fc1b02018-09-03 15:37:01 +0100528 /*
529 * If the modulus is 1024 bit long or shorter, then the security strength of
530 * the RSA algorithm is less than or equal to 80 bits and therefore an error
531 * rate of 2^-80 is sufficient.
532 */
533 if( nbits > 1024 )
534 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
535
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100536 mbedtls_mpi_init( &H );
537 mbedtls_mpi_init( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800538 mbedtls_mpi_init( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100540 if( nbits < 128 || exponent < 3 || nbits % 2 != 0 )
541 {
542 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
543 goto cleanup;
544 }
545
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 /*
547 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800548 * 1. |P-Q| > 2^( nbits / 2 - 100 )
549 * 2. GCD( E, (P-1)*(Q-1) ) == 1
550 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
554 do
555 {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100556 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1,
557 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
Janos Follathb8fc1b02018-09-03 15:37:01 +0100559 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1,
560 prime_quality, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800562 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
563 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &H, &ctx->P, &ctx->Q ) );
564 if( mbedtls_mpi_bitlen( &H ) <= ( ( nbits >= 200 ) ? ( ( nbits >> 1 ) - 99 ) : 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 continue;
566
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800567 /* not required by any standards, but some users rely on the fact that P > Q */
568 if( H.s < 0 )
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100569 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Janos Follathef441782016-09-21 13:18:12 +0100570
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100571 /* Temporarily replace P,Q by P-1, Q-1 */
572 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) );
573 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) );
574 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800575
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800576 /* 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 +0200577 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800578 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
579 continue;
580
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800581 /* 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 -0800582 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->P, &ctx->Q ) );
583 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L, NULL, &H, &G ) );
584 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &L ) );
585
586 if( mbedtls_mpi_bitlen( &ctx->D ) <= ( ( nbits + 1 ) / 2 ) ) // (FIPS 186-4 §B.3.1 criterion 3(a))
587 continue;
588
589 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800591 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100593 /* Restore P,Q */
594 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) );
595 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) );
596
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800597 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
598
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100599 ctx->len = mbedtls_mpi_size( &ctx->N );
600
Jethro Beekman97f95c92018-02-13 15:50:36 -0800601#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000602 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 * DP = D mod (P - 1)
604 * DQ = D mod (Q - 1)
605 * QP = Q^-1 mod P
606 */
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100607 MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
608 &ctx->DP, &ctx->DQ, &ctx->QP ) );
609#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000610
Hanno Becker83aad1f2017-08-23 06:45:10 +0100611 /* Double-check */
612 MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614cleanup:
615
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100616 mbedtls_mpi_free( &H );
617 mbedtls_mpi_free( &G );
Jethro Beekman97f95c92018-02-13 15:50:36 -0800618 mbedtls_mpi_free( &L );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620 if( ret != 0 )
621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_rsa_free( ctx );
Chris Jones74392092021-04-01 16:00:01 +0100623
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100624 if( ( -ret & ~0x7f ) == 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100625 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret );
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100626 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 }
628
Paul Bakker48377d92013-08-30 12:06:24 +0200629 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630}
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
634/*
635 * Check a public RSA key
636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000638{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500639 RSA_VALIDATE_RET( ctx != NULL );
640
Hanno Beckerebd2c022017-10-12 10:54:53 +0100641 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000643
Hanno Becker3a760a12018-01-05 08:14:49 +0000644 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 )
Hanno Becker98838b02017-10-02 13:16:10 +0100645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100647 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
Hanno Becker705fc682017-10-10 17:57:02 +0100649 if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 ||
650 mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Hanno Becker98838b02017-10-02 13:16:10 +0100652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Hanno Becker98838b02017-10-02 13:16:10 +0100654 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
656 return( 0 );
657}
658
659/*
Hanno Becker705fc682017-10-10 17:57:02 +0100660 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000663{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500664 RSA_VALIDATE_RET( ctx != NULL );
665
Hanno Becker705fc682017-10-10 17:57:02 +0100666 if( mbedtls_rsa_check_pubkey( ctx ) != 0 ||
Hanno Beckerebd2c022017-10-12 10:54:53 +0100667 rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 {
Hanno Becker98838b02017-10-02 13:16:10 +0100669 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 }
Paul Bakker48377d92013-08-30 12:06:24 +0200671
Hanno Becker98838b02017-10-02 13:16:10 +0100672 if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q,
Hanno Beckerb269a852017-08-25 08:03:21 +0100673 &ctx->D, &ctx->E, NULL, NULL ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000674 {
Hanno Beckerb269a852017-08-25 08:03:21 +0100675 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000677
Hanno Beckerb269a852017-08-25 08:03:21 +0100678#if !defined(MBEDTLS_RSA_NO_CRT)
679 else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D,
680 &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 )
681 {
682 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
683 }
684#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000685
686 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000687}
688
689/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100690 * Check if contexts holding a public and private key match
691 */
Hanno Becker98838b02017-10-02 13:16:10 +0100692int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
693 const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100694{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500695 RSA_VALIDATE_RET( pub != NULL );
696 RSA_VALIDATE_RET( prv != NULL );
697
Hanno Becker98838b02017-10-02 13:16:10 +0100698 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100702 }
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
705 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708 }
709
710 return( 0 );
711}
712
713/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 * Do an RSA public key operation
715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000717 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000718 unsigned char *output )
719{
Janos Follath24eed8d2019-11-22 13:21:35 +0000720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000721 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_mpi T;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500723 RSA_VALIDATE_RET( ctx != NULL );
724 RSA_VALIDATE_RET( input != NULL );
725 RSA_VALIDATE_RET( output != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000726
Hanno Beckerebd2c022017-10-12 10:54:53 +0100727 if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) )
Hanno Becker705fc682017-10-10 17:57:02 +0100728 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200732#if defined(MBEDTLS_THREADING_C)
733 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
734 return( ret );
735#endif
736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200741 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
742 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000743 }
744
745 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
747 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000748
749cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200751 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
752 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100753#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
757 if( ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +0100758 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
760 return( 0 );
761}
762
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200763/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200764 * Generate or update blinding values, see section 10 of:
765 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200766 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200767 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200768 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200769static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200770 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
771{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200772 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200773 mbedtls_mpi R;
774
775 mbedtls_mpi_init( &R );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200776
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200777 if( ctx->Vf.p != NULL )
778 {
779 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
781 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
782 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
783 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200784
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200785 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200786 }
787
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200788 /* Unblinding value: Vf = random number, invertible mod N */
789 do {
790 if( count++ > 10 )
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200791 {
792 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
793 goto cleanup;
794 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 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 +0200797
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200798 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200799 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) );
800 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) );
801 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
802
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200803 /* At this point, Vi is invertible mod N if and only if both Vf and R
804 * are invertible mod N. If one of them isn't, we don't need to know
805 * which one, we just loop and choose new values for both of them.
806 * (Each iteration succeeds with overwhelming probability.) */
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200807 ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N );
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500808 if( ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200809 goto cleanup;
810
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500811 } while( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
812
813 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
814 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) );
815 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200816
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200817 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200818 * (Vi already contains Vf^-1 at this point) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 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 +0200820
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200821
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200822cleanup:
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200823 mbedtls_mpi_free( &R );
824
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200825 return( ret );
826}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200827
Paul Bakker5121ce52009-01-03 21:22:43 +0000828/*
Janos Follathe81102e2017-03-22 13:38:28 +0000829 * Exponent blinding supposed to prevent side-channel attacks using multiple
830 * traces of measurements to recover the RSA key. The more collisions are there,
831 * the more bits of the key can be recovered. See [3].
832 *
833 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
834 * observations on avarage.
835 *
836 * For example with 28 byte blinding to achieve 2 collisions the adversary has
837 * to make 2^112 observations on avarage.
838 *
839 * (With the currently (as of 2017 April) known best algorithms breaking 2048
840 * bit RSA requires approximately as much time as trying out 2^112 random keys.
841 * Thus in this sense with 28 byte blinding the security is not reduced by
842 * side-channel attacks like the one in [3])
843 *
844 * This countermeasure does not help if the key recovery is possible with a
845 * single trace.
846 */
847#define RSA_EXPONENT_BLINDING 28
848
849/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000850 * Do an RSA private key operation
851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200853 int (*f_rng)(void *, unsigned char *, size_t),
854 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000855 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 unsigned char *output )
857{
Janos Follath24eed8d2019-11-22 13:21:35 +0000858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000859 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100860
861 /* Temporary holding the result */
862 mbedtls_mpi T;
863
864 /* Temporaries holding P-1, Q-1 and the
865 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000866 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100867
868#if !defined(MBEDTLS_RSA_NO_CRT)
869 /* Temporaries holding the results mod p resp. mod q. */
870 mbedtls_mpi TP, TQ;
871
872 /* Temporaries holding the blinded exponents for
873 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000874 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100875
876 /* Pointers to actual exponents to be used - either the unblinded
877 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000878 mbedtls_mpi *DP = &ctx->DP;
879 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100880#else
881 /* Temporary holding the blinded exponent (if used). */
882 mbedtls_mpi D_blind;
883
884 /* Pointer to actual exponent to be used - either the unblinded
885 * or the blinded one, depending on the presence of a PRNG. */
886 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100887#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100888
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100889 /* Temporaries holding the initial input and the double
890 * checked result; should be the same in the end. */
891 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500893 RSA_VALIDATE_RET( ctx != NULL );
894 RSA_VALIDATE_RET( input != NULL );
895 RSA_VALIDATE_RET( output != NULL );
896
Hanno Beckerebd2c022017-10-12 10:54:53 +0100897 if( rsa_check_context( ctx, 1 /* private key checks */,
898 f_rng != NULL /* blinding y/n */ ) != 0 )
899 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100900 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerebd2c022017-10-12 10:54:53 +0100901 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100902
Hanno Becker06811ce2017-05-03 15:10:34 +0100903#if defined(MBEDTLS_THREADING_C)
904 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
905 return( ret );
906#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000907
Hanno Becker06811ce2017-05-03 15:10:34 +0100908 /* MPI Initialization */
Hanno Becker06811ce2017-05-03 15:10:34 +0100909 mbedtls_mpi_init( &T );
910
911 mbedtls_mpi_init( &P1 );
912 mbedtls_mpi_init( &Q1 );
913 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000914
Janos Follathf9203b42017-03-22 15:13:15 +0000915 if( f_rng != NULL )
916 {
Janos Follathe81102e2017-03-22 13:38:28 +0000917#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000918 mbedtls_mpi_init( &D_blind );
919#else
920 mbedtls_mpi_init( &DP_blind );
921 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000922#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000923 }
Janos Follathe81102e2017-03-22 13:38:28 +0000924
Hanno Becker06811ce2017-05-03 15:10:34 +0100925#if !defined(MBEDTLS_RSA_NO_CRT)
926 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200927#endif
928
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100929 mbedtls_mpi_init( &I );
930 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100931
932 /* End of MPI initialization */
933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
935 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000936 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200937 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
938 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 }
940
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100941 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100942
Paul Bakkerf451bac2013-08-30 15:37:02 +0200943 if( f_rng != NULL )
944 {
945 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200946 * Blinding
947 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200948 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200949 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
950 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000952
Janos Follathe81102e2017-03-22 13:38:28 +0000953 /*
954 * Exponent blinding
955 */
956 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
957 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
958
Janos Follathf9203b42017-03-22 15:13:15 +0000959#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000960 /*
961 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
962 */
963 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
964 f_rng, p_rng ) );
965 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
966 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
967 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
968
969 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000970#else
971 /*
972 * DP_blind = ( P - 1 ) * R + DP
973 */
974 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
975 f_rng, p_rng ) );
976 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
977 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
978 &ctx->DP ) );
979
980 DP = &DP_blind;
981
982 /*
983 * DQ_blind = ( Q - 1 ) * R + DQ
984 */
985 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
986 f_rng, p_rng ) );
987 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
988 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
989 &ctx->DQ ) );
990
991 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000992#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200993 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000996 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100997#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200998 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000999 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001001 * TP = input ^ dP mod P
1002 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001004
1005 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
1006 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
1008 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001009 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001011 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
1012 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
1013 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
1015 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001016 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001017 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001018 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
1019 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001021
Paul Bakkerf451bac2013-08-30 15:37:02 +02001022 if( f_rng != NULL )
1023 {
1024 /*
1025 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001026 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +02001027 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001028 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +02001030 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
Hanno Becker2dec5e82017-10-03 07:49:52 +01001032 /* Verify the result to prevent glitching attacks. */
1033 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
1034 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001035 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +01001036 {
Hanno Becker06811ce2017-05-03 15:10:34 +01001037 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1038 goto cleanup;
1039 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001040
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
1044cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001046 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
1047 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001048#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001049
Hanno Becker06811ce2017-05-03 15:10:34 +01001050 mbedtls_mpi_free( &P1 );
1051 mbedtls_mpi_free( &Q1 );
1052 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +00001053
1054 if( f_rng != NULL )
1055 {
Janos Follathe81102e2017-03-22 13:38:28 +00001056#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +00001057 mbedtls_mpi_free( &D_blind );
1058#else
1059 mbedtls_mpi_free( &DP_blind );
1060 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +00001061#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001062 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001063
Hanno Becker06811ce2017-05-03 15:10:34 +01001064 mbedtls_mpi_free( &T );
1065
1066#if !defined(MBEDTLS_RSA_NO_CRT)
1067 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
1068#endif
1069
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001070 mbedtls_mpi_free( &C );
1071 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +01001072
Gilles Peskineae3741e2020-11-25 00:10:31 +01001073 if( ret != 0 && ret >= -0x007f )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001074 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
Gilles Peskineae3741e2020-11-25 00:10:31 +01001076 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001077}
1078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001080/**
1081 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1082 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001083 * \param dst buffer to mask
1084 * \param dlen length of destination buffer
1085 * \param src source of the mask generation
1086 * \param slen length of the source buffer
1087 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001088 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001089static int mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001093 unsigned char counter[4];
1094 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001095 unsigned int hlen;
1096 size_t i, use_len;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001097 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001100 memset( counter, 0, 4 );
1101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001103
Simon Butcher02037452016-03-01 21:19:12 +00001104 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001105 p = dst;
1106
1107 while( dlen > 0 )
1108 {
1109 use_len = hlen;
1110 if( dlen < hlen )
1111 use_len = dlen;
1112
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001113 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
1114 goto exit;
1115 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
1116 goto exit;
1117 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
1118 goto exit;
1119 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
1120 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001121
1122 for( i = 0; i < use_len; ++i )
1123 *p++ ^= mask[i];
1124
1125 counter[3]++;
1126
1127 dlen -= use_len;
1128 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001129
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001130exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001131 mbedtls_platform_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001132
1133 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001138/*
1139 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001142 int (*f_rng)(void *, unsigned char *, size_t),
1143 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +01001144 int mode,
1145 const unsigned char *label, size_t label_len,
1146 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001147 const unsigned char *input,
1148 unsigned char *output )
1149{
1150 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001151 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001152 unsigned char *p = output;
1153 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 const mbedtls_md_info_t *md_info;
1155 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001156
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001157 RSA_VALIDATE_RET( ctx != NULL );
1158 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1159 mode == MBEDTLS_RSA_PUBLIC );
1160 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001161 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001162 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1165 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001166
1167 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001171 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001173
1174 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001176
Simon Butcher02037452016-03-01 21:19:12 +00001177 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001178 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001180
1181 memset( output, 0, olen );
1182
1183 *p++ = 0;
1184
Simon Butcher02037452016-03-01 21:19:12 +00001185 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001186 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001187 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001188
1189 p += hlen;
1190
Simon Butcher02037452016-03-01 21:19:12 +00001191 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001192 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
1193 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001194 p += hlen;
1195 p += olen - 2 * hlen - 2 - ilen;
1196 *p++ = 1;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001197 if( ilen != 0 )
1198 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001201 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001202 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001203
Simon Butcher02037452016-03-01 21:19:12 +00001204 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001205 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1206 &md_ctx ) ) != 0 )
1207 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001208
Simon Butcher02037452016-03-01 21:19:12 +00001209 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001210 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1211 &md_ctx ) ) != 0 )
1212 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001213
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001214exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001216
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001217 if( ret != 0 )
1218 return( ret );
1219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 return( ( mode == MBEDTLS_RSA_PUBLIC )
1221 ? mbedtls_rsa_public( ctx, output, output )
1222 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001223}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001227/*
1228 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001231 int (*f_rng)(void *, unsigned char *, size_t),
1232 void *p_rng,
1233 int mode, size_t ilen,
1234 const unsigned char *input,
1235 unsigned char *output )
1236{
1237 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001239 unsigned char *p = output;
1240
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001241 RSA_VALIDATE_RET( ctx != NULL );
1242 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1243 mode == MBEDTLS_RSA_PUBLIC );
1244 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001245 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001246
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001247 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001249
1250 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001251
Simon Butcher02037452016-03-01 21:19:12 +00001252 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +00001253 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001255
1256 nb_pad = olen - 3 - ilen;
1257
1258 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +01001260 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001261 if( f_rng == NULL )
1262 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +01001265
1266 while( nb_pad-- > 0 )
1267 {
1268 int rng_dl = 100;
1269
1270 do {
1271 ret = f_rng( p_rng, p, 1 );
1272 } while( *p == 0 && --rng_dl && ret == 0 );
1273
Simon Butcher02037452016-03-01 21:19:12 +00001274 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +02001275 if( rng_dl == 0 || ret != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001276 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001277
1278 p++;
1279 }
1280 }
1281 else
1282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001284
1285 while( nb_pad-- > 0 )
1286 *p++ = 0xFF;
1287 }
1288
1289 *p++ = 0;
Gilles Peskine004f87b2018-07-06 15:47:54 +02001290 if( ilen != 0 )
1291 memcpy( p, input, ilen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 return( ( mode == MBEDTLS_RSA_PUBLIC )
1294 ? mbedtls_rsa_public( ctx, output, output )
1295 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001296}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001298
Paul Bakker5121ce52009-01-03 21:22:43 +00001299/*
1300 * Add the message padding, then do an RSA operation
1301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001303 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +00001304 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +00001305 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001306 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 unsigned char *output )
1308{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001309 RSA_VALIDATE_RET( ctx != NULL );
1310 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1311 mode == MBEDTLS_RSA_PUBLIC );
1312 RSA_VALIDATE_RET( output != NULL );
Jaeden Amerofb236732019-02-08 13:11:59 +00001313 RSA_VALIDATE_RET( ilen == 0 || input != NULL );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001314
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 switch( ctx->padding )
1316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#if defined(MBEDTLS_PKCS1_V15)
1318 case MBEDTLS_RSA_PKCS_V15:
1319 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001320 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +02001321#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_PKCS1_V21)
1324 case MBEDTLS_RSA_PKCS_V21:
1325 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +01001326 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001327#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
1329 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001332}
1333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001335/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001336 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001339 int (*f_rng)(void *, unsigned char *, size_t),
1340 void *p_rng,
1341 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +01001342 const unsigned char *label, size_t label_len,
1343 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001344 const unsigned char *input,
1345 unsigned char *output,
1346 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00001347{
Janos Follath24eed8d2019-11-22 13:21:35 +00001348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001349 size_t ilen, i, pad_len;
1350 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1352 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001353 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 const mbedtls_md_info_t *md_info;
1355 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001356
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001357 RSA_VALIDATE_RET( ctx != NULL );
1358 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1359 mode == MBEDTLS_RSA_PUBLIC );
1360 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1361 RSA_VALIDATE_RET( label_len == 0 || label != NULL );
1362 RSA_VALIDATE_RET( input != NULL );
1363 RSA_VALIDATE_RET( olen != NULL );
1364
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001365 /*
1366 * Parameters sanity checks
1367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1369 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
1371 ilen = ctx->len;
1372
Paul Bakker27fdf462011-06-09 13:55:13 +00001373 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001377 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001379
Janos Follathc17cda12016-02-11 11:08:18 +00001380 hlen = mbedtls_md_get_size( md_info );
1381
1382 // checking for integer underflow
1383 if( 2 * hlen + 2 > ilen )
1384 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1385
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001386 /*
1387 * RSA operation
1388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1390 ? mbedtls_rsa_public( ctx, input, buf )
1391 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
1393 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001394 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001395
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001396 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001397 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001400 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1401 {
1402 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001403 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001404 }
1405
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001406 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001407 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1408 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001409 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001410 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1411 &md_ctx ) ) != 0 )
1412 {
1413 mbedtls_md_free( &md_ctx );
1414 goto cleanup;
1415 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001418
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001419 /* Generate lHash */
1420 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
1421 goto cleanup;
1422
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001423 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001424 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001425 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001427 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001429 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001430
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001431 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001432
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001433 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001434 for( i = 0; i < hlen; i++ )
1435 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001436
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001437 /* Get zero-padding len, but always read till end of buffer
1438 * (minus one, for the 01 byte) */
1439 pad_len = 0;
1440 pad_done = 0;
1441 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
1442 {
1443 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +01001444 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001445 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001446
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001447 p += pad_len;
1448 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001449
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001450 /*
1451 * The only information "leaked" is whether the padding was correct or not
1452 * (eg, no data is copied if it was not correct). This meets the
1453 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1454 * the different error conditions.
1455 */
1456 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001457 {
1458 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1459 goto cleanup;
1460 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001461
Paul Bakker66d5d072014-06-17 16:39:18 +02001462 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001463 {
1464 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1465 goto cleanup;
1466 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001467
1468 *olen = ilen - (p - buf);
Gilles Peskine004f87b2018-07-06 15:47:54 +02001469 if( *olen != 0 )
1470 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001471 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001472
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001473cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001474 mbedtls_platform_zeroize( buf, sizeof( buf ) );
1475 mbedtls_platform_zeroize( lhash, sizeof( lhash ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001476
1477 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001478}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#if defined(MBEDTLS_PKCS1_V15)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001482/** Choose between two integer values, without branches.
1483 *
1484 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
1485 * to code using bitwise operation rather than a branch.
1486 *
1487 * \param cond Condition to test.
1488 * \param if1 Value to use if \p cond is nonzero.
1489 * \param if0 Value to use if \p cond is zero.
1490 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
1491 */
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001492static unsigned mbedtls_cf_uint_if( unsigned cond, unsigned if1, unsigned if0 )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001493{
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001494 unsigned mask = mbedtls_cf_uint_mask( cond );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001495 return( ( mask & if1 ) | (~mask & if0 ) );
1496}
1497
1498/** Shift some data towards the left inside a buffer without leaking
1499 * the length of the data through side channels.
1500 *
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001501 * `mbedtls_cf_mem_move_to_left(start, total, offset)` is functionally
1502 * equivalent to
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001503 * ```
1504 * memmove(start, start + offset, total - offset);
1505 * memset(start + offset, 0, total - offset);
1506 * ```
1507 * but it strives to use a memory access pattern (and thus total timing)
1508 * that does not depend on \p offset. This timing independence comes at
1509 * the expense of performance.
1510 *
1511 * \param start Pointer to the start of the buffer.
1512 * \param total Total size of the buffer.
1513 * \param offset Offset from which to copy \p total - \p offset bytes.
1514 */
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001515static void mbedtls_cf_mem_move_to_left( void *start,
1516 size_t total,
1517 size_t offset )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001518{
1519 volatile unsigned char *buf = start;
1520 size_t i, n;
1521 if( total == 0 )
1522 return;
1523 for( i = 0; i < total; i++ )
1524 {
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001525 unsigned no_op = mbedtls_cf_size_gt( total - offset, i );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001526 /* The first `total - offset` passes are a no-op. The last
1527 * `offset` passes shift the data one byte to the left and
1528 * zero out the last byte. */
1529 for( n = 0; n < total - 1; n++ )
1530 {
1531 unsigned char current = buf[n];
1532 unsigned char next = buf[n+1];
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001533 buf[n] = mbedtls_cf_uint_if( no_op, current, next );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001534 }
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001535 buf[total-1] = mbedtls_cf_uint_if( no_op, buf[total-1], 0 );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001536 }
1537}
1538
Paul Bakkerb3869132013-02-28 17:21:01 +01001539/*
1540 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001543 int (*f_rng)(void *, unsigned char *, size_t),
1544 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001545 int mode, size_t *olen,
1546 const unsigned char *input,
1547 unsigned char *output,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001548 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001549{
Janos Follath24eed8d2019-11-22 13:21:35 +00001550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001551 size_t ilen, i, plaintext_max_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001553 /* The following variables take sensitive values: their value must
1554 * not leak into the observable behavior of the function other than
1555 * the designated outputs (output, olen, return value). Otherwise
1556 * this would open the execution of the function to
1557 * side-channel-based variants of the Bleichenbacher padding oracle
1558 * attack. Potential side channels include overall timing, memory
1559 * access patterns (especially visible to an adversary who has access
1560 * to a shared memory cache), and branches (especially visible to
1561 * an adversary who has access to a shared code cache or to a shared
1562 * branch predictor). */
1563 size_t pad_count = 0;
1564 unsigned bad = 0;
1565 unsigned char pad_done = 0;
1566 size_t plaintext_size = 0;
1567 unsigned output_too_large;
1568
1569 RSA_VALIDATE_RET( ctx != NULL );
1570 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1571 mode == MBEDTLS_RSA_PUBLIC );
1572 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1573 RSA_VALIDATE_RET( input != NULL );
1574 RSA_VALIDATE_RET( olen != NULL );
1575
1576 ilen = ctx->len;
1577 plaintext_max_size = ( output_max_len > ilen - 11 ?
1578 ilen - 11 :
1579 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1582 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001583
Paul Bakkerb3869132013-02-28 17:21:01 +01001584 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1588 ? mbedtls_rsa_public( ctx, input, buf )
1589 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001590
1591 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001592 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001593
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001594 /* Check and get padding length in constant time and constant
1595 * memory trace. The first byte must be 0. */
1596 bad |= buf[0];
Paul Bakkerb3869132013-02-28 17:21:01 +01001597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001599 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001600 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
1601 * where PS must be at least 8 nonzero bytes. */
1602 bad |= buf[1] ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001604 /* Read the whole buffer. Set pad_done to nonzero if we find
1605 * the 0x00 byte and remember the padding length in pad_count. */
1606 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001607 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001608 pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01001609 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001610 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001611 }
1612 else
1613 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001614 /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00
1615 * where PS must be at least 8 bytes with the value 0xFF. */
1616 bad |= buf[1] ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001617
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001618 /* Read the whole buffer. Set pad_done to nonzero if we find
1619 * the 0x00 byte and remember the padding length in pad_count.
1620 * If there's a non-0xff byte in the padding, the padding is bad. */
1621 for( i = 2; i < ilen; i++ )
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001622 {
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001623 pad_done |= mbedtls_cf_uint_if( buf[i], 0, 1 );
1624 pad_count += mbedtls_cf_uint_if( pad_done, 0, 1 );
1625 bad |= mbedtls_cf_uint_if( pad_done, 0, buf[i] ^ 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001626 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001627 }
1628
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001629 /* If pad_done is still zero, there's no data, only unfinished padding. */
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001630 bad |= mbedtls_cf_uint_if( pad_done, 0, 1 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001631
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001632 /* There must be at least 8 bytes of padding. */
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001633 bad |= mbedtls_cf_size_gt( 8, pad_count );
Paul Bakker8804f692013-02-28 18:06:26 +01001634
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001635 /* If the padding is valid, set plaintext_size to the number of
1636 * remaining bytes after stripping the padding. If the padding
1637 * is invalid, avoid leaking this fact through the size of the
1638 * output: use the maximum message size that fits in the output
1639 * buffer. Do it without branches to avoid leaking the padding
1640 * validity through timing. RSA keys are small enough that all the
1641 * size_t values involved fit in unsigned int. */
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001642 plaintext_size = mbedtls_cf_uint_if(
1643 bad, (unsigned) plaintext_max_size,
1644 (unsigned) ( ilen - pad_count - 3 ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001645
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001646 /* Set output_too_large to 0 if the plaintext fits in the output
1647 * buffer and to 1 otherwise. */
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001648 output_too_large = mbedtls_cf_size_gt( plaintext_size,
1649 plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001650
1651 /* Set ret without branches to avoid timing attacks. Return:
1652 * - INVALID_PADDING if the padding is bad (bad != 0).
1653 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1654 * plaintext does not fit in the output buffer.
1655 * - 0 if the padding is correct. */
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001656 ret = - (int) mbedtls_cf_uint_if(
1657 bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1658 mbedtls_cf_uint_if( output_too_large,
1659 - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1660 0 ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001661
1662 /* If the padding is bad or the plaintext is too large, zero the
1663 * data that we're about to copy to the output buffer.
1664 * We need to copy the same amount of data
1665 * from the same buffer whether the padding is good or not to
1666 * avoid leaking the padding validity through overall timing or
1667 * through memory or cache access patterns. */
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001668 bad = mbedtls_cf_uint_mask( bad | output_too_large );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001669 for( i = 11; i < ilen; i++ )
1670 buf[i] &= ~bad;
1671
1672 /* If the plaintext is too large, truncate it to the buffer size.
1673 * Copy anyway to avoid revealing the length through timing, because
1674 * revealing the length is as bad as revealing the padding validity
1675 * for a Bleichenbacher attack. */
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001676 plaintext_size = mbedtls_cf_uint_if( output_too_large,
1677 (unsigned) plaintext_max_size,
1678 (unsigned) plaintext_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001679
1680 /* Move the plaintext to the leftmost position where it can start in
1681 * the working buffer, i.e. make it start plaintext_max_size from
1682 * the end of the buffer. Do this with a memory access trace that
1683 * does not depend on the plaintext size. After this move, the
1684 * starting location of the plaintext is no longer sensitive
1685 * information. */
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02001686 mbedtls_cf_mem_move_to_left( buf + ilen - plaintext_max_size,
1687 plaintext_max_size,
1688 plaintext_max_size - plaintext_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001689
Jaeden Amero6f7703d2019-02-06 10:44:56 +00001690 /* Finally copy the decrypted plaintext plus trailing zeros into the output
1691 * buffer. If output_max_len is 0, then output may be an invalid pointer
1692 * and the result of memcpy() would be undefined; prevent undefined
1693 * behavior making sure to depend only on output_max_len (the size of the
1694 * user-provided output buffer), which is independent from plaintext
1695 * length, validity of padding, success of the decryption, and other
1696 * secrets. */
1697 if( output_max_len != 0 )
1698 memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001699
1700 /* Report the amount of data we copied to the output buffer. In case
1701 * of errors (bad padding or output too large), the value of *olen
1702 * when this function returns is not specified. Making it equivalent
1703 * to the good case limits the risks of leaking the padding validity. */
1704 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001705
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001706cleanup:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001707 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001708
1709 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001710}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001712
1713/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001714 * Do an RSA operation, then remove the message padding
1715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001717 int (*f_rng)(void *, unsigned char *, size_t),
1718 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001719 int mode, size_t *olen,
1720 const unsigned char *input,
1721 unsigned char *output,
1722 size_t output_max_len)
1723{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001724 RSA_VALIDATE_RET( ctx != NULL );
1725 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
1726 mode == MBEDTLS_RSA_PUBLIC );
1727 RSA_VALIDATE_RET( output_max_len == 0 || output != NULL );
1728 RSA_VALIDATE_RET( input != NULL );
1729 RSA_VALIDATE_RET( olen != NULL );
1730
Paul Bakkerb3869132013-02-28 17:21:01 +01001731 switch( ctx->padding )
1732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#if defined(MBEDTLS_PKCS1_V15)
1734 case MBEDTLS_RSA_PKCS_V15:
1735 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001736 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001737#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#if defined(MBEDTLS_PKCS1_V21)
1740 case MBEDTLS_RSA_PKCS_V21:
1741 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001742 olen, input, output,
1743 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001744#endif
1745
1746 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001748 }
1749}
1750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751#if defined(MBEDTLS_PKCS1_V21)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001752static int rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001753 int (*f_rng)(void *, unsigned char *, size_t),
1754 void *p_rng,
1755 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001757 unsigned int hashlen,
1758 const unsigned char *hash,
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001759 int saltlen,
Paul Bakkerb3869132013-02-28 17:21:01 +01001760 unsigned char *sig )
1761{
1762 size_t olen;
1763 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001764 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001765 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001767 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 const mbedtls_md_info_t *md_info;
1769 mbedtls_md_context_t md_ctx;
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( ( md_alg == MBEDTLS_MD_NONE &&
1774 hashlen == 0 ) ||
1775 hash != NULL );
1776 RSA_VALIDATE_RET( sig != NULL );
Paul Bakkerb3869132013-02-28 17:21:01 +01001777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1779 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001780
1781 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001783
1784 olen = ctx->len;
1785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001787 {
Simon Butcher02037452016-03-01 21:19:12 +00001788 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001790 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001794 }
1795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001797 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001801
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001802 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY)
1803 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001804 /* Calculate the largest possible salt length, up to the hash size.
1805 * Normally this is the hash length, which is the maximum salt length
1806 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001807 * enough room, use the maximum salt length that fits. The constraint is
1808 * that the hash length plus the salt length plus 2 bytes must be at most
1809 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1810 * (PKCS#1 v2.2) §9.1.1 step 3. */
1811 min_slen = hlen - 2;
1812 if( olen < hlen + min_slen + 2 )
1813 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1814 else if( olen >= hlen + hlen + 2 )
1815 slen = hlen;
1816 else
1817 slen = olen - hlen - 2;
1818 }
Cédric Meuter46bad332021-01-10 12:57:19 +01001819 else if ( (saltlen < 0) || (saltlen + hlen + 2 > olen) )
Cédric Meuter010ddc22020-04-25 09:24:11 +02001820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Cédric Meuter010ddc22020-04-25 09:24:11 +02001822 }
Jaeden Amero3725bb22018-09-07 19:12:36 +01001823 else
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001824 {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001825 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001826 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001827
1828 memset( sig, 0, olen );
1829
Simon Butcher02037452016-03-01 21:19:12 +00001830 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001831 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001832 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001833 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001834
1835 /* Generate salt of length slen in place in the encoded message */
1836 salt = p;
1837 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Chris Jonesb7d02e02021-04-01 17:40:03 +01001838 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_RSA_RNG_FAILED, ret ) );
Cédric Meuter668a78d2020-04-30 11:57:04 +02001839
Paul Bakkerb3869132013-02-28 17:21:01 +01001840 p += slen;
1841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001843 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001844 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001845
Simon Butcher02037452016-03-01 21:19:12 +00001846 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001847 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1848 goto exit;
1849 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1850 goto exit;
1851 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1852 goto exit;
1853 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1854 goto exit;
1855 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1856 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001857
Simon Butcher02037452016-03-01 21:19:12 +00001858 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001859 if( msb % 8 == 0 )
1860 offset = 1;
1861
Simon Butcher02037452016-03-01 21:19:12 +00001862 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001863 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1864 &md_ctx ) ) != 0 )
1865 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001866
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001867 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001868 sig[0] &= 0xFF >> ( olen * 8 - msb );
1869
1870 p += hlen;
1871 *p++ = 0xBC;
1872
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001873exit:
1874 mbedtls_md_free( &md_ctx );
1875
1876 if( ret != 0 )
1877 return( ret );
1878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 return( ( mode == MBEDTLS_RSA_PUBLIC )
1880 ? mbedtls_rsa_public( ctx, sig, sig )
1881 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001882}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001883
1884/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001885 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1886 * the option to pass in the salt length.
1887 */
1888int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx,
1889 int (*f_rng)(void *, unsigned char *, size_t),
1890 void *p_rng,
1891 mbedtls_md_type_t md_alg,
1892 unsigned int hashlen,
1893 const unsigned char *hash,
1894 int saltlen,
1895 unsigned char *sig )
1896{
1897 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, md_alg,
1898 hashlen, hash, saltlen, sig );
1899}
1900
1901
1902/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001903 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1904 */
1905int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
1906 int (*f_rng)(void *, unsigned char *, size_t),
1907 void *p_rng,
1908 int mode,
1909 mbedtls_md_type_t md_alg,
1910 unsigned int hashlen,
1911 const unsigned char *hash,
1912 unsigned char *sig )
1913{
Cédric Meuterf3fab332020-04-25 11:30:45 +02001914 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
1915 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig );
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001920/*
1921 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1922 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001923
1924/* Construct a PKCS v1.5 encoding of a hashed message
1925 *
1926 * This is used both for signature generation and verification.
1927 *
1928 * Parameters:
1929 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001930 * MBEDTLS_MD_NONE if raw data is signed.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001931 * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001932 * - hash: Buffer containing the hashed message or the raw data.
1933 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001934 * - dst: Buffer to hold the encoded message.
1935 *
1936 * Assumptions:
1937 * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE.
1938 * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001939 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001940 *
1941 */
1942static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg,
1943 unsigned int hashlen,
1944 const unsigned char *hash,
Hanno Beckere58d38c2017-09-27 17:09:00 +01001945 size_t dst_len,
Hanno Beckerfdf38032017-09-06 12:35:55 +01001946 unsigned char *dst )
1947{
1948 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001949 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001950 unsigned char *p = dst;
1951 const char *oid = NULL;
1952
1953 /* Are we signing hashed or raw data? */
1954 if( md_alg != MBEDTLS_MD_NONE )
1955 {
1956 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
1957 if( md_info == NULL )
1958 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1959
1960 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1961 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1962
1963 hashlen = mbedtls_md_get_size( md_info );
1964
1965 /* Double-check that 8 + hashlen + oid_size can be used as a
1966 * 1-byte ASN.1 length encoding and that there's no overflow. */
1967 if( 8 + hashlen + oid_size >= 0x80 ||
1968 10 + hashlen < hashlen ||
1969 10 + hashlen + oid_size < 10 + hashlen )
1970 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1971
1972 /*
1973 * Static bounds check:
1974 * - Need 10 bytes for five tag-length pairs.
1975 * (Insist on 1-byte length encodings to protect against variants of
1976 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1977 * - Need hashlen bytes for hash
1978 * - Need oid_size bytes for hash alg OID.
1979 */
1980 if( nb_pad < 10 + hashlen + oid_size )
1981 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1982 nb_pad -= 10 + hashlen + oid_size;
1983 }
1984 else
1985 {
1986 if( nb_pad < hashlen )
1987 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1988
1989 nb_pad -= hashlen;
1990 }
1991
Hanno Becker2b2f8982017-09-27 17:10:03 +01001992 /* Need space for signature header and padding delimiter (3 bytes),
1993 * and 8 bytes for the minimal padding */
1994 if( nb_pad < 3 + 8 )
Hanno Beckerfdf38032017-09-06 12:35:55 +01001995 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1996 nb_pad -= 3;
1997
1998 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001999 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002000
2001 /* Write signature header and padding */
2002 *p++ = 0;
2003 *p++ = MBEDTLS_RSA_SIGN;
2004 memset( p, 0xFF, nb_pad );
2005 p += nb_pad;
2006 *p++ = 0;
2007
2008 /* Are we signing raw data? */
2009 if( md_alg == MBEDTLS_MD_NONE )
2010 {
2011 memcpy( p, hash, hashlen );
2012 return( 0 );
2013 }
2014
2015 /* Signing hashed data, add corresponding ASN.1 structure
2016 *
2017 * DigestInfo ::= SEQUENCE {
2018 * digestAlgorithm DigestAlgorithmIdentifier,
2019 * digest Digest }
2020 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2021 * Digest ::= OCTET STRING
2022 *
2023 * Schematic:
2024 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2025 * TAG-NULL + LEN [ NULL ] ]
2026 * TAG-OCTET + LEN [ HASH ] ]
2027 */
2028 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002029 *p++ = (unsigned char)( 0x08 + oid_size + hashlen );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002030 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Hanno Becker87ae1972018-01-15 15:27:56 +00002031 *p++ = (unsigned char)( 0x04 + oid_size );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002032 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002033 *p++ = (unsigned char) oid_size;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002034 memcpy( p, oid, oid_size );
2035 p += oid_size;
2036 *p++ = MBEDTLS_ASN1_NULL;
2037 *p++ = 0x00;
2038 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002039 *p++ = (unsigned char) hashlen;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002040 memcpy( p, hash, hashlen );
2041 p += hashlen;
2042
2043 /* Just a sanity-check, should be automatic
2044 * after the initial bounds check. */
Hanno Beckere58d38c2017-09-27 17:09:00 +01002045 if( p != dst + dst_len )
Hanno Beckerfdf38032017-09-06 12:35:55 +01002046 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002047 mbedtls_platform_zeroize( dst, dst_len );
Hanno Beckerfdf38032017-09-06 12:35:55 +01002048 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2049 }
2050
2051 return( 0 );
2052}
2053
Paul Bakkerb3869132013-02-28 17:21:01 +01002054/*
2055 * Do an RSA operation to sign the message digest
2056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002058 int (*f_rng)(void *, unsigned char *, size_t),
2059 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002060 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002062 unsigned int hashlen,
2063 const unsigned char *hash,
2064 unsigned char *sig )
2065{
Janos Follath24eed8d2019-11-22 13:21:35 +00002066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002067 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002068
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002069 RSA_VALIDATE_RET( ctx != NULL );
2070 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2071 mode == MBEDTLS_RSA_PUBLIC );
2072 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2073 hashlen == 0 ) ||
2074 hash != NULL );
2075 RSA_VALIDATE_RET( sig != NULL );
2076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2078 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002079
Hanno Beckerfdf38032017-09-06 12:35:55 +01002080 /*
2081 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2082 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002083
Hanno Beckerfdf38032017-09-06 12:35:55 +01002084 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash,
2085 ctx->len, sig ) ) != 0 )
2086 return( ret );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002087
2088 /*
Hanno Beckerfdf38032017-09-06 12:35:55 +01002089 * Call respective RSA primitive
2090 */
2091
2092 if( mode == MBEDTLS_RSA_PUBLIC )
2093 {
2094 /* Skip verification on a public key operation */
2095 return( mbedtls_rsa_public( ctx, sig, sig ) );
2096 }
2097
2098 /* Private key operation
2099 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002100 * In order to prevent Lenstra's attack, make the signature in a
2101 * temporary buffer and check it before returning it.
2102 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002103
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002104 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002105 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002106 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2107
Hanno Beckerfdf38032017-09-06 12:35:55 +01002108 verif = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00002109 if( verif == NULL )
2110 {
2111 mbedtls_free( sig_try );
2112 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
2113 }
2114
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002115 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
2116 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
2117
Hanno Becker171a8f12017-09-06 12:32:16 +01002118 if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002119 {
2120 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2121 goto cleanup;
2122 }
2123
2124 memcpy( sig, sig_try, ctx->len );
2125
2126cleanup:
2127 mbedtls_free( sig_try );
2128 mbedtls_free( verif );
2129
2130 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002133
2134/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 * Do an RSA operation to sign the message digest
2136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002138 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00002139 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00002142 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00002143 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 unsigned char *sig )
2145{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002146 RSA_VALIDATE_RET( ctx != NULL );
2147 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2148 mode == MBEDTLS_RSA_PUBLIC );
2149 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2150 hashlen == 0 ) ||
2151 hash != NULL );
2152 RSA_VALIDATE_RET( sig != NULL );
2153
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 switch( ctx->padding )
2155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156#if defined(MBEDTLS_PKCS1_V15)
2157 case MBEDTLS_RSA_PKCS_V15:
2158 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002159 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002160#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#if defined(MBEDTLS_PKCS1_V21)
2163 case MBEDTLS_RSA_PKCS_V21:
2164 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002165 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002166#endif
2167
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002171}
2172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002174/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002175 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002178 int (*f_rng)(void *, unsigned char *, size_t),
2179 void *p_rng,
2180 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002182 unsigned int hashlen,
2183 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002185 int expected_salt_len,
2186 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00002187{
Janos Follath24eed8d2019-11-22 13:21:35 +00002188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002189 size_t siglen;
2190 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002191 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00002193 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00002194 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002195 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 const mbedtls_md_info_t *md_info;
2197 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01002198 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01002199
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002200 RSA_VALIDATE_RET( ctx != NULL );
2201 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2202 mode == MBEDTLS_RSA_PUBLIC );
2203 RSA_VALIDATE_RET( sig != NULL );
2204 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2205 hashlen == 0 ) ||
2206 hash != NULL );
2207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
2209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002210
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 siglen = ctx->len;
2212
Paul Bakker27fdf462011-06-09 13:55:13 +00002213 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 ret = ( mode == MBEDTLS_RSA_PUBLIC )
2217 ? mbedtls_rsa_public( ctx, sig, buf )
2218 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002219
2220 if( ret != 0 )
2221 return( ret );
2222
2223 p = buf;
2224
Paul Bakkerb3869132013-02-28 17:21:01 +01002225 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 {
Simon Butcher02037452016-03-01 21:19:12 +00002230 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002232 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01002236 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01002239 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002243
Paul Bakkerb3869132013-02-28 17:21:01 +01002244 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00002245
Simon Butcher02037452016-03-01 21:19:12 +00002246 /*
2247 * Note: EMSA-PSS verification is over the length of N - 1 bits
2248 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002249 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002250
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002251 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
2252 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2253
Simon Butcher02037452016-03-01 21:19:12 +00002254 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01002255 if( msb % 8 == 0 )
2256 {
2257 p++;
2258 siglen -= 1;
2259 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002260
Gilles Peskine139108a2017-10-18 19:03:42 +02002261 if( siglen < hlen + 2 )
2262 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
2263 hash_start = p + siglen - hlen - 1;
2264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07002266 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002267 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002268
Jaeden Amero66954e12018-01-25 16:05:54 +00002269 ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
2270 if( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002271 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01002272
Paul Bakkerb3869132013-02-28 17:21:01 +01002273 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002274
Gilles Peskine6a54b022017-10-17 19:02:13 +02002275 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002276 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002277
Gilles Peskine91048a32017-10-19 17:46:14 +02002278 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01002279 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002280 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2281 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01002282 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002283
Gilles Peskine6a54b022017-10-17 19:02:13 +02002284 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskine6a54b022017-10-17 19:02:13 +02002287 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002288 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002289 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
2290 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002291 }
2292
Simon Butcher02037452016-03-01 21:19:12 +00002293 /*
2294 * Generate H = Hash( M' )
2295 */
Jaeden Amero66954e12018-01-25 16:05:54 +00002296 ret = mbedtls_md_starts( &md_ctx );
2297 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002298 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002299 ret = mbedtls_md_update( &md_ctx, zeros, 8 );
2300 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002301 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002302 ret = mbedtls_md_update( &md_ctx, hash, hashlen );
2303 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002304 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002305 ret = mbedtls_md_update( &md_ctx, p, observed_salt_len );
2306 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002307 goto exit;
Jaeden Amero66954e12018-01-25 16:05:54 +00002308 ret = mbedtls_md_finish( &md_ctx, result );
2309 if ( ret != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002310 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00002311
Jaeden Amero66954e12018-01-25 16:05:54 +00002312 if( memcmp( hash_start, result, hlen ) != 0 )
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002313 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002314 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01002315 goto exit;
2316 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002317
2318exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00002320
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002321 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01002322}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002323
2324/*
2325 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002328 int (*f_rng)(void *, unsigned char *, size_t),
2329 void *p_rng,
2330 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002332 unsigned int hashlen,
2333 const unsigned char *hash,
2334 const unsigned char *sig )
2335{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002336 mbedtls_md_type_t mgf1_hash_id;
2337 RSA_VALIDATE_RET( ctx != NULL );
2338 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2339 mode == MBEDTLS_RSA_PUBLIC );
2340 RSA_VALIDATE_RET( sig != NULL );
2341 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2342 hashlen == 0 ) ||
2343 hash != NULL );
2344
2345 mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002347 : md_alg;
2348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002350 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002352 sig ) );
2353
2354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002358/*
2359 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002362 int (*f_rng)(void *, unsigned char *, size_t),
2363 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002364 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002366 unsigned int hashlen,
2367 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002368 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002369{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002370 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002371 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002372 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002373
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002374 RSA_VALIDATE_RET( ctx != NULL );
2375 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2376 mode == MBEDTLS_RSA_PUBLIC );
2377 RSA_VALIDATE_RET( sig != NULL );
2378 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2379 hashlen == 0 ) ||
2380 hash != NULL );
2381
2382 sig_len = ctx->len;
2383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
2385 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01002386
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002387 /*
2388 * Prepare expected PKCS1 v1.5 encoding of hash.
2389 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002390
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002391 if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL ||
2392 ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL )
2393 {
2394 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2395 goto cleanup;
2396 }
2397
2398 if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len,
2399 encoded_expected ) ) != 0 )
2400 goto cleanup;
2401
2402 /*
2403 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2404 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002407 ? mbedtls_rsa_public( ctx, sig, encoded )
2408 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded );
Paul Bakkerb3869132013-02-28 17:21:01 +01002409 if( ret != 0 )
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002410 goto cleanup;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002411
Simon Butcher02037452016-03-01 21:19:12 +00002412 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002413 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002414 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002415
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002416 if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected,
2417 sig_len ) ) != 0 )
2418 {
2419 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2420 goto cleanup;
2421 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002422
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002423cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002424
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002425 if( encoded != NULL )
2426 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002427 mbedtls_platform_zeroize( encoded, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002428 mbedtls_free( encoded );
2429 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002430
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002431 if( encoded_expected != NULL )
2432 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002433 mbedtls_platform_zeroize( encoded_expected, sig_len );
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002434 mbedtls_free( encoded_expected );
2435 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002436
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002437 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
2441/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002442 * Do an RSA operation and check the message digest
2443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02002445 int (*f_rng)(void *, unsigned char *, size_t),
2446 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01002447 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002449 unsigned int hashlen,
2450 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02002451 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01002452{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002453 RSA_VALIDATE_RET( ctx != NULL );
2454 RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE ||
2455 mode == MBEDTLS_RSA_PUBLIC );
2456 RSA_VALIDATE_RET( sig != NULL );
2457 RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE &&
2458 hashlen == 0 ) ||
2459 hash != NULL );
2460
Paul Bakkerb3869132013-02-28 17:21:01 +01002461 switch( ctx->padding )
2462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463#if defined(MBEDTLS_PKCS1_V15)
2464 case MBEDTLS_RSA_PKCS_V15:
2465 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002466 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02002467#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469#if defined(MBEDTLS_PKCS1_V21)
2470 case MBEDTLS_RSA_PKCS_V21:
2471 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01002472 hashlen, hash, sig );
2473#endif
2474
2475 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01002477 }
2478}
2479
2480/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002481 * Copy the components of an RSA key
2482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002484{
Janos Follath24eed8d2019-11-22 13:21:35 +00002485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002486 RSA_VALIDATE_RET( dst != NULL );
2487 RSA_VALIDATE_RET( src != NULL );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002488
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002489 dst->len = src->len;
2490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
2492 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
2495 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
2496 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002497
2498#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
2500 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
2501 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
2503 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Hanno Becker33c30a02017-08-23 07:00:22 +01002504#endif
2505
2506 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
2509 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002510
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002511 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002512 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002513
2514cleanup:
2515 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002517
2518 return( ret );
2519}
2520
2521/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 * Free the components of an RSA key
2523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00002525{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002526 if( ctx == NULL )
2527 return;
2528
2529 mbedtls_mpi_free( &ctx->Vi );
2530 mbedtls_mpi_free( &ctx->Vf );
2531 mbedtls_mpi_free( &ctx->RN );
2532 mbedtls_mpi_free( &ctx->D );
2533 mbedtls_mpi_free( &ctx->Q );
2534 mbedtls_mpi_free( &ctx->P );
2535 mbedtls_mpi_free( &ctx->E );
2536 mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002537
Hanno Becker33c30a02017-08-23 07:00:22 +01002538#if !defined(MBEDTLS_RSA_NO_CRT)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002539 mbedtls_mpi_free( &ctx->RQ );
2540 mbedtls_mpi_free( &ctx->RP );
2541 mbedtls_mpi_free( &ctx->QP );
2542 mbedtls_mpi_free( &ctx->DQ );
Hanno Becker33c30a02017-08-23 07:00:22 +01002543 mbedtls_mpi_free( &ctx->DP );
2544#endif /* MBEDTLS_RSA_NO_CRT */
2545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002547 /* Free the mutex, but only if it hasn't been freed already. */
2548 if( ctx->ver != 0 )
2549 {
2550 mbedtls_mutex_free( &ctx->mutex );
2551 ctx->ver = 0;
2552 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002553#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002554}
2555
Hanno Beckerab377312017-08-23 16:24:51 +01002556#endif /* !MBEDTLS_RSA_ALT */
2557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002560#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
2562/*
2563 * Example RSA-1024 keypair, for test purposes
2564 */
2565#define KEY_LEN 128
2566
2567#define RSA_N "9292758453063D803DD603D5E777D788" \
2568 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2569 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2570 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2571 "93A89813FBF3C4F8066D2D800F7C38A8" \
2572 "1AE31942917403FF4946B0A83D3D3E05" \
2573 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2574 "5E94BB77B07507233A0BC7BAC8F90F79"
2575
2576#define RSA_E "10001"
2577
2578#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2579 "66CA472BC44D253102F8B4A9D3BFA750" \
2580 "91386C0077937FE33FA3252D28855837" \
2581 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2582 "DF79C5CE07EE72C7F123142198164234" \
2583 "CABB724CF78B8173B9F880FC86322407" \
2584 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2585 "071513A1E85B5DFA031F21ECAE91A34D"
2586
2587#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2588 "2C01CAD19EA484A87EA4377637E75500" \
2589 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2590 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2591
2592#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2593 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2594 "910E4168387E3C30AA1E00C339A79508" \
2595 "8452DD96A9A5EA5D9DCA68DA636032AF"
2596
Paul Bakker5121ce52009-01-03 21:22:43 +00002597#define PT_LEN 24
2598#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2599 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002602static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00002603{
gufe44c2620da2020-08-03 17:56:50 +02002604#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002605 size_t i;
2606
Paul Bakker545570e2010-07-18 09:00:25 +00002607 if( rng_state != NULL )
2608 rng_state = NULL;
2609
Paul Bakkera3d195c2011-11-27 21:07:34 +00002610 for( i = 0; i < len; ++i )
2611 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002612#else
2613 if( rng_state != NULL )
2614 rng_state = NULL;
2615
2616 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +02002617#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002618
Paul Bakkera3d195c2011-11-27 21:07:34 +00002619 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00002620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002622
Paul Bakker5121ce52009-01-03 21:22:43 +00002623/*
2624 * Checkup routine
2625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002627{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002628 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002630 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 unsigned char rsa_plaintext[PT_LEN];
2633 unsigned char rsa_decrypted[PT_LEN];
2634 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002636 unsigned char sha1sum[20];
2637#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Hanno Becker3a701162017-08-22 13:52:43 +01002639 mbedtls_mpi K;
2640
2641 mbedtls_mpi_init( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
Hanno Becker3a701162017-08-22 13:52:43 +01002644 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
2645 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
2646 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) );
2647 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) );
2648 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) );
2649 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) );
2650 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) );
2651 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) );
2652 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) );
2653 MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) );
2654
Hanno Becker7f25f852017-10-10 16:56:22 +01002655 MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
2657 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
2661 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 {
2663 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Hanno Becker5bc87292017-05-03 15:09:31 +01002666 ret = 1;
2667 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 }
2669
2670 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
2673 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
2674
Hanno Becker98838b02017-10-02 13:16:10 +01002675 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC,
2676 PT_LEN, rsa_plaintext,
2677 rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 {
2679 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
Hanno Becker5bc87292017-05-03 15:09:31 +01002682 ret = 1;
2683 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 }
2685
2686 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Hanno Becker98838b02017-10-02 13:16:10 +01002689 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE,
2690 &len, rsa_ciphertext, rsa_decrypted,
2691 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 {
2693 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Hanno Becker5bc87292017-05-03 15:09:31 +01002696 ret = 1;
2697 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 }
2699
2700 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
2701 {
2702 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Hanno Becker5bc87292017-05-03 15:09:31 +01002705 ret = 1;
2706 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 }
2708
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002709 if( verbose != 0 )
2710 mbedtls_printf( "passed\n" );
2711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07002714 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01002716 if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002717 {
2718 if( verbose != 0 )
2719 mbedtls_printf( "failed\n" );
2720
2721 return( 1 );
2722 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Hanno Becker98838b02017-10-02 13:16:10 +01002724 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL,
2725 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
2726 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 {
2728 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730
Hanno Becker5bc87292017-05-03 15:09:31 +01002731 ret = 1;
2732 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 }
2734
2735 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
Hanno Becker98838b02017-10-02 13:16:10 +01002738 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL,
2739 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
2740 sha1sum, rsa_ciphertext ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 {
2742 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Hanno Becker5bc87292017-05-03 15:09:31 +01002745 ret = 1;
2746 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002747 }
2748
2749 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002750 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002753 if( verbose != 0 )
2754 mbedtls_printf( "\n" );
2755
Paul Bakker3d8fb632014-04-17 12:42:41 +02002756cleanup:
Hanno Becker3a701162017-08-22 13:52:43 +01002757 mbedtls_mpi_free( &K );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 mbedtls_rsa_free( &rsa );
2759#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002760 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02002762 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002763}
2764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767#endif /* MBEDTLS_RSA_C */