blob: 4955052bea47453695f150e66ca59983991b85d2 [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é-Gonnard2d6d9932023-03-28 11:38:08 +020049#include "md_psa.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. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200481 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 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
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000548 if (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
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000553 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Waleed Elmelegy76336c32023-06-30 16:48:19 +0100554 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
555 goto cleanup;
556 }
557
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 /*
559 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800560 * 1. |P-Q| > 2^( nbits / 2 - 100 )
561 * 2. GCD( E, (P-1)*(Q-1) ) == 1
562 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 do {
567 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
568 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
571 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800573 /* 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 +0100574 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
575 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800579 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (H.s < 0) {
581 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
582 }
Janos Follathef441782016-09-21 13:18:12 +0100583
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100584 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
586 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
587 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800588
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800589 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
591 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800592 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800594
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800595 /* 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 +0100596 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
597 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
598 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 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 -0800601 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800603
604 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100607 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
609 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100614
Jethro Beekman97f95c92018-02-13 15:50:36 -0800615#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 * DP = D mod (P - 1)
618 * DQ = D mod (Q - 1)
619 * QP = Q^-1 mod P
620 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
622 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100623#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
Hanno Becker83aad1f2017-08-23 06:45:10 +0100625 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
628cleanup:
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 mbedtls_mpi_free(&H);
631 mbedtls_mpi_free(&G);
632 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (ret != 0) {
635 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 if ((-ret & ~0x7f) == 0) {
638 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
639 }
640 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 }
642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000644}
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000647
648/*
649 * Check a public RSA key
650 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100651int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000652{
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
654 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100655 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
658 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100659 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
662 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
663 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
664 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
665 }
666
667 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000668}
669
670/*
Hanno Becker705fc682017-10-10 17:57:02 +0100671 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000674{
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
676 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
677 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 }
Paul Bakker48377d92013-08-30 12:06:24 +0200679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
681 &ctx->D, &ctx->E, NULL, NULL) != 0) {
682 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000684
Hanno Beckerb269a852017-08-25 08:03:21 +0100685#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
687 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
688 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100689 }
690#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000693}
694
695/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696 * Check if contexts holding a public and private key match
697 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
699 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100700{
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
702 mbedtls_rsa_check_privkey(prv) != 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 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
707 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
708 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100709 }
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100712}
713
714/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 * Do an RSA public key operation
716 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
718 const unsigned char *input,
719 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000720{
Janos Follath24eed8d2019-11-22 13:21:35 +0000721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000722 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
726 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
727 }
Hanno Becker705fc682017-10-10 17:57:02 +0100728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200731#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
733 return ret;
734 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200735#endif
736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200740 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
741 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 }
743
744 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
746 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
748cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
751 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
752 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100753#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if (ret != 0) {
758 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
759 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000762}
763
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200764/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200765 * Generate or update blinding values, see section 10 of:
766 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200767 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200768 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200769 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100770static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
771 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200772{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200773 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200774 mbedtls_mpi R;
775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200779 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
781 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
782 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
783 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200784
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200785 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200786 }
787
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200788 /* Unblinding value: Vf = random number, invertible mod N */
789 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200791 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
792 goto cleanup;
793 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 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 +0200796
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200797 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
799 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
800 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200801
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200802 /* At this point, Vi is invertible mod N if and only if both Vf and R
803 * are invertible mod N. If one of them isn't, we don't need to know
804 * which one, we just loop and choose new values for both of them.
805 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
807 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200808 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500812
813 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
815 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200816
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200817 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200818 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 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 +0200820
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200821
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200822cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200826}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200827
Paul Bakker5121ce52009-01-03 21:22:43 +0000828/*
Janos Follathe81102e2017-03-22 13:38:28 +0000829 * Exponent blinding supposed to prevent side-channel attacks using multiple
830 * traces of measurements to recover the RSA key. The more collisions are there,
831 * the more bits of the key can be recovered. See [3].
832 *
833 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800834 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000835 *
836 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800837 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +0000838 *
839 * (With the currently (as of 2017 April) known best algorithms breaking 2048
840 * bit RSA requires approximately as much time as trying out 2^112 random keys.
841 * Thus in this sense with 28 byte blinding the security is not reduced by
842 * side-channel attacks like the one in [3])
843 *
844 * This countermeasure does not help if the key recovery is possible with a
845 * single trace.
846 */
847#define RSA_EXPONENT_BLINDING 28
848
849/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000850 * Do an RSA private key operation
851 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100852int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
853 int (*f_rng)(void *, unsigned char *, size_t),
854 void *p_rng,
855 const unsigned char *input,
856 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000857{
Janos Follath24eed8d2019-11-22 13:21:35 +0000858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000859 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100860
861 /* Temporary holding the result */
862 mbedtls_mpi T;
863
864 /* Temporaries holding P-1, Q-1 and the
865 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000866 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100867
868#if !defined(MBEDTLS_RSA_NO_CRT)
869 /* Temporaries holding the results mod p resp. mod q. */
870 mbedtls_mpi TP, TQ;
871
872 /* Temporaries holding the blinded exponents for
873 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000874 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100875
876 /* Pointers to actual exponents to be used - either the unblinded
877 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000878 mbedtls_mpi *DP = &ctx->DP;
879 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100880#else
881 /* Temporary holding the blinded exponent (if used). */
882 mbedtls_mpi D_blind;
883
884 /* Pointer to actual exponent to be used - either the unblinded
885 * or the blinded one, depending on the presence of a PRNG. */
886 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100887#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100888
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100889 /* Temporaries holding the initial input and the double
890 * checked result; should be the same in the end. */
891 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (f_rng == NULL) {
894 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
895 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (rsa_check_context(ctx, 1 /* private key checks */,
898 1 /* blinding on */) != 0) {
899 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +0100900 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100901
Hanno Becker06811ce2017-05-03 15:10:34 +0100902#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
904 return ret;
905 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100906#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000907
Hanno Becker06811ce2017-05-03 15:10:34 +0100908 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +0100910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 mbedtls_mpi_init(&P1);
912 mbedtls_mpi_init(&Q1);
913 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +0000914
Janos Follathe81102e2017-03-22 13:38:28 +0000915#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +0000917#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 mbedtls_mpi_init(&DP_blind);
919 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +0000920#endif
921
Hanno Becker06811ce2017-05-03 15:10:34 +0100922#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200924#endif
925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 mbedtls_mpi_init(&I);
927 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +0100928
929 /* End of MPI initialization */
930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
932 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200933 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
934 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 }
936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +0100938
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200939 /*
940 * Blinding
941 * T = T * Vi mod N
942 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
944 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
945 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +0000946
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200947 /*
948 * Exponent blinding
949 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
951 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +0000952
Janos Follathf9203b42017-03-22 15:13:15 +0000953#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200954 /*
955 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
956 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
958 f_rng, p_rng));
959 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
960 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
961 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +0000962
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200963 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000964#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200965 /*
966 * DP_blind = ( P - 1 ) * R + DP
967 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
969 f_rng, p_rng));
970 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
971 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
972 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +0000973
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200974 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000975
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200976 /*
977 * DQ_blind = ( Q - 1 ) * R + DQ
978 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
980 f_rng, p_rng));
981 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
982 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
983 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +0000984
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +0200985 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000986#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100990#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200991 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000992 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100994 * TP = input ^ dP mod P
995 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
999 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
1001 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001002 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1005 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1006 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
1008 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001009 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1012 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001014
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001015 /*
1016 * Unblind
1017 * T = T * Vf mod N
1018 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1020 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Hanno Becker2dec5e82017-10-03 07:49:52 +01001022 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1024 &ctx->N, &ctx->RN));
1025 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001026 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1027 goto cleanup;
1028 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001029
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
1033cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1036 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1037 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001038#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 mbedtls_mpi_free(&P1);
1041 mbedtls_mpi_free(&Q1);
1042 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001043
Janos Follathe81102e2017-03-22 13:38:28 +00001044#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001046#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 mbedtls_mpi_free(&DP_blind);
1048 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001052
1053#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001055#endif
1056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 mbedtls_mpi_free(&C);
1058 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (ret != 0 && ret >= -0x007f) {
1061 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1062 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001065}
1066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001068/**
1069 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1070 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001071 * \param dst buffer to mask
1072 * \param dlen length of destination buffer
1073 * \param src source of the mask generation
1074 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001075 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1078 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001079{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001080 unsigned char counter[4];
1081 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001082 unsigned int hlen;
1083 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001084 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001085 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001086 const mbedtls_md_info_t *md_info;
1087 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 mbedtls_md_init(&md_ctx);
1090 md_info = mbedtls_md_info_from_type(md_alg);
1091 if (md_info == NULL) {
1092 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1093 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 mbedtls_md_init(&md_ctx);
1096 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001097 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 memset(mask, 0, sizeof(mask));
1103 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001104
Simon Butcher02037452016-03-01 21:19:12 +00001105 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001106 p = dst;
1107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001109 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001111 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 }
1117 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 }
1120 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 }
1123 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001128 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001130
1131 counter[3]++;
1132
1133 dlen -= use_len;
1134 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001135
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001136exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001141}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001142
1143/**
1144 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1145 *
1146 * \param hash the input hash
1147 * \param hlen length of the input hash
1148 * \param salt the input salt
1149 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001150 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001151 * \param md_alg message digest to use
1152 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001153static int hash_mprime(const unsigned char *hash, size_t hlen,
1154 const unsigned char *salt, size_t slen,
1155 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001156{
1157 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001158
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001159 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1163 if (md_info == NULL) {
1164 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1165 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 mbedtls_md_init(&md_ctx);
1168 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001169 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 }
1171 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001172 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 }
1174 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001175 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 }
1177 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 }
1180 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 }
1183 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001184 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001186
1187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001191}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001192
1193/**
1194 * Compute a hash.
1195 *
1196 * \param md_alg algorithm to use
1197 * \param input input message to hash
1198 * \param ilen input length
1199 * \param output the output buffer - must be large enough for \p md_alg
1200 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001201static int compute_hash(mbedtls_md_type_t md_alg,
1202 const unsigned char *input, size_t ilen,
1203 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001204{
1205 const mbedtls_md_info_t *md_info;
1206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 md_info = mbedtls_md_info_from_type(md_alg);
1208 if (md_info == NULL) {
1209 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1210 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001213}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001217/*
1218 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001220int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1221 int (*f_rng)(void *, unsigned char *, size_t),
1222 void *p_rng,
1223 const unsigned char *label, size_t label_len,
1224 size_t ilen,
1225 const unsigned char *input,
1226 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001227{
1228 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001230 unsigned char *p = output;
1231 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 if (f_rng == NULL) {
1234 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1235 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001236
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001237 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (hlen == 0) {
1239 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1240 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001241
1242 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001243
Simon Butcher02037452016-03-01 21:19:12 +00001244 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1246 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1247 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001248
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001250
1251 *p++ = 0;
1252
Simon Butcher02037452016-03-01 21:19:12 +00001253 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1255 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1256 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001257
1258 p += hlen;
1259
Simon Butcher02037452016-03-01 21:19:12 +00001260 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1262 if (ret != 0) {
1263 return ret;
1264 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001265 p += hlen;
1266 p += olen - 2 * hlen - 2 - ilen;
1267 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (ilen != 0) {
1269 memcpy(p, input, ilen);
1270 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001271
Simon Butcher02037452016-03-01 21:19:12 +00001272 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001274 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 return ret;
1276 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001277
Simon Butcher02037452016-03-01 21:19:12 +00001278 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001280 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 return ret;
1282 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001289/*
1290 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1293 int (*f_rng)(void *, unsigned char *, size_t),
1294 void *p_rng, size_t ilen,
1295 const unsigned char *input,
1296 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001297{
1298 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001300 unsigned char *p = output;
1301
Paul Bakkerb3869132013-02-28 17:21:01 +01001302 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001303
Simon Butcher02037452016-03-01 21:19:12 +00001304 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (ilen + 11 < ilen || olen < ilen + 11) {
1306 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1307 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001308
1309 nb_pad = olen - 3 - ilen;
1310
1311 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (f_rng == NULL) {
1314 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1315 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001316
1317 *p++ = MBEDTLS_RSA_CRYPT;
1318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001320 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001321
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001322 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 ret = f_rng(p_rng, p, 1);
1324 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001325
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001326 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 if (rng_dl == 0 || ret != 0) {
1328 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1329 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001330
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001331 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001332 }
1333
1334 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (ilen != 0) {
1336 memcpy(p, input, ilen);
1337 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001340}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001342
Paul Bakker5121ce52009-01-03 21:22:43 +00001343/*
1344 * Add the message padding, then do an RSA operation
1345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1347 int (*f_rng)(void *, unsigned char *, size_t),
1348 void *p_rng,
1349 size_t ilen,
1350 const unsigned char *input,
1351 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001352{
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#if defined(MBEDTLS_PKCS1_V15)
1355 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1357 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001358#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#if defined(MBEDTLS_PKCS1_V21)
1361 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1363 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001364#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
1366 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001369}
1370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001372/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001373 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001375int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1376 int (*f_rng)(void *, unsigned char *, size_t),
1377 void *p_rng,
1378 const unsigned char *label, size_t label_len,
1379 size_t *olen,
1380 const unsigned char *input,
1381 unsigned char *output,
1382 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001383{
Janos Follath24eed8d2019-11-22 13:21:35 +00001384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001385 size_t ilen, i, pad_len;
1386 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001388 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001389 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001390
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001391 /*
1392 * Parameters sanity checks
1393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1395 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1396 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
1398 ilen = ctx->len;
1399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 if (ilen < 16 || ilen > sizeof(buf)) {
1401 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1402 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001404 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if (hlen == 0) {
1406 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1407 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001408
Janos Follathc17cda12016-02-11 11:08:18 +00001409 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 if (2 * hlen + 2 > ilen) {
1411 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1412 }
Janos Follathc17cda12016-02-11 11:08:18 +00001413
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001414 /*
1415 * RSA operation
1416 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001420 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001422
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001423 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001424 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001425 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001426 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001428 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 /* DB: Apply dbMask to maskedDB */
1430 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001431 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001432 goto cleanup;
1433 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001434
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001435 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1437 label, label_len, lhash);
1438 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001439 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001441
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001442 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001443 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001444 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001446 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001447
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001448 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001449
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001450 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001451
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001452 /* Check lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 for (i = 0; i < hlen; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001454 bad |= lhash[i] ^ *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001456
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001457 /* Get zero-padding len, but always read till end of buffer
1458 * (minus one, for the 01 byte) */
1459 pad_len = 0;
1460 pad_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001462 pad_done |= p[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001464 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001465
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001466 p += pad_len;
1467 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +01001468
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001469 /*
1470 * The only information "leaked" is whether the padding was correct or not
1471 * (eg, no data is copied if it was not correct). This meets the
1472 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1473 * the different error conditions.
1474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if (bad != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001476 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1477 goto cleanup;
1478 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if (ilen - (p - buf) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001481 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1482 goto cleanup;
1483 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001484
1485 *olen = ilen - (p - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (*olen != 0) {
1487 memcpy(output, p, *olen);
1488 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001489 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001490
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001491cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 mbedtls_platform_zeroize(buf, sizeof(buf));
1493 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001500/*
1501 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1504 int (*f_rng)(void *, unsigned char *, size_t),
1505 void *p_rng,
1506 size_t *olen,
1507 const unsigned char *input,
1508 unsigned char *output,
1509 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001510{
1511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1512 size_t ilen;
1513 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1514
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001515 ilen = ctx->len;
1516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1518 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1519 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 if (ilen < 16 || ilen > sizeof(buf)) {
1522 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1523 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001528 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1532 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001533
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001534cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001538}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
1541/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001542 * Do an RSA operation, then remove the message padding
1543 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001544int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1545 int (*f_rng)(void *, unsigned char *, size_t),
1546 void *p_rng,
1547 size_t *olen,
1548 const unsigned char *input,
1549 unsigned char *output,
1550 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001551{
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#if defined(MBEDTLS_PKCS1_V15)
1554 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1556 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001557#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#if defined(MBEDTLS_PKCS1_V21)
1560 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1562 olen, input, output,
1563 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001564#endif
1565
1566 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001568 }
1569}
1570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +01001572static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1573 int (*f_rng)(void *, unsigned char *, size_t),
1574 void *p_rng,
1575 mbedtls_md_type_t md_alg,
1576 unsigned int hashlen,
1577 const unsigned char *hash,
1578 int saltlen,
1579 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001580{
1581 size_t olen;
1582 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001583 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001584 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001586 size_t msb;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001589 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1593 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1594 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (f_rng == NULL) {
1597 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1598 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001599
1600 olen = ctx->len;
1601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001603 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001604 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 if (exp_hashlen == 0) {
1606 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1607 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 if (hashlen != exp_hashlen) {
1610 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1611 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001612 }
1613
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001614 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 if (hlen == 0) {
1616 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1617 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1620 /* Calculate the largest possible salt length, up to the hash size.
1621 * Normally this is the hash length, which is the maximum salt length
1622 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1623 * enough room, use the maximum salt length that fits. The constraint is
1624 * that the hash length plus the salt length plus 2 bytes must be at most
1625 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1626 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001627 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 if (olen < hlen + min_slen + 2) {
1629 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1630 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001631 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001633 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 }
1635 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1636 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1637 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001638 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001639 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001642
Simon Butcher02037452016-03-01 21:19:12 +00001643 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001645 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001646 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001647
1648 /* Generate salt of length slen in place in the encoded message */
1649 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1651 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1652 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001653
Paul Bakkerb3869132013-02-28 17:21:01 +01001654 p += slen;
1655
Simon Butcher02037452016-03-01 21:19:12 +00001656 /* Generate H = Hash( M' ) */
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001657 ret = hash_mprime(hash, hashlen, salt, slen, p, (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 if (ret != 0) {
1659 return ret;
1660 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001661
Simon Butcher02037452016-03-01 21:19:12 +00001662 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001664 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001666
Simon Butcher02037452016-03-01 21:19:12 +00001667 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001669 (mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (ret != 0) {
1671 return ret;
1672 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1675 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001676
1677 p += hlen;
1678 *p++ = 0xBC;
1679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001681}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001682
1683/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001684 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1685 * the option to pass in the salt length.
1686 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001687int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1688 int (*f_rng)(void *, unsigned char *, size_t),
1689 void *p_rng,
1690 mbedtls_md_type_t md_alg,
1691 unsigned int hashlen,
1692 const unsigned char *hash,
1693 int saltlen,
1694 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001695{
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1697 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001698}
1699
1700
1701/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001702 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1703 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001704int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1705 int (*f_rng)(void *, unsigned char *, size_t),
1706 void *p_rng,
1707 mbedtls_md_type_t md_alg,
1708 unsigned int hashlen,
1709 const unsigned char *hash,
1710 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001711{
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1713 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001714}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001718/*
1719 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1720 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001721
1722/* Construct a PKCS v1.5 encoding of a hashed message
1723 *
1724 * This is used both for signature generation and verification.
1725 *
1726 * Parameters:
1727 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001728 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001729 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001730 * - hash: Buffer containing the hashed message or the raw data.
1731 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001732 * - dst: Buffer to hold the encoded message.
1733 *
1734 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001735 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001736 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001737 *
1738 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001739static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1740 unsigned int hashlen,
1741 const unsigned char *hash,
1742 size_t dst_len,
1743 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001744{
1745 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001746 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001747 unsigned char *p = dst;
1748 const char *oid = NULL;
1749
1750 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001752 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (md_size == 0) {
1754 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1755 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1758 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1759 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 if (hashlen != md_size) {
1762 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1763 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001764
1765 /* Double-check that 8 + hashlen + oid_size can be used as a
1766 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001768 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 10 + hashlen + oid_size < 10 + hashlen) {
1770 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1771 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001772
1773 /*
1774 * Static bounds check:
1775 * - Need 10 bytes for five tag-length pairs.
1776 * (Insist on 1-byte length encodings to protect against variants of
1777 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1778 * - Need hashlen bytes for hash
1779 * - Need oid_size bytes for hash alg OID.
1780 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (nb_pad < 10 + hashlen + oid_size) {
1782 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1783 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001784 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 } else {
1786 if (nb_pad < hashlen) {
1787 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1788 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001789
1790 nb_pad -= hashlen;
1791 }
1792
Hanno Becker2b2f8982017-09-27 17:10:03 +01001793 /* Need space for signature header and padding delimiter (3 bytes),
1794 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if (nb_pad < 3 + 8) {
1796 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1797 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001798 nb_pad -= 3;
1799
1800 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001801 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001802
1803 /* Write signature header and padding */
1804 *p++ = 0;
1805 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001807 p += nb_pad;
1808 *p++ = 0;
1809
1810 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 if (md_alg == MBEDTLS_MD_NONE) {
1812 memcpy(p, hash, hashlen);
1813 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001814 }
1815
1816 /* Signing hashed data, add corresponding ASN.1 structure
1817 *
1818 * DigestInfo ::= SEQUENCE {
1819 * digestAlgorithm DigestAlgorithmIdentifier,
1820 * digest Digest }
1821 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1822 * Digest ::= OCTET STRING
1823 *
1824 * Schematic:
1825 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
1826 * TAG-NULL + LEN [ NULL ] ]
1827 * TAG-OCTET + LEN [ HASH ] ]
1828 */
1829 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001831 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001833 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00001834 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001836 p += oid_size;
1837 *p++ = MBEDTLS_ASN1_NULL;
1838 *p++ = 0x00;
1839 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00001840 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01001842 p += hashlen;
1843
1844 /* Just a sanity-check, should be automatic
1845 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if (p != dst + dst_len) {
1847 mbedtls_platform_zeroize(dst, dst_len);
1848 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001849 }
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001852}
1853
Paul Bakkerb3869132013-02-28 17:21:01 +01001854/*
1855 * Do an RSA operation to sign the message digest
1856 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
1858 int (*f_rng)(void *, unsigned char *, size_t),
1859 void *p_rng,
1860 mbedtls_md_type_t md_alg,
1861 unsigned int hashlen,
1862 const unsigned char *hash,
1863 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001864{
Janos Follath24eed8d2019-11-22 13:21:35 +00001865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001866 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001869 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001871
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1873 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1874 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01001875
Hanno Beckerfdf38032017-09-06 12:35:55 +01001876 /*
1877 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
1878 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
1881 ctx->len, sig)) != 0) {
1882 return ret;
1883 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001884
Hanno Beckerfdf38032017-09-06 12:35:55 +01001885 /* Private key operation
1886 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001887 * In order to prevent Lenstra's attack, make the signature in a
1888 * temporary buffer and check it before returning it.
1889 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001890
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 sig_try = mbedtls_calloc(1, ctx->len);
1892 if (sig_try == NULL) {
1893 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00001894 }
1895
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 verif = mbedtls_calloc(1, ctx->len);
1897 if (verif == NULL) {
1898 mbedtls_free(sig_try);
1899 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
1900 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001901
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
1903 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
1904
1905 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001906 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1907 goto cleanup;
1908 }
1909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001911
1912cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 mbedtls_platform_zeroize(sig_try, ctx->len);
1914 mbedtls_platform_zeroize(verif, ctx->len);
1915 mbedtls_free(sig_try);
1916 mbedtls_free(verif);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 if (ret != 0) {
1919 memset(sig, '!', ctx->len);
1920 }
1921 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001922}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001924
1925/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 * Do an RSA operation to sign the message digest
1927 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001928int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
1929 int (*f_rng)(void *, unsigned char *, size_t),
1930 void *p_rng,
1931 mbedtls_md_type_t md_alg,
1932 unsigned int hashlen,
1933 const unsigned char *hash,
1934 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00001935{
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001937 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941#if defined(MBEDTLS_PKCS1_V15)
1942 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
1944 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02001945#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947#if defined(MBEDTLS_PKCS1_V21)
1948 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1950 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001951#endif
1952
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001956}
1957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001959/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001960 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001962int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
1963 mbedtls_md_type_t md_alg,
1964 unsigned int hashlen,
1965 const unsigned char *hash,
1966 mbedtls_md_type_t mgf1_hash_id,
1967 int expected_salt_len,
1968 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00001969{
Janos Follath24eed8d2019-11-22 13:21:35 +00001970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001971 size_t siglen;
1972 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001973 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001974 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001975 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02001976 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01001978
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001980 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001982
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 siglen = ctx->len;
1984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if (siglen < 16 || siglen > sizeof(buf)) {
1986 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1987 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 if (ret != 0) {
1992 return ret;
1993 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001994
1995 p = buf;
1996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 if (buf[siglen - 1] != 0xBC) {
1998 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001999 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 if (md_alg != MBEDTLS_MD_NONE) {
2002 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002003 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 if (exp_hashlen == 0) {
2005 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2006 }
2007
2008 if (hashlen != exp_hashlen) {
2009 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2010 }
2011 }
2012
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002013 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 if (hlen == 0) {
2015 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2016 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002017
Simon Butcher02037452016-03-01 21:19:12 +00002018 /*
2019 * Note: EMSA-PSS verification is over the length of N - 1 bits
2020 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 if (buf[0] >> (8 - siglen * 8 + msb)) {
2024 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2025 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002026
Simon Butcher02037452016-03-01 21:19:12 +00002027 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002029 p++;
2030 siglen -= 1;
2031 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 if (siglen < hlen + 2) {
2034 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2035 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002036 hash_start = p + siglen - hlen - 1;
2037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2039 if (ret != 0) {
2040 return ret;
2041 }
Paul Bakker02303e82013-01-03 11:08:31 +01002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002046 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (*p++ != 0x01) {
2050 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2051 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002052
Gilles Peskine6a54b022017-10-17 19:02:13 +02002053 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2056 observed_salt_len != (size_t) expected_salt_len) {
2057 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002058 }
2059
Simon Butcher02037452016-03-01 21:19:12 +00002060 /*
2061 * Generate H = Hash( M' )
2062 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2064 result, mgf1_hash_id);
2065 if (ret != 0) {
2066 return ret;
2067 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 if (memcmp(hash_start, result, hlen) != 0) {
2070 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2071 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002074}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002075
2076/*
2077 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002079int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2080 mbedtls_md_type_t md_alg,
2081 unsigned int hashlen,
2082 const unsigned char *hash,
2083 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002084{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002085 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002087 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002092 : md_alg;
2093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2095 md_alg, hashlen, hash,
2096 mgf1_hash_id,
2097 MBEDTLS_RSA_SALT_LEN_ANY,
2098 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002099
2100}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002104/*
2105 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2106 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002107int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2108 mbedtls_md_type_t md_alg,
2109 unsigned int hashlen,
2110 const unsigned char *hash,
2111 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002112{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002113 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002114 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002115 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002118 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002120
2121 sig_len = ctx->len;
2122
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002123 /*
2124 * Prepare expected PKCS1 v1.5 encoding of hash.
2125 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2128 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002129 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2130 goto cleanup;
2131 }
2132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2134 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002135 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002137
2138 /*
2139 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2140 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 ret = mbedtls_rsa_public(ctx, sig, encoded);
2143 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002144 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002146
Simon Butcher02037452016-03-01 21:19:12 +00002147 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002148 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002149 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2152 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002153 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2154 goto cleanup;
2155 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002156
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002157cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (encoded != NULL) {
2160 mbedtls_platform_zeroize(encoded, sig_len);
2161 mbedtls_free(encoded);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002162 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (encoded_expected != NULL) {
2165 mbedtls_platform_zeroize(encoded_expected, sig_len);
2166 mbedtls_free(encoded_expected);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002167 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002172
2173/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002174 * Do an RSA operation and check the message digest
2175 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002176int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2177 mbedtls_md_type_t md_alg,
2178 unsigned int hashlen,
2179 const unsigned char *hash,
2180 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002181{
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002183 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002185
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187#if defined(MBEDTLS_PKCS1_V15)
2188 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2190 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002191#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#if defined(MBEDTLS_PKCS1_V21)
2194 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2196 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002197#endif
2198
2199 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002201 }
2202}
2203
2204/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002205 * Copy the components of an RSA key
2206 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002207int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002208{
Janos Follath24eed8d2019-11-22 13:21:35 +00002209 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002210
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002211 dst->len = src->len;
2212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2214 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2217 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2218 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002219
2220#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2222 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2223 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2224 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2225 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002226#endif
2227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2231 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002232
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002233 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002234 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002235
2236cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (ret != 0) {
2238 mbedtls_rsa_free(dst);
2239 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002242}
2243
2244/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 * Free the components of an RSA key
2246 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002247void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002248{
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002250 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 mbedtls_mpi_free(&ctx->Vi);
2254 mbedtls_mpi_free(&ctx->Vf);
2255 mbedtls_mpi_free(&ctx->RN);
2256 mbedtls_mpi_free(&ctx->D);
2257 mbedtls_mpi_free(&ctx->Q);
2258 mbedtls_mpi_free(&ctx->P);
2259 mbedtls_mpi_free(&ctx->E);
2260 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002261
Hanno Becker33c30a02017-08-23 07:00:22 +01002262#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 mbedtls_mpi_free(&ctx->RQ);
2264 mbedtls_mpi_free(&ctx->RP);
2265 mbedtls_mpi_free(&ctx->QP);
2266 mbedtls_mpi_free(&ctx->DQ);
2267 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002268#endif /* MBEDTLS_RSA_NO_CRT */
2269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002271 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 if (ctx->ver != 0) {
2273 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002274 ctx->ver = 0;
2275 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002276#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002277}
2278
Hanno Beckerab377312017-08-23 16:24:51 +01002279#endif /* !MBEDTLS_RSA_ALT */
2280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002283#include "mbedtls/md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00002284
2285/*
2286 * Example RSA-1024 keypair, for test purposes
2287 */
2288#define KEY_LEN 128
2289
2290#define RSA_N "9292758453063D803DD603D5E777D788" \
2291 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2292 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2293 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2294 "93A89813FBF3C4F8066D2D800F7C38A8" \
2295 "1AE31942917403FF4946B0A83D3D3E05" \
2296 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2297 "5E94BB77B07507233A0BC7BAC8F90F79"
2298
2299#define RSA_E "10001"
2300
2301#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2302 "66CA472BC44D253102F8B4A9D3BFA750" \
2303 "91386C0077937FE33FA3252D28855837" \
2304 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2305 "DF79C5CE07EE72C7F123142198164234" \
2306 "CABB724CF78B8173B9F880FC86322407" \
2307 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2308 "071513A1E85B5DFA031F21ECAE91A34D"
2309
2310#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2311 "2C01CAD19EA484A87EA4377637E75500" \
2312 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2313 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2314
2315#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2316 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2317 "910E4168387E3C30AA1E00C339A79508" \
2318 "8452DD96A9A5EA5D9DCA68DA636032AF"
2319
Paul Bakker5121ce52009-01-03 21:22:43 +00002320#define PT_LEN 24
2321#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2322 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002325static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002326{
gufe44c2620da2020-08-03 17:56:50 +02002327#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002328 size_t i;
2329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002331 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 }
Paul Bakker545570e2010-07-18 09:00:25 +00002333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002335 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002337#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002339 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002343#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002346}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002348
Paul Bakker5121ce52009-01-03 21:22:43 +00002349/*
2350 * Checkup routine
2351 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002352int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002353{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002354 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002356 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 unsigned char rsa_plaintext[PT_LEN];
2359 unsigned char rsa_decrypted[PT_LEN];
2360 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002361#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002362 unsigned char sha1sum[20];
2363#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
Hanno Becker3a701162017-08-22 13:52:43 +01002365 mbedtls_mpi K;
2366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 mbedtls_mpi_init(&K);
2368 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2371 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2372 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2373 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2374 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2375 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2376 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2377 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2378 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2379 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 if (verbose != 0) {
2384 mbedtls_printf(" RSA key validation: ");
2385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2388 mbedtls_rsa_check_privkey(&rsa) != 0) {
2389 if (verbose != 0) {
2390 mbedtls_printf("failed\n");
2391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
Hanno Becker5bc87292017-05-03 15:09:31 +01002393 ret = 1;
2394 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 }
2396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 if (verbose != 0) {
2398 mbedtls_printf("passed\n PKCS#1 encryption : ");
2399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2404 PT_LEN, rsa_plaintext,
2405 rsa_ciphertext) != 0) {
2406 if (verbose != 0) {
2407 mbedtls_printf("failed\n");
2408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
Hanno Becker5bc87292017-05-03 15:09:31 +01002410 ret = 1;
2411 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 }
2413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if (verbose != 0) {
2415 mbedtls_printf("passed\n PKCS#1 decryption : ");
2416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2419 &len, rsa_ciphertext, rsa_decrypted,
2420 sizeof(rsa_decrypted)) != 0) {
2421 if (verbose != 0) {
2422 mbedtls_printf("failed\n");
2423 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002424
Hanno Becker5bc87292017-05-03 15:09:31 +01002425 ret = 1;
2426 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 }
2428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2430 if (verbose != 0) {
2431 mbedtls_printf("failed\n");
2432 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
Hanno Becker5bc87292017-05-03 15:09:31 +01002434 ret = 1;
2435 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 }
2437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if (verbose != 0) {
2439 mbedtls_printf("passed\n");
2440 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002441
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002442#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 if (verbose != 0) {
2444 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002445 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002447 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2448 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 if (verbose != 0) {
2450 mbedtls_printf("failed\n");
2451 }
2452
2453 return 1;
2454 }
2455
2456 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2457 MBEDTLS_MD_SHA1, 20,
2458 sha1sum, rsa_ciphertext) != 0) {
2459 if (verbose != 0) {
2460 mbedtls_printf("failed\n");
2461 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
Hanno Becker5bc87292017-05-03 15:09:31 +01002463 ret = 1;
2464 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 }
2466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 if (verbose != 0) {
2468 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2469 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2472 sha1sum, rsa_ciphertext) != 0) {
2473 if (verbose != 0) {
2474 mbedtls_printf("failed\n");
2475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
Hanno Becker5bc87292017-05-03 15:09:31 +01002477 ret = 1;
2478 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 }
2480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (verbose != 0) {
2482 mbedtls_printf("passed\n");
2483 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002484#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (verbose != 0) {
2487 mbedtls_printf("\n");
2488 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002489
Paul Bakker3d8fb632014-04-17 12:42:41 +02002490cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 mbedtls_mpi_free(&K);
2492 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002494 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002497}
2498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#endif /* MBEDTLS_RSA_C */