blob: a18c4b1b0e94acbe4c1f0d385d635c1552317a00 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
Hanno Becker74716312017-10-02 10:00:37 +01007
Paul Bakker5121ce52009-01-03 21:22:43 +00008/*
Simon Butcherbdae02c2016-01-20 00:44:42 +00009 * The following sources were referenced in the design of this implementation
10 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000011 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000012 * [1] A method for obtaining digital signatures and public-key cryptosystems
13 * R Rivest, A Shamir, and L Adleman
14 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
15 *
16 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
17 * Menezes, van Oorschot and Vanstone
18 *
Janos Follathe81102e2017-03-22 13:38:28 +000019 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
20 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
21 * Stefan Mangard
22 * https://arxiv.org/abs/1702.08719v2
23 *
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/rsa.h"
Chris Jones66a4cd42021-03-09 16:04:12 +000031#include "rsa_alt_helpers.h"
Tomi Fontanilles573dc232023-12-10 14:57:51 +020032#include "rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/oid.h"
Valerio Settib328c442024-01-23 10:48:45 +010034#include "mbedtls/asn1write.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050035#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000036#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020037#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020038#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020039#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
gufe44c2620da2020-08-03 17:56:50 +020043#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000044#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000045#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010048
Dave Rodgman19e8cd02023-05-09 11:10:21 +010049
50#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
51
52/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
53 * operation (EME-PKCS1-v1_5 decoding).
54 *
55 * \note The return value from this function is a sensitive value
56 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
57 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
58 * is often a situation that an attacker can provoke and leaking which
59 * one is the result is precisely the information the attacker wants.
60 *
61 * \param input The input buffer which is the payload inside PKCS#1v1.5
62 * encryption padding, called the "encoded message EM"
63 * by the terminology.
64 * \param ilen The length of the payload in the \p input buffer.
65 * \param output The buffer for the payload, called "message M" by the
66 * PKCS#1 terminology. This must be a writable buffer of
67 * length \p output_max_len bytes.
68 * \param olen The address at which to store the length of
69 * the payload. This must not be \c NULL.
70 * \param output_max_len The length in bytes of the output buffer \p output.
71 *
72 * \return \c 0 on success.
73 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
74 * The output buffer is too small for the unpadded payload.
75 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
76 * The input doesn't contain properly formatted padding.
77 */
78static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
79 size_t ilen,
80 unsigned char *output,
81 size_t output_max_len,
82 size_t *olen)
83{
84 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
85 size_t i, plaintext_max_size;
86
87 /* The following variables take sensitive values: their value must
88 * not leak into the observable behavior of the function other than
89 * the designated outputs (output, olen, return value). Otherwise
90 * this would open the execution of the function to
91 * side-channel-based variants of the Bleichenbacher padding oracle
92 * attack. Potential side channels include overall timing, memory
93 * access patterns (especially visible to an adversary who has access
94 * to a shared memory cache), and branches (especially visible to
95 * an adversary who has access to a shared code cache or to a shared
96 * branch predictor). */
97 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +010098 mbedtls_ct_condition_t bad;
99 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100100 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100101 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100102
103 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
104 : output_max_len;
105
106 /* Check and get padding length in constant time and constant
107 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100108 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100109
110
111 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
112 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100113 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100114
115 /* Read the whole buffer. Set pad_done to nonzero if we find
116 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100117 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100118 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100119 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100120 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100121 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100122 }
123
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100124 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100125 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100126
127 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100128 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100129
130 /* If the padding is valid, set plaintext_size to the number of
131 * remaining bytes after stripping the padding. If the padding
132 * is invalid, avoid leaking this fact through the size of the
133 * output: use the maximum message size that fits in the output
134 * buffer. Do it without branches to avoid leaking the padding
135 * validity through timing. RSA keys are small enough that all the
136 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100137 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100138 bad, (unsigned) plaintext_max_size,
139 (unsigned) (ilen - pad_count - 3));
140
141 /* Set output_too_large to 0 if the plaintext fits in the output
142 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100143 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100144 plaintext_max_size);
145
146 /* Set ret without branches to avoid timing attacks. Return:
147 * - INVALID_PADDING if the padding is bad (bad != 0).
148 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
149 * plaintext does not fit in the output buffer.
150 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100151 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100152 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100153 MBEDTLS_ERR_RSA_INVALID_PADDING,
154 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100155 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100156
157 /* If the padding is bad or the plaintext is too large, zero the
158 * data that we're about to copy to the output buffer.
159 * We need to copy the same amount of data
160 * from the same buffer whether the padding is good or not to
161 * avoid leaking the padding validity through overall timing or
162 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100163 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100164
165 /* If the plaintext is too large, truncate it to the buffer size.
166 * Copy anyway to avoid revealing the length through timing, because
167 * revealing the length is as bad as revealing the padding validity
168 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100169 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100170 (unsigned) plaintext_max_size,
171 (unsigned) plaintext_size);
172
173 /* Move the plaintext to the leftmost position where it can start in
174 * the working buffer, i.e. make it start plaintext_max_size from
175 * the end of the buffer. Do this with a memory access trace that
176 * does not depend on the plaintext size. After this move, the
177 * starting location of the plaintext is no longer sensitive
178 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100179 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
180 plaintext_max_size,
181 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100182
183 /* Finally copy the decrypted plaintext plus trailing zeros into the output
184 * buffer. If output_max_len is 0, then output may be an invalid pointer
185 * and the result of memcpy() would be undefined; prevent undefined
186 * behavior making sure to depend only on output_max_len (the size of the
187 * user-provided output buffer), which is independent from plaintext
188 * length, validity of padding, success of the decryption, and other
189 * secrets. */
190 if (output_max_len != 0) {
191 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
192 }
193
194 /* Report the amount of data we copied to the output buffer. In case
195 * of errors (bad padding or output too large), the value of *olen
196 * when this function returns is not specified. Making it equivalent
197 * to the good case limits the risks of leaking the padding validity. */
198 *olen = plaintext_size;
199
200 return ret;
201}
202
203#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
204
Hanno Beckera565f542017-10-11 11:00:19 +0100205#if !defined(MBEDTLS_RSA_ALT)
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
208 const mbedtls_mpi *N,
209 const mbedtls_mpi *P, const mbedtls_mpi *Q,
210 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100211{
Janos Follath24eed8d2019-11-22 13:21:35 +0000212 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
215 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
216 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
217 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
218 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
219 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100220 }
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (N != NULL) {
223 ctx->len = mbedtls_mpi_size(&ctx->N);
224 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100227}
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
230 unsigned char const *N, size_t N_len,
231 unsigned char const *P, size_t P_len,
232 unsigned char const *Q, size_t Q_len,
233 unsigned char const *D, size_t D_len,
234 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100235{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000236 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if (N != NULL) {
239 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
240 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100241 }
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (P != NULL) {
244 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
245 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (Q != NULL) {
248 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
249 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (D != NULL) {
252 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
253 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (E != NULL) {
256 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
257 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100258
259cleanup:
260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (ret != 0) {
262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
263 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100266}
267
Hanno Becker705fc682017-10-10 17:57:02 +0100268/*
269 * Checks whether the context fields are set in such a way
270 * that the RSA primitives will be able to execute without error.
271 * It does *not* make guarantees for consistency of the parameters.
272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
274 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100275{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100276#if !defined(MBEDTLS_RSA_NO_CRT)
277 /* blinding_needed is only used for NO_CRT to decide whether
278 * P,Q need to be present or not. */
279 ((void) blinding_needed);
280#endif
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
283 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
284 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000285 }
Hanno Becker705fc682017-10-10 17:57:02 +0100286
287 /*
288 * 1. Modular exponentiation needs positive, odd moduli.
289 */
290
291 /* Modular exponentiation wrt. N is always used for
292 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
294 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
295 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100296 }
297
298#if !defined(MBEDTLS_RSA_NO_CRT)
299 /* Modular exponentiation for P and Q is only
300 * used for private key operations and if CRT
301 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (is_priv &&
303 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
304 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
305 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
306 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
307 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100308 }
309#endif /* !MBEDTLS_RSA_NO_CRT */
310
311 /*
312 * 2. Exponents must be positive
313 */
314
315 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
317 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
318 }
Hanno Becker705fc682017-10-10 17:57:02 +0100319
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100320#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100321 /* For private key operations, use D or DP & DQ
322 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
324 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
325 }
Hanno Becker705fc682017-10-10 17:57:02 +0100326#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (is_priv &&
328 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
329 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
330 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100331 }
332#endif /* MBEDTLS_RSA_NO_CRT */
333
334 /* Blinding shouldn't make exponents negative either,
335 * so check that P, Q >= 1 if that hasn't yet been
336 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100337#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (is_priv && blinding_needed &&
339 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
340 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
341 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100342 }
343#endif
344
345 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100346 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100347#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (is_priv &&
349 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
350 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100351 }
352#endif
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100355}
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100358{
359 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500360 int have_N, have_P, have_Q, have_D, have_E;
361#if !defined(MBEDTLS_RSA_NO_CRT)
362 int have_DP, have_DQ, have_QP;
363#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500364 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
367 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
368 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
369 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
370 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500371
372#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
374 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
375 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500376#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100377
Hanno Becker617c1ae2017-08-23 14:11:24 +0100378 /*
379 * Check whether provided parameters are enough
380 * to deduce all others. The following incomplete
381 * parameter sets for private keys are supported:
382 *
383 * (1) P, Q missing.
384 * (2) D and potentially N missing.
385 *
386 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100387
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500388 n_missing = have_P && have_Q && have_D && have_E;
389 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
390 d_missing = have_P && have_Q && !have_D && have_E;
391 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100392
393 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500394 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (!is_priv && !is_pub) {
397 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
398 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100399
400 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100401 * Step 1: Deduce N if P, Q are provided.
402 */
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (!have_N && have_P && have_Q) {
405 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
406 &ctx->Q)) != 0) {
407 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100408 }
409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100411 }
412
413 /*
414 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100415 */
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (pq_missing) {
418 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
419 &ctx->P, &ctx->Q);
420 if (ret != 0) {
421 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
422 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 } else if (d_missing) {
425 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
426 &ctx->Q,
427 &ctx->E,
428 &ctx->D)) != 0) {
429 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100430 }
431 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100432
Hanno Becker617c1ae2017-08-23 14:11:24 +0100433 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100434 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100435 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100436 */
437
Hanno Becker23344b52017-08-23 07:43:27 +0100438#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (is_priv && !(have_DP && have_DQ && have_QP)) {
440 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
441 &ctx->DP, &ctx->DQ, &ctx->QP);
442 if (ret != 0) {
443 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
444 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100445 }
Hanno Becker23344b52017-08-23 07:43:27 +0100446#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100447
448 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100449 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100450 */
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100453}
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
456 unsigned char *N, size_t N_len,
457 unsigned char *P, size_t P_len,
458 unsigned char *Q, size_t Q_len,
459 unsigned char *D, size_t D_len,
460 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100461{
462 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500463 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100464
465 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500466 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
468 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
469 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
470 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
471 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100474 /* If we're trying to export private parameters for a public key,
475 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (P != NULL || Q != NULL || D != NULL) {
477 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
478 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100479
480 }
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if (N != NULL) {
483 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
484 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 if (P != NULL) {
487 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
488 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (Q != NULL) {
491 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
492 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (D != NULL) {
495 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
496 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (E != NULL) {
499 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
500 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100501
502cleanup:
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100505}
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
508 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
509 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100510{
Janos Follath24eed8d2019-11-22 13:21:35 +0000511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500512 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100513
514 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500515 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
517 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
518 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
519 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
520 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100523 /* If we're trying to export private parameters for a public key,
524 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (P != NULL || Q != NULL || D != NULL) {
526 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
527 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100528
529 }
530
531 /* Export all requested core parameters. */
532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
534 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
535 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
536 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
537 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
538 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100539 }
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100542}
543
544/*
545 * Export CRT parameters
546 * This must also be implemented if CRT is not used, for being able to
547 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
548 * can be used in this case.
549 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100550int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
551 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100552{
Janos Follath24eed8d2019-11-22 13:21:35 +0000553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500554 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100555
556 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500557 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
559 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
560 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
561 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
562 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (!is_priv) {
565 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
566 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100567
Hanno Beckerdc95c892017-08-23 06:57:02 +0100568#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100569 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
571 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
572 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
573 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100574 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100575#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
577 DP, DQ, QP)) != 0) {
578 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100579 }
580#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100583}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100584
Paul Bakker5121ce52009-01-03 21:22:43 +0000585/*
586 * Initialize an RSA context
587 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100588void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000589{
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Ronald Cronc1905a12021-06-05 11:11:14 +0200592 ctx->padding = MBEDTLS_RSA_PKCS_V15;
593 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100596 /* Set ctx->ver to nonzero to indicate that the mutex has been
597 * initialized and will need to be freed. */
598 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200600#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000601}
602
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100603/*
604 * Set padding for an existing RSA context
605 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
607 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100608{
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200610#if defined(MBEDTLS_PKCS1_V15)
611 case MBEDTLS_RSA_PKCS_V15:
612 break;
613#endif
614
615#if defined(MBEDTLS_PKCS1_V21)
616 case MBEDTLS_RSA_PKCS_V21:
617 break;
618#endif
619 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200621 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200622
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200623#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
625 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200626 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200627 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return MBEDTLS_ERR_RSA_INVALID_PADDING;
629 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200630 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200631#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500632
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100633 ctx->padding = padding;
634 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100637}
638
Hanno Becker617c1ae2017-08-23 14:11:24 +0100639/*
Yanray Wang83548b52023-03-15 16:46:34 +0800640 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800641 */
642int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
643{
Yanray Wang644b9012023-03-15 16:50:31 +0800644 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +0800645}
646
647/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800648 * Get hash identifier of mbedtls_md_type_t type
649 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800650int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +0800651{
Yanray Wang644b9012023-03-15 16:50:31 +0800652 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +0800653}
654
655/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100656 * Get length in bytes of RSA modulus
657 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100658size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100659{
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100661}
662
Valerio Settib328c442024-01-23 10:48:45 +0100663/*
664 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
665 *
666 * The value zero is:
667 * - never a valid value for an RSA parameter
668 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
669 *
670 * Since values can't be omitted in PKCS#1, passing a zero value to
671 * rsa_complete() would be incorrect, so reject zero values early.
672 */
673static int asn1_get_nonzero_mpi(unsigned char **p,
674 const unsigned char *end,
675 mbedtls_mpi *X)
676{
677 int ret;
678
679 ret = mbedtls_asn1_get_mpi(p, end, X);
680 if (ret != 0) {
681 return ret;
682 }
683
684 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
685 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
686 }
687
688 return 0;
689}
690
691/*
692 * Parse a PKCS#1 encoded private RSA key
693 */
694int mbedtls_rsa_key_parse(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
695{
696 int ret, version;
697 size_t len;
698 unsigned char *p, *end;
699
700 mbedtls_mpi T;
701 mbedtls_mpi_init(&T);
702
703 p = (unsigned char *) key;
704 end = p + keylen;
705
706 /*
707 * This function parses the RSAPrivateKey (PKCS#1)
708 *
709 * RSAPrivateKey ::= SEQUENCE {
710 * version Version,
711 * modulus INTEGER, -- n
712 * publicExponent INTEGER, -- e
713 * privateExponent INTEGER, -- d
714 * prime1 INTEGER, -- p
715 * prime2 INTEGER, -- q
716 * exponent1 INTEGER, -- d mod (p-1)
717 * exponent2 INTEGER, -- d mod (q-1)
718 * coefficient INTEGER, -- (inverse of q) mod p
719 * otherPrimeInfos OtherPrimeInfos OPTIONAL
720 * }
721 */
722 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
723 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
724 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
725 }
726
727 end = p + len;
728
729 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
730 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
731 }
732
733 if (version != 0) {
734 return MBEDTLS_ERR_PK_KEY_INVALID_VERSION;
735 }
736
737 /* Import N */
738 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
739 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
740 NULL, NULL)) != 0) {
741 goto cleanup;
742 }
743
744 /* Import E */
745 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
746 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
747 NULL, &T)) != 0) {
748 goto cleanup;
749 }
750
751 /* Import D */
752 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
753 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
754 &T, NULL)) != 0) {
755 goto cleanup;
756 }
757
758 /* Import P */
759 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
760 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
761 NULL, NULL)) != 0) {
762 goto cleanup;
763 }
764
765 /* Import Q */
766 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
767 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
768 NULL, NULL)) != 0) {
769 goto cleanup;
770 }
771
772#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
773 /*
774 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
775 * that they can be easily recomputed from D, P and Q. However by
776 * parsing them from the PKCS1 structure it is possible to avoid
777 * recalculating them which both reduces the overhead of loading
778 * RSA private keys into memory and also avoids side channels which
779 * can arise when computing those values, since all of D, P, and Q
780 * are secret. See https://eprint.iacr.org/2020/055 for a
781 * description of one such attack.
782 */
783
784 /* Import DP */
785 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
786 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
787 goto cleanup;
788 }
789
790 /* Import DQ */
791 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
792 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
793 goto cleanup;
794 }
795
796 /* Import QP */
797 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
798 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
799 goto cleanup;
800 }
801
802#else
803 /* Verify existence of the CRT params */
804 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
805 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
806 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
807 goto cleanup;
808 }
809#endif
810
811 /* rsa_complete() doesn't complete anything with the default
812 * implementation but is still called:
813 * - for the benefit of alternative implementation that may want to
814 * pre-compute stuff beyond what's provided (eg Montgomery factors)
815 * - as is also sanity-checks the key
816 *
817 * Furthermore, we also check the public part for consistency with
818 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
819 */
820 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
821 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
822 goto cleanup;
823 }
824
825 if (p != end) {
826 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
827 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
828 }
829
830cleanup:
831
832 mbedtls_mpi_free(&T);
833
834 if (ret != 0) {
835 /* Wrap error code if it's coming from a lower level */
836 if ((ret & 0xff80) == 0) {
837 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
838 } else {
839 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
840 }
841
842 mbedtls_rsa_free(rsa);
843 }
844
845 return ret;
846}
847
848/*
849 * RSAPublicKey ::= SEQUENCE {
850 * modulus INTEGER, -- n
851 * publicExponent INTEGER -- e
852 * }
853 */
854int mbedtls_rsa_pubkey_parse(mbedtls_rsa_context *rsa, unsigned char **p,
855 const unsigned char *end)
856{
857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
858 size_t len;
859
860 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
861 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
862 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
863 }
864
865 if (*p + len != end) {
866 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
867 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
868 }
869
870 /* Import N */
871 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
872 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
873 }
874
875 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
876 NULL, 0, NULL, 0)) != 0) {
877 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
878 }
879
880 *p += len;
881
882 /* Import E */
883 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
884 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret);
885 }
886
887 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
888 NULL, 0, *p, len)) != 0) {
889 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
890 }
891
892 *p += len;
893
894 if (mbedtls_rsa_complete(rsa) != 0 ||
895 mbedtls_rsa_check_pubkey(rsa) != 0) {
896 return MBEDTLS_ERR_PK_INVALID_PUBKEY;
897 }
898
899 if (*p != end) {
900 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY,
901 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
902 }
903
904 return 0;
905}
906
907int mbedtls_rsa_key_write(const mbedtls_rsa_context *rsa, unsigned char *start,
908 unsigned char **p)
909{
910 size_t len = 0;
911 int ret;
912
913 mbedtls_mpi T; /* Temporary holding the exported parameters */
914
915 /*
916 * Export the parameters one after another to avoid simultaneous copies.
917 */
918
919 mbedtls_mpi_init(&T);
920
921 /* Export QP */
922 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
923 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
924 goto end_of_export;
925 }
926 len += ret;
927
928 /* Export DQ */
929 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
930 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
931 goto end_of_export;
932 }
933 len += ret;
934
935 /* Export DP */
936 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
937 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
938 goto end_of_export;
939 }
940 len += ret;
941
942 /* Export Q */
943 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, &T, NULL, NULL)) != 0 ||
944 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
945 goto end_of_export;
946 }
947 len += ret;
948
949 /* Export P */
950 if ((ret = mbedtls_rsa_export(rsa, NULL, &T, NULL, NULL, NULL)) != 0 ||
951 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
952 goto end_of_export;
953 }
954 len += ret;
955
956 /* Export D */
957 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, &T, NULL)) != 0 ||
958 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
959 goto end_of_export;
960 }
961 len += ret;
962
963 /* Export E */
964 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
965 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
966 goto end_of_export;
967 }
968 len += ret;
969
970 /* Export N */
971 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
972 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
973 goto end_of_export;
974 }
975 len += ret;
976
977end_of_export:
978
979 mbedtls_mpi_free(&T);
980 if (ret < 0) {
981 return ret;
982 }
983
984 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
985 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
986 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
987 MBEDTLS_ASN1_CONSTRUCTED |
988 MBEDTLS_ASN1_SEQUENCE));
989
990 return (int) len;
991}
992
993/*
994 * RSAPublicKey ::= SEQUENCE {
995 * modulus INTEGER, -- n
996 * publicExponent INTEGER -- e
997 * }
998 */
999int mbedtls_rsa_pubkey_write(const mbedtls_rsa_context *rsa, unsigned char *start,
1000 unsigned char **p)
1001{
1002 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1003 size_t len = 0;
1004 mbedtls_mpi T;
1005
1006 mbedtls_mpi_init(&T);
1007
1008 /* Export E */
1009 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
1010 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
1011 goto end_of_export;
1012 }
1013 len += ret;
1014
1015 /* Export N */
1016 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
1017 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
1018 goto end_of_export;
1019 }
1020 len += ret;
1021
1022end_of_export:
1023
1024 mbedtls_mpi_free(&T);
1025 if (ret < 0) {
1026 return ret;
1027 }
1028
1029 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
1030 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
1031 MBEDTLS_ASN1_SEQUENCE));
1032
1033 return (int) len;
1034}
Hanno Becker617c1ae2017-08-23 14:11:24 +01001035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
1038/*
1039 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001040 *
1041 * This generation method follows the RSA key pair generation procedure of
1042 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001044int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
1045 int (*f_rng)(void *, unsigned char *, size_t),
1046 void *p_rng,
1047 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +00001048{
Janos Follath24eed8d2019-11-22 13:21:35 +00001049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -08001050 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +01001051 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Janos Follathb8fc1b02018-09-03 15:37:01 +01001053 /*
1054 * If the modulus is 1024 bit long or shorter, then the security strength of
1055 * the RSA algorithm is less than or equal to 80 bits and therefore an error
1056 * rate of 2^-80 is sufficient.
1057 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +01001059 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 }
Janos Follathb8fc1b02018-09-03 15:37:01 +01001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 mbedtls_mpi_init(&H);
1063 mbedtls_mpi_init(&G);
1064 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001066 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +01001067 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1068 goto cleanup;
1069 }
1070
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001071 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1073 goto cleanup;
1074 }
1075
1076 /*
1077 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001078 * 1. |P-Q| > 2^( nbits / 2 - 100 )
1079 * 2. GCD( E, (P-1)*(Q-1) ) == 1
1080 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +00001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 do {
1085 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
1086 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
1089 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001091 /* 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 +01001092 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
1093 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001094 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001096
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001097 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if (H.s < 0) {
1099 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
1100 }
Janos Follathef441782016-09-21 13:18:12 +01001101
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001102 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
1104 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
1105 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001106
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001107 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
1109 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -08001110 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001112
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001113 /* 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 +01001114 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
1115 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
1116 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 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 -08001119 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001121
1122 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001125 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
1127 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001132
Jethro Beekman97f95c92018-02-13 15:50:36 -08001133#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 * DP = D mod (P - 1)
1136 * DQ = D mod (Q - 1)
1137 * QP = Q^-1 mod P
1138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
1140 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001141#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
Hanno Becker83aad1f2017-08-23 06:45:10 +01001143 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +00001145
1146cleanup:
1147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 mbedtls_mpi_free(&H);
1149 mbedtls_mpi_free(&G);
1150 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if (ret != 0) {
1153 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +01001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if ((-ret & ~0x7f) == 0) {
1156 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
1157 }
1158 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 }
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001162}
1163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
1166/*
1167 * Check a public RSA key
1168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001170{
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
1172 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
1176 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001177 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
1180 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
1181 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
1182 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1183 }
1184
1185 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001186}
1187
1188/*
Hanno Becker705fc682017-10-10 17:57:02 +01001189 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001192{
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
1194 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
1195 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 }
Paul Bakker48377d92013-08-30 12:06:24 +02001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
1199 &ctx->D, &ctx->E, NULL, NULL) != 0) {
1200 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001202
Hanno Beckerb269a852017-08-25 08:03:21 +01001203#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
1205 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
1206 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +01001207 }
1208#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001211}
1212
1213/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001214 * Check if contexts holding a public and private key match
1215 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
1217 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001218{
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
1220 mbedtls_rsa_check_privkey(prv) != 0) {
1221 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001222 }
1223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
1225 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
1226 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001227 }
1228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001230}
1231
1232/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 * Do an RSA public key operation
1234 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001235int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
1236 const unsigned char *input,
1237 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001238{
Janos Follath24eed8d2019-11-22 13:21:35 +00001239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001240 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
1244 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1245 }
Hanno Becker705fc682017-10-10 17:57:02 +01001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001249#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1251 return ret;
1252 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001253#endif
1254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001258 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1259 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 }
1261
1262 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
1264 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
1266cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1269 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1270 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001271#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (ret != 0) {
1276 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
1277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001280}
1281
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001282/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001283 * Generate or update blinding values, see section 10 of:
1284 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001285 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001286 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1289 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001290{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001291 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001292 mbedtls_mpi R;
1293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001297 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1299 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1300 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1301 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001302
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001303 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001304 }
1305
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001306 /* Unblinding value: Vf = random number, invertible mod N */
1307 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +02001309 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
1310 goto cleanup;
1311 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 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 +02001314
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001315 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
1317 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
1318 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001319
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001320 /* At this point, Vi is invertible mod N if and only if both Vf and R
1321 * are invertible mod N. If one of them isn't, we don't need to know
1322 * which one, we just loop and choose new values for both of them.
1323 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
1325 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001326 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -05001330
1331 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
1333 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001334
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001335 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001336 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 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 +02001338
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001339
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001340cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001344}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001345
Paul Bakker5121ce52009-01-03 21:22:43 +00001346/*
Janos Follathe81102e2017-03-22 13:38:28 +00001347 * Exponent blinding supposed to prevent side-channel attacks using multiple
1348 * traces of measurements to recover the RSA key. The more collisions are there,
1349 * the more bits of the key can be recovered. See [3].
1350 *
1351 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001352 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001353 *
1354 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001355 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001356 *
1357 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1358 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1359 * Thus in this sense with 28 byte blinding the security is not reduced by
1360 * side-channel attacks like the one in [3])
1361 *
1362 * This countermeasure does not help if the key recovery is possible with a
1363 * single trace.
1364 */
1365#define RSA_EXPONENT_BLINDING 28
1366
1367/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 * Do an RSA private key operation
1369 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001370int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1371 int (*f_rng)(void *, unsigned char *, size_t),
1372 void *p_rng,
1373 const unsigned char *input,
1374 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001375{
Janos Follath24eed8d2019-11-22 13:21:35 +00001376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001377 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001378
1379 /* Temporary holding the result */
1380 mbedtls_mpi T;
1381
1382 /* Temporaries holding P-1, Q-1 and the
1383 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001384 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001385
1386#if !defined(MBEDTLS_RSA_NO_CRT)
1387 /* Temporaries holding the results mod p resp. mod q. */
1388 mbedtls_mpi TP, TQ;
1389
1390 /* Temporaries holding the blinded exponents for
1391 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001392 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001393
1394 /* Pointers to actual exponents to be used - either the unblinded
1395 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +00001396 mbedtls_mpi *DP = &ctx->DP;
1397 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +01001398#else
1399 /* Temporary holding the blinded exponent (if used). */
1400 mbedtls_mpi D_blind;
1401
1402 /* Pointer to actual exponent to be used - either the unblinded
1403 * or the blinded one, depending on the presence of a PRNG. */
1404 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +01001405#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001406
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001407 /* Temporaries holding the initial input and the double
1408 * checked result; should be the same in the end. */
1409 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (f_rng == NULL) {
1412 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1413 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (rsa_check_context(ctx, 1 /* private key checks */,
1416 1 /* blinding on */) != 0) {
1417 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001418 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001419
Hanno Becker06811ce2017-05-03 15:10:34 +01001420#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1422 return ret;
1423 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001424#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001425
Hanno Becker06811ce2017-05-03 15:10:34 +01001426 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 mbedtls_mpi_init(&P1);
1430 mbedtls_mpi_init(&Q1);
1431 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001432
Janos Follathe81102e2017-03-22 13:38:28 +00001433#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001435#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 mbedtls_mpi_init(&DP_blind);
1437 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001438#endif
1439
Hanno Becker06811ce2017-05-03 15:10:34 +01001440#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001442#endif
1443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 mbedtls_mpi_init(&I);
1445 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001446
1447 /* End of MPI initialization */
1448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1450 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001451 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1452 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 }
1454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +01001456
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001457 /*
1458 * Blinding
1459 * T = T * Vi mod N
1460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1462 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1463 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001464
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001465 /*
1466 * Exponent blinding
1467 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1469 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001470
Janos Follathf9203b42017-03-22 15:13:15 +00001471#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001472 /*
1473 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1476 f_rng, p_rng));
1477 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1478 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1479 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +00001480
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001481 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001482#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001483 /*
1484 * DP_blind = ( P - 1 ) * R + DP
1485 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1487 f_rng, p_rng));
1488 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1489 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1490 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001491
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001492 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001493
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001494 /*
1495 * DQ_blind = ( Q - 1 ) * R + DQ
1496 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1498 f_rng, p_rng));
1499 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1500 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1501 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001502
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001503 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001504#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001508#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001509 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001510 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001512 * TP = input ^ dP mod P
1513 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1517 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
1519 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001520 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1523 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1524 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001525
1526 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001527 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1530 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001532
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001533 /*
1534 * Unblind
1535 * T = T * Vf mod N
1536 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1538 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
Hanno Becker2dec5e82017-10-03 07:49:52 +01001540 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1542 &ctx->N, &ctx->RN));
1543 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001544 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1545 goto cleanup;
1546 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001547
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001550
1551cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1554 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1555 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001556#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 mbedtls_mpi_free(&P1);
1559 mbedtls_mpi_free(&Q1);
1560 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001561
Janos Follathe81102e2017-03-22 13:38:28 +00001562#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001564#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 mbedtls_mpi_free(&DP_blind);
1566 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001567#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001570
1571#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001573#endif
1574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 mbedtls_mpi_free(&C);
1576 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (ret != 0 && ret >= -0x007f) {
1579 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1580 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001583}
1584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001586/**
1587 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1588 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001589 * \param dst buffer to mask
1590 * \param dlen length of destination buffer
1591 * \param src source of the mask generation
1592 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001593 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001594 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001595static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1596 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001597{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001598 unsigned char counter[4];
1599 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001600 unsigned int hlen;
1601 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001602 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001603 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001604 const mbedtls_md_info_t *md_info;
1605 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 mbedtls_md_init(&md_ctx);
1608 md_info = mbedtls_md_info_from_type(md_alg);
1609 if (md_info == NULL) {
1610 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1611 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001612
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 mbedtls_md_init(&md_ctx);
1614 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001615 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 memset(mask, 0, sizeof(mask));
1621 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001622
Simon Butcher02037452016-03-01 21:19:12 +00001623 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001624 p = dst;
1625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001627 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001629 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001633 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 }
1635 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001636 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 }
1638 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001639 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 }
1641 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001642 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001646 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001648
1649 counter[3]++;
1650
1651 dlen -= use_len;
1652 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001653
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001654exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001659}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001660
1661/**
1662 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1663 *
1664 * \param hash the input hash
1665 * \param hlen length of the input hash
1666 * \param salt the input salt
1667 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001668 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001669 * \param md_alg message digest to use
1670 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001671static int hash_mprime(const unsigned char *hash, size_t hlen,
1672 const unsigned char *salt, size_t slen,
1673 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001674{
1675 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001676
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001677 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1681 if (md_info == NULL) {
1682 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1683 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 mbedtls_md_init(&md_ctx);
1686 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001687 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 }
1689 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001690 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 }
1692 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001693 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 }
1695 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001696 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 }
1698 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001699 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 }
1701 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001702 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001704
1705exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001709}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001710
1711/**
1712 * Compute a hash.
1713 *
1714 * \param md_alg algorithm to use
1715 * \param input input message to hash
1716 * \param ilen input length
1717 * \param output the output buffer - must be large enough for \p md_alg
1718 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001719static int compute_hash(mbedtls_md_type_t md_alg,
1720 const unsigned char *input, size_t ilen,
1721 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001722{
1723 const mbedtls_md_info_t *md_info;
1724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 md_info = mbedtls_md_info_from_type(md_alg);
1726 if (md_info == NULL) {
1727 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1728 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001731}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001735/*
1736 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1737 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001738int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1739 int (*f_rng)(void *, unsigned char *, size_t),
1740 void *p_rng,
1741 const unsigned char *label, size_t label_len,
1742 size_t ilen,
1743 const unsigned char *input,
1744 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001745{
1746 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001747 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001748 unsigned char *p = output;
1749 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (f_rng == NULL) {
1752 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1753 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001754
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001755 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 if (hlen == 0) {
1757 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1758 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001759
1760 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001761
Simon Butcher02037452016-03-01 21:19:12 +00001762 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1764 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1765 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001768
1769 *p++ = 0;
1770
Simon Butcher02037452016-03-01 21:19:12 +00001771 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1773 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1774 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001775
1776 p += hlen;
1777
Simon Butcher02037452016-03-01 21:19:12 +00001778 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1780 if (ret != 0) {
1781 return ret;
1782 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001783 p += hlen;
1784 p += olen - 2 * hlen - 2 - ilen;
1785 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 if (ilen != 0) {
1787 memcpy(p, input, ilen);
1788 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001789
Simon Butcher02037452016-03-01 21:19:12 +00001790 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001792 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 return ret;
1794 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001795
Simon Butcher02037452016-03-01 21:19:12 +00001796 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001798 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 return ret;
1800 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001803}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001807/*
1808 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1809 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001810int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1811 int (*f_rng)(void *, unsigned char *, size_t),
1812 void *p_rng, size_t ilen,
1813 const unsigned char *input,
1814 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001815{
1816 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001818 unsigned char *p = output;
1819
Paul Bakkerb3869132013-02-28 17:21:01 +01001820 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001821
Simon Butcher02037452016-03-01 21:19:12 +00001822 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (ilen + 11 < ilen || olen < ilen + 11) {
1824 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1825 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001826
1827 nb_pad = olen - 3 - ilen;
1828
1829 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001830
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 if (f_rng == NULL) {
1832 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1833 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001834
1835 *p++ = MBEDTLS_RSA_CRYPT;
1836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001838 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001839
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001840 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 ret = f_rng(p_rng, p, 1);
1842 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001843
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001844 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (rng_dl == 0 || ret != 0) {
1846 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1847 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001848
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001849 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001850 }
1851
1852 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (ilen != 0) {
1854 memcpy(p, input, ilen);
1855 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001858}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001860
Paul Bakker5121ce52009-01-03 21:22:43 +00001861/*
1862 * Add the message padding, then do an RSA operation
1863 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001864int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1865 int (*f_rng)(void *, unsigned char *, size_t),
1866 void *p_rng,
1867 size_t ilen,
1868 const unsigned char *input,
1869 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001870{
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#if defined(MBEDTLS_PKCS1_V15)
1873 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1875 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001876#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878#if defined(MBEDTLS_PKCS1_V21)
1879 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1881 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001882#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
1884 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001887}
1888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001890/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001891 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001893int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1894 int (*f_rng)(void *, unsigned char *, size_t),
1895 void *p_rng,
1896 const unsigned char *label, size_t label_len,
1897 size_t *olen,
1898 const unsigned char *input,
1899 unsigned char *output,
1900 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001901{
Janos Follath24eed8d2019-11-22 13:21:35 +00001902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001903 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001904 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001905 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001907 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001908 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001909
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001910 /*
1911 * Parameters sanity checks
1912 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1914 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1915 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001916
1917 ilen = ctx->len;
1918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 if (ilen < 16 || ilen > sizeof(buf)) {
1920 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1921 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001922
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001923 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 if (hlen == 0) {
1925 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1926 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001927
Janos Follathc17cda12016-02-11 11:08:18 +00001928 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 if (2 * hlen + 2 > ilen) {
1930 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1931 }
Janos Follathc17cda12016-02-11 11:08:18 +00001932
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001933 /*
1934 * RSA operation
1935 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001939 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001941
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001942 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001943 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001944 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001945 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001947 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 /* DB: Apply dbMask to maskedDB */
1949 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001950 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001951 goto cleanup;
1952 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001953
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001954 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1956 label, label_len, lhash);
1957 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001958 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001960
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001961 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001962 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001963 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 p = buf;
1965
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001966 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001967
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001968 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001969
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001970 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001971 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001972 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001973
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001974 /* Get zero-padding len, but always read till end of buffer
1975 * (minus one, for the 01 byte) */
1976 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001977 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001979 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1980 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001981 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001982
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001983 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001984 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001985
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001986 /*
1987 * The only information "leaked" is whether the padding was correct or not
1988 * (eg, no data is copied if it was not correct). This meets the
1989 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1990 * the different error conditions.
1991 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001992 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001993 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1994 goto cleanup;
1995 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001996
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001997 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001998 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1999 goto cleanup;
2000 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002001
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002002 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 if (*olen != 0) {
2004 memcpy(output, p, *olen);
2005 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002006 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002007
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002008cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 mbedtls_platform_zeroize(buf, sizeof(buf));
2010 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002013}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002017/*
2018 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
2019 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002020int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
2021 int (*f_rng)(void *, unsigned char *, size_t),
2022 void *p_rng,
2023 size_t *olen,
2024 const unsigned char *input,
2025 unsigned char *output,
2026 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002027{
2028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2029 size_t ilen;
2030 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
2031
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002032 ilen = ctx->len;
2033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2035 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2036 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if (ilen < 16 || ilen > sizeof(buf)) {
2039 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2040 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002045 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
2049 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002050
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002051cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002055}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002057
2058/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002059 * Do an RSA operation, then remove the message padding
2060 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002061int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
2062 int (*f_rng)(void *, unsigned char *, size_t),
2063 void *p_rng,
2064 size_t *olen,
2065 const unsigned char *input,
2066 unsigned char *output,
2067 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01002068{
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#if defined(MBEDTLS_PKCS1_V15)
2071 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
2073 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02002074#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#if defined(MBEDTLS_PKCS1_V21)
2077 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
2079 olen, input, output,
2080 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01002081#endif
2082
2083 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002085 }
2086}
2087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002089static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2090 int (*f_rng)(void *, unsigned char *, size_t),
2091 void *p_rng,
2092 mbedtls_md_type_t md_alg,
2093 unsigned int hashlen,
2094 const unsigned char *hash,
2095 int saltlen,
2096 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002097{
2098 size_t olen;
2099 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002100 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002101 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00002102 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002103 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002104 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002107 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 if (f_rng == NULL) {
2111 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2112 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002113
2114 olen = ctx->len;
2115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002117 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002118 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 if (exp_hashlen == 0) {
2120 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2121 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if (hashlen != exp_hashlen) {
2124 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2125 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002126 }
2127
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002128 hash_id = (mbedtls_md_type_t) ctx->hash_id;
2129 if (hash_id == MBEDTLS_MD_NONE) {
2130 hash_id = md_alg;
2131 }
2132 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (hlen == 0) {
2134 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2135 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
2138 /* Calculate the largest possible salt length, up to the hash size.
2139 * Normally this is the hash length, which is the maximum salt length
2140 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
2141 * enough room, use the maximum salt length that fits. The constraint is
2142 * that the hash length plus the salt length plus 2 bytes must be at most
2143 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
2144 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002145 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (olen < hlen + min_slen + 2) {
2147 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2148 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002149 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002151 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 }
2153 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
2154 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2155 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02002156 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002157 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01002160
Simon Butcher02037452016-03-01 21:19:12 +00002161 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002163 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01002164 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002165
2166 /* Generate salt of length slen in place in the encoded message */
2167 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
2169 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
2170 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02002171
Paul Bakkerb3869132013-02-28 17:21:01 +01002172 p += slen;
2173
Simon Butcher02037452016-03-01 21:19:12 +00002174 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002175 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (ret != 0) {
2177 return ret;
2178 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002179
Simon Butcher02037452016-03-01 21:19:12 +00002180 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002182 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002184
Simon Butcher02037452016-03-01 21:19:12 +00002185 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002186 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 if (ret != 0) {
2188 return ret;
2189 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2192 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01002193
2194 p += hlen;
2195 *p++ = 0xBC;
2196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002198}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002199
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002200static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2201 int (*f_rng)(void *, unsigned char *, size_t),
2202 void *p_rng,
2203 mbedtls_md_type_t md_alg,
2204 unsigned int hashlen,
2205 const unsigned char *hash,
2206 int saltlen,
2207 unsigned char *sig)
2208{
2209 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2210 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2211 }
2212 if (ctx->hash_id == MBEDTLS_MD_NONE) {
2213 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2214 }
2215 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
2216 sig);
2217}
2218
2219int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2220 int (*f_rng)(void *, unsigned char *, size_t),
2221 void *p_rng,
2222 mbedtls_md_type_t md_alg,
2223 unsigned int hashlen,
2224 const unsigned char *hash,
2225 unsigned char *sig)
2226{
2227 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
2228 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2229}
2230
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002231/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02002232 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
2233 * the option to pass in the salt length.
2234 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002235int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
2236 int (*f_rng)(void *, unsigned char *, size_t),
2237 void *p_rng,
2238 mbedtls_md_type_t md_alg,
2239 unsigned int hashlen,
2240 const unsigned char *hash,
2241 int saltlen,
2242 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02002243{
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2245 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02002246}
2247
Cédric Meuterf3fab332020-04-25 11:30:45 +02002248/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002249 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
2250 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002251int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2252 int (*f_rng)(void *, unsigned char *, size_t),
2253 void *p_rng,
2254 mbedtls_md_type_t md_alg,
2255 unsigned int hashlen,
2256 const unsigned char *hash,
2257 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002258{
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2260 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002265/*
2266 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
2267 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002268
2269/* Construct a PKCS v1.5 encoding of a hashed message
2270 *
2271 * This is used both for signature generation and verification.
2272 *
2273 * Parameters:
2274 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002275 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002276 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002277 * - hash: Buffer containing the hashed message or the raw data.
2278 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002279 * - dst: Buffer to hold the encoded message.
2280 *
2281 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002282 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002283 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002284 *
2285 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002286static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
2287 unsigned int hashlen,
2288 const unsigned char *hash,
2289 size_t dst_len,
2290 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01002291{
2292 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002293 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002294 unsigned char *p = dst;
2295 const char *oid = NULL;
2296
2297 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002299 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 if (md_size == 0) {
2301 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2302 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2305 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2306 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 if (hashlen != md_size) {
2309 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2310 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002311
2312 /* Double-check that 8 + hashlen + oid_size can be used as a
2313 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01002315 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 10 + hashlen + oid_size < 10 + hashlen) {
2317 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2318 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002319
2320 /*
2321 * Static bounds check:
2322 * - Need 10 bytes for five tag-length pairs.
2323 * (Insist on 1-byte length encodings to protect against variants of
2324 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2325 * - Need hashlen bytes for hash
2326 * - Need oid_size bytes for hash alg OID.
2327 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (nb_pad < 10 + hashlen + oid_size) {
2329 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2330 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002331 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 } else {
2333 if (nb_pad < hashlen) {
2334 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2335 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002336
2337 nb_pad -= hashlen;
2338 }
2339
Hanno Becker2b2f8982017-09-27 17:10:03 +01002340 /* Need space for signature header and padding delimiter (3 bytes),
2341 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 if (nb_pad < 3 + 8) {
2343 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2344 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002345 nb_pad -= 3;
2346
2347 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002348 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002349
2350 /* Write signature header and padding */
2351 *p++ = 0;
2352 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002354 p += nb_pad;
2355 *p++ = 0;
2356
2357 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (md_alg == MBEDTLS_MD_NONE) {
2359 memcpy(p, hash, hashlen);
2360 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002361 }
2362
2363 /* Signing hashed data, add corresponding ASN.1 structure
2364 *
2365 * DigestInfo ::= SEQUENCE {
2366 * digestAlgorithm DigestAlgorithmIdentifier,
2367 * digest Digest }
2368 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2369 * Digest ::= OCTET STRING
2370 *
2371 * Schematic:
2372 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2373 * TAG-NULL + LEN [ NULL ] ]
2374 * TAG-OCTET + LEN [ HASH ] ]
2375 */
2376 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002378 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002380 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002381 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002383 p += oid_size;
2384 *p++ = MBEDTLS_ASN1_NULL;
2385 *p++ = 0x00;
2386 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002387 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002389 p += hashlen;
2390
2391 /* Just a sanity-check, should be automatic
2392 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 if (p != dst + dst_len) {
2394 mbedtls_platform_zeroize(dst, dst_len);
2395 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002396 }
2397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002399}
2400
Paul Bakkerb3869132013-02-28 17:21:01 +01002401/*
2402 * Do an RSA operation to sign the message digest
2403 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002404int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2405 int (*f_rng)(void *, unsigned char *, size_t),
2406 void *p_rng,
2407 mbedtls_md_type_t md_alg,
2408 unsigned int hashlen,
2409 const unsigned char *hash,
2410 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002411{
Janos Follath24eed8d2019-11-22 13:21:35 +00002412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002413 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002416 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2420 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2421 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002422
Hanno Beckerfdf38032017-09-06 12:35:55 +01002423 /*
2424 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2425 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2428 ctx->len, sig)) != 0) {
2429 return ret;
2430 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002431
Hanno Beckerfdf38032017-09-06 12:35:55 +01002432 /* Private key operation
2433 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002434 * In order to prevent Lenstra's attack, make the signature in a
2435 * temporary buffer and check it before returning it.
2436 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 sig_try = mbedtls_calloc(1, ctx->len);
2439 if (sig_try == NULL) {
2440 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002441 }
2442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 verif = mbedtls_calloc(1, ctx->len);
2444 if (verif == NULL) {
2445 mbedtls_free(sig_try);
2446 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2447 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2450 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2451
2452 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002453 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2454 goto cleanup;
2455 }
2456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002458
2459cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002460 mbedtls_zeroize_and_free(sig_try, ctx->len);
2461 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 if (ret != 0) {
2464 memset(sig, '!', ctx->len);
2465 }
2466 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002469
2470/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 * Do an RSA operation to sign the message digest
2472 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002473int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2474 int (*f_rng)(void *, unsigned char *, size_t),
2475 void *p_rng,
2476 mbedtls_md_type_t md_alg,
2477 unsigned int hashlen,
2478 const unsigned char *hash,
2479 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002480{
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002482 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#if defined(MBEDTLS_PKCS1_V15)
2487 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2489 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002490#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492#if defined(MBEDTLS_PKCS1_V21)
2493 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2495 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002496#endif
2497
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002501}
2502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002504/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002505 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002507int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2508 mbedtls_md_type_t md_alg,
2509 unsigned int hashlen,
2510 const unsigned char *hash,
2511 mbedtls_md_type_t mgf1_hash_id,
2512 int expected_salt_len,
2513 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002514{
Janos Follath24eed8d2019-11-22 13:21:35 +00002515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002516 size_t siglen;
2517 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002518 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002519 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002520 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002521 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002525 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002527
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 siglen = ctx->len;
2529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (siglen < 16 || siglen > sizeof(buf)) {
2531 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2532 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (ret != 0) {
2537 return ret;
2538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
2540 p = buf;
2541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 if (buf[siglen - 1] != 0xBC) {
2543 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002544 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (md_alg != MBEDTLS_MD_NONE) {
2547 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002548 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 if (exp_hashlen == 0) {
2550 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2551 }
2552
2553 if (hashlen != exp_hashlen) {
2554 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2555 }
2556 }
2557
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002558 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (hlen == 0) {
2560 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2561 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002562
Simon Butcher02037452016-03-01 21:19:12 +00002563 /*
2564 * Note: EMSA-PSS verification is over the length of N - 1 bits
2565 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (buf[0] >> (8 - siglen * 8 + msb)) {
2569 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2570 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002571
Simon Butcher02037452016-03-01 21:19:12 +00002572 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002574 p++;
2575 siglen -= 1;
2576 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 if (siglen < hlen + 2) {
2579 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2580 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002581 hash_start = p + siglen - hlen - 1;
2582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2584 if (ret != 0) {
2585 return ret;
2586 }
Paul Bakker02303e82013-01-03 11:08:31 +01002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002591 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 if (*p++ != 0x01) {
2595 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2596 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002597
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002598 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2601 observed_salt_len != (size_t) expected_salt_len) {
2602 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002603 }
2604
Simon Butcher02037452016-03-01 21:19:12 +00002605 /*
2606 * Generate H = Hash( M' )
2607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2609 result, mgf1_hash_id);
2610 if (ret != 0) {
2611 return ret;
2612 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (memcmp(hash_start, result, hlen) != 0) {
2615 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2616 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002619}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002620
2621/*
2622 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2623 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002624int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2625 mbedtls_md_type_t md_alg,
2626 unsigned int hashlen,
2627 const unsigned char *hash,
2628 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002629{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002630 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002632 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002637 : md_alg;
2638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2640 md_alg, hashlen, hash,
2641 mgf1_hash_id,
2642 MBEDTLS_RSA_SALT_LEN_ANY,
2643 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002644
2645}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002649/*
2650 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2651 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002652int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2653 mbedtls_md_type_t md_alg,
2654 unsigned int hashlen,
2655 const unsigned char *hash,
2656 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002657{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002658 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002659 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002660 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002663 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002665
2666 sig_len = ctx->len;
2667
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002668 /*
2669 * Prepare expected PKCS1 v1.5 encoding of hash.
2670 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2673 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002674 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2675 goto cleanup;
2676 }
2677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2679 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002680 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002682
2683 /*
2684 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2685 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 ret = mbedtls_rsa_public(ctx, sig, encoded);
2688 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002689 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002691
Simon Butcher02037452016-03-01 21:19:12 +00002692 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002693 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002694 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2697 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002698 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2699 goto cleanup;
2700 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002701
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002702cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002703
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002705 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002706 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002709 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002710 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
2716/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002717 * Do an RSA operation and check the message digest
2718 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002719int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2720 mbedtls_md_type_t md_alg,
2721 unsigned int hashlen,
2722 const unsigned char *hash,
2723 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002724{
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002726 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730#if defined(MBEDTLS_PKCS1_V15)
2731 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2733 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002734#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736#if defined(MBEDTLS_PKCS1_V21)
2737 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2739 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002740#endif
2741
2742 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002744 }
2745}
2746
2747/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002748 * Copy the components of an RSA key
2749 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002750int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002751{
Janos Follath24eed8d2019-11-22 13:21:35 +00002752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002753
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002754 dst->len = src->len;
2755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2757 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2760 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2761 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002762
2763#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2765 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2766 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2767 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2768 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002769#endif
2770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2774 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002775
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002776 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002777 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002778
2779cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 if (ret != 0) {
2781 mbedtls_rsa_free(dst);
2782 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002785}
2786
2787/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 * Free the components of an RSA key
2789 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002790void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002791{
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002793 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002795
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 mbedtls_mpi_free(&ctx->Vi);
2797 mbedtls_mpi_free(&ctx->Vf);
2798 mbedtls_mpi_free(&ctx->RN);
2799 mbedtls_mpi_free(&ctx->D);
2800 mbedtls_mpi_free(&ctx->Q);
2801 mbedtls_mpi_free(&ctx->P);
2802 mbedtls_mpi_free(&ctx->E);
2803 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002804
Hanno Becker33c30a02017-08-23 07:00:22 +01002805#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 mbedtls_mpi_free(&ctx->RQ);
2807 mbedtls_mpi_free(&ctx->RP);
2808 mbedtls_mpi_free(&ctx->QP);
2809 mbedtls_mpi_free(&ctx->DQ);
2810 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002811#endif /* MBEDTLS_RSA_NO_CRT */
2812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002814 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 if (ctx->ver != 0) {
2816 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002817 ctx->ver = 0;
2818 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002819#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002820}
2821
Hanno Beckerab377312017-08-23 16:24:51 +01002822#endif /* !MBEDTLS_RSA_ALT */
2823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
Paul Bakker5121ce52009-01-03 21:22:43 +00002826
2827/*
2828 * Example RSA-1024 keypair, for test purposes
2829 */
2830#define KEY_LEN 128
2831
2832#define RSA_N "9292758453063D803DD603D5E777D788" \
2833 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2834 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2835 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2836 "93A89813FBF3C4F8066D2D800F7C38A8" \
2837 "1AE31942917403FF4946B0A83D3D3E05" \
2838 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2839 "5E94BB77B07507233A0BC7BAC8F90F79"
2840
2841#define RSA_E "10001"
2842
2843#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2844 "66CA472BC44D253102F8B4A9D3BFA750" \
2845 "91386C0077937FE33FA3252D28855837" \
2846 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2847 "DF79C5CE07EE72C7F123142198164234" \
2848 "CABB724CF78B8173B9F880FC86322407" \
2849 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2850 "071513A1E85B5DFA031F21ECAE91A34D"
2851
2852#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2853 "2C01CAD19EA484A87EA4377637E75500" \
2854 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2855 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2856
2857#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2858 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2859 "910E4168387E3C30AA1E00C339A79508" \
2860 "8452DD96A9A5EA5D9DCA68DA636032AF"
2861
Paul Bakker5121ce52009-01-03 21:22:43 +00002862#define PT_LEN 24
2863#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2864 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002867static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002868{
gufe44c2620da2020-08-03 17:56:50 +02002869#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002870 size_t i;
2871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002873 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 }
Paul Bakker545570e2010-07-18 09:00:25 +00002875
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002877 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002879#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002881 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002883
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002885#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002886
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002888}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002890
Paul Bakker5121ce52009-01-03 21:22:43 +00002891/*
2892 * Checkup routine
2893 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002894int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002895{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002896 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002898 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 unsigned char rsa_plaintext[PT_LEN];
2901 unsigned char rsa_decrypted[PT_LEN];
2902 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002903#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002904 unsigned char sha1sum[20];
2905#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Hanno Becker3a701162017-08-22 13:52:43 +01002907 mbedtls_mpi K;
2908
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 mbedtls_mpi_init(&K);
2910 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2913 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2914 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2915 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2916 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2917 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2918 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2919 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2920 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2921 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 if (verbose != 0) {
2926 mbedtls_printf(" RSA key validation: ");
2927 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2930 mbedtls_rsa_check_privkey(&rsa) != 0) {
2931 if (verbose != 0) {
2932 mbedtls_printf("failed\n");
2933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002934
Hanno Becker5bc87292017-05-03 15:09:31 +01002935 ret = 1;
2936 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002937 }
2938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 if (verbose != 0) {
2940 mbedtls_printf("passed\n PKCS#1 encryption : ");
2941 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2946 PT_LEN, rsa_plaintext,
2947 rsa_ciphertext) != 0) {
2948 if (verbose != 0) {
2949 mbedtls_printf("failed\n");
2950 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
Hanno Becker5bc87292017-05-03 15:09:31 +01002952 ret = 1;
2953 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002954 }
2955
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 if (verbose != 0) {
2957 mbedtls_printf("passed\n PKCS#1 decryption : ");
2958 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2961 &len, rsa_ciphertext, rsa_decrypted,
2962 sizeof(rsa_decrypted)) != 0) {
2963 if (verbose != 0) {
2964 mbedtls_printf("failed\n");
2965 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Hanno Becker5bc87292017-05-03 15:09:31 +01002967 ret = 1;
2968 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002969 }
2970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2972 if (verbose != 0) {
2973 mbedtls_printf("failed\n");
2974 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Hanno Becker5bc87292017-05-03 15:09:31 +01002976 ret = 1;
2977 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002978 }
2979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 if (verbose != 0) {
2981 mbedtls_printf("passed\n");
2982 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002983
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002984#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 if (verbose != 0) {
2986 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002987 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002989 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2990 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 if (verbose != 0) {
2992 mbedtls_printf("failed\n");
2993 }
2994
2995 return 1;
2996 }
2997
2998 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2999 MBEDTLS_MD_SHA1, 20,
3000 sha1sum, rsa_ciphertext) != 0) {
3001 if (verbose != 0) {
3002 mbedtls_printf("failed\n");
3003 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003004
Hanno Becker5bc87292017-05-03 15:09:31 +01003005 ret = 1;
3006 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003007 }
3008
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 if (verbose != 0) {
3010 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
3011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
3014 sha1sum, rsa_ciphertext) != 0) {
3015 if (verbose != 0) {
3016 mbedtls_printf("failed\n");
3017 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
Hanno Becker5bc87292017-05-03 15:09:31 +01003019 ret = 1;
3020 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 }
3022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 if (verbose != 0) {
3024 mbedtls_printf("passed\n");
3025 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003026#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 if (verbose != 0) {
3029 mbedtls_printf("\n");
3030 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003031
Paul Bakker3d8fb632014-04-17 12:42:41 +02003032cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 mbedtls_mpi_free(&K);
3034 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02003036 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003039}
3040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043#endif /* MBEDTLS_RSA_C */