blob: 584b363cb0b5b1ed590720bf03a9bc7500b37051 [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/*
506 * Get length in bytes of RSA modulus
507 */
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100510{
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100512}
513
514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
517/*
518 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800519 *
520 * This generation method follows the RSA key pair generation procedure of
521 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100523int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
524 int (*f_rng)(void *, unsigned char *, size_t),
525 void *p_rng,
526 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000527{
Janos Follath24eed8d2019-11-22 13:21:35 +0000528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800529 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100530 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Janos Follathb8fc1b02018-09-03 15:37:01 +0100532 /*
533 * If the modulus is 1024 bit long or shorter, then the security strength of
534 * the RSA algorithm is less than or equal to 80 bits and therefore an error
535 * rate of 2^-80 is sufficient.
536 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100538 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 mbedtls_mpi_init(&H);
542 mbedtls_mpi_init(&G);
543 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (nbits < 128 || exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100546 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
547 goto cleanup;
548 }
549
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 /*
551 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800552 * 1. |P-Q| > 2^( nbits / 2 - 100 )
553 * 2. GCD( E, (P-1)*(Q-1) ) == 1
554 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 do {
559 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
560 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
563 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800565 /* 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 +0100566 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
567 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800571 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (H.s < 0) {
573 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
574 }
Janos Follathef441782016-09-21 13:18:12 +0100575
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100576 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
578 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
579 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800580
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800581 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
583 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800584 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800586
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800587 /* 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 +0100588 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
589 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
590 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 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 -0800593 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800595
596 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100599 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
601 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100606
Jethro Beekman97f95c92018-02-13 15:50:36 -0800607#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 * DP = D mod (P - 1)
610 * DQ = D mod (Q - 1)
611 * QP = Q^-1 mod P
612 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
614 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100615#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
Hanno Becker83aad1f2017-08-23 06:45:10 +0100617 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620cleanup:
621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 mbedtls_mpi_free(&H);
623 mbedtls_mpi_free(&G);
624 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (ret != 0) {
627 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if ((-ret & ~0x7f) == 0) {
630 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
631 }
632 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 }
634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000636}
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640/*
641 * Check a public RSA key
642 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100643int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000644{
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
646 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100647 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
650 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100651 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
654 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
655 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
656 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
657 }
658
659 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000660}
661
662/*
Hanno Becker705fc682017-10-10 17:57:02 +0100663 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000666{
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
668 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
669 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 }
Paul Bakker48377d92013-08-30 12:06:24 +0200671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
673 &ctx->D, &ctx->E, NULL, NULL) != 0) {
674 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000676
Hanno Beckerb269a852017-08-25 08:03:21 +0100677#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
679 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
680 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100681 }
682#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000685}
686
687/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100688 * Check if contexts holding a public and private key match
689 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
691 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100692{
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
694 mbedtls_rsa_check_privkey(prv) != 0) {
695 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696 }
697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
699 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
700 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100701 }
702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100704}
705
706/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 * Do an RSA public key operation
708 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
710 const unsigned char *input,
711 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000712{
Janos Follath24eed8d2019-11-22 13:21:35 +0000713 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000714 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
718 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
719 }
Hanno Becker705fc682017-10-10 17:57:02 +0100720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200723#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
725 return ret;
726 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200727#endif
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200732 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
733 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 }
735
736 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
738 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
740cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
743 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
744 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100745#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (ret != 0) {
750 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
751 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000754}
755
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200756/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200757 * Generate or update blinding values, see section 10 of:
758 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200759 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200760 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200761 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
763 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200764{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200765 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200766 mbedtls_mpi R;
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200771 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
773 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
774 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
775 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200776
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200777 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200778 }
779
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200780 /* Unblinding value: Vf = random number, invertible mod N */
781 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200783 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
784 goto cleanup;
785 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 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 +0200788
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200789 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
791 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
792 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200793
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200794 /* At this point, Vi is invertible mod N if and only if both Vf and R
795 * are invertible mod N. If one of them isn't, we don't need to know
796 * which one, we just loop and choose new values for both of them.
797 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
799 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200800 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500804
805 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
807 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200808
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200809 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200810 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 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 +0200812
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200813
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200814cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200818}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200819
Paul Bakker5121ce52009-01-03 21:22:43 +0000820/*
Janos Follathe81102e2017-03-22 13:38:28 +0000821 * Exponent blinding supposed to prevent side-channel attacks using multiple
822 * traces of measurements to recover the RSA key. The more collisions are there,
823 * the more bits of the key can be recovered. See [3].
824 *
825 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800826 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000827 *
828 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800829 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000830 *
831 * (With the currently (as of 2017 April) known best algorithms breaking 2048
832 * bit RSA requires approximately as much time as trying out 2^112 random keys.
833 * Thus in this sense with 28 byte blinding the security is not reduced by
834 * side-channel attacks like the one in [3])
835 *
836 * This countermeasure does not help if the key recovery is possible with a
837 * single trace.
838 */
839#define RSA_EXPONENT_BLINDING 28
840
841/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 * Do an RSA private key operation
843 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
845 int (*f_rng)(void *, unsigned char *, size_t),
846 void *p_rng,
847 const unsigned char *input,
848 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000849{
Janos Follath24eed8d2019-11-22 13:21:35 +0000850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000851 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100852
853 /* Temporary holding the result */
854 mbedtls_mpi T;
855
856 /* Temporaries holding P-1, Q-1 and the
857 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000858 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100859
860#if !defined(MBEDTLS_RSA_NO_CRT)
861 /* Temporaries holding the results mod p resp. mod q. */
862 mbedtls_mpi TP, TQ;
863
864 /* Temporaries holding the blinded exponents for
865 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000866 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100867
868 /* Pointers to actual exponents to be used - either the unblinded
869 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000870 mbedtls_mpi *DP = &ctx->DP;
871 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100872#else
873 /* Temporary holding the blinded exponent (if used). */
874 mbedtls_mpi D_blind;
875
876 /* Pointer to actual exponent to be used - either the unblinded
877 * or the blinded one, depending on the presence of a PRNG. */
878 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100879#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100880
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100881 /* Temporaries holding the initial input and the double
882 * checked result; should be the same in the end. */
883 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (f_rng == NULL) {
886 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
887 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (rsa_check_context(ctx, 1 /* private key checks */,
890 1 /* blinding on */) != 0) {
891 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +0100892 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100893
Hanno Becker06811ce2017-05-03 15:10:34 +0100894#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
896 return ret;
897 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100898#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000899
Hanno Becker06811ce2017-05-03 15:10:34 +0100900 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +0100902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 mbedtls_mpi_init(&P1);
904 mbedtls_mpi_init(&Q1);
905 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +0000906
Janos Follathe81102e2017-03-22 13:38:28 +0000907#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +0000909#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 mbedtls_mpi_init(&DP_blind);
911 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +0000912#endif
913
Hanno Becker06811ce2017-05-03 15:10:34 +0100914#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200916#endif
917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 mbedtls_mpi_init(&I);
919 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +0100920
921 /* End of MPI initialization */
922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
924 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200925 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
926 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 }
928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +0100930
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200931 /*
932 * Blinding
933 * T = T * Vi mod N
934 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
936 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
937 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +0000938
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200939 /*
940 * Exponent blinding
941 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
943 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +0000944
Janos Follathf9203b42017-03-22 15:13:15 +0000945#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200946 /*
947 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
948 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
950 f_rng, p_rng));
951 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
952 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
953 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +0000954
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200955 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000956#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200957 /*
958 * DP_blind = ( P - 1 ) * R + DP
959 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
961 f_rng, p_rng));
962 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
963 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
964 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +0000965
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200966 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000967
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200968 /*
969 * DQ_blind = ( Q - 1 ) * R + DQ
970 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
972 f_rng, p_rng));
973 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
974 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
975 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +0000976
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200977 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000978#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100982#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200983 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000984 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000985 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100986 * TP = input ^ dP mod P
987 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
991 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
993 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100994 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
997 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
998 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +0000999
1000 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001001 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001002 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1004 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001006
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001007 /*
1008 * Unblind
1009 * T = T * Vf mod N
1010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1012 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
Hanno Becker2dec5e82017-10-03 07:49:52 +01001014 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1016 &ctx->N, &ctx->RN));
1017 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001018 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1019 goto cleanup;
1020 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001021
Paul Bakker5121ce52009-01-03 21:22:43 +00001022 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
1025cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1028 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1029 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001030#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 mbedtls_mpi_free(&P1);
1033 mbedtls_mpi_free(&Q1);
1034 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001035
Janos Follathe81102e2017-03-22 13:38:28 +00001036#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001038#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 mbedtls_mpi_free(&DP_blind);
1040 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001041#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001044
1045#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001047#endif
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 mbedtls_mpi_free(&C);
1050 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (ret != 0 && ret >= -0x007f) {
1053 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1054 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001057}
1058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001060/**
1061 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1062 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001063 * \param dst buffer to mask
1064 * \param dlen length of destination buffer
1065 * \param src source of the mask generation
1066 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001067 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1070 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001071{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001072 unsigned char counter[4];
1073 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001074 unsigned int hlen;
1075 size_t i, use_len;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001076 unsigned char mask[MBEDTLS_HASH_MAX_SIZE];
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001077#if defined(MBEDTLS_MD_C)
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001078 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001079 const mbedtls_md_info_t *md_info;
1080 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 mbedtls_md_init(&md_ctx);
1083 md_info = mbedtls_md_info_from_type(md_alg);
1084 if (md_info == NULL) {
1085 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1086 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 mbedtls_md_init(&md_ctx);
1089 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001090 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001094#else
1095 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 psa_algorithm_t alg = mbedtls_psa_translate_md(md_alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001097 psa_status_t status = PSA_SUCCESS;
1098 size_t out_len;
1099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 hlen = PSA_HASH_LENGTH(alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001101#endif
1102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 memset(mask, 0, sizeof(mask));
1104 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001105
Simon Butcher02037452016-03-01 21:19:12 +00001106 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001107 p = dst;
1108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001110 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001112 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001115#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 }
1119 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 }
1122 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 }
1125 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001126 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 }
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001128#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if ((status = psa_hash_setup(&op, alg)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 }
1132 if ((status = psa_hash_update(&op, src, slen)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 }
1135 if ((status = psa_hash_update(&op, counter, 4)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001136 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 }
1138 status = psa_hash_finish(&op, mask, sizeof(mask), &out_len);
1139 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 }
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001142#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001145 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001147
1148 counter[3]++;
1149
1150 dlen -= use_len;
1151 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001152
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001153exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 mbedtls_platform_zeroize(mask, sizeof(mask));
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001155#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 return ret;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001159#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 psa_hash_abort(&op);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001161
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001162 return PSA_TO_MBEDTLS_ERR(status);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001163#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001164}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001165
1166/**
1167 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1168 *
1169 * \param hash the input hash
1170 * \param hlen length of the input hash
1171 * \param salt the input salt
1172 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001173 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001174 * \param md_alg message digest to use
1175 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001176static int hash_mprime(const unsigned char *hash, size_t hlen,
1177 const unsigned char *salt, size_t slen,
1178 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001179{
1180 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001181
1182#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001183 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1187 if (md_info == NULL) {
1188 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1189 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 mbedtls_md_init(&md_ctx);
1192 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001193 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 }
1195 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001196 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 }
1198 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001199 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 }
1201 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001202 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 }
1204 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001205 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 }
1207 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001208 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001210
1211exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 return ret;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001215#else
1216 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 psa_algorithm_t alg = mbedtls_psa_translate_md(md_alg);
1218 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1219 size_t out_size = PSA_HASH_LENGTH(alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001220 size_t out_len;
1221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if ((status = psa_hash_setup(&op, alg)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001223 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 }
1225 if ((status = psa_hash_update(&op, zeros, sizeof(zeros))) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 }
1228 if ((status = psa_hash_update(&op, hash, hlen)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 }
1231 if ((status = psa_hash_update(&op, salt, slen)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001232 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 }
1234 status = psa_hash_finish(&op, out, out_size, &out_len);
1235 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 }
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001238
1239exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 psa_hash_abort(&op);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001241
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001242 return PSA_TO_MBEDTLS_ERR(status);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001243#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001244}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001245
1246/**
1247 * Compute a hash.
1248 *
1249 * \param md_alg algorithm to use
1250 * \param input input message to hash
1251 * \param ilen input length
1252 * \param output the output buffer - must be large enough for \p md_alg
1253 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001254static int compute_hash(mbedtls_md_type_t md_alg,
1255 const unsigned char *input, size_t ilen,
1256 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001257{
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001258#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001259 const mbedtls_md_info_t *md_info;
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 md_info = mbedtls_md_info_from_type(md_alg);
1262 if (md_info == NULL) {
1263 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1264 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001267#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 psa_algorithm_t alg = mbedtls_psa_translate_md(md_alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001269 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 size_t out_size = PSA_HASH_LENGTH(alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001271 size_t out_len;
1272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 status = psa_hash_compute(alg, input, ilen, output, out_size, &out_len);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001274
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001275 return PSA_TO_MBEDTLS_ERR(status);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001276#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001277}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001281/*
1282 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1283 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001284int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1285 int (*f_rng)(void *, unsigned char *, size_t),
1286 void *p_rng,
1287 const unsigned char *label, size_t label_len,
1288 size_t ilen,
1289 const unsigned char *input,
1290 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001291{
1292 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001294 unsigned char *p = output;
1295 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (f_rng == NULL) {
1298 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1299 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1302 if (hlen == 0) {
1303 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1304 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001305
1306 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001307
Simon Butcher02037452016-03-01 21:19:12 +00001308 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1310 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1311 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001314
1315 *p++ = 0;
1316
Simon Butcher02037452016-03-01 21:19:12 +00001317 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1319 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1320 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001321
1322 p += hlen;
1323
Simon Butcher02037452016-03-01 21:19:12 +00001324 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1326 if (ret != 0) {
1327 return ret;
1328 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001329 p += hlen;
1330 p += olen - 2 * hlen - 2 - ilen;
1331 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if (ilen != 0) {
1333 memcpy(p, input, ilen);
1334 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001335
Simon Butcher02037452016-03-01 21:19:12 +00001336 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1338 ctx->hash_id)) != 0) {
1339 return ret;
1340 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001341
Simon Butcher02037452016-03-01 21:19:12 +00001342 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1344 ctx->hash_id)) != 0) {
1345 return ret;
1346 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001349}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001353/*
1354 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1355 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001356int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1357 int (*f_rng)(void *, unsigned char *, size_t),
1358 void *p_rng, size_t ilen,
1359 const unsigned char *input,
1360 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001361{
1362 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001364 unsigned char *p = output;
1365
Paul Bakkerb3869132013-02-28 17:21:01 +01001366 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001367
Simon Butcher02037452016-03-01 21:19:12 +00001368 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (ilen + 11 < ilen || olen < ilen + 11) {
1370 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1371 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001372
1373 nb_pad = olen - 3 - ilen;
1374
1375 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 if (f_rng == NULL) {
1378 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1379 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001380
1381 *p++ = MBEDTLS_RSA_CRYPT;
1382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001384 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001385
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001386 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 ret = f_rng(p_rng, p, 1);
1388 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001389
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001390 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if (rng_dl == 0 || ret != 0) {
1392 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1393 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001394
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001395 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001396 }
1397
1398 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 if (ilen != 0) {
1400 memcpy(p, input, ilen);
1401 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001406
Paul Bakker5121ce52009-01-03 21:22:43 +00001407/*
1408 * Add the message padding, then do an RSA operation
1409 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001410int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1411 int (*f_rng)(void *, unsigned char *, size_t),
1412 void *p_rng,
1413 size_t ilen,
1414 const unsigned char *input,
1415 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001416{
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#if defined(MBEDTLS_PKCS1_V15)
1419 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1421 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001422#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#if defined(MBEDTLS_PKCS1_V21)
1425 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1427 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001428#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001429
1430 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001433}
1434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001436/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001437 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001439int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1440 int (*f_rng)(void *, unsigned char *, size_t),
1441 void *p_rng,
1442 const unsigned char *label, size_t label_len,
1443 size_t *olen,
1444 const unsigned char *input,
1445 unsigned char *output,
1446 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001447{
Janos Follath24eed8d2019-11-22 13:21:35 +00001448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001449 size_t ilen, i, pad_len;
1450 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001452 unsigned char lhash[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001453 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001454
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001455 /*
1456 * Parameters sanity checks
1457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1459 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001461
1462 ilen = ctx->len;
1463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 if (ilen < 16 || ilen > sizeof(buf)) {
1465 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1466 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1469 if (hlen == 0) {
1470 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1471 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001472
Janos Follathc17cda12016-02-11 11:08:18 +00001473 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if (2 * hlen + 2 > ilen) {
1475 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1476 }
Janos Follathc17cda12016-02-11 11:08:18 +00001477
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001478 /*
1479 * RSA operation
1480 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001484 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001486
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001487 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001488 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001489 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001490 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1492 ctx->hash_id)) != 0 ||
1493 /* DB: Apply dbMask to maskedDB */
1494 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1495 ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001496 goto cleanup;
1497 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001498
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001499 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1501 label, label_len, lhash);
1502 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001503 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001505
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001506 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001507 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001508 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001510 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001511
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001512 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001513
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001514 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001515
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001516 /* Check lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 for (i = 0; i < hlen; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001518 bad |= lhash[i] ^ *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001520
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001521 /* Get zero-padding len, but always read till end of buffer
1522 * (minus one, for the 01 byte) */
1523 pad_len = 0;
1524 pad_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001526 pad_done |= p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001528 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001529
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001530 p += pad_len;
1531 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001532
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001533 /*
1534 * The only information "leaked" is whether the padding was correct or not
1535 * (eg, no data is copied if it was not correct). This meets the
1536 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1537 * the different error conditions.
1538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001540 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1541 goto cleanup;
1542 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001545 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1546 goto cleanup;
1547 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001548
1549 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 if (*olen != 0) {
1551 memcpy(output, p, *olen);
1552 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001553 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001554
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001555cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 mbedtls_platform_zeroize(buf, sizeof(buf));
1557 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001560}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001564/*
1565 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1566 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001567int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1568 int (*f_rng)(void *, unsigned char *, size_t),
1569 void *p_rng,
1570 size_t *olen,
1571 const unsigned char *input,
1572 unsigned char *output,
1573 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001574{
1575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1576 size_t ilen;
1577 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1578
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001579 ilen = ctx->len;
1580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1582 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1583 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if (ilen < 16 || ilen > sizeof(buf)) {
1586 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1587 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001592 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1596 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001597
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001598cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
1605/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001606 * Do an RSA operation, then remove the message padding
1607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001608int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1609 int (*f_rng)(void *, unsigned char *, size_t),
1610 void *p_rng,
1611 size_t *olen,
1612 const unsigned char *input,
1613 unsigned char *output,
1614 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001615{
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617#if defined(MBEDTLS_PKCS1_V15)
1618 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1620 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001621#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623#if defined(MBEDTLS_PKCS1_V21)
1624 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1626 olen, input, output,
1627 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001628#endif
1629
1630 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001632 }
1633}
1634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001636static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1637 int (*f_rng)(void *, unsigned char *, size_t),
1638 void *p_rng,
1639 mbedtls_md_type_t md_alg,
1640 unsigned int hashlen,
1641 const unsigned char *hash,
1642 int saltlen,
1643 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001644{
1645 size_t olen;
1646 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001647 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001648 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001650 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001653 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1657 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1658 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 if (f_rng == NULL) {
1661 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1662 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001663
1664 olen = ctx->len;
1665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001667 /* Gather length of hash to sign */
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 size_t exp_hashlen = mbedtls_hash_info_get_size(md_alg);
1669 if (exp_hashlen == 0) {
1670 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1671 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 if (hashlen != exp_hashlen) {
1674 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1675 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001676 }
1677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1679 if (hlen == 0) {
1680 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1681 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1684 /* Calculate the largest possible salt length, up to the hash size.
1685 * Normally this is the hash length, which is the maximum salt length
1686 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1687 * enough room, use the maximum salt length that fits. The constraint is
1688 * that the hash length plus the salt length plus 2 bytes must be at most
1689 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1690 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001691 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 if (olen < hlen + min_slen + 2) {
1693 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1694 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001695 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001697 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 }
1699 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1700 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1701 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001702 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001703 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001706
Simon Butcher02037452016-03-01 21:19:12 +00001707 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001709 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001710 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001711
1712 /* Generate salt of length slen in place in the encoded message */
1713 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1715 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1716 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001717
Paul Bakkerb3869132013-02-28 17:21:01 +01001718 p += slen;
1719
Simon Butcher02037452016-03-01 21:19:12 +00001720 /* Generate H = Hash( M' ) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 ret = hash_mprime(hash, hashlen, salt, slen, p, ctx->hash_id);
1722 if (ret != 0) {
1723 return ret;
1724 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001725
Simon Butcher02037452016-03-01 21:19:12 +00001726 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001728 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001730
Simon Butcher02037452016-03-01 21:19:12 +00001731 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
1733 ctx->hash_id);
1734 if (ret != 0) {
1735 return ret;
1736 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1739 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001740
1741 p += hlen;
1742 *p++ = 0xBC;
1743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001745}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001746
1747/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001748 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1749 * the option to pass in the salt length.
1750 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001751int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1752 int (*f_rng)(void *, unsigned char *, size_t),
1753 void *p_rng,
1754 mbedtls_md_type_t md_alg,
1755 unsigned int hashlen,
1756 const unsigned char *hash,
1757 int saltlen,
1758 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001759{
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1761 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001762}
1763
1764
1765/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001766 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1767 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001768int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1769 int (*f_rng)(void *, unsigned char *, size_t),
1770 void *p_rng,
1771 mbedtls_md_type_t md_alg,
1772 unsigned int hashlen,
1773 const unsigned char *hash,
1774 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001775{
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1777 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001778}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001782/*
1783 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1784 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001785
1786/* Construct a PKCS v1.5 encoding of a hashed message
1787 *
1788 * This is used both for signature generation and verification.
1789 *
1790 * Parameters:
1791 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001792 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001793 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001794 * - hash: Buffer containing the hashed message or the raw data.
1795 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001796 * - dst: Buffer to hold the encoded message.
1797 *
1798 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001799 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001800 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001801 *
1802 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001803static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1804 unsigned int hashlen,
1805 const unsigned char *hash,
1806 size_t dst_len,
1807 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001808{
1809 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001810 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001811 unsigned char *p = dst;
1812 const char *oid = NULL;
1813
1814 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 if (md_alg != MBEDTLS_MD_NONE) {
1816 unsigned char md_size = mbedtls_hash_info_get_size(md_alg);
1817 if (md_size == 0) {
1818 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1819 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001820
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1822 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1823 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 if (hashlen != md_size) {
1826 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1827 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001828
1829 /* Double-check that 8 + hashlen + oid_size can be used as a
1830 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001832 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 10 + hashlen + oid_size < 10 + hashlen) {
1834 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1835 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001836
1837 /*
1838 * Static bounds check:
1839 * - Need 10 bytes for five tag-length pairs.
1840 * (Insist on 1-byte length encodings to protect against variants of
1841 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1842 * - Need hashlen bytes for hash
1843 * - Need oid_size bytes for hash alg OID.
1844 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (nb_pad < 10 + hashlen + oid_size) {
1846 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1847 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001848 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 } else {
1850 if (nb_pad < hashlen) {
1851 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1852 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001853
1854 nb_pad -= hashlen;
1855 }
1856
Hanno Becker2b2f8982017-09-27 17:10:03 +01001857 /* Need space for signature header and padding delimiter (3 bytes),
1858 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 if (nb_pad < 3 + 8) {
1860 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1861 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001862 nb_pad -= 3;
1863
1864 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001865 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001866
1867 /* Write signature header and padding */
1868 *p++ = 0;
1869 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001871 p += nb_pad;
1872 *p++ = 0;
1873
1874 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if (md_alg == MBEDTLS_MD_NONE) {
1876 memcpy(p, hash, hashlen);
1877 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001878 }
1879
1880 /* Signing hashed data, add corresponding ASN.1 structure
1881 *
1882 * DigestInfo ::= SEQUENCE {
1883 * digestAlgorithm DigestAlgorithmIdentifier,
1884 * digest Digest }
1885 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1886 * Digest ::= OCTET STRING
1887 *
1888 * Schematic:
1889 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1890 * TAG-NULL + LEN [ NULL ] ]
1891 * TAG-OCTET + LEN [ HASH ] ]
1892 */
1893 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001895 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001897 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001898 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001900 p += oid_size;
1901 *p++ = MBEDTLS_ASN1_NULL;
1902 *p++ = 0x00;
1903 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001904 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001906 p += hashlen;
1907
1908 /* Just a sanity-check, should be automatic
1909 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 if (p != dst + dst_len) {
1911 mbedtls_platform_zeroize(dst, dst_len);
1912 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001913 }
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001916}
1917
Paul Bakkerb3869132013-02-28 17:21:01 +01001918/*
1919 * Do an RSA operation to sign the message digest
1920 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001921int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
1922 int (*f_rng)(void *, unsigned char *, size_t),
1923 void *p_rng,
1924 mbedtls_md_type_t md_alg,
1925 unsigned int hashlen,
1926 const unsigned char *hash,
1927 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001928{
Janos Follath24eed8d2019-11-22 13:21:35 +00001929 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001930 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001933 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1937 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1938 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001939
Hanno Beckerfdf38032017-09-06 12:35:55 +01001940 /*
1941 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1942 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
1945 ctx->len, sig)) != 0) {
1946 return ret;
1947 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001948
Hanno Beckerfdf38032017-09-06 12:35:55 +01001949 /* Private key operation
1950 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001951 * In order to prevent Lenstra's attack, make the signature in a
1952 * temporary buffer and check it before returning it.
1953 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 sig_try = mbedtls_calloc(1, ctx->len);
1956 if (sig_try == NULL) {
1957 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00001958 }
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 verif = mbedtls_calloc(1, ctx->len);
1961 if (verif == NULL) {
1962 mbedtls_free(sig_try);
1963 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
1964 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
1967 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
1968
1969 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001970 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1971 goto cleanup;
1972 }
1973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001975
1976cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 mbedtls_platform_zeroize(sig_try, ctx->len);
1978 mbedtls_platform_zeroize(verif, ctx->len);
1979 mbedtls_free(sig_try);
1980 mbedtls_free(verif);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 if (ret != 0) {
1983 memset(sig, '!', ctx->len);
1984 }
1985 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001986}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001988
1989/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 * Do an RSA operation to sign the message digest
1991 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001992int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
1993 int (*f_rng)(void *, unsigned char *, size_t),
1994 void *p_rng,
1995 mbedtls_md_type_t md_alg,
1996 unsigned int hashlen,
1997 const unsigned char *hash,
1998 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00001999{
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002001 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#if defined(MBEDTLS_PKCS1_V15)
2006 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2008 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002009#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011#if defined(MBEDTLS_PKCS1_V21)
2012 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2014 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002015#endif
2016
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002020}
2021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002023/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002024 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002026int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2027 mbedtls_md_type_t md_alg,
2028 unsigned int hashlen,
2029 const unsigned char *hash,
2030 mbedtls_md_type_t mgf1_hash_id,
2031 int expected_salt_len,
2032 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002033{
Janos Follath24eed8d2019-11-22 13:21:35 +00002034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002035 size_t siglen;
2036 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002037 unsigned char *hash_start;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002038 unsigned char result[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002039 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002040 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002044 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002046
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 siglen = ctx->len;
2048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (siglen < 16 || siglen > sizeof(buf)) {
2050 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2051 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 if (ret != 0) {
2056 return ret;
2057 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
2059 p = buf;
2060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (buf[siglen - 1] != 0xBC) {
2062 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002063 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 if (md_alg != MBEDTLS_MD_NONE) {
2066 /* Gather length of hash to sign */
2067 size_t exp_hashlen = mbedtls_hash_info_get_size(md_alg);
2068 if (exp_hashlen == 0) {
2069 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2070 }
2071
2072 if (hashlen != exp_hashlen) {
2073 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2074 }
2075 }
2076
2077 hlen = mbedtls_hash_info_get_size(mgf1_hash_id);
2078 if (hlen == 0) {
2079 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2080 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002081
Simon Butcher02037452016-03-01 21:19:12 +00002082 /*
2083 * Note: EMSA-PSS verification is over the length of N - 1 bits
2084 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (buf[0] >> (8 - siglen * 8 + msb)) {
2088 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2089 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002090
Simon Butcher02037452016-03-01 21:19:12 +00002091 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002093 p++;
2094 siglen -= 1;
2095 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 if (siglen < hlen + 2) {
2098 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2099 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002100 hash_start = p + siglen - hlen - 1;
2101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2103 if (ret != 0) {
2104 return ret;
2105 }
Paul Bakker02303e82013-01-03 11:08:31 +01002106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002110 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (*p++ != 0x01) {
2114 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2115 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002116
Gilles Peskine6a54b022017-10-17 19:02:13 +02002117 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2120 observed_salt_len != (size_t) expected_salt_len) {
2121 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002122 }
2123
Simon Butcher02037452016-03-01 21:19:12 +00002124 /*
2125 * Generate H = Hash( M' )
2126 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2128 result, mgf1_hash_id);
2129 if (ret != 0) {
2130 return ret;
2131 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (memcmp(hash_start, result, hlen) != 0) {
2134 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2135 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002138}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002139
2140/*
2141 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002143int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2144 mbedtls_md_type_t md_alg,
2145 unsigned int hashlen,
2146 const unsigned char *hash,
2147 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002148{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002149 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002151 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002156 : md_alg;
2157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2159 md_alg, hashlen, hash,
2160 mgf1_hash_id,
2161 MBEDTLS_RSA_SALT_LEN_ANY,
2162 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002163
2164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002168/*
2169 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002171int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2172 mbedtls_md_type_t md_alg,
2173 unsigned int hashlen,
2174 const unsigned char *hash,
2175 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002176{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002177 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002178 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002179 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002182 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002184
2185 sig_len = ctx->len;
2186
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002187 /*
2188 * Prepare expected PKCS1 v1.5 encoding of hash.
2189 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2192 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002193 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2194 goto cleanup;
2195 }
2196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2198 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002199 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002201
2202 /*
2203 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2204 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 ret = mbedtls_rsa_public(ctx, sig, encoded);
2207 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002208 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002210
Simon Butcher02037452016-03-01 21:19:12 +00002211 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002212 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002213 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2216 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002217 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2218 goto cleanup;
2219 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002220
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002221cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 if (encoded != NULL) {
2224 mbedtls_platform_zeroize(encoded, sig_len);
2225 mbedtls_free(encoded);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002226 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 if (encoded_expected != NULL) {
2229 mbedtls_platform_zeroize(encoded_expected, sig_len);
2230 mbedtls_free(encoded_expected);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002231 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
2237/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002238 * Do an RSA operation and check the message digest
2239 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002240int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2241 mbedtls_md_type_t md_alg,
2242 unsigned int hashlen,
2243 const unsigned char *hash,
2244 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002245{
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002247 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251#if defined(MBEDTLS_PKCS1_V15)
2252 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2254 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002255#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#if defined(MBEDTLS_PKCS1_V21)
2258 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2260 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002261#endif
2262
2263 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002265 }
2266}
2267
2268/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002269 * Copy the components of an RSA key
2270 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002271int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002272{
Janos Follath24eed8d2019-11-22 13:21:35 +00002273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002274
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002275 dst->len = src->len;
2276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2278 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2281 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2282 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002283
2284#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2286 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2287 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2288 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2289 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002290#endif
2291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2295 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002296
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002297 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002298 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002299
2300cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 if (ret != 0) {
2302 mbedtls_rsa_free(dst);
2303 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002306}
2307
2308/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 * Free the components of an RSA key
2310 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002311void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002312{
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002314 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 mbedtls_mpi_free(&ctx->Vi);
2318 mbedtls_mpi_free(&ctx->Vf);
2319 mbedtls_mpi_free(&ctx->RN);
2320 mbedtls_mpi_free(&ctx->D);
2321 mbedtls_mpi_free(&ctx->Q);
2322 mbedtls_mpi_free(&ctx->P);
2323 mbedtls_mpi_free(&ctx->E);
2324 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002325
Hanno Becker33c30a02017-08-23 07:00:22 +01002326#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 mbedtls_mpi_free(&ctx->RQ);
2328 mbedtls_mpi_free(&ctx->RP);
2329 mbedtls_mpi_free(&ctx->QP);
2330 mbedtls_mpi_free(&ctx->DQ);
2331 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002332#endif /* MBEDTLS_RSA_NO_CRT */
2333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002335 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (ctx->ver != 0) {
2337 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002338 ctx->ver = 0;
2339 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002340#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002341}
2342
Hanno Beckerab377312017-08-23 16:24:51 +01002343#endif /* !MBEDTLS_RSA_ALT */
2344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002346
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002347#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002348
2349/*
2350 * Example RSA-1024 keypair, for test purposes
2351 */
2352#define KEY_LEN 128
2353
2354#define RSA_N "9292758453063D803DD603D5E777D788" \
2355 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2356 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2357 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2358 "93A89813FBF3C4F8066D2D800F7C38A8" \
2359 "1AE31942917403FF4946B0A83D3D3E05" \
2360 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2361 "5E94BB77B07507233A0BC7BAC8F90F79"
2362
2363#define RSA_E "10001"
2364
2365#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2366 "66CA472BC44D253102F8B4A9D3BFA750" \
2367 "91386C0077937FE33FA3252D28855837" \
2368 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2369 "DF79C5CE07EE72C7F123142198164234" \
2370 "CABB724CF78B8173B9F880FC86322407" \
2371 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2372 "071513A1E85B5DFA031F21ECAE91A34D"
2373
2374#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2375 "2C01CAD19EA484A87EA4377637E75500" \
2376 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2377 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2378
2379#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2380 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2381 "910E4168387E3C30AA1E00C339A79508" \
2382 "8452DD96A9A5EA5D9DCA68DA636032AF"
2383
Paul Bakker5121ce52009-01-03 21:22:43 +00002384#define PT_LEN 24
2385#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2386 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002389static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002390{
gufe44c2620da2020-08-03 17:56:50 +02002391#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002392 size_t i;
2393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002395 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 }
Paul Bakker545570e2010-07-18 09:00:25 +00002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002399 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002401#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002403 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002407#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002412
Paul Bakker5121ce52009-01-03 21:22:43 +00002413/*
2414 * Checkup routine
2415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002417{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002418 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002420 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 unsigned char rsa_plaintext[PT_LEN];
2423 unsigned char rsa_decrypted[PT_LEN];
2424 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002426 unsigned char sha1sum[20];
2427#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
Hanno Becker3a701162017-08-22 13:52:43 +01002429 mbedtls_mpi K;
2430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 mbedtls_mpi_init(&K);
2432 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2435 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2436 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2437 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2438 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2439 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2440 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2441 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2442 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2443 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (verbose != 0) {
2448 mbedtls_printf(" RSA key validation: ");
2449 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2452 mbedtls_rsa_check_privkey(&rsa) != 0) {
2453 if (verbose != 0) {
2454 mbedtls_printf("failed\n");
2455 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
Hanno Becker5bc87292017-05-03 15:09:31 +01002457 ret = 1;
2458 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 }
2460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 if (verbose != 0) {
2462 mbedtls_printf("passed\n PKCS#1 encryption : ");
2463 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2468 PT_LEN, rsa_plaintext,
2469 rsa_ciphertext) != 0) {
2470 if (verbose != 0) {
2471 mbedtls_printf("failed\n");
2472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Hanno Becker5bc87292017-05-03 15:09:31 +01002474 ret = 1;
2475 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 }
2477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 if (verbose != 0) {
2479 mbedtls_printf("passed\n PKCS#1 decryption : ");
2480 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2483 &len, rsa_ciphertext, rsa_decrypted,
2484 sizeof(rsa_decrypted)) != 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 (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2494 if (verbose != 0) {
2495 mbedtls_printf("failed\n");
2496 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Hanno Becker5bc87292017-05-03 15:09:31 +01002498 ret = 1;
2499 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 }
2501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if (verbose != 0) {
2503 mbedtls_printf("passed\n");
2504 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 if (verbose != 0) {
2508 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002511 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2512 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 if (verbose != 0) {
2514 mbedtls_printf("failed\n");
2515 }
2516
2517 return 1;
2518 }
2519
2520 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2521 MBEDTLS_MD_SHA1, 20,
2522 sha1sum, rsa_ciphertext) != 0) {
2523 if (verbose != 0) {
2524 mbedtls_printf("failed\n");
2525 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Hanno Becker5bc87292017-05-03 15:09:31 +01002527 ret = 1;
2528 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 }
2530
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (verbose != 0) {
2532 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2533 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2536 sha1sum, rsa_ciphertext) != 0) {
2537 if (verbose != 0) {
2538 mbedtls_printf("failed\n");
2539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
Hanno Becker5bc87292017-05-03 15:09:31 +01002541 ret = 1;
2542 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 }
2544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 if (verbose != 0) {
2546 mbedtls_printf("passed\n");
2547 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (verbose != 0) {
2551 mbedtls_printf("\n");
2552 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002553
Paul Bakker3d8fb632014-04-17 12:42:41 +02002554cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 mbedtls_mpi_free(&K);
2556 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002558 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002561}
2562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565#endif /* MBEDTLS_RSA_C */