blob: ba54d25bc198e878938cffb56110206e24d204a4 [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"
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +020063#endif /* MBEDTLS_MD_C */
64#endif /* MBEDTLS_PKCS1_V21 */
65
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010067
Hanno Beckera565f542017-10-11 11:00:19 +010068#if !defined(MBEDTLS_RSA_ALT)
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
71 const mbedtls_mpi *N,
72 const mbedtls_mpi *P, const mbedtls_mpi *Q,
73 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +010074{
Janos Follath24eed8d2019-11-22 13:21:35 +000075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +010076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
78 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
79 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
80 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
81 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
82 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +010083 }
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if (N != NULL) {
86 ctx->len = mbedtls_mpi_size(&ctx->N);
87 }
Hanno Becker617c1ae2017-08-23 14:11:24 +010088
Gilles Peskine449bd832023-01-11 14:50:10 +010089 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +010090}
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
93 unsigned char const *N, size_t N_len,
94 unsigned char const *P, size_t P_len,
95 unsigned char const *Q, size_t Q_len,
96 unsigned char const *D, size_t D_len,
97 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +010098{
Hanno Beckerd4d60572018-01-10 07:12:01 +000099 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (N != NULL) {
102 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
103 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100104 }
105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (P != NULL) {
107 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
108 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (Q != NULL) {
111 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
112 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (D != NULL) {
115 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
116 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (E != NULL) {
119 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
120 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100121
122cleanup:
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (ret != 0) {
125 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
126 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100129}
130
Hanno Becker705fc682017-10-10 17:57:02 +0100131/*
132 * Checks whether the context fields are set in such a way
133 * that the RSA primitives will be able to execute without error.
134 * It does *not* make guarantees for consistency of the parameters.
135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
137 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100138{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100139#if !defined(MBEDTLS_RSA_NO_CRT)
140 /* blinding_needed is only used for NO_CRT to decide whether
141 * P,Q need to be present or not. */
142 ((void) blinding_needed);
143#endif
144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
146 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
147 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000148 }
Hanno Becker705fc682017-10-10 17:57:02 +0100149
150 /*
151 * 1. Modular exponentiation needs positive, odd moduli.
152 */
153
154 /* Modular exponentiation wrt. N is always used for
155 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
157 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
158 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100159 }
160
161#if !defined(MBEDTLS_RSA_NO_CRT)
162 /* Modular exponentiation for P and Q is only
163 * used for private key operations and if CRT
164 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (is_priv &&
166 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
167 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
168 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
169 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
170 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100171 }
172#endif /* !MBEDTLS_RSA_NO_CRT */
173
174 /*
175 * 2. Exponents must be positive
176 */
177
178 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
180 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
181 }
Hanno Becker705fc682017-10-10 17:57:02 +0100182
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100183#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100184 /* For private key operations, use D or DP & DQ
185 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
187 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
188 }
Hanno Becker705fc682017-10-10 17:57:02 +0100189#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (is_priv &&
191 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
192 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
193 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100194 }
195#endif /* MBEDTLS_RSA_NO_CRT */
196
197 /* Blinding shouldn't make exponents negative either,
198 * so check that P, Q >= 1 if that hasn't yet been
199 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100200#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (is_priv && blinding_needed &&
202 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
203 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
204 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100205 }
206#endif
207
208 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100209 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100210#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (is_priv &&
212 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
213 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100214 }
215#endif
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100218}
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100221{
222 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500223 int have_N, have_P, have_Q, have_D, have_E;
224#if !defined(MBEDTLS_RSA_NO_CRT)
225 int have_DP, have_DQ, have_QP;
226#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
230 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
231 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
232 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
233 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500234
235#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
237 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
238 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500239#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100240
Hanno Becker617c1ae2017-08-23 14:11:24 +0100241 /*
242 * Check whether provided parameters are enough
243 * to deduce all others. The following incomplete
244 * parameter sets for private keys are supported:
245 *
246 * (1) P, Q missing.
247 * (2) D and potentially N missing.
248 *
249 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100250
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500251 n_missing = have_P && have_Q && have_D && have_E;
252 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
253 d_missing = have_P && have_Q && !have_D && have_E;
254 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100255
256 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500257 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (!is_priv && !is_pub) {
260 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
261 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100262
263 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100264 * Step 1: Deduce N if P, Q are provided.
265 */
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (!have_N && have_P && have_Q) {
268 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
269 &ctx->Q)) != 0) {
270 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100271 }
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100274 }
275
276 /*
277 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100278 */
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (pq_missing) {
281 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
282 &ctx->P, &ctx->Q);
283 if (ret != 0) {
284 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
285 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 } else if (d_missing) {
288 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
289 &ctx->Q,
290 &ctx->E,
291 &ctx->D)) != 0) {
292 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100293 }
294 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100295
Hanno Becker617c1ae2017-08-23 14:11:24 +0100296 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100297 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100298 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100299 */
300
Hanno Becker23344b52017-08-23 07:43:27 +0100301#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (is_priv && !(have_DP && have_DQ && have_QP)) {
303 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
304 &ctx->DP, &ctx->DQ, &ctx->QP);
305 if (ret != 0) {
306 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
307 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100308 }
Hanno Becker23344b52017-08-23 07:43:27 +0100309#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100310
311 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100312 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100313 */
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100316}
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
319 unsigned char *N, size_t N_len,
320 unsigned char *P, size_t P_len,
321 unsigned char *Q, size_t Q_len,
322 unsigned char *D, size_t D_len,
323 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100324{
325 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500326 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100327
328 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500329 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
331 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
332 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
333 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
334 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100337 /* If we're trying to export private parameters for a public key,
338 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (P != NULL || Q != NULL || D != NULL) {
340 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
341 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100342
343 }
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (N != NULL) {
346 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
347 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (P != NULL) {
350 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
351 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (Q != NULL) {
354 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
355 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (D != NULL) {
358 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
359 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (E != NULL) {
362 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
363 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100364
365cleanup:
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100368}
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
371 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
372 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100373{
Janos Follath24eed8d2019-11-22 13:21:35 +0000374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500375 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100376
377 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500378 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
380 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
381 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
382 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
383 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100386 /* If we're trying to export private parameters for a public key,
387 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (P != NULL || Q != NULL || D != NULL) {
389 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
390 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100391
392 }
393
394 /* Export all requested core parameters. */
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
397 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
398 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
399 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
400 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
401 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100402 }
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100405}
406
407/*
408 * Export CRT parameters
409 * This must also be implemented if CRT is not used, for being able to
410 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
411 * can be used in this case.
412 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
414 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100415{
Janos Follath24eed8d2019-11-22 13:21:35 +0000416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500417 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100418
419 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500420 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
422 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
423 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
424 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
425 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (!is_priv) {
428 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
429 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100430
Hanno Beckerdc95c892017-08-23 06:57:02 +0100431#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100432 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
434 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
435 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
436 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100437 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100438#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
440 DP, DQ, QP)) != 0) {
441 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100442 }
443#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100446}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100447
Paul Bakker5121ce52009-01-03 21:22:43 +0000448/*
449 * Initialize an RSA context
450 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000452{
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000454
Ronald Cronc1905a12021-06-05 11:11:14 +0200455 ctx->padding = MBEDTLS_RSA_PKCS_V15;
456 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100459 /* Set ctx->ver to nonzero to indicate that the mutex has been
460 * initialized and will need to be freed. */
461 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200463#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000464}
465
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100466/*
467 * Set padding for an existing RSA context
468 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
470 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100471{
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200473#if defined(MBEDTLS_PKCS1_V15)
474 case MBEDTLS_RSA_PKCS_V15:
475 break;
476#endif
477
478#if defined(MBEDTLS_PKCS1_V21)
479 case MBEDTLS_RSA_PKCS_V21:
480 break;
481#endif
482 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200484 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200485
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200486#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
488 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200489 /* Just make sure this hash is supported in this build. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (mbedtls_hash_info_psa_from_md(hash_id) == PSA_ALG_NONE) {
491 return MBEDTLS_ERR_RSA_INVALID_PADDING;
492 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200493 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200494#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500495
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100496 ctx->padding = padding;
497 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100500}
501
Hanno Becker617c1ae2017-08-23 14:11:24 +0100502/*
Yanray Wanga730df62023-03-01 10:18:19 +0800503 * Get padding mode of RSA modulus
504 */
505int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
506{
507 return ctx->MBEDTLS_PRIVATE(padding);
508}
509
510/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100511 * Get length in bytes of RSA modulus
512 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100514{
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100516}
517
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521/*
522 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800523 *
524 * This generation method follows the RSA key pair generation procedure of
525 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
528 int (*f_rng)(void *, unsigned char *, size_t),
529 void *p_rng,
530 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000531{
Janos Follath24eed8d2019-11-22 13:21:35 +0000532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800533 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100534 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Janos Follathb8fc1b02018-09-03 15:37:01 +0100536 /*
537 * If the modulus is 1024 bit long or shorter, then the security strength of
538 * the RSA algorithm is less than or equal to 80 bits and therefore an error
539 * rate of 2^-80 is sufficient.
540 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100542 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 mbedtls_mpi_init(&H);
546 mbedtls_mpi_init(&G);
547 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (nbits < 128 || exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100550 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
551 goto cleanup;
552 }
553
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 /*
555 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800556 * 1. |P-Q| > 2^( nbits / 2 - 100 )
557 * 2. GCD( E, (P-1)*(Q-1) ) == 1
558 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 do {
563 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
564 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
567 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800569 /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
571 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800575 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (H.s < 0) {
577 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
578 }
Janos Follathef441782016-09-21 13:18:12 +0100579
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100580 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
582 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
583 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800584
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800585 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
587 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800588 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800590
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800591 /* compute smallest possible D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
593 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
594 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (mbedtls_mpi_bitlen(&ctx->D) <= ((nbits + 1) / 2)) { // (FIPS 186-4 §B.3.1 criterion 3(a))
Jethro Beekman97f95c92018-02-13 15:50:36 -0800597 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800599
600 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100603 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
605 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100610
Jethro Beekman97f95c92018-02-13 15:50:36 -0800611#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 * DP = D mod (P - 1)
614 * DQ = D mod (Q - 1)
615 * QP = Q^-1 mod P
616 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
618 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100619#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Hanno Becker83aad1f2017-08-23 06:45:10 +0100621 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
624cleanup:
625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 mbedtls_mpi_free(&H);
627 mbedtls_mpi_free(&G);
628 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (ret != 0) {
631 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if ((-ret & ~0x7f) == 0) {
634 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
635 }
636 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 }
638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000640}
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
644/*
645 * Check a public RSA key
646 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100647int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000648{
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
650 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100651 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
654 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100655 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
658 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
659 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
660 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
661 }
662
663 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000664}
665
666/*
Hanno Becker705fc682017-10-10 17:57:02 +0100667 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
672 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
673 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000674 }
Paul Bakker48377d92013-08-30 12:06:24 +0200675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
677 &ctx->D, &ctx->E, NULL, NULL) != 0) {
678 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000680
Hanno Beckerb269a852017-08-25 08:03:21 +0100681#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
683 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
684 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100685 }
686#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000689}
690
691/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100692 * Check if contexts holding a public and private key match
693 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100694int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
695 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696{
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
698 mbedtls_rsa_check_privkey(prv) != 0) {
699 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100700 }
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
703 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
704 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100705 }
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708}
709
710/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 * Do an RSA public key operation
712 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
714 const unsigned char *input,
715 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000716{
Janos Follath24eed8d2019-11-22 13:21:35 +0000717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000718 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
722 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
723 }
Hanno Becker705fc682017-10-10 17:57:02 +0100724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000726
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200727#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
729 return ret;
730 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200731#endif
732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200736 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
737 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 }
739
740 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
742 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
744cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
747 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
748 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100749#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (ret != 0) {
754 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
755 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000758}
759
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200760/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200761 * Generate or update blinding values, see section 10 of:
762 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200763 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200764 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200765 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
767 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200768{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200769 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200770 mbedtls_mpi R;
771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200775 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
777 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
778 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
779 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200780
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200781 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200782 }
783
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200784 /* Unblinding value: Vf = random number, invertible mod N */
785 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200787 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
788 goto cleanup;
789 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&ctx->Vf, ctx->len - 1, f_rng, p_rng));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200792
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200793 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
795 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
796 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200797
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200798 /* At this point, Vi is invertible mod N if and only if both Vf and R
799 * are invertible mod N. If one of them isn't, we don't need to know
800 * which one, we just loop and choose new values for both of them.
801 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
803 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200804 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500808
809 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
811 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200812
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200813 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200814 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200816
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200817
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200818cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200822}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200823
Paul Bakker5121ce52009-01-03 21:22:43 +0000824/*
Janos Follathe81102e2017-03-22 13:38:28 +0000825 * Exponent blinding supposed to prevent side-channel attacks using multiple
826 * traces of measurements to recover the RSA key. The more collisions are there,
827 * the more bits of the key can be recovered. See [3].
828 *
829 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800830 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000831 *
832 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800833 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000834 *
835 * (With the currently (as of 2017 April) known best algorithms breaking 2048
836 * bit RSA requires approximately as much time as trying out 2^112 random keys.
837 * Thus in this sense with 28 byte blinding the security is not reduced by
838 * side-channel attacks like the one in [3])
839 *
840 * This countermeasure does not help if the key recovery is possible with a
841 * single trace.
842 */
843#define RSA_EXPONENT_BLINDING 28
844
845/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000846 * Do an RSA private key operation
847 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
849 int (*f_rng)(void *, unsigned char *, size_t),
850 void *p_rng,
851 const unsigned char *input,
852 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000853{
Janos Follath24eed8d2019-11-22 13:21:35 +0000854 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000855 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100856
857 /* Temporary holding the result */
858 mbedtls_mpi T;
859
860 /* Temporaries holding P-1, Q-1 and the
861 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000862 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100863
864#if !defined(MBEDTLS_RSA_NO_CRT)
865 /* Temporaries holding the results mod p resp. mod q. */
866 mbedtls_mpi TP, TQ;
867
868 /* Temporaries holding the blinded exponents for
869 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000870 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100871
872 /* Pointers to actual exponents to be used - either the unblinded
873 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000874 mbedtls_mpi *DP = &ctx->DP;
875 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100876#else
877 /* Temporary holding the blinded exponent (if used). */
878 mbedtls_mpi D_blind;
879
880 /* Pointer to actual exponent to be used - either the unblinded
881 * or the blinded one, depending on the presence of a PRNG. */
882 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100883#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100884
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100885 /* Temporaries holding the initial input and the double
886 * checked result; should be the same in the end. */
887 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (f_rng == NULL) {
890 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
891 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (rsa_check_context(ctx, 1 /* private key checks */,
894 1 /* blinding on */) != 0) {
895 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +0100896 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100897
Hanno Becker06811ce2017-05-03 15:10:34 +0100898#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
900 return ret;
901 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100902#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000903
Hanno Becker06811ce2017-05-03 15:10:34 +0100904 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +0100906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 mbedtls_mpi_init(&P1);
908 mbedtls_mpi_init(&Q1);
909 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +0000910
Janos Follathe81102e2017-03-22 13:38:28 +0000911#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +0000913#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 mbedtls_mpi_init(&DP_blind);
915 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +0000916#endif
917
Hanno Becker06811ce2017-05-03 15:10:34 +0100918#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200920#endif
921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 mbedtls_mpi_init(&I);
923 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +0100924
925 /* End of MPI initialization */
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
928 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200929 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
930 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 }
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +0100934
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200935 /*
936 * Blinding
937 * T = T * Vi mod N
938 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
940 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
941 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +0000942
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200943 /*
944 * Exponent blinding
945 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
947 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +0000948
Janos Follathf9203b42017-03-22 15:13:15 +0000949#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200950 /*
951 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
952 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
954 f_rng, p_rng));
955 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
956 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
957 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +0000958
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200959 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000960#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200961 /*
962 * DP_blind = ( P - 1 ) * R + DP
963 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
965 f_rng, p_rng));
966 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
967 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
968 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +0000969
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200970 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000971
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200972 /*
973 * DQ_blind = ( Q - 1 ) * R + DQ
974 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
976 f_rng, p_rng));
977 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
978 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
979 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +0000980
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200981 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000982#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100986#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200987 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000988 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100990 * TP = input ^ dP mod P
991 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
995 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +0000996
997 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100998 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1001 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1002 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
1004 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001005 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1008 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001010
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001011 /*
1012 * Unblind
1013 * T = T * Vf mod N
1014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1016 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Hanno Becker2dec5e82017-10-03 07:49:52 +01001018 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1020 &ctx->N, &ctx->RN));
1021 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001022 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1023 goto cleanup;
1024 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001025
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
1029cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1032 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1033 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001034#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 mbedtls_mpi_free(&P1);
1037 mbedtls_mpi_free(&Q1);
1038 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001039
Janos Follathe81102e2017-03-22 13:38:28 +00001040#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001042#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 mbedtls_mpi_free(&DP_blind);
1044 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001045#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001048
1049#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001051#endif
1052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 mbedtls_mpi_free(&C);
1054 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (ret != 0 && ret >= -0x007f) {
1057 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1058 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001061}
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001064/**
1065 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1066 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001067 * \param dst buffer to mask
1068 * \param dlen length of destination buffer
1069 * \param src source of the mask generation
1070 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001071 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001072 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1074 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001075{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001076 unsigned char counter[4];
1077 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001078 unsigned int hlen;
1079 size_t i, use_len;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001080 unsigned char mask[MBEDTLS_HASH_MAX_SIZE];
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001081#if defined(MBEDTLS_MD_C)
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001082 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001083 const mbedtls_md_info_t *md_info;
1084 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 mbedtls_md_init(&md_ctx);
1087 md_info = mbedtls_md_info_from_type(md_alg);
1088 if (md_info == NULL) {
1089 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1090 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 mbedtls_md_init(&md_ctx);
1093 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001094 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001098#else
1099 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 psa_algorithm_t alg = mbedtls_psa_translate_md(md_alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001101 psa_status_t status = PSA_SUCCESS;
1102 size_t out_len;
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 hlen = PSA_HASH_LENGTH(alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001105#endif
1106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 memset(mask, 0, sizeof(mask));
1108 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001109
Simon Butcher02037452016-03-01 21:19:12 +00001110 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001111 p = dst;
1112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001114 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001116 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001118
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001119#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 }
1123 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 }
1126 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 }
1129 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 }
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001132#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if ((status = psa_hash_setup(&op, alg)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 }
1136 if ((status = psa_hash_update(&op, src, slen)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 }
1139 if ((status = psa_hash_update(&op, counter, 4)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 }
1142 status = psa_hash_finish(&op, mask, sizeof(mask), &out_len);
1143 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001144 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 }
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001146#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001149 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001151
1152 counter[3]++;
1153
1154 dlen -= use_len;
1155 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001156
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 mbedtls_platform_zeroize(mask, sizeof(mask));
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001159#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 return ret;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001163#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 psa_hash_abort(&op);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001167#endif
Paul Bakker9dcc3222011-03-08 14:16:06 +00001168}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001169
1170/**
1171 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1172 *
1173 * \param hash the input hash
1174 * \param hlen length of the input hash
1175 * \param salt the input salt
1176 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001177 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001178 * \param md_alg message digest to use
1179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001180static int hash_mprime(const unsigned char *hash, size_t hlen,
1181 const unsigned char *salt, size_t slen,
1182 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001183{
1184 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001185
1186#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001187 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1191 if (md_info == NULL) {
1192 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1193 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 mbedtls_md_init(&md_ctx);
1196 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001197 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 }
1199 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001200 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 }
1202 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001203 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 }
1205 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001206 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 }
1208 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001209 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 }
1211 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001212 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001214
1215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 return ret;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001219#else
1220 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 psa_algorithm_t alg = mbedtls_psa_translate_md(md_alg);
1222 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1223 size_t out_size = PSA_HASH_LENGTH(alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001224 size_t out_len;
1225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 if ((status = psa_hash_setup(&op, alg)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001227 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 }
1229 if ((status = psa_hash_update(&op, zeros, sizeof(zeros))) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 }
1232 if ((status = psa_hash_update(&op, hash, hlen)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001233 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 }
1235 if ((status = psa_hash_update(&op, salt, slen)) != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 }
1238 status = psa_hash_finish(&op, out, out_size, &out_len);
1239 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001240 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 }
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001242
1243exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 psa_hash_abort(&op);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001247#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001248}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001249
1250/**
1251 * Compute a hash.
1252 *
1253 * \param md_alg algorithm to use
1254 * \param input input message to hash
1255 * \param ilen input length
1256 * \param output the output buffer - must be large enough for \p md_alg
1257 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001258static int compute_hash(mbedtls_md_type_t md_alg,
1259 const unsigned char *input, size_t ilen,
1260 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001261{
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001262#if defined(MBEDTLS_MD_C)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001263 const mbedtls_md_info_t *md_info;
1264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 md_info = mbedtls_md_info_from_type(md_alg);
1266 if (md_info == NULL) {
1267 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1268 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001271#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 psa_algorithm_t alg = mbedtls_psa_translate_md(md_alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001273 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 size_t out_size = PSA_HASH_LENGTH(alg);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001275 size_t out_len;
1276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 status = psa_hash_compute(alg, input, ilen, output, out_size, &out_len);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001280#endif /* !MBEDTLS_MD_C */
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001281}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001285/*
1286 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1289 int (*f_rng)(void *, unsigned char *, size_t),
1290 void *p_rng,
1291 const unsigned char *label, size_t label_len,
1292 size_t ilen,
1293 const unsigned char *input,
1294 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001295{
1296 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001298 unsigned char *p = output;
1299 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 if (f_rng == NULL) {
1302 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1303 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1306 if (hlen == 0) {
1307 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1308 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001309
1310 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001311
Simon Butcher02037452016-03-01 21:19:12 +00001312 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1314 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1315 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001318
1319 *p++ = 0;
1320
Simon Butcher02037452016-03-01 21:19:12 +00001321 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1323 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1324 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001325
1326 p += hlen;
1327
Simon Butcher02037452016-03-01 21:19:12 +00001328 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1330 if (ret != 0) {
1331 return ret;
1332 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001333 p += hlen;
1334 p += olen - 2 * hlen - 2 - ilen;
1335 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (ilen != 0) {
1337 memcpy(p, input, ilen);
1338 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001339
Simon Butcher02037452016-03-01 21:19:12 +00001340 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1342 ctx->hash_id)) != 0) {
1343 return ret;
1344 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001345
Simon Butcher02037452016-03-01 21:19:12 +00001346 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1348 ctx->hash_id)) != 0) {
1349 return ret;
1350 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001357/*
1358 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1361 int (*f_rng)(void *, unsigned char *, size_t),
1362 void *p_rng, size_t ilen,
1363 const unsigned char *input,
1364 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001365{
1366 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001368 unsigned char *p = output;
1369
Paul Bakkerb3869132013-02-28 17:21:01 +01001370 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001371
Simon Butcher02037452016-03-01 21:19:12 +00001372 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 if (ilen + 11 < ilen || olen < ilen + 11) {
1374 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1375 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001376
1377 nb_pad = olen - 3 - ilen;
1378
1379 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if (f_rng == NULL) {
1382 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1383 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001384
1385 *p++ = MBEDTLS_RSA_CRYPT;
1386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001388 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001389
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001390 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 ret = f_rng(p_rng, p, 1);
1392 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001393
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001394 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 if (rng_dl == 0 || ret != 0) {
1396 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1397 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001398
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001399 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001400 }
1401
1402 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (ilen != 0) {
1404 memcpy(p, input, ilen);
1405 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001410
Paul Bakker5121ce52009-01-03 21:22:43 +00001411/*
1412 * Add the message padding, then do an RSA operation
1413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1415 int (*f_rng)(void *, unsigned char *, size_t),
1416 void *p_rng,
1417 size_t ilen,
1418 const unsigned char *input,
1419 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001420{
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_PKCS1_V15)
1423 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1425 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001426#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_PKCS1_V21)
1429 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1431 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001432#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
1434 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001437}
1438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001440/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001441 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001443int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1444 int (*f_rng)(void *, unsigned char *, size_t),
1445 void *p_rng,
1446 const unsigned char *label, size_t label_len,
1447 size_t *olen,
1448 const unsigned char *input,
1449 unsigned char *output,
1450 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001451{
Janos Follath24eed8d2019-11-22 13:21:35 +00001452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001453 size_t ilen, i, pad_len;
1454 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001456 unsigned char lhash[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001457 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001458
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001459 /*
1460 * Parameters sanity checks
1461 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1463 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001465
1466 ilen = ctx->len;
1467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (ilen < 16 || ilen > sizeof(buf)) {
1469 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1470 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1473 if (hlen == 0) {
1474 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1475 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001476
Janos Follathc17cda12016-02-11 11:08:18 +00001477 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 if (2 * hlen + 2 > ilen) {
1479 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1480 }
Janos Follathc17cda12016-02-11 11:08:18 +00001481
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001482 /*
1483 * RSA operation
1484 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001488 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001490
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001491 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001492 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001493 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001494 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1496 ctx->hash_id)) != 0 ||
1497 /* DB: Apply dbMask to maskedDB */
1498 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1499 ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001500 goto cleanup;
1501 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001502
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001503 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1505 label, label_len, lhash);
1506 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001507 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001509
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001510 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001511 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001512 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001513 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001514 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001515
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001516 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001517
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001518 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001519
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001520 /* Check lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 for (i = 0; i < hlen; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001522 bad |= lhash[i] ^ *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001524
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001525 /* Get zero-padding len, but always read till end of buffer
1526 * (minus one, for the 01 byte) */
1527 pad_len = 0;
1528 pad_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001530 pad_done |= p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001532 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001533
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001534 p += pad_len;
1535 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001536
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001537 /*
1538 * The only information "leaked" is whether the padding was correct or not
1539 * (eg, no data is copied if it was not correct). This meets the
1540 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1541 * the different error conditions.
1542 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001544 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1545 goto cleanup;
1546 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001549 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1550 goto cleanup;
1551 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001552
1553 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if (*olen != 0) {
1555 memcpy(output, p, *olen);
1556 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001557 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001558
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001559cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 mbedtls_platform_zeroize(buf, sizeof(buf));
1561 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001568/*
1569 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1570 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001571int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1572 int (*f_rng)(void *, unsigned char *, size_t),
1573 void *p_rng,
1574 size_t *olen,
1575 const unsigned char *input,
1576 unsigned char *output,
1577 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001578{
1579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1580 size_t ilen;
1581 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1582
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001583 ilen = ctx->len;
1584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
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 if (ilen < 16 || ilen > sizeof(buf)) {
1590 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1591 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001592
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001596 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1600 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001601
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001602cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001606}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001608
1609/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001610 * Do an RSA operation, then remove the message padding
1611 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001612int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1613 int (*f_rng)(void *, unsigned char *, size_t),
1614 void *p_rng,
1615 size_t *olen,
1616 const unsigned char *input,
1617 unsigned char *output,
1618 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001619{
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#if defined(MBEDTLS_PKCS1_V15)
1622 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1624 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001625#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#if defined(MBEDTLS_PKCS1_V21)
1628 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1630 olen, input, output,
1631 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001632#endif
1633
1634 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001636 }
1637}
1638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001640static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1641 int (*f_rng)(void *, unsigned char *, size_t),
1642 void *p_rng,
1643 mbedtls_md_type_t md_alg,
1644 unsigned int hashlen,
1645 const unsigned char *hash,
1646 int saltlen,
1647 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001648{
1649 size_t olen;
1650 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001651 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001652 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001654 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001657 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1661 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1662 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if (f_rng == NULL) {
1665 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1666 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001667
1668 olen = ctx->len;
1669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001671 /* Gather length of hash to sign */
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 size_t exp_hashlen = mbedtls_hash_info_get_size(md_alg);
1673 if (exp_hashlen == 0) {
1674 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1675 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 if (hashlen != exp_hashlen) {
1678 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1679 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001680 }
1681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1683 if (hlen == 0) {
1684 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1685 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1688 /* Calculate the largest possible salt length, up to the hash size.
1689 * Normally this is the hash length, which is the maximum salt length
1690 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1691 * enough room, use the maximum salt length that fits. The constraint is
1692 * that the hash length plus the salt length plus 2 bytes must be at most
1693 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1694 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001695 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (olen < hlen + min_slen + 2) {
1697 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1698 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001699 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001701 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 }
1703 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1704 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1705 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001706 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001707 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001710
Simon Butcher02037452016-03-01 21:19:12 +00001711 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001713 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001714 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001715
1716 /* Generate salt of length slen in place in the encoded message */
1717 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1719 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1720 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001721
Paul Bakkerb3869132013-02-28 17:21:01 +01001722 p += slen;
1723
Simon Butcher02037452016-03-01 21:19:12 +00001724 /* Generate H = Hash( M' ) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 ret = hash_mprime(hash, hashlen, salt, slen, p, ctx->hash_id);
1726 if (ret != 0) {
1727 return ret;
1728 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001729
Simon Butcher02037452016-03-01 21:19:12 +00001730 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001732 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001734
Simon Butcher02037452016-03-01 21:19:12 +00001735 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
1737 ctx->hash_id);
1738 if (ret != 0) {
1739 return ret;
1740 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1743 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001744
1745 p += hlen;
1746 *p++ = 0xBC;
1747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001749}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001750
1751/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001752 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1753 * the option to pass in the salt length.
1754 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001755int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1756 int (*f_rng)(void *, unsigned char *, size_t),
1757 void *p_rng,
1758 mbedtls_md_type_t md_alg,
1759 unsigned int hashlen,
1760 const unsigned char *hash,
1761 int saltlen,
1762 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001763{
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1765 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001766}
1767
1768
1769/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001770 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1771 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001772int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1773 int (*f_rng)(void *, unsigned char *, size_t),
1774 void *p_rng,
1775 mbedtls_md_type_t md_alg,
1776 unsigned int hashlen,
1777 const unsigned char *hash,
1778 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001779{
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1781 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001782}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001786/*
1787 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1788 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001789
1790/* Construct a PKCS v1.5 encoding of a hashed message
1791 *
1792 * This is used both for signature generation and verification.
1793 *
1794 * Parameters:
1795 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001796 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001797 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001798 * - hash: Buffer containing the hashed message or the raw data.
1799 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001800 * - dst: Buffer to hold the encoded message.
1801 *
1802 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001803 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001804 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001805 *
1806 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001807static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1808 unsigned int hashlen,
1809 const unsigned char *hash,
1810 size_t dst_len,
1811 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001812{
1813 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001814 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001815 unsigned char *p = dst;
1816 const char *oid = NULL;
1817
1818 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (md_alg != MBEDTLS_MD_NONE) {
1820 unsigned char md_size = mbedtls_hash_info_get_size(md_alg);
1821 if (md_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 (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1826 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1827 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 if (hashlen != md_size) {
1830 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1831 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001832
1833 /* Double-check that 8 + hashlen + oid_size can be used as a
1834 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001836 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 10 + hashlen + oid_size < 10 + hashlen) {
1838 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1839 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001840
1841 /*
1842 * Static bounds check:
1843 * - Need 10 bytes for five tag-length pairs.
1844 * (Insist on 1-byte length encodings to protect against variants of
1845 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1846 * - Need hashlen bytes for hash
1847 * - Need oid_size bytes for hash alg OID.
1848 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if (nb_pad < 10 + hashlen + oid_size) {
1850 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1851 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001852 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 } else {
1854 if (nb_pad < hashlen) {
1855 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1856 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001857
1858 nb_pad -= hashlen;
1859 }
1860
Hanno Becker2b2f8982017-09-27 17:10:03 +01001861 /* Need space for signature header and padding delimiter (3 bytes),
1862 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if (nb_pad < 3 + 8) {
1864 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1865 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001866 nb_pad -= 3;
1867
1868 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001869 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001870
1871 /* Write signature header and padding */
1872 *p++ = 0;
1873 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001875 p += nb_pad;
1876 *p++ = 0;
1877
1878 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 if (md_alg == MBEDTLS_MD_NONE) {
1880 memcpy(p, hash, hashlen);
1881 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001882 }
1883
1884 /* Signing hashed data, add corresponding ASN.1 structure
1885 *
1886 * DigestInfo ::= SEQUENCE {
1887 * digestAlgorithm DigestAlgorithmIdentifier,
1888 * digest Digest }
1889 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1890 * Digest ::= OCTET STRING
1891 *
1892 * Schematic:
1893 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1894 * TAG-NULL + LEN [ NULL ] ]
1895 * TAG-OCTET + LEN [ HASH ] ]
1896 */
1897 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001899 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001901 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001902 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001904 p += oid_size;
1905 *p++ = MBEDTLS_ASN1_NULL;
1906 *p++ = 0x00;
1907 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001908 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001910 p += hashlen;
1911
1912 /* Just a sanity-check, should be automatic
1913 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 if (p != dst + dst_len) {
1915 mbedtls_platform_zeroize(dst, dst_len);
1916 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001917 }
1918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001920}
1921
Paul Bakkerb3869132013-02-28 17:21:01 +01001922/*
1923 * Do an RSA operation to sign the message digest
1924 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001925int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
1926 int (*f_rng)(void *, unsigned char *, size_t),
1927 void *p_rng,
1928 mbedtls_md_type_t md_alg,
1929 unsigned int hashlen,
1930 const unsigned char *hash,
1931 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001932{
Janos Follath24eed8d2019-11-22 13:21:35 +00001933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001934 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001937 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1941 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1942 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001943
Hanno Beckerfdf38032017-09-06 12:35:55 +01001944 /*
1945 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1946 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
1949 ctx->len, sig)) != 0) {
1950 return ret;
1951 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001952
Hanno Beckerfdf38032017-09-06 12:35:55 +01001953 /* Private key operation
1954 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001955 * In order to prevent Lenstra's attack, make the signature in a
1956 * temporary buffer and check it before returning it.
1957 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 sig_try = mbedtls_calloc(1, ctx->len);
1960 if (sig_try == NULL) {
1961 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00001962 }
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 verif = mbedtls_calloc(1, ctx->len);
1965 if (verif == NULL) {
1966 mbedtls_free(sig_try);
1967 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
1968 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
1971 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
1972
1973 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001974 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1975 goto cleanup;
1976 }
1977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001979
1980cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 mbedtls_platform_zeroize(sig_try, ctx->len);
1982 mbedtls_platform_zeroize(verif, ctx->len);
1983 mbedtls_free(sig_try);
1984 mbedtls_free(verif);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 if (ret != 0) {
1987 memset(sig, '!', ctx->len);
1988 }
1989 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001992
1993/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 * Do an RSA operation to sign the message digest
1995 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
1997 int (*f_rng)(void *, unsigned char *, size_t),
1998 void *p_rng,
1999 mbedtls_md_type_t md_alg,
2000 unsigned int hashlen,
2001 const unsigned char *hash,
2002 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002003{
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002005 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009#if defined(MBEDTLS_PKCS1_V15)
2010 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2012 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002013#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#if defined(MBEDTLS_PKCS1_V21)
2016 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2018 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002019#endif
2020
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002024}
2025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002027/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002028 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002030int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2031 mbedtls_md_type_t md_alg,
2032 unsigned int hashlen,
2033 const unsigned char *hash,
2034 mbedtls_md_type_t mgf1_hash_id,
2035 int expected_salt_len,
2036 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002037{
Janos Follath24eed8d2019-11-22 13:21:35 +00002038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002039 size_t siglen;
2040 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002041 unsigned char *hash_start;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002042 unsigned char result[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002043 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002044 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002048 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002050
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 siglen = ctx->len;
2052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 if (siglen < 16 || siglen > sizeof(buf)) {
2054 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (ret != 0) {
2060 return ret;
2061 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002062
2063 p = buf;
2064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 if (buf[siglen - 1] != 0xBC) {
2066 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002067 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 if (md_alg != MBEDTLS_MD_NONE) {
2070 /* Gather length of hash to sign */
2071 size_t exp_hashlen = mbedtls_hash_info_get_size(md_alg);
2072 if (exp_hashlen == 0) {
2073 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2074 }
2075
2076 if (hashlen != exp_hashlen) {
2077 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2078 }
2079 }
2080
2081 hlen = mbedtls_hash_info_get_size(mgf1_hash_id);
2082 if (hlen == 0) {
2083 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2084 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002085
Simon Butcher02037452016-03-01 21:19:12 +00002086 /*
2087 * Note: EMSA-PSS verification is over the length of N - 1 bits
2088 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002090
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (buf[0] >> (8 - siglen * 8 + msb)) {
2092 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2093 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002094
Simon Butcher02037452016-03-01 21:19:12 +00002095 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002097 p++;
2098 siglen -= 1;
2099 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 if (siglen < hlen + 2) {
2102 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2103 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002104 hash_start = p + siglen - hlen - 1;
2105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2107 if (ret != 0) {
2108 return ret;
2109 }
Paul Bakker02303e82013-01-03 11:08:31 +01002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002114 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if (*p++ != 0x01) {
2118 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2119 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002120
Gilles Peskine6a54b022017-10-17 19:02:13 +02002121 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2124 observed_salt_len != (size_t) expected_salt_len) {
2125 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002126 }
2127
Simon Butcher02037452016-03-01 21:19:12 +00002128 /*
2129 * Generate H = Hash( M' )
2130 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2132 result, mgf1_hash_id);
2133 if (ret != 0) {
2134 return ret;
2135 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (memcmp(hash_start, result, hlen) != 0) {
2138 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2139 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002142}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002143
2144/*
2145 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002147int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2148 mbedtls_md_type_t md_alg,
2149 unsigned int hashlen,
2150 const unsigned char *hash,
2151 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002152{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002153 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002155 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002160 : md_alg;
2161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2163 md_alg, hashlen, hash,
2164 mgf1_hash_id,
2165 MBEDTLS_RSA_SALT_LEN_ANY,
2166 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002167
2168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002172/*
2173 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2174 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002175int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2176 mbedtls_md_type_t md_alg,
2177 unsigned int hashlen,
2178 const unsigned char *hash,
2179 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002180{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002181 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002182 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002183 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002186 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002188
2189 sig_len = ctx->len;
2190
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002191 /*
2192 * Prepare expected PKCS1 v1.5 encoding of hash.
2193 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2196 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002197 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2198 goto cleanup;
2199 }
2200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2202 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002203 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002205
2206 /*
2207 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2208 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 ret = mbedtls_rsa_public(ctx, sig, encoded);
2211 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002212 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002214
Simon Butcher02037452016-03-01 21:19:12 +00002215 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002216 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002217 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2220 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002221 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2222 goto cleanup;
2223 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002224
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002225cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (encoded != NULL) {
2228 mbedtls_platform_zeroize(encoded, sig_len);
2229 mbedtls_free(encoded);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002230 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 if (encoded_expected != NULL) {
2233 mbedtls_platform_zeroize(encoded_expected, sig_len);
2234 mbedtls_free(encoded_expected);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002235 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
2241/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002242 * Do an RSA operation and check the message digest
2243 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002244int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2245 mbedtls_md_type_t md_alg,
2246 unsigned int hashlen,
2247 const unsigned char *hash,
2248 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002249{
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002251 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255#if defined(MBEDTLS_PKCS1_V15)
2256 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2258 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002259#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#if defined(MBEDTLS_PKCS1_V21)
2262 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2264 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002265#endif
2266
2267 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002269 }
2270}
2271
2272/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002273 * Copy the components of an RSA key
2274 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002275int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002276{
Janos Follath24eed8d2019-11-22 13:21:35 +00002277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002278
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002279 dst->len = src->len;
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2282 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2285 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2286 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002287
2288#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2290 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2291 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2292 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2293 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002294#endif
2295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2299 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002300
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002301 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002302 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002303
2304cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (ret != 0) {
2306 mbedtls_rsa_free(dst);
2307 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002310}
2311
2312/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 * Free the components of an RSA key
2314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002315void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002316{
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002318 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 mbedtls_mpi_free(&ctx->Vi);
2322 mbedtls_mpi_free(&ctx->Vf);
2323 mbedtls_mpi_free(&ctx->RN);
2324 mbedtls_mpi_free(&ctx->D);
2325 mbedtls_mpi_free(&ctx->Q);
2326 mbedtls_mpi_free(&ctx->P);
2327 mbedtls_mpi_free(&ctx->E);
2328 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002329
Hanno Becker33c30a02017-08-23 07:00:22 +01002330#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 mbedtls_mpi_free(&ctx->RQ);
2332 mbedtls_mpi_free(&ctx->RP);
2333 mbedtls_mpi_free(&ctx->QP);
2334 mbedtls_mpi_free(&ctx->DQ);
2335 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002336#endif /* MBEDTLS_RSA_NO_CRT */
2337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002339 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (ctx->ver != 0) {
2341 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002342 ctx->ver = 0;
2343 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002344#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002345}
2346
Hanno Beckerab377312017-08-23 16:24:51 +01002347#endif /* !MBEDTLS_RSA_ALT */
2348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002350
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002351#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002352
2353/*
2354 * Example RSA-1024 keypair, for test purposes
2355 */
2356#define KEY_LEN 128
2357
2358#define RSA_N "9292758453063D803DD603D5E777D788" \
2359 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2360 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2361 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2362 "93A89813FBF3C4F8066D2D800F7C38A8" \
2363 "1AE31942917403FF4946B0A83D3D3E05" \
2364 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2365 "5E94BB77B07507233A0BC7BAC8F90F79"
2366
2367#define RSA_E "10001"
2368
2369#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2370 "66CA472BC44D253102F8B4A9D3BFA750" \
2371 "91386C0077937FE33FA3252D28855837" \
2372 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2373 "DF79C5CE07EE72C7F123142198164234" \
2374 "CABB724CF78B8173B9F880FC86322407" \
2375 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2376 "071513A1E85B5DFA031F21ECAE91A34D"
2377
2378#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2379 "2C01CAD19EA484A87EA4377637E75500" \
2380 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2381 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2382
2383#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2384 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2385 "910E4168387E3C30AA1E00C339A79508" \
2386 "8452DD96A9A5EA5D9DCA68DA636032AF"
2387
Paul Bakker5121ce52009-01-03 21:22:43 +00002388#define PT_LEN 24
2389#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2390 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002393static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002394{
gufe44c2620da2020-08-03 17:56:50 +02002395#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002396 size_t i;
2397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002399 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 }
Paul Bakker545570e2010-07-18 09:00:25 +00002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002403 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002405#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002407 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002411#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002416
Paul Bakker5121ce52009-01-03 21:22:43 +00002417/*
2418 * Checkup routine
2419 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002421{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002422 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002424 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 unsigned char rsa_plaintext[PT_LEN];
2427 unsigned char rsa_decrypted[PT_LEN];
2428 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002430 unsigned char sha1sum[20];
2431#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
Hanno Becker3a701162017-08-22 13:52:43 +01002433 mbedtls_mpi K;
2434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 mbedtls_mpi_init(&K);
2436 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2439 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2440 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2441 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2442 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2443 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2444 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2445 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2446 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2447 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 if (verbose != 0) {
2452 mbedtls_printf(" RSA key validation: ");
2453 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2456 mbedtls_rsa_check_privkey(&rsa) != 0) {
2457 if (verbose != 0) {
2458 mbedtls_printf("failed\n");
2459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
Hanno Becker5bc87292017-05-03 15:09:31 +01002461 ret = 1;
2462 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 }
2464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 if (verbose != 0) {
2466 mbedtls_printf("passed\n PKCS#1 encryption : ");
2467 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2472 PT_LEN, rsa_plaintext,
2473 rsa_ciphertext) != 0) {
2474 if (verbose != 0) {
2475 mbedtls_printf("failed\n");
2476 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002477
Hanno Becker5bc87292017-05-03 15:09:31 +01002478 ret = 1;
2479 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002480 }
2481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 if (verbose != 0) {
2483 mbedtls_printf("passed\n PKCS#1 decryption : ");
2484 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2487 &len, rsa_ciphertext, rsa_decrypted,
2488 sizeof(rsa_decrypted)) != 0) {
2489 if (verbose != 0) {
2490 mbedtls_printf("failed\n");
2491 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Hanno Becker5bc87292017-05-03 15:09:31 +01002493 ret = 1;
2494 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 }
2496
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2498 if (verbose != 0) {
2499 mbedtls_printf("failed\n");
2500 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002501
Hanno Becker5bc87292017-05-03 15:09:31 +01002502 ret = 1;
2503 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 }
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (verbose != 0) {
2507 mbedtls_printf("passed\n");
2508 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if (verbose != 0) {
2512 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002513 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 if (mbedtls_sha1(rsa_plaintext, PT_LEN, sha1sum) != 0) {
2516 if (verbose != 0) {
2517 mbedtls_printf("failed\n");
2518 }
2519
2520 return 1;
2521 }
2522
2523 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2524 MBEDTLS_MD_SHA1, 20,
2525 sha1sum, rsa_ciphertext) != 0) {
2526 if (verbose != 0) {
2527 mbedtls_printf("failed\n");
2528 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
Hanno Becker5bc87292017-05-03 15:09:31 +01002530 ret = 1;
2531 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 }
2533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 if (verbose != 0) {
2535 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2536 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2539 sha1sum, rsa_ciphertext) != 0) {
2540 if (verbose != 0) {
2541 mbedtls_printf("failed\n");
2542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002543
Hanno Becker5bc87292017-05-03 15:09:31 +01002544 ret = 1;
2545 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 }
2547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 if (verbose != 0) {
2549 mbedtls_printf("passed\n");
2550 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (verbose != 0) {
2554 mbedtls_printf("\n");
2555 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002556
Paul Bakker3d8fb632014-04-17 12:42:41 +02002557cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 mbedtls_mpi_free(&K);
2559 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002561 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002564}
2565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568#endif /* MBEDTLS_RSA_C */