blob: 01159dfa20f91ca5978870fe4a72d7a472da2a34 [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"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
gufe44c2620da2020-08-03 17:56:50 +020053#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000054#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000055#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +020057/* We use MD first if it's available (for compatibility reasons)
58 * and "fall back" to PSA otherwise (which needs psa_crypto_init()). */
59#if defined(MBEDTLS_PKCS1_V21)
Przemek Stekiel40afdd22022-09-06 13:08:28 +020060#if !defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +020061#include "psa/crypto.h"
62#include "mbedtls/psa_util.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050063#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
64 psa_to_md_errors, \
65 psa_generic_status_to_mbedtls)
66#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +020067#endif /* MBEDTLS_PKCS1_V21 */
68
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010070
Hanno Beckera565f542017-10-11 11:00:19 +010071#if !defined(MBEDTLS_RSA_ALT)
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
74 const mbedtls_mpi *N,
75 const mbedtls_mpi *P, const mbedtls_mpi *Q,
76 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +010077{
Janos Follath24eed8d2019-11-22 13:21:35 +000078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +010079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
81 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
82 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
83 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
84 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
85 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +010086 }
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (N != NULL) {
89 ctx->len = mbedtls_mpi_size(&ctx->N);
90 }
Hanno Becker617c1ae2017-08-23 14:11:24 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +010093}
94
Gilles Peskine449bd832023-01-11 14:50:10 +010095int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
96 unsigned char const *N, size_t N_len,
97 unsigned char const *P, size_t P_len,
98 unsigned char const *Q, size_t Q_len,
99 unsigned char const *D, size_t D_len,
100 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100101{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000102 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 if (N != NULL) {
105 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
106 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100107 }
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (P != NULL) {
110 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
111 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if (Q != NULL) {
114 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
115 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if (D != NULL) {
118 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
119 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (E != NULL) {
122 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
123 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100124
125cleanup:
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if (ret != 0) {
128 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
129 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100132}
133
Hanno Becker705fc682017-10-10 17:57:02 +0100134/*
135 * Checks whether the context fields are set in such a way
136 * that the RSA primitives will be able to execute without error.
137 * It does *not* make guarantees for consistency of the parameters.
138 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
140 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100141{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100142#if !defined(MBEDTLS_RSA_NO_CRT)
143 /* blinding_needed is only used for NO_CRT to decide whether
144 * P,Q need to be present or not. */
145 ((void) blinding_needed);
146#endif
147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
149 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
150 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000151 }
Hanno Becker705fc682017-10-10 17:57:02 +0100152
153 /*
154 * 1. Modular exponentiation needs positive, odd moduli.
155 */
156
157 /* Modular exponentiation wrt. N is always used for
158 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
160 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
161 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100162 }
163
164#if !defined(MBEDTLS_RSA_NO_CRT)
165 /* Modular exponentiation for P and Q is only
166 * used for private key operations and if CRT
167 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 if (is_priv &&
169 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
170 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
171 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
172 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
173 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100174 }
175#endif /* !MBEDTLS_RSA_NO_CRT */
176
177 /*
178 * 2. Exponents must be positive
179 */
180
181 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
183 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
184 }
Hanno Becker705fc682017-10-10 17:57:02 +0100185
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100186#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100187 /* For private key operations, use D or DP & DQ
188 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
190 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
191 }
Hanno Becker705fc682017-10-10 17:57:02 +0100192#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (is_priv &&
194 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
195 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
196 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100197 }
198#endif /* MBEDTLS_RSA_NO_CRT */
199
200 /* Blinding shouldn't make exponents negative either,
201 * so check that P, Q >= 1 if that hasn't yet been
202 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100203#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (is_priv && blinding_needed &&
205 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
206 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
207 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100208 }
209#endif
210
211 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100212 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100213#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (is_priv &&
215 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
216 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100217 }
218#endif
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100221}
222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100224{
225 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500226 int have_N, have_P, have_Q, have_D, have_E;
227#if !defined(MBEDTLS_RSA_NO_CRT)
228 int have_DP, have_DQ, have_QP;
229#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500230 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
233 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
234 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
235 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
236 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500237
238#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
240 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
241 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500242#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100243
Hanno Becker617c1ae2017-08-23 14:11:24 +0100244 /*
245 * Check whether provided parameters are enough
246 * to deduce all others. The following incomplete
247 * parameter sets for private keys are supported:
248 *
249 * (1) P, Q missing.
250 * (2) D and potentially N missing.
251 *
252 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100253
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500254 n_missing = have_P && have_Q && have_D && have_E;
255 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
256 d_missing = have_P && have_Q && !have_D && have_E;
257 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100258
259 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500260 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (!is_priv && !is_pub) {
263 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
264 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100265
266 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100267 * Step 1: Deduce N if P, Q are provided.
268 */
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (!have_N && have_P && have_Q) {
271 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
272 &ctx->Q)) != 0) {
273 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100274 }
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100277 }
278
279 /*
280 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100281 */
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (pq_missing) {
284 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
285 &ctx->P, &ctx->Q);
286 if (ret != 0) {
287 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
288 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 } else if (d_missing) {
291 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
292 &ctx->Q,
293 &ctx->E,
294 &ctx->D)) != 0) {
295 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100296 }
297 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100298
Hanno Becker617c1ae2017-08-23 14:11:24 +0100299 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100300 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100301 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100302 */
303
Hanno Becker23344b52017-08-23 07:43:27 +0100304#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (is_priv && !(have_DP && have_DQ && have_QP)) {
306 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
307 &ctx->DP, &ctx->DQ, &ctx->QP);
308 if (ret != 0) {
309 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
310 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100311 }
Hanno Becker23344b52017-08-23 07:43:27 +0100312#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100313
314 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100315 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100316 */
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100319}
320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
322 unsigned char *N, size_t N_len,
323 unsigned char *P, size_t P_len,
324 unsigned char *Q, size_t Q_len,
325 unsigned char *D, size_t D_len,
326 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327{
328 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500329 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100330
331 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500332 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
334 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
335 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
336 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
337 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100340 /* If we're trying to export private parameters for a public key,
341 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if (P != NULL || Q != NULL || D != NULL) {
343 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
344 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100345
346 }
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (N != NULL) {
349 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
350 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (P != NULL) {
353 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
354 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if (Q != NULL) {
357 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
358 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (D != NULL) {
361 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
362 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (E != NULL) {
365 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
366 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100367
368cleanup:
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100371}
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
374 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
375 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100376{
Janos Follath24eed8d2019-11-22 13:21:35 +0000377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500378 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100379
380 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500381 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
383 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
384 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
385 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
386 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100389 /* If we're trying to export private parameters for a public key,
390 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (P != NULL || Q != NULL || D != NULL) {
392 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
393 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100394
395 }
396
397 /* Export all requested core parameters. */
398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
400 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
401 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
402 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
403 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
404 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100405 }
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100408}
409
410/*
411 * Export CRT parameters
412 * This must also be implemented if CRT is not used, for being able to
413 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
414 * can be used in this case.
415 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100416int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
417 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100418{
Janos Follath24eed8d2019-11-22 13:21:35 +0000419 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500420 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100421
422 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500423 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
425 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
426 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
427 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
428 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (!is_priv) {
431 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
432 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100433
Hanno Beckerdc95c892017-08-23 06:57:02 +0100434#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100435 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
437 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
438 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
439 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100440 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100441#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
443 DP, DQ, QP)) != 0) {
444 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100445 }
446#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100449}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100450
Paul Bakker5121ce52009-01-03 21:22:43 +0000451/*
452 * Initialize an RSA context
453 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100454void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000455{
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
Ronald Cronc1905a12021-06-05 11:11:14 +0200458 ctx->padding = MBEDTLS_RSA_PKCS_V15;
459 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100462 /* Set ctx->ver to nonzero to indicate that the mutex has been
463 * initialized and will need to be freed. */
464 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200466#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000467}
468
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100469/*
470 * Set padding for an existing RSA context
471 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
473 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100474{
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200476#if defined(MBEDTLS_PKCS1_V15)
477 case MBEDTLS_RSA_PKCS_V15:
478 break;
479#endif
480
481#if defined(MBEDTLS_PKCS1_V21)
482 case MBEDTLS_RSA_PKCS_V21:
483 break;
484#endif
485 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200487 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200488
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200489#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
491 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200492 /* Just make sure this hash is supported in this build. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (mbedtls_hash_info_psa_from_md(hash_id) == PSA_ALG_NONE) {
494 return MBEDTLS_ERR_RSA_INVALID_PADDING;
495 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200496 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200497#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500498
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100499 ctx->padding = padding;
500 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100503}
504
Hanno Becker617c1ae2017-08-23 14:11:24 +0100505/*
Yanray Wang83548b52023-03-15 16:46:34 +0800506 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800507 */
508int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
509{
Yanray Wang644b9012023-03-15 16:50:31 +0800510 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +0800511}
512
513/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800514 * Get hash identifier of mbedtls_md_type_t type
515 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800516int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +0800517{
Yanray Wang644b9012023-03-15 16:50:31 +0800518 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +0800519}
520
521/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100522 * Get length in bytes of RSA modulus
523 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100525{
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100527}
528
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
532/*
533 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800534 *
535 * This generation method follows the RSA key pair generation procedure of
536 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100538int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
539 int (*f_rng)(void *, unsigned char *, size_t),
540 void *p_rng,
541 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000542{
Janos Follath24eed8d2019-11-22 13:21:35 +0000543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800544 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100545 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
Janos Follathb8fc1b02018-09-03 15:37:01 +0100547 /*
548 * If the modulus is 1024 bit long or shorter, then the security strength of
549 * the RSA algorithm is less than or equal to 80 bits and therefore an error
550 * rate of 2^-80 is sufficient.
551 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100553 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 mbedtls_mpi_init(&H);
557 mbedtls_mpi_init(&G);
558 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (nbits < 128 || exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100561 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
562 goto cleanup;
563 }
564
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 /*
566 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800567 * 1. |P-Q| > 2^( nbits / 2 - 100 )
568 * 2. GCD( E, (P-1)*(Q-1) ) == 1
569 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 do {
574 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
575 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
578 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800580 /* 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 +0100581 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
582 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800586 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (H.s < 0) {
588 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
589 }
Janos Follathef441782016-09-21 13:18:12 +0100590
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100591 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
593 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
594 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800595
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800596 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
598 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800599 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800601
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800602 /* 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 +0100603 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
604 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
605 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 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 -0800608 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800610
611 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100614 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
616 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100621
Jethro Beekman97f95c92018-02-13 15:50:36 -0800622#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 * DP = D mod (P - 1)
625 * DQ = D mod (Q - 1)
626 * QP = Q^-1 mod P
627 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
629 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100630#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Hanno Becker83aad1f2017-08-23 06:45:10 +0100632 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
635cleanup:
636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 mbedtls_mpi_free(&H);
638 mbedtls_mpi_free(&G);
639 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (ret != 0) {
642 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if ((-ret & ~0x7f) == 0) {
645 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
646 }
647 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 }
649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000651}
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
655/*
656 * Check a public RSA key
657 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100658int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000659{
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
661 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100662 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
665 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100666 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
669 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
670 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
671 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
672 }
673
674 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000675}
676
677/*
Hanno Becker705fc682017-10-10 17:57:02 +0100678 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100680int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000681{
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
683 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
684 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 }
Paul Bakker48377d92013-08-30 12:06:24 +0200686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
688 &ctx->D, &ctx->E, NULL, NULL) != 0) {
689 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000691
Hanno Beckerb269a852017-08-25 08:03:21 +0100692#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
694 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
695 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100696 }
697#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000700}
701
702/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100703 * Check if contexts holding a public and private key match
704 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100705int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
706 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100707{
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
709 mbedtls_rsa_check_privkey(prv) != 0) {
710 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100711 }
712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
714 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
715 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100716 }
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100719}
720
721/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 * Do an RSA public key operation
723 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
725 const unsigned char *input,
726 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000727{
Janos Follath24eed8d2019-11-22 13:21:35 +0000728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000729 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
733 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
734 }
Hanno Becker705fc682017-10-10 17:57:02 +0100735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200738#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
740 return ret;
741 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200742#endif
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200747 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
748 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 }
750
751 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
753 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
755cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
758 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
759 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100760#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 if (ret != 0) {
765 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
766 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000769}
770
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200771/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200772 * Generate or update blinding values, see section 10 of:
773 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200774 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200775 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200776 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100777static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
778 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200779{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200780 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200781 mbedtls_mpi R;
782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200786 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
788 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
789 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
790 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200791
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200792 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200793 }
794
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200795 /* Unblinding value: Vf = random number, invertible mod N */
796 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200798 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
799 goto cleanup;
800 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 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 +0200803
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200804 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
806 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
807 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200808
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200809 /* At this point, Vi is invertible mod N if and only if both Vf and R
810 * are invertible mod N. If one of them isn't, we don't need to know
811 * which one, we just loop and choose new values for both of them.
812 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
814 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200815 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500819
820 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
822 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200823
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200824 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200825 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 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 +0200827
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200828
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200829cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200833}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200834
Paul Bakker5121ce52009-01-03 21:22:43 +0000835/*
Janos Follathe81102e2017-03-22 13:38:28 +0000836 * Exponent blinding supposed to prevent side-channel attacks using multiple
837 * traces of measurements to recover the RSA key. The more collisions are there,
838 * the more bits of the key can be recovered. See [3].
839 *
840 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800841 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000842 *
843 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800844 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000845 *
846 * (With the currently (as of 2017 April) known best algorithms breaking 2048
847 * bit RSA requires approximately as much time as trying out 2^112 random keys.
848 * Thus in this sense with 28 byte blinding the security is not reduced by
849 * side-channel attacks like the one in [3])
850 *
851 * This countermeasure does not help if the key recovery is possible with a
852 * single trace.
853 */
854#define RSA_EXPONENT_BLINDING 28
855
856/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 * Do an RSA private key operation
858 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
860 int (*f_rng)(void *, unsigned char *, size_t),
861 void *p_rng,
862 const unsigned char *input,
863 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000864{
Janos Follath24eed8d2019-11-22 13:21:35 +0000865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000866 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100867
868 /* Temporary holding the result */
869 mbedtls_mpi T;
870
871 /* Temporaries holding P-1, Q-1 and the
872 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000873 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100874
875#if !defined(MBEDTLS_RSA_NO_CRT)
876 /* Temporaries holding the results mod p resp. mod q. */
877 mbedtls_mpi TP, TQ;
878
879 /* Temporaries holding the blinded exponents for
880 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000881 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100882
883 /* Pointers to actual exponents to be used - either the unblinded
884 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000885 mbedtls_mpi *DP = &ctx->DP;
886 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100887#else
888 /* Temporary holding the blinded exponent (if used). */
889 mbedtls_mpi D_blind;
890
891 /* Pointer to actual exponent to be used - either the unblinded
892 * or the blinded one, depending on the presence of a PRNG. */
893 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100894#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100895
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100896 /* Temporaries holding the initial input and the double
897 * checked result; should be the same in the end. */
898 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (f_rng == NULL) {
901 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
902 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (rsa_check_context(ctx, 1 /* private key checks */,
905 1 /* blinding on */) != 0) {
906 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +0100907 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100908
Hanno Becker06811ce2017-05-03 15:10:34 +0100909#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
911 return ret;
912 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100913#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000914
Hanno Becker06811ce2017-05-03 15:10:34 +0100915 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +0100917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 mbedtls_mpi_init(&P1);
919 mbedtls_mpi_init(&Q1);
920 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +0000921
Janos Follathe81102e2017-03-22 13:38:28 +0000922#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +0000924#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 mbedtls_mpi_init(&DP_blind);
926 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +0000927#endif
928
Hanno Becker06811ce2017-05-03 15:10:34 +0100929#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200931#endif
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 mbedtls_mpi_init(&I);
934 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +0100935
936 /* End of MPI initialization */
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
939 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200940 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
941 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000942 }
943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +0100945
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200946 /*
947 * Blinding
948 * T = T * Vi mod N
949 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
951 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
952 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +0000953
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200954 /*
955 * Exponent blinding
956 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
958 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +0000959
Janos Follathf9203b42017-03-22 15:13:15 +0000960#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200961 /*
962 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
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(&D_blind, &P1, &Q1));
967 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
968 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +0000969
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200970 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000971#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200972 /*
973 * DP_blind = ( P - 1 ) * R + DP
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(&DP_blind, &P1, &R));
978 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
979 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +0000980
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200981 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000982
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200983 /*
984 * DQ_blind = ( Q - 1 ) * R + DQ
985 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
987 f_rng, p_rng));
988 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
989 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
990 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +0000991
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200992 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000993#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100997#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200998 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000999 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001001 * TP = input ^ dP mod P
1002 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1006 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
1008 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001009 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1012 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1013 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
1015 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001016 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001017 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1019 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001021
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001022 /*
1023 * Unblind
1024 * T = T * Vf mod N
1025 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1027 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Hanno Becker2dec5e82017-10-03 07:49:52 +01001029 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1031 &ctx->N, &ctx->RN));
1032 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001033 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1034 goto cleanup;
1035 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001036
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001039
1040cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1043 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1044 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001045#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 mbedtls_mpi_free(&P1);
1048 mbedtls_mpi_free(&Q1);
1049 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001050
Janos Follathe81102e2017-03-22 13:38:28 +00001051#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001053#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 mbedtls_mpi_free(&DP_blind);
1055 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001059
1060#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001062#endif
1063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 mbedtls_mpi_free(&C);
1065 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (ret != 0 && ret >= -0x007f) {
1068 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001072}
1073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001075/**
1076 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1077 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001078 * \param dst buffer to mask
1079 * \param dlen length of destination buffer
1080 * \param src source of the mask generation
1081 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001082 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001084static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1085 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001086{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001087 unsigned char counter[4];
1088 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001089 unsigned int hlen;
1090 size_t i, use_len;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001091 unsigned char mask[MBEDTLS_HASH_MAX_SIZE];
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001092#if defined(MBEDTLS_MD_C)
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001093 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001094 const mbedtls_md_info_t *md_info;
1095 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 mbedtls_md_init(&md_ctx);
1098 md_info = mbedtls_md_info_from_type(md_alg);
1099 if (md_info == NULL) {
1100 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1101 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 mbedtls_md_init(&md_ctx);
1104 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001105 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001109#else
1110 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 psa_algorithm_t alg = mbedtls_psa_translate_md(md_alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001112 psa_status_t status = PSA_SUCCESS;
1113 size_t out_len;
1114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 hlen = PSA_HASH_LENGTH(alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001116#endif
1117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 memset(mask, 0, sizeof(mask));
1119 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001120
Simon Butcher02037452016-03-01 21:19:12 +00001121 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001122 p = dst;
1123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001125 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001127 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001129
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001130#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 }
1134 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 }
1137 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 }
1140 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 }
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001143#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if ((status = psa_hash_setup(&op, alg)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001145 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 }
1147 if ((status = psa_hash_update(&op, src, slen)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001148 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 }
1150 if ((status = psa_hash_update(&op, counter, 4)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 }
1153 status = psa_hash_finish(&op, mask, sizeof(mask), &out_len);
1154 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001155 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 }
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001157#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001160 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001162
1163 counter[3]++;
1164
1165 dlen -= use_len;
1166 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001167
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 mbedtls_platform_zeroize(mask, sizeof(mask));
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001170#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 return ret;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001174#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 psa_hash_abort(&op);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001176
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001177 return PSA_TO_MBEDTLS_ERR(status);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001178#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001179}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001180
1181/**
1182 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1183 *
1184 * \param hash the input hash
1185 * \param hlen length of the input hash
1186 * \param salt the input salt
1187 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001188 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001189 * \param md_alg message digest to use
1190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191static int hash_mprime(const unsigned char *hash, size_t hlen,
1192 const unsigned char *salt, size_t slen,
1193 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001194{
1195 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001196
1197#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001198 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001199 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1202 if (md_info == NULL) {
1203 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1204 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 mbedtls_md_init(&md_ctx);
1207 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001208 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 }
1210 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001211 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 }
1213 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001214 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 }
1216 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001217 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 }
1219 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 }
1222 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001223 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001225
1226exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 return ret;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001230#else
1231 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 psa_algorithm_t alg = mbedtls_psa_translate_md(md_alg);
1233 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1234 size_t out_size = PSA_HASH_LENGTH(alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001235 size_t out_len;
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if ((status = psa_hash_setup(&op, alg)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001238 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 }
1240 if ((status = psa_hash_update(&op, zeros, sizeof(zeros))) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001241 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 }
1243 if ((status = psa_hash_update(&op, hash, hlen)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001244 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 }
1246 if ((status = psa_hash_update(&op, salt, slen)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001247 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 }
1249 status = psa_hash_finish(&op, out, out_size, &out_len);
1250 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001251 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 }
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001253
1254exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 psa_hash_abort(&op);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001256
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001257 return PSA_TO_MBEDTLS_ERR(status);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001258#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001259}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001260
1261/**
1262 * Compute a hash.
1263 *
1264 * \param md_alg algorithm to use
1265 * \param input input message to hash
1266 * \param ilen input length
1267 * \param output the output buffer - must be large enough for \p md_alg
1268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269static int compute_hash(mbedtls_md_type_t md_alg,
1270 const unsigned char *input, size_t ilen,
1271 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001272{
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001273#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001274 const mbedtls_md_info_t *md_info;
1275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 md_info = mbedtls_md_info_from_type(md_alg);
1277 if (md_info == NULL) {
1278 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1279 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001282#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 psa_algorithm_t alg = mbedtls_psa_translate_md(md_alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001284 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 size_t out_size = PSA_HASH_LENGTH(alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001286 size_t out_len;
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 status = psa_hash_compute(alg, input, ilen, output, out_size, &out_len);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001289
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001290 return PSA_TO_MBEDTLS_ERR(status);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001291#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001296/*
1297 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1298 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001299int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1300 int (*f_rng)(void *, unsigned char *, size_t),
1301 void *p_rng,
1302 const unsigned char *label, size_t label_len,
1303 size_t ilen,
1304 const unsigned char *input,
1305 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001306{
1307 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001308 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001309 unsigned char *p = output;
1310 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (f_rng == NULL) {
1313 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1314 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1317 if (hlen == 0) {
1318 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1319 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001320
1321 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001322
Simon Butcher02037452016-03-01 21:19:12 +00001323 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1325 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1326 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001329
1330 *p++ = 0;
1331
Simon Butcher02037452016-03-01 21:19:12 +00001332 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1334 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1335 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001336
1337 p += hlen;
1338
Simon Butcher02037452016-03-01 21:19:12 +00001339 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1341 if (ret != 0) {
1342 return ret;
1343 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001344 p += hlen;
1345 p += olen - 2 * hlen - 2 - ilen;
1346 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (ilen != 0) {
1348 memcpy(p, input, ilen);
1349 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001350
Simon Butcher02037452016-03-01 21:19:12 +00001351 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1353 ctx->hash_id)) != 0) {
1354 return ret;
1355 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001356
Simon Butcher02037452016-03-01 21:19:12 +00001357 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1359 ctx->hash_id)) != 0) {
1360 return ret;
1361 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001368/*
1369 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1370 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001371int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1372 int (*f_rng)(void *, unsigned char *, size_t),
1373 void *p_rng, size_t ilen,
1374 const unsigned char *input,
1375 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001376{
1377 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001379 unsigned char *p = output;
1380
Paul Bakkerb3869132013-02-28 17:21:01 +01001381 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001382
Simon Butcher02037452016-03-01 21:19:12 +00001383 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if (ilen + 11 < ilen || olen < ilen + 11) {
1385 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1386 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001387
1388 nb_pad = olen - 3 - ilen;
1389
1390 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 if (f_rng == NULL) {
1393 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1394 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001395
1396 *p++ = MBEDTLS_RSA_CRYPT;
1397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001399 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001400
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001401 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 ret = f_rng(p_rng, p, 1);
1403 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001404
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001405 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if (rng_dl == 0 || ret != 0) {
1407 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1408 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001409
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001410 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001411 }
1412
1413 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if (ilen != 0) {
1415 memcpy(p, input, ilen);
1416 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001419}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001421
Paul Bakker5121ce52009-01-03 21:22:43 +00001422/*
1423 * Add the message padding, then do an RSA operation
1424 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001425int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1426 int (*f_rng)(void *, unsigned char *, size_t),
1427 void *p_rng,
1428 size_t ilen,
1429 const unsigned char *input,
1430 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001431{
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_PKCS1_V15)
1434 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1436 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001437#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_PKCS1_V21)
1440 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1442 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001443#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001444
1445 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001448}
1449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001451/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001452 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001454int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1455 int (*f_rng)(void *, unsigned char *, size_t),
1456 void *p_rng,
1457 const unsigned char *label, size_t label_len,
1458 size_t *olen,
1459 const unsigned char *input,
1460 unsigned char *output,
1461 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001462{
Janos Follath24eed8d2019-11-22 13:21:35 +00001463 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001464 size_t ilen, i, pad_len;
1465 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001467 unsigned char lhash[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001468 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001469
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001470 /*
1471 * Parameters sanity checks
1472 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1474 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001476
1477 ilen = ctx->len;
1478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (ilen < 16 || ilen > sizeof(buf)) {
1480 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1484 if (hlen == 0) {
1485 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1486 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001487
Janos Follathc17cda12016-02-11 11:08:18 +00001488 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (2 * hlen + 2 > ilen) {
1490 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1491 }
Janos Follathc17cda12016-02-11 11:08:18 +00001492
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001493 /*
1494 * RSA operation
1495 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001499 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001501
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001502 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001503 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001504 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001505 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1507 ctx->hash_id)) != 0 ||
1508 /* DB: Apply dbMask to maskedDB */
1509 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1510 ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001511 goto cleanup;
1512 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001513
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001514 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1516 label, label_len, lhash);
1517 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001518 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001520
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001521 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001522 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001523 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001524 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001525 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001526
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001527 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001528
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001529 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001530
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001531 /* Check lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 for (i = 0; i < hlen; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001533 bad |= lhash[i] ^ *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001535
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001536 /* Get zero-padding len, but always read till end of buffer
1537 * (minus one, for the 01 byte) */
1538 pad_len = 0;
1539 pad_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001541 pad_done |= p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001543 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001544
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001545 p += pad_len;
1546 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001547
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001548 /*
1549 * The only information "leaked" is whether the padding was correct or not
1550 * (eg, no data is copied if it was not correct). This meets the
1551 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1552 * the different error conditions.
1553 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001555 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1556 goto cleanup;
1557 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001560 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1561 goto cleanup;
1562 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001563
1564 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (*olen != 0) {
1566 memcpy(output, p, *olen);
1567 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001568 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001569
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001570cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 mbedtls_platform_zeroize(buf, sizeof(buf));
1572 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001579/*
1580 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1581 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001582int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1583 int (*f_rng)(void *, unsigned char *, size_t),
1584 void *p_rng,
1585 size_t *olen,
1586 const unsigned char *input,
1587 unsigned char *output,
1588 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001589{
1590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1591 size_t ilen;
1592 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1593
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001594 ilen = ctx->len;
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1597 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1598 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (ilen < 16 || ilen > sizeof(buf)) {
1601 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1602 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001607 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1611 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001612
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001613cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001617}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001619
1620/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001621 * Do an RSA operation, then remove the message padding
1622 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1624 int (*f_rng)(void *, unsigned char *, size_t),
1625 void *p_rng,
1626 size_t *olen,
1627 const unsigned char *input,
1628 unsigned char *output,
1629 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001630{
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632#if defined(MBEDTLS_PKCS1_V15)
1633 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1635 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001636#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_PKCS1_V21)
1639 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1641 olen, input, output,
1642 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001643#endif
1644
1645 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001647 }
1648}
1649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001651static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1652 int (*f_rng)(void *, unsigned char *, size_t),
1653 void *p_rng,
1654 mbedtls_md_type_t md_alg,
1655 unsigned int hashlen,
1656 const unsigned char *hash,
1657 int saltlen,
1658 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001659{
1660 size_t olen;
1661 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001662 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001663 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001665 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001668 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1672 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1673 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 if (f_rng == NULL) {
1676 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1677 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001678
1679 olen = ctx->len;
1680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001682 /* Gather length of hash to sign */
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 size_t exp_hashlen = mbedtls_hash_info_get_size(md_alg);
1684 if (exp_hashlen == 0) {
1685 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1686 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 if (hashlen != exp_hashlen) {
1689 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1690 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001691 }
1692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1694 if (hlen == 0) {
1695 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1696 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1699 /* Calculate the largest possible salt length, up to the hash size.
1700 * Normally this is the hash length, which is the maximum salt length
1701 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1702 * enough room, use the maximum salt length that fits. The constraint is
1703 * that the hash length plus the salt length plus 2 bytes must be at most
1704 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1705 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001706 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 if (olen < hlen + min_slen + 2) {
1708 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1709 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001710 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001712 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 }
1714 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1715 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1716 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001717 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001718 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001721
Simon Butcher02037452016-03-01 21:19:12 +00001722 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001724 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001725 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001726
1727 /* Generate salt of length slen in place in the encoded message */
1728 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1730 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1731 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001732
Paul Bakkerb3869132013-02-28 17:21:01 +01001733 p += slen;
1734
Simon Butcher02037452016-03-01 21:19:12 +00001735 /* Generate H = Hash( M' ) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 ret = hash_mprime(hash, hashlen, salt, slen, p, ctx->hash_id);
1737 if (ret != 0) {
1738 return ret;
1739 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001740
Simon Butcher02037452016-03-01 21:19:12 +00001741 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001743 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001745
Simon Butcher02037452016-03-01 21:19:12 +00001746 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
1748 ctx->hash_id);
1749 if (ret != 0) {
1750 return ret;
1751 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1754 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001755
1756 p += hlen;
1757 *p++ = 0xBC;
1758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001760}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001761
1762/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001763 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1764 * the option to pass in the salt length.
1765 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001766int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1767 int (*f_rng)(void *, unsigned char *, size_t),
1768 void *p_rng,
1769 mbedtls_md_type_t md_alg,
1770 unsigned int hashlen,
1771 const unsigned char *hash,
1772 int saltlen,
1773 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001774{
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1776 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001777}
1778
1779
1780/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001781 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1782 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001783int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1784 int (*f_rng)(void *, unsigned char *, size_t),
1785 void *p_rng,
1786 mbedtls_md_type_t md_alg,
1787 unsigned int hashlen,
1788 const unsigned char *hash,
1789 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001790{
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1792 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001793}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001797/*
1798 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1799 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001800
1801/* Construct a PKCS v1.5 encoding of a hashed message
1802 *
1803 * This is used both for signature generation and verification.
1804 *
1805 * Parameters:
1806 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001807 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001808 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001809 * - hash: Buffer containing the hashed message or the raw data.
1810 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001811 * - dst: Buffer to hold the encoded message.
1812 *
1813 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001814 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001815 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001816 *
1817 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001818static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1819 unsigned int hashlen,
1820 const unsigned char *hash,
1821 size_t dst_len,
1822 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001823{
1824 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001825 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001826 unsigned char *p = dst;
1827 const char *oid = NULL;
1828
1829 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 if (md_alg != MBEDTLS_MD_NONE) {
1831 unsigned char md_size = mbedtls_hash_info_get_size(md_alg);
1832 if (md_size == 0) {
1833 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1834 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1837 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1838 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if (hashlen != md_size) {
1841 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1842 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001843
1844 /* Double-check that 8 + hashlen + oid_size can be used as a
1845 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001847 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 10 + hashlen + oid_size < 10 + hashlen) {
1849 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1850 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001851
1852 /*
1853 * Static bounds check:
1854 * - Need 10 bytes for five tag-length pairs.
1855 * (Insist on 1-byte length encodings to protect against variants of
1856 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1857 * - Need hashlen bytes for hash
1858 * - Need oid_size bytes for hash alg OID.
1859 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (nb_pad < 10 + hashlen + oid_size) {
1861 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1862 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001863 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 } else {
1865 if (nb_pad < hashlen) {
1866 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1867 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001868
1869 nb_pad -= hashlen;
1870 }
1871
Hanno Becker2b2f8982017-09-27 17:10:03 +01001872 /* Need space for signature header and padding delimiter (3 bytes),
1873 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 if (nb_pad < 3 + 8) {
1875 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1876 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001877 nb_pad -= 3;
1878
1879 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001880 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001881
1882 /* Write signature header and padding */
1883 *p++ = 0;
1884 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001886 p += nb_pad;
1887 *p++ = 0;
1888
1889 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 if (md_alg == MBEDTLS_MD_NONE) {
1891 memcpy(p, hash, hashlen);
1892 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001893 }
1894
1895 /* Signing hashed data, add corresponding ASN.1 structure
1896 *
1897 * DigestInfo ::= SEQUENCE {
1898 * digestAlgorithm DigestAlgorithmIdentifier,
1899 * digest Digest }
1900 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1901 * Digest ::= OCTET STRING
1902 *
1903 * Schematic:
1904 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1905 * TAG-NULL + LEN [ NULL ] ]
1906 * TAG-OCTET + LEN [ HASH ] ]
1907 */
1908 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001910 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001912 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001913 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001915 p += oid_size;
1916 *p++ = MBEDTLS_ASN1_NULL;
1917 *p++ = 0x00;
1918 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001919 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001921 p += hashlen;
1922
1923 /* Just a sanity-check, should be automatic
1924 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 if (p != dst + dst_len) {
1926 mbedtls_platform_zeroize(dst, dst_len);
1927 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001928 }
1929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001931}
1932
Paul Bakkerb3869132013-02-28 17:21:01 +01001933/*
1934 * Do an RSA operation to sign the message digest
1935 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001936int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
1937 int (*f_rng)(void *, unsigned char *, size_t),
1938 void *p_rng,
1939 mbedtls_md_type_t md_alg,
1940 unsigned int hashlen,
1941 const unsigned char *hash,
1942 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001943{
Janos Follath24eed8d2019-11-22 13:21:35 +00001944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001945 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001948 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1952 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1953 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001954
Hanno Beckerfdf38032017-09-06 12:35:55 +01001955 /*
1956 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1957 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
1960 ctx->len, sig)) != 0) {
1961 return ret;
1962 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001963
Hanno Beckerfdf38032017-09-06 12:35:55 +01001964 /* Private key operation
1965 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001966 * In order to prevent Lenstra's attack, make the signature in a
1967 * temporary buffer and check it before returning it.
1968 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 sig_try = mbedtls_calloc(1, ctx->len);
1971 if (sig_try == NULL) {
1972 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00001973 }
1974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 verif = mbedtls_calloc(1, ctx->len);
1976 if (verif == NULL) {
1977 mbedtls_free(sig_try);
1978 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
1979 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
1982 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
1983
1984 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001985 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1986 goto cleanup;
1987 }
1988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001990
1991cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 mbedtls_platform_zeroize(sig_try, ctx->len);
1993 mbedtls_platform_zeroize(verif, ctx->len);
1994 mbedtls_free(sig_try);
1995 mbedtls_free(verif);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 if (ret != 0) {
1998 memset(sig, '!', ctx->len);
1999 }
2000 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002003
2004/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 * Do an RSA operation to sign the message digest
2006 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002007int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2008 int (*f_rng)(void *, unsigned char *, size_t),
2009 void *p_rng,
2010 mbedtls_md_type_t md_alg,
2011 unsigned int hashlen,
2012 const unsigned char *hash,
2013 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002014{
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002016 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020#if defined(MBEDTLS_PKCS1_V15)
2021 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2023 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002024#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026#if defined(MBEDTLS_PKCS1_V21)
2027 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2029 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002030#endif
2031
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002035}
2036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002038/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002039 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002041int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2042 mbedtls_md_type_t md_alg,
2043 unsigned int hashlen,
2044 const unsigned char *hash,
2045 mbedtls_md_type_t mgf1_hash_id,
2046 int expected_salt_len,
2047 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002048{
Janos Follath24eed8d2019-11-22 13:21:35 +00002049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002050 size_t siglen;
2051 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002052 unsigned char *hash_start;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002053 unsigned char result[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002054 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002055 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002059 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002061
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 siglen = ctx->len;
2063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (siglen < 16 || siglen > sizeof(buf)) {
2065 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2066 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 if (ret != 0) {
2071 return ret;
2072 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002073
2074 p = buf;
2075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 if (buf[siglen - 1] != 0xBC) {
2077 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002078 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (md_alg != MBEDTLS_MD_NONE) {
2081 /* Gather length of hash to sign */
2082 size_t exp_hashlen = mbedtls_hash_info_get_size(md_alg);
2083 if (exp_hashlen == 0) {
2084 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2085 }
2086
2087 if (hashlen != exp_hashlen) {
2088 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2089 }
2090 }
2091
2092 hlen = mbedtls_hash_info_get_size(mgf1_hash_id);
2093 if (hlen == 0) {
2094 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2095 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002096
Simon Butcher02037452016-03-01 21:19:12 +00002097 /*
2098 * Note: EMSA-PSS verification is over the length of N - 1 bits
2099 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 if (buf[0] >> (8 - siglen * 8 + msb)) {
2103 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2104 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002105
Simon Butcher02037452016-03-01 21:19:12 +00002106 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002108 p++;
2109 siglen -= 1;
2110 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 if (siglen < hlen + 2) {
2113 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2114 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002115 hash_start = p + siglen - hlen - 1;
2116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2118 if (ret != 0) {
2119 return ret;
2120 }
Paul Bakker02303e82013-01-03 11:08:31 +01002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002125 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if (*p++ != 0x01) {
2129 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2130 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002131
Gilles Peskine6a54b022017-10-17 19:02:13 +02002132 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2135 observed_salt_len != (size_t) expected_salt_len) {
2136 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002137 }
2138
Simon Butcher02037452016-03-01 21:19:12 +00002139 /*
2140 * Generate H = Hash( M' )
2141 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2143 result, mgf1_hash_id);
2144 if (ret != 0) {
2145 return ret;
2146 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if (memcmp(hash_start, result, hlen) != 0) {
2149 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2150 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002153}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002154
2155/*
2156 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2157 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002158int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2159 mbedtls_md_type_t md_alg,
2160 unsigned int hashlen,
2161 const unsigned char *hash,
2162 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002163{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002164 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002166 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002171 : md_alg;
2172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2174 md_alg, hashlen, hash,
2175 mgf1_hash_id,
2176 MBEDTLS_RSA_SALT_LEN_ANY,
2177 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002178
2179}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002183/*
2184 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2185 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2187 mbedtls_md_type_t md_alg,
2188 unsigned int hashlen,
2189 const unsigned char *hash,
2190 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002191{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002192 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002193 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002194 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002197 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002199
2200 sig_len = ctx->len;
2201
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002202 /*
2203 * Prepare expected PKCS1 v1.5 encoding of hash.
2204 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2207 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002208 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2209 goto cleanup;
2210 }
2211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2213 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002214 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002216
2217 /*
2218 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2219 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 ret = mbedtls_rsa_public(ctx, sig, encoded);
2222 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002223 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002225
Simon Butcher02037452016-03-01 21:19:12 +00002226 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002227 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002228 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2231 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002232 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2233 goto cleanup;
2234 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002235
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002236cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 if (encoded != NULL) {
2239 mbedtls_platform_zeroize(encoded, sig_len);
2240 mbedtls_free(encoded);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002241 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 if (encoded_expected != NULL) {
2244 mbedtls_platform_zeroize(encoded_expected, sig_len);
2245 mbedtls_free(encoded_expected);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002246 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002251
2252/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002253 * Do an RSA operation and check the message digest
2254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002255int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2256 mbedtls_md_type_t md_alg,
2257 unsigned int hashlen,
2258 const unsigned char *hash,
2259 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002260{
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002262 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266#if defined(MBEDTLS_PKCS1_V15)
2267 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2269 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002270#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272#if defined(MBEDTLS_PKCS1_V21)
2273 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2275 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002276#endif
2277
2278 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002280 }
2281}
2282
2283/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002284 * Copy the components of an RSA key
2285 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002286int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002287{
Janos Follath24eed8d2019-11-22 13:21:35 +00002288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002289
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002290 dst->len = src->len;
2291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2293 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2296 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2297 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002298
2299#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2301 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2302 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2303 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2304 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002305#endif
2306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2310 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002311
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002312 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002313 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002314
2315cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (ret != 0) {
2317 mbedtls_rsa_free(dst);
2318 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002321}
2322
2323/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002324 * Free the components of an RSA key
2325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002327{
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002329 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 mbedtls_mpi_free(&ctx->Vi);
2333 mbedtls_mpi_free(&ctx->Vf);
2334 mbedtls_mpi_free(&ctx->RN);
2335 mbedtls_mpi_free(&ctx->D);
2336 mbedtls_mpi_free(&ctx->Q);
2337 mbedtls_mpi_free(&ctx->P);
2338 mbedtls_mpi_free(&ctx->E);
2339 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002340
Hanno Becker33c30a02017-08-23 07:00:22 +01002341#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 mbedtls_mpi_free(&ctx->RQ);
2343 mbedtls_mpi_free(&ctx->RP);
2344 mbedtls_mpi_free(&ctx->QP);
2345 mbedtls_mpi_free(&ctx->DQ);
2346 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002347#endif /* MBEDTLS_RSA_NO_CRT */
2348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002350 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 if (ctx->ver != 0) {
2352 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002353 ctx->ver = 0;
2354 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002355#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002356}
2357
Hanno Beckerab377312017-08-23 16:24:51 +01002358#endif /* !MBEDTLS_RSA_ALT */
2359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002361
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002362#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
2364/*
2365 * Example RSA-1024 keypair, for test purposes
2366 */
2367#define KEY_LEN 128
2368
2369#define RSA_N "9292758453063D803DD603D5E777D788" \
2370 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2371 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2372 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2373 "93A89813FBF3C4F8066D2D800F7C38A8" \
2374 "1AE31942917403FF4946B0A83D3D3E05" \
2375 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2376 "5E94BB77B07507233A0BC7BAC8F90F79"
2377
2378#define RSA_E "10001"
2379
2380#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2381 "66CA472BC44D253102F8B4A9D3BFA750" \
2382 "91386C0077937FE33FA3252D28855837" \
2383 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2384 "DF79C5CE07EE72C7F123142198164234" \
2385 "CABB724CF78B8173B9F880FC86322407" \
2386 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2387 "071513A1E85B5DFA031F21ECAE91A34D"
2388
2389#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2390 "2C01CAD19EA484A87EA4377637E75500" \
2391 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2392 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2393
2394#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2395 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2396 "910E4168387E3C30AA1E00C339A79508" \
2397 "8452DD96A9A5EA5D9DCA68DA636032AF"
2398
Paul Bakker5121ce52009-01-03 21:22:43 +00002399#define PT_LEN 24
2400#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2401 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002404static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002405{
gufe44c2620da2020-08-03 17:56:50 +02002406#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002407 size_t i;
2408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002410 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 }
Paul Bakker545570e2010-07-18 09:00:25 +00002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002414 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002416#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002418 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002422#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002427
Paul Bakker5121ce52009-01-03 21:22:43 +00002428/*
2429 * Checkup routine
2430 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002431int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002432{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002433 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002435 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 unsigned char rsa_plaintext[PT_LEN];
2438 unsigned char rsa_decrypted[PT_LEN];
2439 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002441 unsigned char sha1sum[20];
2442#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
Hanno Becker3a701162017-08-22 13:52:43 +01002444 mbedtls_mpi K;
2445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 mbedtls_mpi_init(&K);
2447 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2450 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2451 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2452 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2453 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2454 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2455 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2456 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2457 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2458 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (verbose != 0) {
2463 mbedtls_printf(" RSA key validation: ");
2464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2467 mbedtls_rsa_check_privkey(&rsa) != 0) {
2468 if (verbose != 0) {
2469 mbedtls_printf("failed\n");
2470 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
Hanno Becker5bc87292017-05-03 15:09:31 +01002472 ret = 1;
2473 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 }
2475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 if (verbose != 0) {
2477 mbedtls_printf("passed\n PKCS#1 encryption : ");
2478 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2483 PT_LEN, rsa_plaintext,
2484 rsa_ciphertext) != 0) {
2485 if (verbose != 0) {
2486 mbedtls_printf("failed\n");
2487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
Hanno Becker5bc87292017-05-03 15:09:31 +01002489 ret = 1;
2490 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 }
2492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (verbose != 0) {
2494 mbedtls_printf("passed\n PKCS#1 decryption : ");
2495 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2498 &len, rsa_ciphertext, rsa_decrypted,
2499 sizeof(rsa_decrypted)) != 0) {
2500 if (verbose != 0) {
2501 mbedtls_printf("failed\n");
2502 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Hanno Becker5bc87292017-05-03 15:09:31 +01002504 ret = 1;
2505 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 }
2507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2509 if (verbose != 0) {
2510 mbedtls_printf("failed\n");
2511 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Hanno Becker5bc87292017-05-03 15:09:31 +01002513 ret = 1;
2514 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 }
2516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 if (verbose != 0) {
2518 mbedtls_printf("passed\n");
2519 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 if (verbose != 0) {
2523 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002524 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002525
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002526 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2527 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 if (verbose != 0) {
2529 mbedtls_printf("failed\n");
2530 }
2531
2532 return 1;
2533 }
2534
2535 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2536 MBEDTLS_MD_SHA1, 20,
2537 sha1sum, rsa_ciphertext) != 0) {
2538 if (verbose != 0) {
2539 mbedtls_printf("failed\n");
2540 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
Hanno Becker5bc87292017-05-03 15:09:31 +01002542 ret = 1;
2543 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 }
2545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (verbose != 0) {
2547 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2551 sha1sum, rsa_ciphertext) != 0) {
2552 if (verbose != 0) {
2553 mbedtls_printf("failed\n");
2554 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Hanno Becker5bc87292017-05-03 15:09:31 +01002556 ret = 1;
2557 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 }
2559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (verbose != 0) {
2561 mbedtls_printf("passed\n");
2562 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (verbose != 0) {
2566 mbedtls_printf("\n");
2567 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002568
Paul Bakker3d8fb632014-04-17 12:42:41 +02002569cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 mbedtls_mpi_free(&K);
2571 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002573 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002576}
2577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#endif /* MBEDTLS_RSA_C */