blob: aa8cdf6a826e82a161b1ec9a03a5365446a928ef [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"
Chris Jones66a4cd42021-03-09 16:04:12 +000043#include "rsa_alt_helpers.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 Mezei22c9a6f2021-10-20 12:09:35 +020047#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020048#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020049#include "hash_info.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020050#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
gufe44c2620da2020-08-03 17:56:50 +020054#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010059
Hanno Beckera565f542017-10-11 11:00:19 +010060#if !defined(MBEDTLS_RSA_ALT)
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
63 const mbedtls_mpi *N,
64 const mbedtls_mpi *P, const mbedtls_mpi *Q,
65 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +010066{
Janos Follath24eed8d2019-11-22 13:21:35 +000067 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +010068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
70 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
71 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
72 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
73 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
74 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +010075 }
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if (N != NULL) {
78 ctx->len = mbedtls_mpi_size(&ctx->N);
79 }
Hanno Becker617c1ae2017-08-23 14:11:24 +010080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +010082}
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
85 unsigned char const *N, size_t N_len,
86 unsigned char const *P, size_t P_len,
87 unsigned char const *Q, size_t Q_len,
88 unsigned char const *D, size_t D_len,
89 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +010090{
Hanno Beckerd4d60572018-01-10 07:12:01 +000091 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +010092
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (N != NULL) {
94 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
95 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +010096 }
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098 if (P != NULL) {
99 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
100 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if (Q != NULL) {
103 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
104 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (D != NULL) {
107 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
108 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (E != NULL) {
111 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
112 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100113
114cleanup:
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (ret != 0) {
117 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
118 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100121}
122
Hanno Becker705fc682017-10-10 17:57:02 +0100123/*
124 * Checks whether the context fields are set in such a way
125 * that the RSA primitives will be able to execute without error.
126 * It does *not* make guarantees for consistency of the parameters.
127 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
129 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100130{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100131#if !defined(MBEDTLS_RSA_NO_CRT)
132 /* blinding_needed is only used for NO_CRT to decide whether
133 * P,Q need to be present or not. */
134 ((void) blinding_needed);
135#endif
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
138 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
139 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000140 }
Hanno Becker705fc682017-10-10 17:57:02 +0100141
142 /*
143 * 1. Modular exponentiation needs positive, odd moduli.
144 */
145
146 /* Modular exponentiation wrt. N is always used for
147 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
149 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
150 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100151 }
152
153#if !defined(MBEDTLS_RSA_NO_CRT)
154 /* Modular exponentiation for P and Q is only
155 * used for private key operations and if CRT
156 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 if (is_priv &&
158 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
159 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
160 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
161 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
162 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100163 }
164#endif /* !MBEDTLS_RSA_NO_CRT */
165
166 /*
167 * 2. Exponents must be positive
168 */
169
170 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
172 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
173 }
Hanno Becker705fc682017-10-10 17:57:02 +0100174
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100175#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100176 /* For private key operations, use D or DP & DQ
177 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
179 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
180 }
Hanno Becker705fc682017-10-10 17:57:02 +0100181#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if (is_priv &&
183 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
184 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
185 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100186 }
187#endif /* MBEDTLS_RSA_NO_CRT */
188
189 /* Blinding shouldn't make exponents negative either,
190 * so check that P, Q >= 1 if that hasn't yet been
191 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100192#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (is_priv && blinding_needed &&
194 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
195 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
196 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100197 }
198#endif
199
200 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100201 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100202#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (is_priv &&
204 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
205 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100206 }
207#endif
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100210}
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100213{
214 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500215 int have_N, have_P, have_Q, have_D, have_E;
216#if !defined(MBEDTLS_RSA_NO_CRT)
217 int have_DP, have_DQ, have_QP;
218#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500219 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
222 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
223 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
224 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
225 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500226
227#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
229 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
230 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500231#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100232
Hanno Becker617c1ae2017-08-23 14:11:24 +0100233 /*
234 * Check whether provided parameters are enough
235 * to deduce all others. The following incomplete
236 * parameter sets for private keys are supported:
237 *
238 * (1) P, Q missing.
239 * (2) D and potentially N missing.
240 *
241 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100242
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500243 n_missing = have_P && have_Q && have_D && have_E;
244 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
245 d_missing = have_P && have_Q && !have_D && have_E;
246 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100247
248 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500249 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (!is_priv && !is_pub) {
252 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
253 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100254
255 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100256 * Step 1: Deduce N if P, Q are provided.
257 */
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (!have_N && have_P && have_Q) {
260 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
261 &ctx->Q)) != 0) {
262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100263 }
264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100266 }
267
268 /*
269 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100270 */
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (pq_missing) {
273 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
274 &ctx->P, &ctx->Q);
275 if (ret != 0) {
276 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
277 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 } else if (d_missing) {
280 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
281 &ctx->Q,
282 &ctx->E,
283 &ctx->D)) != 0) {
284 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100285 }
286 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100287
Hanno Becker617c1ae2017-08-23 14:11:24 +0100288 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100289 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100290 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100291 */
292
Hanno Becker23344b52017-08-23 07:43:27 +0100293#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (is_priv && !(have_DP && have_DQ && have_QP)) {
295 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
296 &ctx->DP, &ctx->DQ, &ctx->QP);
297 if (ret != 0) {
298 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
299 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100300 }
Hanno Becker23344b52017-08-23 07:43:27 +0100301#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100302
303 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100304 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100305 */
306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100308}
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
311 unsigned char *N, size_t N_len,
312 unsigned char *P, size_t P_len,
313 unsigned char *Q, size_t Q_len,
314 unsigned char *D, size_t D_len,
315 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100316{
317 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500318 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100319
320 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500321 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
323 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
324 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
325 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
326 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100329 /* If we're trying to export private parameters for a public key,
330 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if (P != NULL || Q != NULL || D != NULL) {
332 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
333 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100334
335 }
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (N != NULL) {
338 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
339 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (P != NULL) {
342 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
343 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (Q != NULL) {
346 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
347 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (D != NULL) {
350 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
351 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (E != NULL) {
354 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
355 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100356
357cleanup:
358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100360}
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
363 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
364 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100365{
Janos Follath24eed8d2019-11-22 13:21:35 +0000366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500367 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100368
369 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500370 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
372 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
373 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
374 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
375 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100378 /* If we're trying to export private parameters for a public key,
379 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (P != NULL || Q != NULL || D != NULL) {
381 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
382 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100383
384 }
385
386 /* Export all requested core parameters. */
387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
389 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
390 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
391 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
392 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
393 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100394 }
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100397}
398
399/*
400 * Export CRT parameters
401 * This must also be implemented if CRT is not used, for being able to
402 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
403 * can be used in this case.
404 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
406 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100407{
Janos Follath24eed8d2019-11-22 13:21:35 +0000408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500409 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100410
411 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500412 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
414 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
415 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
416 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
417 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (!is_priv) {
420 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
421 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100422
Hanno Beckerdc95c892017-08-23 06:57:02 +0100423#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100424 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
426 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
427 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
428 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100429 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100430#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
432 DP, DQ, QP)) != 0) {
433 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100434 }
435#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100438}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100439
Paul Bakker5121ce52009-01-03 21:22:43 +0000440/*
441 * Initialize an RSA context
442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000444{
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
Ronald Cronc1905a12021-06-05 11:11:14 +0200447 ctx->padding = MBEDTLS_RSA_PKCS_V15;
448 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100451 /* Set ctx->ver to nonzero to indicate that the mutex has been
452 * initialized and will need to be freed. */
453 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200455#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000456}
457
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100458/*
459 * Set padding for an existing RSA context
460 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
462 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100463{
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200465#if defined(MBEDTLS_PKCS1_V15)
466 case MBEDTLS_RSA_PKCS_V15:
467 break;
468#endif
469
470#if defined(MBEDTLS_PKCS1_V21)
471 case MBEDTLS_RSA_PKCS_V21:
472 break;
473#endif
474 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200476 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200477
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200478#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
480 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200481 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200482 if (mbedtls_md_psa_alg_from_type(hash_id) == PSA_ALG_NONE) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return MBEDTLS_ERR_RSA_INVALID_PADDING;
484 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200485 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200486#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500487
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100488 ctx->padding = padding;
489 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100492}
493
Hanno Becker617c1ae2017-08-23 14:11:24 +0100494/*
Yanray Wang83548b52023-03-15 16:46:34 +0800495 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800496 */
497int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
498{
Yanray Wang644b9012023-03-15 16:50:31 +0800499 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +0800500}
501
502/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800503 * Get hash identifier of mbedtls_md_type_t type
504 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800505int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +0800506{
Yanray Wang644b9012023-03-15 16:50:31 +0800507 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +0800508}
509
510/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100511 * Get length in bytes of RSA modulus
512 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100514{
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100516}
517
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521/*
522 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800523 *
524 * This generation method follows the RSA key pair generation procedure of
525 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
528 int (*f_rng)(void *, unsigned char *, size_t),
529 void *p_rng,
530 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000531{
Janos Follath24eed8d2019-11-22 13:21:35 +0000532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800533 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100534 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Janos Follathb8fc1b02018-09-03 15:37:01 +0100536 /*
537 * If the modulus is 1024 bit long or shorter, then the security strength of
538 * the RSA algorithm is less than or equal to 80 bits and therefore an error
539 * rate of 2^-80 is sufficient.
540 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100542 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 mbedtls_mpi_init(&H);
546 mbedtls_mpi_init(&G);
547 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (nbits < 128 || exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100550 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
551 goto cleanup;
552 }
553
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 /*
555 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800556 * 1. |P-Q| > 2^( nbits / 2 - 100 )
557 * 2. GCD( E, (P-1)*(Q-1) ) == 1
558 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 do {
563 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
564 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
567 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800569 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
571 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800575 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (H.s < 0) {
577 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
578 }
Janos Follathef441782016-09-21 13:18:12 +0100579
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100580 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
582 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
583 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800584
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800585 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
587 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800588 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800590
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800591 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
593 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
594 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (mbedtls_mpi_bitlen(&ctx->D) <= ((nbits + 1) / 2)) { // (FIPS 186-4 §B.3.1 criterion 3(a))
Jethro Beekman97f95c92018-02-13 15:50:36 -0800597 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800599
600 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100603 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
605 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100610
Jethro Beekman97f95c92018-02-13 15:50:36 -0800611#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 * DP = D mod (P - 1)
614 * DQ = D mod (Q - 1)
615 * QP = Q^-1 mod P
616 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
618 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100619#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Hanno Becker83aad1f2017-08-23 06:45:10 +0100621 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
624cleanup:
625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 mbedtls_mpi_free(&H);
627 mbedtls_mpi_free(&G);
628 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (ret != 0) {
631 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if ((-ret & ~0x7f) == 0) {
634 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
635 }
636 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 }
638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000640}
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
644/*
645 * Check a public RSA key
646 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100647int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000648{
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
650 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100651 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
654 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100655 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
658 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
659 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
660 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
661 }
662
663 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000664}
665
666/*
Hanno Becker705fc682017-10-10 17:57:02 +0100667 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
672 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
673 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000674 }
Paul Bakker48377d92013-08-30 12:06:24 +0200675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
677 &ctx->D, &ctx->E, NULL, NULL) != 0) {
678 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000680
Hanno Beckerb269a852017-08-25 08:03:21 +0100681#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
683 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
684 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100685 }
686#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000689}
690
691/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100692 * Check if contexts holding a public and private key match
693 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100694int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
695 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696{
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
698 mbedtls_rsa_check_privkey(prv) != 0) {
699 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100700 }
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
703 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
704 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100705 }
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708}
709
710/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 * Do an RSA public key operation
712 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
714 const unsigned char *input,
715 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000716{
Janos Follath24eed8d2019-11-22 13:21:35 +0000717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000718 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
722 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
723 }
Hanno Becker705fc682017-10-10 17:57:02 +0100724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000726
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200727#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
729 return ret;
730 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200731#endif
732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200736 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
737 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 }
739
740 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
742 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
744cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
747 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
748 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100749#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (ret != 0) {
754 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
755 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000758}
759
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200760/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200761 * Generate or update blinding values, see section 10 of:
762 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200763 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200764 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200765 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
767 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200768{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200769 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200770 mbedtls_mpi R;
771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200775 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
777 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
778 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
779 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200780
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200781 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200782 }
783
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200784 /* Unblinding value: Vf = random number, invertible mod N */
785 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200787 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
788 goto cleanup;
789 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 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 +0200792
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200793 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
795 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
796 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200797
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200798 /* At this point, Vi is invertible mod N if and only if both Vf and R
799 * are invertible mod N. If one of them isn't, we don't need to know
800 * which one, we just loop and choose new values for both of them.
801 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
803 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200804 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500808
809 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
811 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200812
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200813 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200814 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 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 +0200816
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200817
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200818cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200822}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200823
Paul Bakker5121ce52009-01-03 21:22:43 +0000824/*
Janos Follathe81102e2017-03-22 13:38:28 +0000825 * Exponent blinding supposed to prevent side-channel attacks using multiple
826 * traces of measurements to recover the RSA key. The more collisions are there,
827 * the more bits of the key can be recovered. See [3].
828 *
829 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800830 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000831 *
832 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800833 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000834 *
835 * (With the currently (as of 2017 April) known best algorithms breaking 2048
836 * bit RSA requires approximately as much time as trying out 2^112 random keys.
837 * Thus in this sense with 28 byte blinding the security is not reduced by
838 * side-channel attacks like the one in [3])
839 *
840 * This countermeasure does not help if the key recovery is possible with a
841 * single trace.
842 */
843#define RSA_EXPONENT_BLINDING 28
844
845/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000846 * Do an RSA private key operation
847 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
849 int (*f_rng)(void *, unsigned char *, size_t),
850 void *p_rng,
851 const unsigned char *input,
852 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000853{
Janos Follath24eed8d2019-11-22 13:21:35 +0000854 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000855 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100856
857 /* Temporary holding the result */
858 mbedtls_mpi T;
859
860 /* Temporaries holding P-1, Q-1 and the
861 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000862 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100863
864#if !defined(MBEDTLS_RSA_NO_CRT)
865 /* Temporaries holding the results mod p resp. mod q. */
866 mbedtls_mpi TP, TQ;
867
868 /* Temporaries holding the blinded exponents for
869 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000870 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100871
872 /* Pointers to actual exponents to be used - either the unblinded
873 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000874 mbedtls_mpi *DP = &ctx->DP;
875 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100876#else
877 /* Temporary holding the blinded exponent (if used). */
878 mbedtls_mpi D_blind;
879
880 /* Pointer to actual exponent to be used - either the unblinded
881 * or the blinded one, depending on the presence of a PRNG. */
882 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100883#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100884
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100885 /* Temporaries holding the initial input and the double
886 * checked result; should be the same in the end. */
887 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (f_rng == NULL) {
890 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
891 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (rsa_check_context(ctx, 1 /* private key checks */,
894 1 /* blinding on */) != 0) {
895 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +0100896 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100897
Hanno Becker06811ce2017-05-03 15:10:34 +0100898#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
900 return ret;
901 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100902#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000903
Hanno Becker06811ce2017-05-03 15:10:34 +0100904 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +0100906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 mbedtls_mpi_init(&P1);
908 mbedtls_mpi_init(&Q1);
909 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +0000910
Janos Follathe81102e2017-03-22 13:38:28 +0000911#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +0000913#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 mbedtls_mpi_init(&DP_blind);
915 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +0000916#endif
917
Hanno Becker06811ce2017-05-03 15:10:34 +0100918#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200920#endif
921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 mbedtls_mpi_init(&I);
923 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +0100924
925 /* End of MPI initialization */
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
928 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200929 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
930 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 }
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +0100934
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200935 /*
936 * Blinding
937 * T = T * Vi mod N
938 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
940 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
941 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +0000942
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200943 /*
944 * Exponent blinding
945 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
947 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +0000948
Janos Follathf9203b42017-03-22 15:13:15 +0000949#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200950 /*
951 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
952 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
954 f_rng, p_rng));
955 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
956 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
957 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +0000958
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200959 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000960#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200961 /*
962 * DP_blind = ( P - 1 ) * R + DP
963 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
965 f_rng, p_rng));
966 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
967 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
968 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +0000969
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200970 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000971
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200972 /*
973 * DQ_blind = ( Q - 1 ) * R + DQ
974 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
976 f_rng, p_rng));
977 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
978 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
979 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +0000980
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200981 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000982#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100986#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200987 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000988 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100990 * TP = input ^ dP mod P
991 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
995 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +0000996
997 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100998 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1001 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1002 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
1004 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001005 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1008 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001010
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001011 /*
1012 * Unblind
1013 * T = T * Vf mod N
1014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1016 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Hanno Becker2dec5e82017-10-03 07:49:52 +01001018 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1020 &ctx->N, &ctx->RN));
1021 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001022 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1023 goto cleanup;
1024 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001025
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
1029cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1032 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1033 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001034#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 mbedtls_mpi_free(&P1);
1037 mbedtls_mpi_free(&Q1);
1038 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001039
Janos Follathe81102e2017-03-22 13:38:28 +00001040#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001042#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 mbedtls_mpi_free(&DP_blind);
1044 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001045#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001048
1049#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001051#endif
1052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 mbedtls_mpi_free(&C);
1054 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (ret != 0 && ret >= -0x007f) {
1057 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1058 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001061}
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001064/**
1065 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1066 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001067 * \param dst buffer to mask
1068 * \param dlen length of destination buffer
1069 * \param src source of the mask generation
1070 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001071 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001072 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1074 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001075{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001076 unsigned char counter[4];
1077 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001078 unsigned int hlen;
1079 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001080 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001081 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001082 const mbedtls_md_info_t *md_info;
1083 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 mbedtls_md_init(&md_ctx);
1086 md_info = mbedtls_md_info_from_type(md_alg);
1087 if (md_info == NULL) {
1088 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1089 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 mbedtls_md_init(&md_ctx);
1092 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001093 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 memset(mask, 0, sizeof(mask));
1099 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001100
Simon Butcher02037452016-03-01 21:19:12 +00001101 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001102 p = dst;
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001105 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001107 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 }
1113 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 }
1116 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 }
1119 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001124 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001126
1127 counter[3]++;
1128
1129 dlen -= use_len;
1130 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001131
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001132exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001137}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001138
1139/**
1140 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1141 *
1142 * \param hash the input hash
1143 * \param hlen length of the input hash
1144 * \param salt the input salt
1145 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001146 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001147 * \param md_alg message digest to use
1148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001149static int hash_mprime(const unsigned char *hash, size_t hlen,
1150 const unsigned char *salt, size_t slen,
1151 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001152{
1153 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001154
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001155 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1159 if (md_info == NULL) {
1160 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1161 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 mbedtls_md_init(&md_ctx);
1164 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001165 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 }
1167 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001168 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 }
1170 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001171 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 }
1173 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 }
1176 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001177 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 }
1179 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001180 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001182
1183exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001187}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001188
1189/**
1190 * Compute a hash.
1191 *
1192 * \param md_alg algorithm to use
1193 * \param input input message to hash
1194 * \param ilen input length
1195 * \param output the output buffer - must be large enough for \p md_alg
1196 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197static int compute_hash(mbedtls_md_type_t md_alg,
1198 const unsigned char *input, size_t ilen,
1199 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001200{
1201 const mbedtls_md_info_t *md_info;
1202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 md_info = mbedtls_md_info_from_type(md_alg);
1204 if (md_info == NULL) {
1205 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1206 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001213/*
1214 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1215 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1217 int (*f_rng)(void *, unsigned char *, size_t),
1218 void *p_rng,
1219 const unsigned char *label, size_t label_len,
1220 size_t ilen,
1221 const unsigned char *input,
1222 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001223{
1224 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001226 unsigned char *p = output;
1227 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if (f_rng == NULL) {
1230 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1231 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001232
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001233 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if (hlen == 0) {
1235 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1236 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001237
1238 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001239
Simon Butcher02037452016-03-01 21:19:12 +00001240 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1242 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1243 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001246
1247 *p++ = 0;
1248
Simon Butcher02037452016-03-01 21:19:12 +00001249 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1251 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1252 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001253
1254 p += hlen;
1255
Simon Butcher02037452016-03-01 21:19:12 +00001256 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1258 if (ret != 0) {
1259 return ret;
1260 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001261 p += hlen;
1262 p += olen - 2 * hlen - 2 - ilen;
1263 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if (ilen != 0) {
1265 memcpy(p, input, ilen);
1266 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001267
Simon Butcher02037452016-03-01 21:19:12 +00001268 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1270 ctx->hash_id)) != 0) {
1271 return ret;
1272 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001273
Simon Butcher02037452016-03-01 21:19:12 +00001274 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1276 ctx->hash_id)) != 0) {
1277 return ret;
1278 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001281}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001285/*
1286 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1289 int (*f_rng)(void *, unsigned char *, size_t),
1290 void *p_rng, size_t ilen,
1291 const unsigned char *input,
1292 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001293{
1294 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001296 unsigned char *p = output;
1297
Paul Bakkerb3869132013-02-28 17:21:01 +01001298 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001299
Simon Butcher02037452016-03-01 21:19:12 +00001300 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 if (ilen + 11 < ilen || olen < ilen + 11) {
1302 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1303 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001304
1305 nb_pad = olen - 3 - ilen;
1306
1307 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (f_rng == NULL) {
1310 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1311 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001312
1313 *p++ = MBEDTLS_RSA_CRYPT;
1314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001316 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001317
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001318 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 ret = f_rng(p_rng, p, 1);
1320 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001321
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001322 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (rng_dl == 0 || ret != 0) {
1324 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1325 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001326
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001327 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001328 }
1329
1330 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if (ilen != 0) {
1332 memcpy(p, input, ilen);
1333 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001336}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001338
Paul Bakker5121ce52009-01-03 21:22:43 +00001339/*
1340 * Add the message padding, then do an RSA operation
1341 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001342int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1343 int (*f_rng)(void *, unsigned char *, size_t),
1344 void *p_rng,
1345 size_t ilen,
1346 const unsigned char *input,
1347 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001348{
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#if defined(MBEDTLS_PKCS1_V15)
1351 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1353 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001354#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#if defined(MBEDTLS_PKCS1_V21)
1357 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1359 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001360#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
1362 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001365}
1366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001368/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001369 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001371int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1372 int (*f_rng)(void *, unsigned char *, size_t),
1373 void *p_rng,
1374 const unsigned char *label, size_t label_len,
1375 size_t *olen,
1376 const unsigned char *input,
1377 unsigned char *output,
1378 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001379{
Janos Follath24eed8d2019-11-22 13:21:35 +00001380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001381 size_t ilen, i, pad_len;
1382 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001384 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001385 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001386
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001387 /*
1388 * Parameters sanity checks
1389 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1391 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
1394 ilen = ctx->len;
1395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (ilen < 16 || ilen > sizeof(buf)) {
1397 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001400 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if (hlen == 0) {
1402 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1403 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001404
Janos Follathc17cda12016-02-11 11:08:18 +00001405 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if (2 * hlen + 2 > ilen) {
1407 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1408 }
Janos Follathc17cda12016-02-11 11:08:18 +00001409
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001410 /*
1411 * RSA operation
1412 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001416 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001419 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001420 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001421 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001422 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1424 ctx->hash_id)) != 0 ||
1425 /* DB: Apply dbMask to maskedDB */
1426 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1427 ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001428 goto cleanup;
1429 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001430
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001431 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1433 label, label_len, lhash);
1434 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001435 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001437
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001438 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001439 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001440 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001442 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001443
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001444 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001446 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001447
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001448 /* Check lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 for (i = 0; i < hlen; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001450 bad |= lhash[i] ^ *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001452
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001453 /* Get zero-padding len, but always read till end of buffer
1454 * (minus one, for the 01 byte) */
1455 pad_len = 0;
1456 pad_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001458 pad_done |= p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001460 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001461
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001462 p += pad_len;
1463 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001464
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001465 /*
1466 * The only information "leaked" is whether the padding was correct or not
1467 * (eg, no data is copied if it was not correct). This meets the
1468 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1469 * the different error conditions.
1470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001472 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1473 goto cleanup;
1474 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001477 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1478 goto cleanup;
1479 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001480
1481 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 if (*olen != 0) {
1483 memcpy(output, p, *olen);
1484 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001485 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001486
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001487cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 mbedtls_platform_zeroize(buf, sizeof(buf));
1489 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001496/*
1497 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1498 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001499int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1500 int (*f_rng)(void *, unsigned char *, size_t),
1501 void *p_rng,
1502 size_t *olen,
1503 const unsigned char *input,
1504 unsigned char *output,
1505 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001506{
1507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1508 size_t ilen;
1509 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1510
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001511 ilen = ctx->len;
1512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1514 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1515 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if (ilen < 16 || ilen > sizeof(buf)) {
1518 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1519 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001524 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1528 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001529
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001530cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001536
1537/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001538 * Do an RSA operation, then remove the message padding
1539 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001540int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1541 int (*f_rng)(void *, unsigned char *, size_t),
1542 void *p_rng,
1543 size_t *olen,
1544 const unsigned char *input,
1545 unsigned char *output,
1546 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001547{
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_PKCS1_V15)
1550 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1552 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001553#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#if defined(MBEDTLS_PKCS1_V21)
1556 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1558 olen, input, output,
1559 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001560#endif
1561
1562 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001564 }
1565}
1566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001568static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1569 int (*f_rng)(void *, unsigned char *, size_t),
1570 void *p_rng,
1571 mbedtls_md_type_t md_alg,
1572 unsigned int hashlen,
1573 const unsigned char *hash,
1574 int saltlen,
1575 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001576{
1577 size_t olen;
1578 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001579 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001580 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001581 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001582 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001585 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1589 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1590 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 if (f_rng == NULL) {
1593 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1594 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001595
1596 olen = ctx->len;
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001599 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001600 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if (exp_hashlen == 0) {
1602 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1603 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 if (hashlen != exp_hashlen) {
1606 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1607 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001608 }
1609
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001610 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if (hlen == 0) {
1612 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1613 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001614
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1616 /* Calculate the largest possible salt length, up to the hash size.
1617 * Normally this is the hash length, which is the maximum salt length
1618 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1619 * enough room, use the maximum salt length that fits. The constraint is
1620 * that the hash length plus the salt length plus 2 bytes must be at most
1621 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1622 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001623 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (olen < hlen + min_slen + 2) {
1625 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1626 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001627 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001629 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 }
1631 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1632 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1633 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001634 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001635 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001638
Simon Butcher02037452016-03-01 21:19:12 +00001639 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001641 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001642 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001643
1644 /* Generate salt of length slen in place in the encoded message */
1645 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1647 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1648 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001649
Paul Bakkerb3869132013-02-28 17:21:01 +01001650 p += slen;
1651
Simon Butcher02037452016-03-01 21:19:12 +00001652 /* Generate H = Hash( M' ) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 ret = hash_mprime(hash, hashlen, salt, slen, p, ctx->hash_id);
1654 if (ret != 0) {
1655 return ret;
1656 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001657
Simon Butcher02037452016-03-01 21:19:12 +00001658 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001660 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001662
Simon Butcher02037452016-03-01 21:19:12 +00001663 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
1665 ctx->hash_id);
1666 if (ret != 0) {
1667 return ret;
1668 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1671 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001672
1673 p += hlen;
1674 *p++ = 0xBC;
1675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001677}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001678
1679/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001680 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1681 * the option to pass in the salt length.
1682 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001683int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1684 int (*f_rng)(void *, unsigned char *, size_t),
1685 void *p_rng,
1686 mbedtls_md_type_t md_alg,
1687 unsigned int hashlen,
1688 const unsigned char *hash,
1689 int saltlen,
1690 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001691{
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1693 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001694}
1695
1696
1697/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001698 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1699 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001700int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1701 int (*f_rng)(void *, unsigned char *, size_t),
1702 void *p_rng,
1703 mbedtls_md_type_t md_alg,
1704 unsigned int hashlen,
1705 const unsigned char *hash,
1706 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001707{
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1709 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001710}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001714/*
1715 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1716 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001717
1718/* Construct a PKCS v1.5 encoding of a hashed message
1719 *
1720 * This is used both for signature generation and verification.
1721 *
1722 * Parameters:
1723 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001724 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001725 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001726 * - hash: Buffer containing the hashed message or the raw data.
1727 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001728 * - dst: Buffer to hold the encoded message.
1729 *
1730 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001731 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001732 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001733 *
1734 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001735static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1736 unsigned int hashlen,
1737 const unsigned char *hash,
1738 size_t dst_len,
1739 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001740{
1741 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001742 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001743 unsigned char *p = dst;
1744 const char *oid = NULL;
1745
1746 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001748 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if (md_size == 0) {
1750 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1751 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1754 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1755 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if (hashlen != md_size) {
1758 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1759 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001760
1761 /* Double-check that 8 + hashlen + oid_size can be used as a
1762 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001764 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 10 + hashlen + oid_size < 10 + hashlen) {
1766 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1767 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001768
1769 /*
1770 * Static bounds check:
1771 * - Need 10 bytes for five tag-length pairs.
1772 * (Insist on 1-byte length encodings to protect against variants of
1773 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1774 * - Need hashlen bytes for hash
1775 * - Need oid_size bytes for hash alg OID.
1776 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 if (nb_pad < 10 + hashlen + oid_size) {
1778 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1779 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001780 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 } else {
1782 if (nb_pad < hashlen) {
1783 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1784 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001785
1786 nb_pad -= hashlen;
1787 }
1788
Hanno Becker2b2f8982017-09-27 17:10:03 +01001789 /* Need space for signature header and padding delimiter (3 bytes),
1790 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 if (nb_pad < 3 + 8) {
1792 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1793 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001794 nb_pad -= 3;
1795
1796 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001797 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001798
1799 /* Write signature header and padding */
1800 *p++ = 0;
1801 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001803 p += nb_pad;
1804 *p++ = 0;
1805
1806 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 if (md_alg == MBEDTLS_MD_NONE) {
1808 memcpy(p, hash, hashlen);
1809 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001810 }
1811
1812 /* Signing hashed data, add corresponding ASN.1 structure
1813 *
1814 * DigestInfo ::= SEQUENCE {
1815 * digestAlgorithm DigestAlgorithmIdentifier,
1816 * digest Digest }
1817 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1818 * Digest ::= OCTET STRING
1819 *
1820 * Schematic:
1821 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1822 * TAG-NULL + LEN [ NULL ] ]
1823 * TAG-OCTET + LEN [ HASH ] ]
1824 */
1825 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001827 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001829 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001830 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001832 p += oid_size;
1833 *p++ = MBEDTLS_ASN1_NULL;
1834 *p++ = 0x00;
1835 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001836 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001838 p += hashlen;
1839
1840 /* Just a sanity-check, should be automatic
1841 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 if (p != dst + dst_len) {
1843 mbedtls_platform_zeroize(dst, dst_len);
1844 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001845 }
1846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001848}
1849
Paul Bakkerb3869132013-02-28 17:21:01 +01001850/*
1851 * Do an RSA operation to sign the message digest
1852 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001853int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
1854 int (*f_rng)(void *, unsigned char *, size_t),
1855 void *p_rng,
1856 mbedtls_md_type_t md_alg,
1857 unsigned int hashlen,
1858 const unsigned char *hash,
1859 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001860{
Janos Follath24eed8d2019-11-22 13:21:35 +00001861 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001862 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001865 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1869 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1870 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001871
Hanno Beckerfdf38032017-09-06 12:35:55 +01001872 /*
1873 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1874 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
1877 ctx->len, sig)) != 0) {
1878 return ret;
1879 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001880
Hanno Beckerfdf38032017-09-06 12:35:55 +01001881 /* Private key operation
1882 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001883 * In order to prevent Lenstra's attack, make the signature in a
1884 * temporary buffer and check it before returning it.
1885 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 sig_try = mbedtls_calloc(1, ctx->len);
1888 if (sig_try == NULL) {
1889 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00001890 }
1891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 verif = mbedtls_calloc(1, ctx->len);
1893 if (verif == NULL) {
1894 mbedtls_free(sig_try);
1895 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
1896 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
1899 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
1900
1901 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001902 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1903 goto cleanup;
1904 }
1905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001907
1908cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 mbedtls_platform_zeroize(sig_try, ctx->len);
1910 mbedtls_platform_zeroize(verif, ctx->len);
1911 mbedtls_free(sig_try);
1912 mbedtls_free(verif);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 if (ret != 0) {
1915 memset(sig, '!', ctx->len);
1916 }
1917 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001918}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001920
1921/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 * Do an RSA operation to sign the message digest
1923 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001924int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
1925 int (*f_rng)(void *, unsigned char *, size_t),
1926 void *p_rng,
1927 mbedtls_md_type_t md_alg,
1928 unsigned int hashlen,
1929 const unsigned char *hash,
1930 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00001931{
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001933 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937#if defined(MBEDTLS_PKCS1_V15)
1938 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
1940 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02001941#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943#if defined(MBEDTLS_PKCS1_V21)
1944 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1946 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001947#endif
1948
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001952}
1953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001955/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001956 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001958int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
1959 mbedtls_md_type_t md_alg,
1960 unsigned int hashlen,
1961 const unsigned char *hash,
1962 mbedtls_md_type_t mgf1_hash_id,
1963 int expected_salt_len,
1964 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00001965{
Janos Follath24eed8d2019-11-22 13:21:35 +00001966 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001967 size_t siglen;
1968 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001969 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001970 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001971 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001972 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01001974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001976 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001978
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 siglen = ctx->len;
1980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 if (siglen < 16 || siglen > sizeof(buf)) {
1982 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1983 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 if (ret != 0) {
1988 return ret;
1989 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001990
1991 p = buf;
1992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 if (buf[siglen - 1] != 0xBC) {
1994 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001995 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 if (md_alg != MBEDTLS_MD_NONE) {
1998 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001999 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 if (exp_hashlen == 0) {
2001 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2002 }
2003
2004 if (hashlen != exp_hashlen) {
2005 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2006 }
2007 }
2008
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002009 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 if (hlen == 0) {
2011 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2012 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002013
Simon Butcher02037452016-03-01 21:19:12 +00002014 /*
2015 * Note: EMSA-PSS verification is over the length of N - 1 bits
2016 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 if (buf[0] >> (8 - siglen * 8 + msb)) {
2020 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2021 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002022
Simon Butcher02037452016-03-01 21:19:12 +00002023 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002025 p++;
2026 siglen -= 1;
2027 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (siglen < hlen + 2) {
2030 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2031 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002032 hash_start = p + siglen - hlen - 1;
2033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2035 if (ret != 0) {
2036 return ret;
2037 }
Paul Bakker02303e82013-01-03 11:08:31 +01002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002042 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 if (*p++ != 0x01) {
2046 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2047 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002048
Gilles Peskine6a54b022017-10-17 19:02:13 +02002049 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2052 observed_salt_len != (size_t) expected_salt_len) {
2053 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002054 }
2055
Simon Butcher02037452016-03-01 21:19:12 +00002056 /*
2057 * Generate H = Hash( M' )
2058 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2060 result, mgf1_hash_id);
2061 if (ret != 0) {
2062 return ret;
2063 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 if (memcmp(hash_start, result, hlen) != 0) {
2066 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2067 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002070}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002071
2072/*
2073 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2074 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002075int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2076 mbedtls_md_type_t md_alg,
2077 unsigned int hashlen,
2078 const unsigned char *hash,
2079 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002080{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002081 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002083 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002088 : md_alg;
2089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2091 md_alg, hashlen, hash,
2092 mgf1_hash_id,
2093 MBEDTLS_RSA_SALT_LEN_ANY,
2094 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002095
2096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002100/*
2101 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2102 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2104 mbedtls_md_type_t md_alg,
2105 unsigned int hashlen,
2106 const unsigned char *hash,
2107 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002108{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002109 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002110 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002111 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002114 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002116
2117 sig_len = ctx->len;
2118
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002119 /*
2120 * Prepare expected PKCS1 v1.5 encoding of hash.
2121 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2124 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002125 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2126 goto cleanup;
2127 }
2128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2130 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002131 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002133
2134 /*
2135 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2136 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 ret = mbedtls_rsa_public(ctx, sig, encoded);
2139 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002140 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002142
Simon Butcher02037452016-03-01 21:19:12 +00002143 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002144 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002145 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2148 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002149 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2150 goto cleanup;
2151 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002152
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002153cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (encoded != NULL) {
2156 mbedtls_platform_zeroize(encoded, sig_len);
2157 mbedtls_free(encoded);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002158 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (encoded_expected != NULL) {
2161 mbedtls_platform_zeroize(encoded_expected, sig_len);
2162 mbedtls_free(encoded_expected);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002163 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002168
2169/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002170 * Do an RSA operation and check the message digest
2171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002172int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2173 mbedtls_md_type_t md_alg,
2174 unsigned int hashlen,
2175 const unsigned char *hash,
2176 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002177{
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002179 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#if defined(MBEDTLS_PKCS1_V15)
2184 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2186 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002187#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189#if defined(MBEDTLS_PKCS1_V21)
2190 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2192 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002193#endif
2194
2195 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002197 }
2198}
2199
2200/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002201 * Copy the components of an RSA key
2202 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002204{
Janos Follath24eed8d2019-11-22 13:21:35 +00002205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002206
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002207 dst->len = src->len;
2208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2210 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2213 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2214 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002215
2216#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2218 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2219 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2220 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2221 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002222#endif
2223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2227 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002228
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002229 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002230 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002231
2232cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 if (ret != 0) {
2234 mbedtls_rsa_free(dst);
2235 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002238}
2239
2240/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 * Free the components of an RSA key
2242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002243void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002244{
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002246 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 mbedtls_mpi_free(&ctx->Vi);
2250 mbedtls_mpi_free(&ctx->Vf);
2251 mbedtls_mpi_free(&ctx->RN);
2252 mbedtls_mpi_free(&ctx->D);
2253 mbedtls_mpi_free(&ctx->Q);
2254 mbedtls_mpi_free(&ctx->P);
2255 mbedtls_mpi_free(&ctx->E);
2256 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002257
Hanno Becker33c30a02017-08-23 07:00:22 +01002258#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 mbedtls_mpi_free(&ctx->RQ);
2260 mbedtls_mpi_free(&ctx->RP);
2261 mbedtls_mpi_free(&ctx->QP);
2262 mbedtls_mpi_free(&ctx->DQ);
2263 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002264#endif /* MBEDTLS_RSA_NO_CRT */
2265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002267 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if (ctx->ver != 0) {
2269 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002270 ctx->ver = 0;
2271 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002273}
2274
Hanno Beckerab377312017-08-23 16:24:51 +01002275#endif /* !MBEDTLS_RSA_ALT */
2276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002279#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
2281/*
2282 * Example RSA-1024 keypair, for test purposes
2283 */
2284#define KEY_LEN 128
2285
2286#define RSA_N "9292758453063D803DD603D5E777D788" \
2287 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2288 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2289 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2290 "93A89813FBF3C4F8066D2D800F7C38A8" \
2291 "1AE31942917403FF4946B0A83D3D3E05" \
2292 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2293 "5E94BB77B07507233A0BC7BAC8F90F79"
2294
2295#define RSA_E "10001"
2296
2297#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2298 "66CA472BC44D253102F8B4A9D3BFA750" \
2299 "91386C0077937FE33FA3252D28855837" \
2300 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2301 "DF79C5CE07EE72C7F123142198164234" \
2302 "CABB724CF78B8173B9F880FC86322407" \
2303 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2304 "071513A1E85B5DFA031F21ECAE91A34D"
2305
2306#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2307 "2C01CAD19EA484A87EA4377637E75500" \
2308 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2309 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2310
2311#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2312 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2313 "910E4168387E3C30AA1E00C339A79508" \
2314 "8452DD96A9A5EA5D9DCA68DA636032AF"
2315
Paul Bakker5121ce52009-01-03 21:22:43 +00002316#define PT_LEN 24
2317#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2318 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002321static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002322{
gufe44c2620da2020-08-03 17:56:50 +02002323#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002324 size_t i;
2325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002327 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 }
Paul Bakker545570e2010-07-18 09:00:25 +00002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002331 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002333#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002335 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002339#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002344
Paul Bakker5121ce52009-01-03 21:22:43 +00002345/*
2346 * Checkup routine
2347 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002348int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002349{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002350 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002352 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002354 unsigned char rsa_plaintext[PT_LEN];
2355 unsigned char rsa_decrypted[PT_LEN];
2356 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002357#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002358 unsigned char sha1sum[20];
2359#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
Hanno Becker3a701162017-08-22 13:52:43 +01002361 mbedtls_mpi K;
2362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 mbedtls_mpi_init(&K);
2364 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2367 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2368 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2369 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2370 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2371 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2372 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2373 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2374 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2375 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 if (verbose != 0) {
2380 mbedtls_printf(" RSA key validation: ");
2381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2384 mbedtls_rsa_check_privkey(&rsa) != 0) {
2385 if (verbose != 0) {
2386 mbedtls_printf("failed\n");
2387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Hanno Becker5bc87292017-05-03 15:09:31 +01002389 ret = 1;
2390 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 }
2392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 if (verbose != 0) {
2394 mbedtls_printf("passed\n PKCS#1 encryption : ");
2395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2400 PT_LEN, rsa_plaintext,
2401 rsa_ciphertext) != 0) {
2402 if (verbose != 0) {
2403 mbedtls_printf("failed\n");
2404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
Hanno Becker5bc87292017-05-03 15:09:31 +01002406 ret = 1;
2407 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 }
2409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (verbose != 0) {
2411 mbedtls_printf("passed\n PKCS#1 decryption : ");
2412 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2415 &len, rsa_ciphertext, rsa_decrypted,
2416 sizeof(rsa_decrypted)) != 0) {
2417 if (verbose != 0) {
2418 mbedtls_printf("failed\n");
2419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
Hanno Becker5bc87292017-05-03 15:09:31 +01002421 ret = 1;
2422 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 }
2424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2426 if (verbose != 0) {
2427 mbedtls_printf("failed\n");
2428 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Hanno Becker5bc87292017-05-03 15:09:31 +01002430 ret = 1;
2431 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 }
2433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 if (verbose != 0) {
2435 mbedtls_printf("passed\n");
2436 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002437
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002438#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (verbose != 0) {
2440 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002443 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2444 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (verbose != 0) {
2446 mbedtls_printf("failed\n");
2447 }
2448
2449 return 1;
2450 }
2451
2452 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2453 MBEDTLS_MD_SHA1, 20,
2454 sha1sum, rsa_ciphertext) != 0) {
2455 if (verbose != 0) {
2456 mbedtls_printf("failed\n");
2457 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
Hanno Becker5bc87292017-05-03 15:09:31 +01002459 ret = 1;
2460 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 }
2462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 if (verbose != 0) {
2464 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2465 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2468 sha1sum, rsa_ciphertext) != 0) {
2469 if (verbose != 0) {
2470 mbedtls_printf("failed\n");
2471 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002472
Hanno Becker5bc87292017-05-03 15:09:31 +01002473 ret = 1;
2474 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 }
2476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (verbose != 0) {
2478 mbedtls_printf("passed\n");
2479 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002480#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 if (verbose != 0) {
2483 mbedtls_printf("\n");
2484 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002485
Paul Bakker3d8fb632014-04-17 12:42:41 +02002486cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 mbedtls_mpi_free(&K);
2488 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002490 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002493}
2494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497#endif /* MBEDTLS_RSA_C */