blob: 558cee0ef589b142db62547a7f7ddda60bf159b9 [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é-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010058
Hanno Beckera565f542017-10-11 11:00:19 +010059#if !defined(MBEDTLS_RSA_ALT)
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
62 const mbedtls_mpi *N,
63 const mbedtls_mpi *P, const mbedtls_mpi *Q,
64 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +010065{
Janos Follath24eed8d2019-11-22 13:21:35 +000066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +010067
Gilles Peskine449bd832023-01-11 14:50:10 +010068 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
69 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
70 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
71 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
72 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
73 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +010074 }
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076 if (N != NULL) {
77 ctx->len = mbedtls_mpi_size(&ctx->N);
78 }
Hanno Becker617c1ae2017-08-23 14:11:24 +010079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +010081}
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
84 unsigned char const *N, size_t N_len,
85 unsigned char const *P, size_t P_len,
86 unsigned char const *Q, size_t Q_len,
87 unsigned char const *D, size_t D_len,
88 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +010089{
Hanno Beckerd4d60572018-01-10 07:12:01 +000090 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (N != NULL) {
93 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
94 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +010095 }
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if (P != NULL) {
98 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
99 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (Q != NULL) {
102 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
103 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if (D != NULL) {
106 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
107 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (E != NULL) {
110 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
111 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100112
113cleanup:
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 if (ret != 0) {
116 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
117 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100120}
121
Hanno Becker705fc682017-10-10 17:57:02 +0100122/*
123 * Checks whether the context fields are set in such a way
124 * that the RSA primitives will be able to execute without error.
125 * It does *not* make guarantees for consistency of the parameters.
126 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100127static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
128 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100129{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100130#if !defined(MBEDTLS_RSA_NO_CRT)
131 /* blinding_needed is only used for NO_CRT to decide whether
132 * P,Q need to be present or not. */
133 ((void) blinding_needed);
134#endif
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
137 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
138 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000139 }
Hanno Becker705fc682017-10-10 17:57:02 +0100140
141 /*
142 * 1. Modular exponentiation needs positive, odd moduli.
143 */
144
145 /* Modular exponentiation wrt. N is always used for
146 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
148 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
149 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100150 }
151
152#if !defined(MBEDTLS_RSA_NO_CRT)
153 /* Modular exponentiation for P and Q is only
154 * used for private key operations and if CRT
155 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if (is_priv &&
157 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
158 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
159 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
160 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
161 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100162 }
163#endif /* !MBEDTLS_RSA_NO_CRT */
164
165 /*
166 * 2. Exponents must be positive
167 */
168
169 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
171 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
172 }
Hanno Becker705fc682017-10-10 17:57:02 +0100173
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100174#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100175 /* For private key operations, use D or DP & DQ
176 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
178 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
179 }
Hanno Becker705fc682017-10-10 17:57:02 +0100180#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (is_priv &&
182 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
183 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
184 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100185 }
186#endif /* MBEDTLS_RSA_NO_CRT */
187
188 /* Blinding shouldn't make exponents negative either,
189 * so check that P, Q >= 1 if that hasn't yet been
190 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100191#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (is_priv && blinding_needed &&
193 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
194 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
195 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100196 }
197#endif
198
199 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100200 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100201#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (is_priv &&
203 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
204 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100205 }
206#endif
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100209}
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100212{
213 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500214 int have_N, have_P, have_Q, have_D, have_E;
215#if !defined(MBEDTLS_RSA_NO_CRT)
216 int have_DP, have_DQ, have_QP;
217#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
221 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
222 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
223 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
224 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500225
226#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
228 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
229 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500230#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100231
Hanno Becker617c1ae2017-08-23 14:11:24 +0100232 /*
233 * Check whether provided parameters are enough
234 * to deduce all others. The following incomplete
235 * parameter sets for private keys are supported:
236 *
237 * (1) P, Q missing.
238 * (2) D and potentially N missing.
239 *
240 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100241
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500242 n_missing = have_P && have_Q && have_D && have_E;
243 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
244 d_missing = have_P && have_Q && !have_D && have_E;
245 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100246
247 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500248 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (!is_priv && !is_pub) {
251 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
252 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100253
254 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100255 * Step 1: Deduce N if P, Q are provided.
256 */
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (!have_N && have_P && have_Q) {
259 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
260 &ctx->Q)) != 0) {
261 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100262 }
263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100265 }
266
267 /*
268 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100269 */
270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (pq_missing) {
272 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
273 &ctx->P, &ctx->Q);
274 if (ret != 0) {
275 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
276 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 } else if (d_missing) {
279 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
280 &ctx->Q,
281 &ctx->E,
282 &ctx->D)) != 0) {
283 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100284 }
285 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100286
Hanno Becker617c1ae2017-08-23 14:11:24 +0100287 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100288 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100289 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100290 */
291
Hanno Becker23344b52017-08-23 07:43:27 +0100292#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (is_priv && !(have_DP && have_DQ && have_QP)) {
294 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
295 &ctx->DP, &ctx->DQ, &ctx->QP);
296 if (ret != 0) {
297 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
298 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100299 }
Hanno Becker23344b52017-08-23 07:43:27 +0100300#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100301
302 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100303 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100304 */
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100307}
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
310 unsigned char *N, size_t N_len,
311 unsigned char *P, size_t P_len,
312 unsigned char *Q, size_t Q_len,
313 unsigned char *D, size_t D_len,
314 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100315{
316 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500317 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100318
319 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500320 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
322 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
323 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
324 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
325 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100328 /* If we're trying to export private parameters for a public key,
329 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (P != NULL || Q != NULL || D != NULL) {
331 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
332 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100333
334 }
335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (N != NULL) {
337 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
338 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (P != NULL) {
341 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
342 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (Q != NULL) {
345 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
346 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (D != NULL) {
349 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
350 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (E != NULL) {
353 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
354 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100355
356cleanup:
357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100359}
360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
362 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
363 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100364{
Janos Follath24eed8d2019-11-22 13:21:35 +0000365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500366 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100367
368 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500369 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
371 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
372 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
373 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
374 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100377 /* If we're trying to export private parameters for a public key,
378 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if (P != NULL || Q != NULL || D != NULL) {
380 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
381 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100382
383 }
384
385 /* Export all requested core parameters. */
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
388 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
389 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
390 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
391 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
392 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100393 }
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100396}
397
398/*
399 * Export CRT parameters
400 * This must also be implemented if CRT is not used, for being able to
401 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
402 * can be used in this case.
403 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
405 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100406{
Janos Follath24eed8d2019-11-22 13:21:35 +0000407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500408 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100409
410 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500411 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
413 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
414 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
415 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
416 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (!is_priv) {
419 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
420 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100421
Hanno Beckerdc95c892017-08-23 06:57:02 +0100422#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100423 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
425 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
426 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
427 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100428 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100429#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
431 DP, DQ, QP)) != 0) {
432 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100433 }
434#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100437}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100438
Paul Bakker5121ce52009-01-03 21:22:43 +0000439/*
440 * Initialize an RSA context
441 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000443{
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000445
Ronald Cronc1905a12021-06-05 11:11:14 +0200446 ctx->padding = MBEDTLS_RSA_PKCS_V15;
447 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100450 /* Set ctx->ver to nonzero to indicate that the mutex has been
451 * initialized and will need to be freed. */
452 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200454#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000455}
456
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100457/*
458 * Set padding for an existing RSA context
459 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
461 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100462{
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200464#if defined(MBEDTLS_PKCS1_V15)
465 case MBEDTLS_RSA_PKCS_V15:
466 break;
467#endif
468
469#if defined(MBEDTLS_PKCS1_V21)
470 case MBEDTLS_RSA_PKCS_V21:
471 break;
472#endif
473 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200475 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200476
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200477#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
479 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200480 /* Just make sure this hash is supported in this build. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (mbedtls_hash_info_psa_from_md(hash_id) == PSA_ALG_NONE) {
482 return MBEDTLS_ERR_RSA_INVALID_PADDING;
483 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200484 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200485#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500486
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100487 ctx->padding = padding;
488 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100491}
492
Hanno Becker617c1ae2017-08-23 14:11:24 +0100493/*
Yanray Wang83548b52023-03-15 16:46:34 +0800494 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800495 */
496int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
497{
Yanray Wang644b9012023-03-15 16:50:31 +0800498 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +0800499}
500
501/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800502 * Get hash identifier of mbedtls_md_type_t type
503 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800504int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +0800505{
Yanray Wang644b9012023-03-15 16:50:31 +0800506 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +0800507}
508
509/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100510 * Get length in bytes of RSA modulus
511 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100512size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100513{
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100515}
516
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520/*
521 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800522 *
523 * This generation method follows the RSA key pair generation procedure of
524 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100526int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
527 int (*f_rng)(void *, unsigned char *, size_t),
528 void *p_rng,
529 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000530{
Janos Follath24eed8d2019-11-22 13:21:35 +0000531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800532 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100533 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
Janos Follathb8fc1b02018-09-03 15:37:01 +0100535 /*
536 * If the modulus is 1024 bit long or shorter, then the security strength of
537 * the RSA algorithm is less than or equal to 80 bits and therefore an error
538 * rate of 2^-80 is sufficient.
539 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100541 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 mbedtls_mpi_init(&H);
545 mbedtls_mpi_init(&G);
546 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (nbits < 128 || exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100549 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
550 goto cleanup;
551 }
552
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 /*
554 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800555 * 1. |P-Q| > 2^( nbits / 2 - 100 )
556 * 2. GCD( E, (P-1)*(Q-1) ) == 1
557 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 do {
562 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
563 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
566 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800568 /* 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 +0100569 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
570 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800574 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (H.s < 0) {
576 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
577 }
Janos Follathef441782016-09-21 13:18:12 +0100578
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100579 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
581 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
582 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800583
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800584 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
586 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800587 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800589
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800590 /* 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 +0100591 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
592 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
593 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 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 -0800596 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800598
599 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100602 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
604 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100609
Jethro Beekman97f95c92018-02-13 15:50:36 -0800610#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 * DP = D mod (P - 1)
613 * DQ = D mod (Q - 1)
614 * QP = Q^-1 mod P
615 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
617 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100618#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
Hanno Becker83aad1f2017-08-23 06:45:10 +0100620 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
623cleanup:
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 mbedtls_mpi_free(&H);
626 mbedtls_mpi_free(&G);
627 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (ret != 0) {
630 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if ((-ret & ~0x7f) == 0) {
633 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
634 }
635 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 }
637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000639}
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
643/*
644 * Check a public RSA key
645 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100646int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000647{
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
649 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100650 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
653 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100654 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
657 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
658 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
659 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
660 }
661
662 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000663}
664
665/*
Hanno Becker705fc682017-10-10 17:57:02 +0100666 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000669{
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
671 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
672 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 }
Paul Bakker48377d92013-08-30 12:06:24 +0200674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
676 &ctx->D, &ctx->E, NULL, NULL) != 0) {
677 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000679
Hanno Beckerb269a852017-08-25 08:03:21 +0100680#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
682 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
683 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100684 }
685#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000688}
689
690/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100691 * Check if contexts holding a public and private key match
692 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
694 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100695{
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
697 mbedtls_rsa_check_privkey(prv) != 0) {
698 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100699 }
700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
702 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
703 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100704 }
705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100707}
708
709/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000710 * Do an RSA public key operation
711 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
713 const unsigned char *input,
714 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000715{
Janos Follath24eed8d2019-11-22 13:21:35 +0000716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000717 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
721 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
722 }
Hanno Becker705fc682017-10-10 17:57:02 +0100723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200726#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
728 return ret;
729 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200730#endif
731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200735 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
736 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 }
738
739 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
741 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
743cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
746 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
747 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100748#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (ret != 0) {
753 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
754 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000757}
758
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200759/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200760 * Generate or update blinding values, see section 10 of:
761 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200762 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200763 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200764 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
766 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200767{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200768 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200769 mbedtls_mpi R;
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200774 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
776 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
777 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
778 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200779
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200780 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200781 }
782
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200783 /* Unblinding value: Vf = random number, invertible mod N */
784 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200786 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
787 goto cleanup;
788 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 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 +0200791
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200792 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
794 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
795 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200796
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200797 /* At this point, Vi is invertible mod N if and only if both Vf and R
798 * are invertible mod N. If one of them isn't, we don't need to know
799 * which one, we just loop and choose new values for both of them.
800 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
802 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200803 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500807
808 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
810 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200811
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200812 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200813 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 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 +0200815
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200816
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200817cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200821}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200822
Paul Bakker5121ce52009-01-03 21:22:43 +0000823/*
Janos Follathe81102e2017-03-22 13:38:28 +0000824 * Exponent blinding supposed to prevent side-channel attacks using multiple
825 * traces of measurements to recover the RSA key. The more collisions are there,
826 * the more bits of the key can be recovered. See [3].
827 *
828 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800829 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000830 *
831 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800832 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000833 *
834 * (With the currently (as of 2017 April) known best algorithms breaking 2048
835 * bit RSA requires approximately as much time as trying out 2^112 random keys.
836 * Thus in this sense with 28 byte blinding the security is not reduced by
837 * side-channel attacks like the one in [3])
838 *
839 * This countermeasure does not help if the key recovery is possible with a
840 * single trace.
841 */
842#define RSA_EXPONENT_BLINDING 28
843
844/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000845 * Do an RSA private key operation
846 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
848 int (*f_rng)(void *, unsigned char *, size_t),
849 void *p_rng,
850 const unsigned char *input,
851 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000852{
Janos Follath24eed8d2019-11-22 13:21:35 +0000853 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000854 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100855
856 /* Temporary holding the result */
857 mbedtls_mpi T;
858
859 /* Temporaries holding P-1, Q-1 and the
860 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000861 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100862
863#if !defined(MBEDTLS_RSA_NO_CRT)
864 /* Temporaries holding the results mod p resp. mod q. */
865 mbedtls_mpi TP, TQ;
866
867 /* Temporaries holding the blinded exponents for
868 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000869 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100870
871 /* Pointers to actual exponents to be used - either the unblinded
872 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000873 mbedtls_mpi *DP = &ctx->DP;
874 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100875#else
876 /* Temporary holding the blinded exponent (if used). */
877 mbedtls_mpi D_blind;
878
879 /* Pointer to actual exponent to be used - either the unblinded
880 * or the blinded one, depending on the presence of a PRNG. */
881 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100882#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100883
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100884 /* Temporaries holding the initial input and the double
885 * checked result; should be the same in the end. */
886 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if (f_rng == NULL) {
889 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
890 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if (rsa_check_context(ctx, 1 /* private key checks */,
893 1 /* blinding on */) != 0) {
894 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +0100895 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100896
Hanno Becker06811ce2017-05-03 15:10:34 +0100897#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
899 return ret;
900 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100901#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000902
Hanno Becker06811ce2017-05-03 15:10:34 +0100903 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +0100905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 mbedtls_mpi_init(&P1);
907 mbedtls_mpi_init(&Q1);
908 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +0000909
Janos Follathe81102e2017-03-22 13:38:28 +0000910#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +0000912#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 mbedtls_mpi_init(&DP_blind);
914 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +0000915#endif
916
Hanno Becker06811ce2017-05-03 15:10:34 +0100917#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200919#endif
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 mbedtls_mpi_init(&I);
922 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +0100923
924 /* End of MPI initialization */
925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
927 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200928 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
929 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 }
931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +0100933
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200934 /*
935 * Blinding
936 * T = T * Vi mod N
937 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
939 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
940 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +0000941
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200942 /*
943 * Exponent blinding
944 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
946 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +0000947
Janos Follathf9203b42017-03-22 15:13:15 +0000948#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200949 /*
950 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
951 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
953 f_rng, p_rng));
954 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
955 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
956 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +0000957
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200958 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000959#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200960 /*
961 * DP_blind = ( P - 1 ) * R + DP
962 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
964 f_rng, p_rng));
965 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
966 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
967 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +0000968
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200969 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000970
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200971 /*
972 * DQ_blind = ( Q - 1 ) * R + DQ
973 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
975 f_rng, p_rng));
976 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
977 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
978 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +0000979
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200980 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000981#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100985#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200986 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000987 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000988 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100989 * TP = input ^ dP mod P
990 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
994 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
996 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100997 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1000 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1001 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
1003 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001004 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1007 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001009
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001010 /*
1011 * Unblind
1012 * T = T * Vf mod N
1013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1015 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Hanno Becker2dec5e82017-10-03 07:49:52 +01001017 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1019 &ctx->N, &ctx->RN));
1020 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001021 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1022 goto cleanup;
1023 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001024
Paul Bakker5121ce52009-01-03 21:22:43 +00001025 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
1028cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1031 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1032 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001033#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 mbedtls_mpi_free(&P1);
1036 mbedtls_mpi_free(&Q1);
1037 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001038
Janos Follathe81102e2017-03-22 13:38:28 +00001039#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001041#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 mbedtls_mpi_free(&DP_blind);
1043 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001047
1048#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001050#endif
1051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 mbedtls_mpi_free(&C);
1053 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (ret != 0 && ret >= -0x007f) {
1056 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1057 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001060}
1061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001063/**
1064 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1065 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001066 * \param dst buffer to mask
1067 * \param dlen length of destination buffer
1068 * \param src source of the mask generation
1069 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001070 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001071 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1073 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001074{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001075 unsigned char counter[4];
1076 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001077 unsigned int hlen;
1078 size_t i, use_len;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001079 unsigned char mask[MBEDTLS_HASH_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001080 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001081 const mbedtls_md_info_t *md_info;
1082 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 mbedtls_md_init(&md_ctx);
1085 md_info = mbedtls_md_info_from_type(md_alg);
1086 if (md_info == NULL) {
1087 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1088 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 mbedtls_md_init(&md_ctx);
1091 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001092 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 memset(mask, 0, sizeof(mask));
1098 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001099
Simon Butcher02037452016-03-01 21:19:12 +00001100 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001101 p = dst;
1102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001106 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 }
1112 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 }
1115 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 }
1118 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001123 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001125
1126 counter[3]++;
1127
1128 dlen -= use_len;
1129 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001130
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001136}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001137
1138/**
1139 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1140 *
1141 * \param hash the input hash
1142 * \param hlen length of the input hash
1143 * \param salt the input salt
1144 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001145 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001146 * \param md_alg message digest to use
1147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001148static int hash_mprime(const unsigned char *hash, size_t hlen,
1149 const unsigned char *salt, size_t slen,
1150 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001151{
1152 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001153
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001154 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1158 if (md_info == NULL) {
1159 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1160 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 mbedtls_md_init(&md_ctx);
1163 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 }
1166 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001167 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 }
1169 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001170 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 }
1172 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 }
1175 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001176 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 }
1178 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001179 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001181
1182exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001186}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001187
1188/**
1189 * Compute a hash.
1190 *
1191 * \param md_alg algorithm to use
1192 * \param input input message to hash
1193 * \param ilen input length
1194 * \param output the output buffer - must be large enough for \p md_alg
1195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196static int compute_hash(mbedtls_md_type_t md_alg,
1197 const unsigned char *input, size_t ilen,
1198 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001199{
1200 const mbedtls_md_info_t *md_info;
1201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 md_info = mbedtls_md_info_from_type(md_alg);
1203 if (md_info == NULL) {
1204 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1205 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001212/*
1213 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1214 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001215int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1216 int (*f_rng)(void *, unsigned char *, size_t),
1217 void *p_rng,
1218 const unsigned char *label, size_t label_len,
1219 size_t ilen,
1220 const unsigned char *input,
1221 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001222{
1223 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001225 unsigned char *p = output;
1226 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 if (f_rng == NULL) {
1229 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1230 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1233 if (hlen == 0) {
1234 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1235 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001236
1237 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001238
Simon Butcher02037452016-03-01 21:19:12 +00001239 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1241 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1242 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001245
1246 *p++ = 0;
1247
Simon Butcher02037452016-03-01 21:19:12 +00001248 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1250 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1251 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001252
1253 p += hlen;
1254
Simon Butcher02037452016-03-01 21:19:12 +00001255 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1257 if (ret != 0) {
1258 return ret;
1259 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001260 p += hlen;
1261 p += olen - 2 * hlen - 2 - ilen;
1262 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 if (ilen != 0) {
1264 memcpy(p, input, ilen);
1265 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001266
Simon Butcher02037452016-03-01 21:19:12 +00001267 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1269 ctx->hash_id)) != 0) {
1270 return ret;
1271 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001272
Simon Butcher02037452016-03-01 21:19:12 +00001273 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1275 ctx->hash_id)) != 0) {
1276 return ret;
1277 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001284/*
1285 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1288 int (*f_rng)(void *, unsigned char *, size_t),
1289 void *p_rng, size_t ilen,
1290 const unsigned char *input,
1291 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001292{
1293 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001295 unsigned char *p = output;
1296
Paul Bakkerb3869132013-02-28 17:21:01 +01001297 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001298
Simon Butcher02037452016-03-01 21:19:12 +00001299 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (ilen + 11 < ilen || olen < ilen + 11) {
1301 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1302 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001303
1304 nb_pad = olen - 3 - ilen;
1305
1306 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if (f_rng == NULL) {
1309 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1310 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001311
1312 *p++ = MBEDTLS_RSA_CRYPT;
1313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001315 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001316
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001317 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 ret = f_rng(p_rng, p, 1);
1319 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001320
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001321 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if (rng_dl == 0 || ret != 0) {
1323 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1324 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001325
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001326 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001327 }
1328
1329 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (ilen != 0) {
1331 memcpy(p, input, ilen);
1332 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001335}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001337
Paul Bakker5121ce52009-01-03 21:22:43 +00001338/*
1339 * Add the message padding, then do an RSA operation
1340 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1342 int (*f_rng)(void *, unsigned char *, size_t),
1343 void *p_rng,
1344 size_t ilen,
1345 const unsigned char *input,
1346 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001347{
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_PKCS1_V15)
1350 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1352 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001353#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#if defined(MBEDTLS_PKCS1_V21)
1356 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1358 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001359#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001360
1361 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001364}
1365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001367/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001368 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001370int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1371 int (*f_rng)(void *, unsigned char *, size_t),
1372 void *p_rng,
1373 const unsigned char *label, size_t label_len,
1374 size_t *olen,
1375 const unsigned char *input,
1376 unsigned char *output,
1377 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001378{
Janos Follath24eed8d2019-11-22 13:21:35 +00001379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001380 size_t ilen, i, pad_len;
1381 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001383 unsigned char lhash[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001384 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001385
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001386 /*
1387 * Parameters sanity checks
1388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1390 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
1393 ilen = ctx->len;
1394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 if (ilen < 16 || ilen > sizeof(buf)) {
1396 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1400 if (hlen == 0) {
1401 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1402 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001403
Janos Follathc17cda12016-02-11 11:08:18 +00001404 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if (2 * hlen + 2 > ilen) {
1406 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1407 }
Janos Follathc17cda12016-02-11 11:08:18 +00001408
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001409 /*
1410 * RSA operation
1411 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001415 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001418 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001419 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001420 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001421 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1423 ctx->hash_id)) != 0 ||
1424 /* DB: Apply dbMask to maskedDB */
1425 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1426 ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001427 goto cleanup;
1428 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001429
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001430 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1432 label, label_len, lhash);
1433 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001434 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001436
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001437 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001438 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001439 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001441 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001443 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001444
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001445 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001446
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001447 /* Check lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 for (i = 0; i < hlen; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001449 bad |= lhash[i] ^ *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001451
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001452 /* Get zero-padding len, but always read till end of buffer
1453 * (minus one, for the 01 byte) */
1454 pad_len = 0;
1455 pad_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001457 pad_done |= p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001459 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001460
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001461 p += pad_len;
1462 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001463
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001464 /*
1465 * The only information "leaked" is whether the padding was correct or not
1466 * (eg, no data is copied if it was not correct). This meets the
1467 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1468 * the different error conditions.
1469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001471 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1472 goto cleanup;
1473 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001476 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1477 goto cleanup;
1478 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001479
1480 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (*olen != 0) {
1482 memcpy(output, p, *olen);
1483 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001484 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001485
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001486cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 mbedtls_platform_zeroize(buf, sizeof(buf));
1488 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001495/*
1496 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1497 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001498int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1499 int (*f_rng)(void *, unsigned char *, size_t),
1500 void *p_rng,
1501 size_t *olen,
1502 const unsigned char *input,
1503 unsigned char *output,
1504 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001505{
1506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1507 size_t ilen;
1508 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1509
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001510 ilen = ctx->len;
1511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1513 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1514 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 if (ilen < 16 || ilen > sizeof(buf)) {
1517 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1518 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001523 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1527 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001528
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001529cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001535
1536/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001537 * Do an RSA operation, then remove the message padding
1538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001539int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1540 int (*f_rng)(void *, unsigned char *, size_t),
1541 void *p_rng,
1542 size_t *olen,
1543 const unsigned char *input,
1544 unsigned char *output,
1545 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001546{
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#if defined(MBEDTLS_PKCS1_V15)
1549 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1551 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001552#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_PKCS1_V21)
1555 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1557 olen, input, output,
1558 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001559#endif
1560
1561 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001563 }
1564}
1565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001567static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1568 int (*f_rng)(void *, unsigned char *, size_t),
1569 void *p_rng,
1570 mbedtls_md_type_t md_alg,
1571 unsigned int hashlen,
1572 const unsigned char *hash,
1573 int saltlen,
1574 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001575{
1576 size_t olen;
1577 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001578 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001579 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001581 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001584 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1588 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1589 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (f_rng == NULL) {
1592 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1593 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001594
1595 olen = ctx->len;
1596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001598 /* Gather length of hash to sign */
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 size_t exp_hashlen = mbedtls_hash_info_get_size(md_alg);
1600 if (exp_hashlen == 0) {
1601 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1602 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 if (hashlen != exp_hashlen) {
1605 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1606 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001607 }
1608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 hlen = mbedtls_hash_info_get_size((mbedtls_md_type_t) ctx->hash_id);
1610 if (hlen == 0) {
1611 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1612 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1615 /* Calculate the largest possible salt length, up to the hash size.
1616 * Normally this is the hash length, which is the maximum salt length
1617 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1618 * enough room, use the maximum salt length that fits. The constraint is
1619 * that the hash length plus the salt length plus 2 bytes must be at most
1620 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1621 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001622 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (olen < hlen + min_slen + 2) {
1624 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1625 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001626 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001628 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 }
1630 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1631 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1632 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001633 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001634 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001637
Simon Butcher02037452016-03-01 21:19:12 +00001638 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001640 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001641 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001642
1643 /* Generate salt of length slen in place in the encoded message */
1644 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1646 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1647 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001648
Paul Bakkerb3869132013-02-28 17:21:01 +01001649 p += slen;
1650
Simon Butcher02037452016-03-01 21:19:12 +00001651 /* Generate H = Hash( M' ) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 ret = hash_mprime(hash, hashlen, salt, slen, p, ctx->hash_id);
1653 if (ret != 0) {
1654 return ret;
1655 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001656
Simon Butcher02037452016-03-01 21:19:12 +00001657 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001659 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001661
Simon Butcher02037452016-03-01 21:19:12 +00001662 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
1664 ctx->hash_id);
1665 if (ret != 0) {
1666 return ret;
1667 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1670 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001671
1672 p += hlen;
1673 *p++ = 0xBC;
1674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001676}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001677
1678/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001679 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1680 * the option to pass in the salt length.
1681 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001682int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1683 int (*f_rng)(void *, unsigned char *, size_t),
1684 void *p_rng,
1685 mbedtls_md_type_t md_alg,
1686 unsigned int hashlen,
1687 const unsigned char *hash,
1688 int saltlen,
1689 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001690{
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1692 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001693}
1694
1695
1696/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001697 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1698 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001699int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1700 int (*f_rng)(void *, unsigned char *, size_t),
1701 void *p_rng,
1702 mbedtls_md_type_t md_alg,
1703 unsigned int hashlen,
1704 const unsigned char *hash,
1705 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001706{
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1708 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001713/*
1714 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1715 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001716
1717/* Construct a PKCS v1.5 encoding of a hashed message
1718 *
1719 * This is used both for signature generation and verification.
1720 *
1721 * Parameters:
1722 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001723 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001724 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001725 * - hash: Buffer containing the hashed message or the raw data.
1726 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001727 * - dst: Buffer to hold the encoded message.
1728 *
1729 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001730 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001731 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001732 *
1733 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1735 unsigned int hashlen,
1736 const unsigned char *hash,
1737 size_t dst_len,
1738 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001739{
1740 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001741 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001742 unsigned char *p = dst;
1743 const char *oid = NULL;
1744
1745 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (md_alg != MBEDTLS_MD_NONE) {
1747 unsigned char md_size = mbedtls_hash_info_get_size(md_alg);
1748 if (md_size == 0) {
1749 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1750 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1753 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1754 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 if (hashlen != md_size) {
1757 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1758 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001759
1760 /* Double-check that 8 + hashlen + oid_size can be used as a
1761 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001763 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 10 + hashlen + oid_size < 10 + hashlen) {
1765 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1766 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001767
1768 /*
1769 * Static bounds check:
1770 * - Need 10 bytes for five tag-length pairs.
1771 * (Insist on 1-byte length encodings to protect against variants of
1772 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1773 * - Need hashlen bytes for hash
1774 * - Need oid_size bytes for hash alg OID.
1775 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if (nb_pad < 10 + hashlen + oid_size) {
1777 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1778 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001779 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 } else {
1781 if (nb_pad < hashlen) {
1782 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1783 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001784
1785 nb_pad -= hashlen;
1786 }
1787
Hanno Becker2b2f8982017-09-27 17:10:03 +01001788 /* Need space for signature header and padding delimiter (3 bytes),
1789 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 if (nb_pad < 3 + 8) {
1791 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1792 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001793 nb_pad -= 3;
1794
1795 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001796 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001797
1798 /* Write signature header and padding */
1799 *p++ = 0;
1800 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001802 p += nb_pad;
1803 *p++ = 0;
1804
1805 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (md_alg == MBEDTLS_MD_NONE) {
1807 memcpy(p, hash, hashlen);
1808 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001809 }
1810
1811 /* Signing hashed data, add corresponding ASN.1 structure
1812 *
1813 * DigestInfo ::= SEQUENCE {
1814 * digestAlgorithm DigestAlgorithmIdentifier,
1815 * digest Digest }
1816 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1817 * Digest ::= OCTET STRING
1818 *
1819 * Schematic:
1820 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1821 * TAG-NULL + LEN [ NULL ] ]
1822 * TAG-OCTET + LEN [ HASH ] ]
1823 */
1824 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001826 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001828 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001829 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001831 p += oid_size;
1832 *p++ = MBEDTLS_ASN1_NULL;
1833 *p++ = 0x00;
1834 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001835 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001837 p += hashlen;
1838
1839 /* Just a sanity-check, should be automatic
1840 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 if (p != dst + dst_len) {
1842 mbedtls_platform_zeroize(dst, dst_len);
1843 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001844 }
1845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001847}
1848
Paul Bakkerb3869132013-02-28 17:21:01 +01001849/*
1850 * Do an RSA operation to sign the message digest
1851 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001852int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
1853 int (*f_rng)(void *, unsigned char *, size_t),
1854 void *p_rng,
1855 mbedtls_md_type_t md_alg,
1856 unsigned int hashlen,
1857 const unsigned char *hash,
1858 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001859{
Janos Follath24eed8d2019-11-22 13:21:35 +00001860 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001861 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001864 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1868 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1869 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001870
Hanno Beckerfdf38032017-09-06 12:35:55 +01001871 /*
1872 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1873 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
1876 ctx->len, sig)) != 0) {
1877 return ret;
1878 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001879
Hanno Beckerfdf38032017-09-06 12:35:55 +01001880 /* Private key operation
1881 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001882 * In order to prevent Lenstra's attack, make the signature in a
1883 * temporary buffer and check it before returning it.
1884 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 sig_try = mbedtls_calloc(1, ctx->len);
1887 if (sig_try == NULL) {
1888 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00001889 }
1890
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 verif = mbedtls_calloc(1, ctx->len);
1892 if (verif == NULL) {
1893 mbedtls_free(sig_try);
1894 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
1895 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
1898 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
1899
1900 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001901 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1902 goto cleanup;
1903 }
1904
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001906
1907cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 mbedtls_platform_zeroize(sig_try, ctx->len);
1909 mbedtls_platform_zeroize(verif, ctx->len);
1910 mbedtls_free(sig_try);
1911 mbedtls_free(verif);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 if (ret != 0) {
1914 memset(sig, '!', ctx->len);
1915 }
1916 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001917}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001919
1920/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001921 * Do an RSA operation to sign the message digest
1922 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001923int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
1924 int (*f_rng)(void *, unsigned char *, size_t),
1925 void *p_rng,
1926 mbedtls_md_type_t md_alg,
1927 unsigned int hashlen,
1928 const unsigned char *hash,
1929 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00001930{
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001932 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001934
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936#if defined(MBEDTLS_PKCS1_V15)
1937 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
1939 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02001940#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942#if defined(MBEDTLS_PKCS1_V21)
1943 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1945 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001946#endif
1947
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001951}
1952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001954/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001955 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001957int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
1958 mbedtls_md_type_t md_alg,
1959 unsigned int hashlen,
1960 const unsigned char *hash,
1961 mbedtls_md_type_t mgf1_hash_id,
1962 int expected_salt_len,
1963 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00001964{
Janos Follath24eed8d2019-11-22 13:21:35 +00001965 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001966 size_t siglen;
1967 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001968 unsigned char *hash_start;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001969 unsigned char result[MBEDTLS_HASH_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001970 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001971 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01001973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001975 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001977
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 siglen = ctx->len;
1979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (siglen < 16 || siglen > sizeof(buf)) {
1981 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1982 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 if (ret != 0) {
1987 return ret;
1988 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
1990 p = buf;
1991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 if (buf[siglen - 1] != 0xBC) {
1993 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001994 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 if (md_alg != MBEDTLS_MD_NONE) {
1997 /* Gather length of hash to sign */
1998 size_t exp_hashlen = mbedtls_hash_info_get_size(md_alg);
1999 if (exp_hashlen == 0) {
2000 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2001 }
2002
2003 if (hashlen != exp_hashlen) {
2004 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2005 }
2006 }
2007
2008 hlen = mbedtls_hash_info_get_size(mgf1_hash_id);
2009 if (hlen == 0) {
2010 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2011 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002012
Simon Butcher02037452016-03-01 21:19:12 +00002013 /*
2014 * Note: EMSA-PSS verification is over the length of N - 1 bits
2015 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 if (buf[0] >> (8 - siglen * 8 + msb)) {
2019 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2020 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002021
Simon Butcher02037452016-03-01 21:19:12 +00002022 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002024 p++;
2025 siglen -= 1;
2026 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (siglen < hlen + 2) {
2029 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2030 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002031 hash_start = p + siglen - hlen - 1;
2032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2034 if (ret != 0) {
2035 return ret;
2036 }
Paul Bakker02303e82013-01-03 11:08:31 +01002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002041 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (*p++ != 0x01) {
2045 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2046 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002047
Gilles Peskine6a54b022017-10-17 19:02:13 +02002048 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2051 observed_salt_len != (size_t) expected_salt_len) {
2052 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002053 }
2054
Simon Butcher02037452016-03-01 21:19:12 +00002055 /*
2056 * Generate H = Hash( M' )
2057 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2059 result, mgf1_hash_id);
2060 if (ret != 0) {
2061 return ret;
2062 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (memcmp(hash_start, result, hlen) != 0) {
2065 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2066 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002069}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002070
2071/*
2072 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2073 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002074int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2075 mbedtls_md_type_t md_alg,
2076 unsigned int hashlen,
2077 const unsigned char *hash,
2078 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002079{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002080 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002082 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002087 : md_alg;
2088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2090 md_alg, hashlen, hash,
2091 mgf1_hash_id,
2092 MBEDTLS_RSA_SALT_LEN_ANY,
2093 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002094
2095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002099/*
2100 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002102int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2103 mbedtls_md_type_t md_alg,
2104 unsigned int hashlen,
2105 const unsigned char *hash,
2106 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002107{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002108 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002109 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002110 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002113 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002115
2116 sig_len = ctx->len;
2117
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002118 /*
2119 * Prepare expected PKCS1 v1.5 encoding of hash.
2120 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2123 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002124 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2125 goto cleanup;
2126 }
2127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2129 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002130 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002132
2133 /*
2134 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2135 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 ret = mbedtls_rsa_public(ctx, sig, encoded);
2138 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002139 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002141
Simon Butcher02037452016-03-01 21:19:12 +00002142 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002143 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002144 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2147 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002148 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2149 goto cleanup;
2150 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002151
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002152cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (encoded != NULL) {
2155 mbedtls_platform_zeroize(encoded, sig_len);
2156 mbedtls_free(encoded);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002157 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (encoded_expected != NULL) {
2160 mbedtls_platform_zeroize(encoded_expected, sig_len);
2161 mbedtls_free(encoded_expected);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002162 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002167
2168/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002169 * Do an RSA operation and check the message digest
2170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002171int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2172 mbedtls_md_type_t md_alg,
2173 unsigned int hashlen,
2174 const unsigned char *hash,
2175 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002176{
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002178 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#if defined(MBEDTLS_PKCS1_V15)
2183 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2185 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002186#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188#if defined(MBEDTLS_PKCS1_V21)
2189 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2191 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002192#endif
2193
2194 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002196 }
2197}
2198
2199/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002200 * Copy the components of an RSA key
2201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002202int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002203{
Janos Follath24eed8d2019-11-22 13:21:35 +00002204 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002205
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002206 dst->len = src->len;
2207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2209 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2212 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2213 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002214
2215#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2217 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2218 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2219 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2220 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002221#endif
2222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2226 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002227
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002228 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002229 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002230
2231cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 if (ret != 0) {
2233 mbedtls_rsa_free(dst);
2234 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002237}
2238
2239/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 * Free the components of an RSA key
2241 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002242void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002243{
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002245 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 mbedtls_mpi_free(&ctx->Vi);
2249 mbedtls_mpi_free(&ctx->Vf);
2250 mbedtls_mpi_free(&ctx->RN);
2251 mbedtls_mpi_free(&ctx->D);
2252 mbedtls_mpi_free(&ctx->Q);
2253 mbedtls_mpi_free(&ctx->P);
2254 mbedtls_mpi_free(&ctx->E);
2255 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002256
Hanno Becker33c30a02017-08-23 07:00:22 +01002257#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 mbedtls_mpi_free(&ctx->RQ);
2259 mbedtls_mpi_free(&ctx->RP);
2260 mbedtls_mpi_free(&ctx->QP);
2261 mbedtls_mpi_free(&ctx->DQ);
2262 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002263#endif /* MBEDTLS_RSA_NO_CRT */
2264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002266 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 if (ctx->ver != 0) {
2268 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002269 ctx->ver = 0;
2270 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002271#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002272}
2273
Hanno Beckerab377312017-08-23 16:24:51 +01002274#endif /* !MBEDTLS_RSA_ALT */
2275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002278#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
2280/*
2281 * Example RSA-1024 keypair, for test purposes
2282 */
2283#define KEY_LEN 128
2284
2285#define RSA_N "9292758453063D803DD603D5E777D788" \
2286 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2287 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2288 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2289 "93A89813FBF3C4F8066D2D800F7C38A8" \
2290 "1AE31942917403FF4946B0A83D3D3E05" \
2291 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2292 "5E94BB77B07507233A0BC7BAC8F90F79"
2293
2294#define RSA_E "10001"
2295
2296#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2297 "66CA472BC44D253102F8B4A9D3BFA750" \
2298 "91386C0077937FE33FA3252D28855837" \
2299 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2300 "DF79C5CE07EE72C7F123142198164234" \
2301 "CABB724CF78B8173B9F880FC86322407" \
2302 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2303 "071513A1E85B5DFA031F21ECAE91A34D"
2304
2305#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2306 "2C01CAD19EA484A87EA4377637E75500" \
2307 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2308 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2309
2310#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2311 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2312 "910E4168387E3C30AA1E00C339A79508" \
2313 "8452DD96A9A5EA5D9DCA68DA636032AF"
2314
Paul Bakker5121ce52009-01-03 21:22:43 +00002315#define PT_LEN 24
2316#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2317 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002320static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002321{
gufe44c2620da2020-08-03 17:56:50 +02002322#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002323 size_t i;
2324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002326 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 }
Paul Bakker545570e2010-07-18 09:00:25 +00002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002330 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002332#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002334 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002338#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002341}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002343
Paul Bakker5121ce52009-01-03 21:22:43 +00002344/*
2345 * Checkup routine
2346 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002347int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002348{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002349 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002351 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002353 unsigned char rsa_plaintext[PT_LEN];
2354 unsigned char rsa_decrypted[PT_LEN];
2355 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00002357 unsigned char sha1sum[20];
2358#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
Hanno Becker3a701162017-08-22 13:52:43 +01002360 mbedtls_mpi K;
2361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 mbedtls_mpi_init(&K);
2363 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2366 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2367 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2368 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2369 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2370 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2371 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2372 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2373 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2374 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (verbose != 0) {
2379 mbedtls_printf(" RSA key validation: ");
2380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2383 mbedtls_rsa_check_privkey(&rsa) != 0) {
2384 if (verbose != 0) {
2385 mbedtls_printf("failed\n");
2386 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
Hanno Becker5bc87292017-05-03 15:09:31 +01002388 ret = 1;
2389 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 }
2391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 if (verbose != 0) {
2393 mbedtls_printf("passed\n PKCS#1 encryption : ");
2394 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2399 PT_LEN, rsa_plaintext,
2400 rsa_ciphertext) != 0) {
2401 if (verbose != 0) {
2402 mbedtls_printf("failed\n");
2403 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002404
Hanno Becker5bc87292017-05-03 15:09:31 +01002405 ret = 1;
2406 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 }
2408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 if (verbose != 0) {
2410 mbedtls_printf("passed\n PKCS#1 decryption : ");
2411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2414 &len, rsa_ciphertext, rsa_decrypted,
2415 sizeof(rsa_decrypted)) != 0) {
2416 if (verbose != 0) {
2417 mbedtls_printf("failed\n");
2418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
Hanno Becker5bc87292017-05-03 15:09:31 +01002420 ret = 1;
2421 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 }
2423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2425 if (verbose != 0) {
2426 mbedtls_printf("failed\n");
2427 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
Hanno Becker5bc87292017-05-03 15:09:31 +01002429 ret = 1;
2430 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 }
2432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (verbose != 0) {
2434 mbedtls_printf("passed\n");
2435 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if (verbose != 0) {
2439 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002440 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002442 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2443 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 if (verbose != 0) {
2445 mbedtls_printf("failed\n");
2446 }
2447
2448 return 1;
2449 }
2450
2451 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2452 MBEDTLS_MD_SHA1, 20,
2453 sha1sum, rsa_ciphertext) != 0) {
2454 if (verbose != 0) {
2455 mbedtls_printf("failed\n");
2456 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
Hanno Becker5bc87292017-05-03 15:09:31 +01002458 ret = 1;
2459 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 }
2461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (verbose != 0) {
2463 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2467 sha1sum, rsa_ciphertext) != 0) {
2468 if (verbose != 0) {
2469 mbedtls_printf("failed\n");
2470 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
Hanno Becker5bc87292017-05-03 15:09:31 +01002472 ret = 1;
2473 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 }
2475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 if (verbose != 0) {
2477 mbedtls_printf("passed\n");
2478 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (verbose != 0) {
2482 mbedtls_printf("\n");
2483 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002484
Paul Bakker3d8fb632014-04-17 12:42:41 +02002485cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 mbedtls_mpi_free(&K);
2487 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002489 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002492}
2493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#endif /* MBEDTLS_RSA_C */