blob: a90b83adf4a7b1ac1c64d806691c49d3c23b241f [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 Rodgmane3c05852023-11-03 12:21:36 +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"
Janos Follathd6b09652023-11-21 09:33:54 +000031#include "bignum_core.h"
Chris Jones66a4cd42021-03-09 16:04:12 +000032#include "rsa_alt_helpers.h"
Tomi Fontanilles573dc232023-12-10 14:57:51 +020033#include "rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/oid.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
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
666/*
667 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800668 *
669 * This generation method follows the RSA key pair generation procedure of
670 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
673 int (*f_rng)(void *, unsigned char *, size_t),
674 void *p_rng,
675 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +0000676{
Janos Follath24eed8d2019-11-22 13:21:35 +0000677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -0800678 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +0100679 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000680
Janos Follathb8fc1b02018-09-03 15:37:01 +0100681 /*
682 * If the modulus is 1024 bit long or shorter, then the security strength of
683 * the RSA algorithm is less than or equal to 80 bits and therefore an error
684 * rate of 2^-80 is sufficient.
685 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +0100687 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 }
Janos Follathb8fc1b02018-09-03 15:37:01 +0100689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 mbedtls_mpi_init(&H);
691 mbedtls_mpi_init(&G);
692 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000694 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +0100695 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
696 goto cleanup;
697 }
698
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +0000699 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
701 goto cleanup;
702 }
703
704 /*
705 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800706 * 1. |P-Q| > 2^( nbits / 2 - 100 )
707 * 2. GCD( E, (P-1)*(Q-1) ) == 1
708 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 do {
713 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
714 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
717 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800719 /* 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 +0100720 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
721 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800725 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (H.s < 0) {
727 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
728 }
Janos Follathef441782016-09-21 13:18:12 +0100729
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100730 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
732 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
733 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800734
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800735 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
737 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -0800738 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800740
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800741 /* 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 +0100742 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
743 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
744 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -0800745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 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 -0800747 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 }
Jethro Beekman97f95c92018-02-13 15:50:36 -0800749
750 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100753 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
755 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -0800758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100760
Jethro Beekman97f95c92018-02-13 15:50:36 -0800761#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000763 * DP = D mod (P - 1)
764 * DQ = D mod (Q - 1)
765 * QP = Q^-1 mod P
766 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
768 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +0100769#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Hanno Becker83aad1f2017-08-23 06:45:10 +0100771 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
774cleanup:
775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 mbedtls_mpi_free(&H);
777 mbedtls_mpi_free(&G);
778 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (ret != 0) {
781 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +0100782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if ((-ret & ~0x7f) == 0) {
784 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
785 }
786 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000787 }
788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000790}
791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
794/*
795 * Check a public RSA key
796 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000798{
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
800 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100801 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
804 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +0100805 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
808 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
809 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
810 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
811 }
812
813 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000814}
815
816/*
Hanno Becker705fc682017-10-10 17:57:02 +0100817 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +0000818 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000820{
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
822 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
823 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000824 }
Paul Bakker48377d92013-08-30 12:06:24 +0200825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
827 &ctx->D, &ctx->E, NULL, NULL) != 0) {
828 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000830
Hanno Beckerb269a852017-08-25 08:03:21 +0100831#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
833 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
834 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +0100835 }
836#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +0000837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000839}
840
841/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100842 * Check if contexts holding a public and private key match
843 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
845 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100846{
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
848 mbedtls_rsa_check_privkey(prv) != 0) {
849 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100850 }
851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
853 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
854 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100855 }
856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100858}
859
860/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 * Do an RSA public key operation
862 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100863int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
864 const unsigned char *input,
865 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000866{
Janos Follath24eed8d2019-11-22 13:21:35 +0000867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000868 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
872 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
873 }
Hanno Becker705fc682017-10-10 17:57:02 +0100874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000876
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200877#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
879 return ret;
880 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200881#endif
882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200886 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
887 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000888 }
889
890 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
892 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
894cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
897 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
898 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100899#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if (ret != 0) {
904 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
905 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000908}
909
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200910/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200911 * Generate or update blinding values, see section 10 of:
912 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200913 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200914 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200915 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
917 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200918{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200919 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200920 mbedtls_mpi R;
921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200925 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
927 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
928 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
929 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200930
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200931 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200932 }
933
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200934 /* Unblinding value: Vf = random number, invertible mod N */
935 do {
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +0200937 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
938 goto cleanup;
939 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 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 +0200942
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200943 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
945 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
946 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200947
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200948 /* At this point, Vi is invertible mod N if and only if both Vf and R
949 * are invertible mod N. If one of them isn't, we don't need to know
950 * which one, we just loop and choose new values for both of them.
951 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
953 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200954 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +0200956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -0500958
959 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
961 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200962
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +0200963 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200964 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 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 +0200966
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200967
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200968cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +0200970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200972}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200973
Paul Bakker5121ce52009-01-03 21:22:43 +0000974/*
Janos Follathd6b09652023-11-21 09:33:54 +0000975 * Unblind
976 * T = T * Vf mod N
977 */
Janos Follathb4b8f3d2023-12-27 10:44:36 +0000978static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, const mbedtls_mpi *N)
Janos Follathd6b09652023-11-21 09:33:54 +0000979{
980 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
981 const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
982 const size_t nlimbs = N->n;
983 const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
984 mbedtls_mpi RR, M_T;
985
986 mbedtls_mpi_init(&RR);
987 mbedtls_mpi_init(&M_T);
988
989 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
990 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
991
992 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
993 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
994
Janos Follathe6750b22023-12-27 10:22:59 +0000995 /* T = T * Vf mod N
996 * Reminder: montmul(A, B, N) = A * B * R^-1 mod N
997 * Usually both operands are multiplied by R mod N beforehand (by calling
998 * `to_mont_rep()` on them), yielding a result that's also * R mod N (aka
999 * "in the Montgomery domain"). Here we only multiply one operand by R mod
1000 * N, so the result is directly what we want - no need to call
1001 * `from_mont_rep()` on it. */
Janos Follathd6b09652023-11-21 09:33:54 +00001002 mbedtls_mpi_core_to_mont_rep(T->p, T->p, N->p, nlimbs, mm, RR.p, M_T.p);
Janos Follathd6b09652023-11-21 09:33:54 +00001003 mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1004
1005cleanup:
1006
1007 mbedtls_mpi_free(&RR);
1008 mbedtls_mpi_free(&M_T);
1009
1010 return ret;
1011}
1012
1013/*
Janos Follathe81102e2017-03-22 13:38:28 +00001014 * Exponent blinding supposed to prevent side-channel attacks using multiple
1015 * traces of measurements to recover the RSA key. The more collisions are there,
1016 * the more bits of the key can be recovered. See [3].
1017 *
1018 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001019 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001020 *
1021 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001022 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001023 *
1024 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1025 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1026 * Thus in this sense with 28 byte blinding the security is not reduced by
1027 * side-channel attacks like the one in [3])
1028 *
1029 * This countermeasure does not help if the key recovery is possible with a
1030 * single trace.
1031 */
1032#define RSA_EXPONENT_BLINDING 28
1033
1034/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 * Do an RSA private key operation
1036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1038 int (*f_rng)(void *, unsigned char *, size_t),
1039 void *p_rng,
1040 const unsigned char *input,
1041 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001042{
Janos Follath24eed8d2019-11-22 13:21:35 +00001043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001044 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001045
1046 /* Temporary holding the result */
1047 mbedtls_mpi T;
1048
1049 /* Temporaries holding P-1, Q-1 and the
1050 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001051 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001052
1053#if !defined(MBEDTLS_RSA_NO_CRT)
1054 /* Temporaries holding the results mod p resp. mod q. */
1055 mbedtls_mpi TP, TQ;
1056
1057 /* Temporaries holding the blinded exponents for
1058 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001059 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001060#else
1061 /* Temporary holding the blinded exponent (if used). */
1062 mbedtls_mpi D_blind;
Hanno Becker43f94722017-08-25 11:50:00 +01001063#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001064
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001065 /* Temporaries holding the initial input and the double
1066 * checked result; should be the same in the end. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001067 mbedtls_mpi input_blinded, check_result_blinded;
Paul Bakker5121ce52009-01-03 21:22:43 +00001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (f_rng == NULL) {
1070 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1071 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if (rsa_check_context(ctx, 1 /* private key checks */,
1074 1 /* blinding on */) != 0) {
1075 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001076 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001077
Hanno Becker06811ce2017-05-03 15:10:34 +01001078#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1080 return ret;
1081 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001082#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001083
Hanno Becker06811ce2017-05-03 15:10:34 +01001084 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 mbedtls_mpi_init(&P1);
1088 mbedtls_mpi_init(&Q1);
1089 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001090
Janos Follathe81102e2017-03-22 13:38:28 +00001091#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001093#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 mbedtls_mpi_init(&DP_blind);
1095 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001096#endif
1097
Hanno Becker06811ce2017-05-03 15:10:34 +01001098#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001100#endif
1101
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001102 mbedtls_mpi_init(&input_blinded);
1103 mbedtls_mpi_init(&check_result_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001104
1105 /* End of MPI initialization */
1106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1108 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001109 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1110 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 }
1112
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001113 /*
1114 * Blinding
1115 * T = T * Vi mod N
1116 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1118 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1119 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001120
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001121 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&input_blinded, &T));
Janos Follath6bcbc922023-11-21 09:46:43 +00001122
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001123 /*
1124 * Exponent blinding
1125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1127 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001128
Janos Follathf9203b42017-03-22 15:13:15 +00001129#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001130 /*
1131 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1132 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1134 f_rng, p_rng));
1135 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1136 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1137 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathf9203b42017-03-22 15:13:15 +00001138#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001139 /*
1140 * DP_blind = ( P - 1 ) * R + DP
1141 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1143 f_rng, p_rng));
1144 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1145 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1146 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001147
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001148 /*
1149 * DQ_blind = ( Q - 1 ) * R + DQ
1150 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1152 f_rng, p_rng));
1153 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1154 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1155 &ctx->DQ));
Janos Follathe81102e2017-03-22 13:38:28 +00001156#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath47ee7702023-12-27 10:33:00 +00001159 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &D_blind, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001160#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001161 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001162 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001164 * TP = input ^ dP mod P
1165 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001167
Janos Follath47ee7702023-12-27 10:33:00 +00001168 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, &DP_blind, &ctx->P, &ctx->RP));
1169 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, &DQ_blind, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001170
1171 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001172 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001173 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1175 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1176 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
1178 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001179 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1182 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001184
Hanno Becker2dec5e82017-10-03 07:49:52 +01001185 /* Verify the result to prevent glitching attacks. */
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001186 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&check_result_blinded, &T, &ctx->E,
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 &ctx->N, &ctx->RN));
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001188 if (mbedtls_mpi_cmp_mpi(&check_result_blinded, &input_blinded) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001189 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1190 goto cleanup;
1191 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001192
Janos Follath6bcbc922023-11-21 09:46:43 +00001193 /*
1194 * Unblind
1195 * T = T * Vf mod N
1196 */
1197 MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1198
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001201
1202cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1205 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1206 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001207#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 mbedtls_mpi_free(&P1);
1210 mbedtls_mpi_free(&Q1);
1211 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001212
Janos Follathe81102e2017-03-22 13:38:28 +00001213#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001215#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 mbedtls_mpi_free(&DP_blind);
1217 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001218#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001221
1222#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001224#endif
1225
Janos Follathb4b8f3d2023-12-27 10:44:36 +00001226 mbedtls_mpi_free(&check_result_blinded);
1227 mbedtls_mpi_free(&input_blinded);
Hanno Becker06811ce2017-05-03 15:10:34 +01001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if (ret != 0 && ret >= -0x007f) {
1230 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1231 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001234}
1235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001237/**
1238 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1239 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001240 * \param dst buffer to mask
1241 * \param dlen length of destination buffer
1242 * \param src source of the mask generation
1243 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001244 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1247 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001248{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001249 unsigned char counter[4];
1250 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001251 unsigned int hlen;
1252 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001253 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001254 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001255 const mbedtls_md_info_t *md_info;
1256 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 mbedtls_md_init(&md_ctx);
1259 md_info = mbedtls_md_info_from_type(md_alg);
1260 if (md_info == NULL) {
1261 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1262 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 mbedtls_md_init(&md_ctx);
1265 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001266 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 memset(mask, 0, sizeof(mask));
1272 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001273
Simon Butcher02037452016-03-01 21:19:12 +00001274 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001275 p = dst;
1276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001278 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001280 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001284 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 }
1286 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001287 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 }
1289 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 }
1292 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001293 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001297 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001299
1300 counter[3]++;
1301
1302 dlen -= use_len;
1303 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001304
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001305exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001310}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001311
1312/**
1313 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1314 *
1315 * \param hash the input hash
1316 * \param hlen length of the input hash
1317 * \param salt the input salt
1318 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001319 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001320 * \param md_alg message digest to use
1321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001322static int hash_mprime(const unsigned char *hash, size_t hlen,
1323 const unsigned char *salt, size_t slen,
1324 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001325{
1326 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001327
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001328 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1332 if (md_info == NULL) {
1333 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1334 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 mbedtls_md_init(&md_ctx);
1337 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001338 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 }
1340 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001341 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 }
1343 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001344 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 }
1346 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001347 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 }
1349 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001350 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 }
1352 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001353 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001355
1356exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001360}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001361
1362/**
1363 * Compute a hash.
1364 *
1365 * \param md_alg algorithm to use
1366 * \param input input message to hash
1367 * \param ilen input length
1368 * \param output the output buffer - must be large enough for \p md_alg
1369 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001370static int compute_hash(mbedtls_md_type_t md_alg,
1371 const unsigned char *input, size_t ilen,
1372 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001373{
1374 const mbedtls_md_info_t *md_info;
1375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 md_info = mbedtls_md_info_from_type(md_alg);
1377 if (md_info == NULL) {
1378 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1379 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001386/*
1387 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001389int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1390 int (*f_rng)(void *, unsigned char *, size_t),
1391 void *p_rng,
1392 const unsigned char *label, size_t label_len,
1393 size_t ilen,
1394 const unsigned char *input,
1395 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001396{
1397 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001399 unsigned char *p = output;
1400 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 if (f_rng == NULL) {
1403 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1404 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001405
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001406 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (hlen == 0) {
1408 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1409 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001410
1411 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001412
Simon Butcher02037452016-03-01 21:19:12 +00001413 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1415 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1416 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001419
1420 *p++ = 0;
1421
Simon Butcher02037452016-03-01 21:19:12 +00001422 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1424 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1425 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001426
1427 p += hlen;
1428
Simon Butcher02037452016-03-01 21:19:12 +00001429 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1431 if (ret != 0) {
1432 return ret;
1433 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001434 p += hlen;
1435 p += olen - 2 * hlen - 2 - ilen;
1436 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if (ilen != 0) {
1438 memcpy(p, input, ilen);
1439 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001440
Simon Butcher02037452016-03-01 21:19:12 +00001441 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001443 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 return ret;
1445 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001446
Simon Butcher02037452016-03-01 21:19:12 +00001447 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001449 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 return ret;
1451 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001458/*
1459 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001461int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1462 int (*f_rng)(void *, unsigned char *, size_t),
1463 void *p_rng, size_t ilen,
1464 const unsigned char *input,
1465 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001466{
1467 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001469 unsigned char *p = output;
1470
Paul Bakkerb3869132013-02-28 17:21:01 +01001471 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001472
Simon Butcher02037452016-03-01 21:19:12 +00001473 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if (ilen + 11 < ilen || olen < ilen + 11) {
1475 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1476 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001477
1478 nb_pad = olen - 3 - ilen;
1479
1480 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 if (f_rng == NULL) {
1483 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1484 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001485
1486 *p++ = MBEDTLS_RSA_CRYPT;
1487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001489 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001490
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001491 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 ret = f_rng(p_rng, p, 1);
1493 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001494
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001495 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (rng_dl == 0 || ret != 0) {
1497 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1498 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001499
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001500 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001501 }
1502
1503 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if (ilen != 0) {
1505 memcpy(p, input, ilen);
1506 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001511
Paul Bakker5121ce52009-01-03 21:22:43 +00001512/*
1513 * Add the message padding, then do an RSA operation
1514 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001515int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1516 int (*f_rng)(void *, unsigned char *, size_t),
1517 void *p_rng,
1518 size_t ilen,
1519 const unsigned char *input,
1520 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001521{
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#if defined(MBEDTLS_PKCS1_V15)
1524 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1526 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001527#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_PKCS1_V21)
1530 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1532 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001533#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001534
1535 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001538}
1539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001541/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001542 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001544int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1545 int (*f_rng)(void *, unsigned char *, size_t),
1546 void *p_rng,
1547 const unsigned char *label, size_t label_len,
1548 size_t *olen,
1549 const unsigned char *input,
1550 unsigned char *output,
1551 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001552{
Janos Follath24eed8d2019-11-22 13:21:35 +00001553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001554 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001555 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001556 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001558 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001559 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001560
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001561 /*
1562 * Parameters sanity checks
1563 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1565 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1566 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001567
1568 ilen = ctx->len;
1569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (ilen < 16 || ilen > sizeof(buf)) {
1571 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1572 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001573
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001574 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (hlen == 0) {
1576 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1577 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001578
Janos Follathc17cda12016-02-11 11:08:18 +00001579 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (2 * hlen + 2 > ilen) {
1581 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1582 }
Janos Follathc17cda12016-02-11 11:08:18 +00001583
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001584 /*
1585 * RSA operation
1586 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001590 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001592
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001593 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001594 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001595 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001596 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001598 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 /* DB: Apply dbMask to maskedDB */
1600 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001601 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001602 goto cleanup;
1603 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001604
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001605 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1607 label, label_len, lhash);
1608 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001609 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001611
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001612 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001613 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001614 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 p = buf;
1616
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001617 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001618
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001619 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001620
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001621 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001622 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001623 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001624
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001625 /* Get zero-padding len, but always read till end of buffer
1626 * (minus one, for the 01 byte) */
1627 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001628 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001630 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1631 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001632 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001633
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001634 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001635 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001636
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001637 /*
1638 * The only information "leaked" is whether the padding was correct or not
1639 * (eg, no data is copied if it was not correct). This meets the
1640 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1641 * the different error conditions.
1642 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001643 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001644 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1645 goto cleanup;
1646 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001647
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001648 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001649 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1650 goto cleanup;
1651 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001652
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001653 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 if (*olen != 0) {
1655 memcpy(output, p, *olen);
1656 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001657 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001658
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001659cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 mbedtls_platform_zeroize(buf, sizeof(buf));
1661 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001664}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001668/*
1669 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1670 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001671int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1672 int (*f_rng)(void *, unsigned char *, size_t),
1673 void *p_rng,
1674 size_t *olen,
1675 const unsigned char *input,
1676 unsigned char *output,
1677 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001678{
1679 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1680 size_t ilen;
1681 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1682
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001683 ilen = ctx->len;
1684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1686 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1687 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 if (ilen < 16 || ilen > sizeof(buf)) {
1690 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1691 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001696 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
1700 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02001701
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001702cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001706}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001708
1709/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001710 * Do an RSA operation, then remove the message padding
1711 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001712int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1713 int (*f_rng)(void *, unsigned char *, size_t),
1714 void *p_rng,
1715 size_t *olen,
1716 const unsigned char *input,
1717 unsigned char *output,
1718 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01001719{
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#if defined(MBEDTLS_PKCS1_V15)
1722 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1724 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02001725#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#if defined(MBEDTLS_PKCS1_V21)
1728 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1730 olen, input, output,
1731 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01001732#endif
1733
1734 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01001736 }
1737}
1738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001740static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
1741 int (*f_rng)(void *, unsigned char *, size_t),
1742 void *p_rng,
1743 mbedtls_md_type_t md_alg,
1744 unsigned int hashlen,
1745 const unsigned char *hash,
1746 int saltlen,
1747 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01001748{
1749 size_t olen;
1750 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001751 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001752 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001753 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001754 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001755 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01001758 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 if (f_rng == NULL) {
1762 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1763 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001764
1765 olen = ctx->len;
1766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00001768 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001769 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 if (exp_hashlen == 0) {
1771 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1772 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if (hashlen != exp_hashlen) {
1775 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1776 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001777 }
1778
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001779 hash_id = (mbedtls_md_type_t) ctx->hash_id;
1780 if (hash_id == MBEDTLS_MD_NONE) {
1781 hash_id = md_alg;
1782 }
1783 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if (hlen == 0) {
1785 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1786 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1789 /* Calculate the largest possible salt length, up to the hash size.
1790 * Normally this is the hash length, which is the maximum salt length
1791 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1792 * enough room, use the maximum salt length that fits. The constraint is
1793 * that the hash length plus the salt length plus 2 bytes must be at most
1794 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1795 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001796 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 if (olen < hlen + min_slen + 2) {
1798 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1799 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001800 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001802 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 }
1804 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1805 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1806 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02001807 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001808 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001811
Simon Butcher02037452016-03-01 21:19:12 +00001812 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01001814 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01001815 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02001816
1817 /* Generate salt of length slen in place in the encoded message */
1818 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1820 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1821 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02001822
Paul Bakkerb3869132013-02-28 17:21:01 +01001823 p += slen;
1824
Simon Butcher02037452016-03-01 21:19:12 +00001825 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001826 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if (ret != 0) {
1828 return ret;
1829 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001830
Simon Butcher02037452016-03-01 21:19:12 +00001831 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01001833 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001835
Simon Butcher02037452016-03-01 21:19:12 +00001836 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001837 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 if (ret != 0) {
1839 return ret;
1840 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1843 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01001844
1845 p += hlen;
1846 *p++ = 0xBC;
1847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01001849}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001850
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001851static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1852 int (*f_rng)(void *, unsigned char *, size_t),
1853 void *p_rng,
1854 mbedtls_md_type_t md_alg,
1855 unsigned int hashlen,
1856 const unsigned char *hash,
1857 int saltlen,
1858 unsigned char *sig)
1859{
1860 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1861 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1862 }
1863 if (ctx->hash_id == MBEDTLS_MD_NONE) {
1864 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1865 }
1866 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
1867 sig);
1868}
1869
1870int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
1871 int (*f_rng)(void *, unsigned char *, size_t),
1872 void *p_rng,
1873 mbedtls_md_type_t md_alg,
1874 unsigned int hashlen,
1875 const unsigned char *hash,
1876 unsigned char *sig)
1877{
1878 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
1879 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
1880}
1881
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001882/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02001883 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1884 * the option to pass in the salt length.
1885 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001886int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1887 int (*f_rng)(void *, unsigned char *, size_t),
1888 void *p_rng,
1889 mbedtls_md_type_t md_alg,
1890 unsigned int hashlen,
1891 const unsigned char *hash,
1892 int saltlen,
1893 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02001894{
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1896 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02001897}
1898
Cédric Meuterf3fab332020-04-25 11:30:45 +02001899/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001900 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1901 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001902int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1903 int (*f_rng)(void *, unsigned char *, size_t),
1904 void *p_rng,
1905 mbedtls_md_type_t md_alg,
1906 unsigned int hashlen,
1907 const unsigned char *hash,
1908 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001909{
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1911 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02001912}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001916/*
1917 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1918 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01001919
1920/* Construct a PKCS v1.5 encoding of a hashed message
1921 *
1922 * This is used both for signature generation and verification.
1923 *
1924 * Parameters:
1925 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001926 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001927 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001928 * - hash: Buffer containing the hashed message or the raw data.
1929 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001930 * - dst: Buffer to hold the encoded message.
1931 *
1932 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02001933 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01001934 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01001935 *
1936 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001937static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1938 unsigned int hashlen,
1939 const unsigned char *hash,
1940 size_t dst_len,
1941 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01001942{
1943 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01001944 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01001945 unsigned char *p = dst;
1946 const char *oid = NULL;
1947
1948 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001950 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 if (md_size == 0) {
1952 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1953 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
1956 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1957 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if (hashlen != md_size) {
1960 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1961 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001962
1963 /* Double-check that 8 + hashlen + oid_size can be used as a
1964 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01001966 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 10 + hashlen + oid_size < 10 + hashlen) {
1968 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1969 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001970
1971 /*
1972 * Static bounds check:
1973 * - Need 10 bytes for five tag-length pairs.
1974 * (Insist on 1-byte length encodings to protect against variants of
1975 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
1976 * - Need hashlen bytes for hash
1977 * - Need oid_size bytes for hash alg OID.
1978 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if (nb_pad < 10 + hashlen + oid_size) {
1980 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1981 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001982 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 } else {
1984 if (nb_pad < hashlen) {
1985 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1986 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001987
1988 nb_pad -= hashlen;
1989 }
1990
Hanno Becker2b2f8982017-09-27 17:10:03 +01001991 /* Need space for signature header and padding delimiter (3 bytes),
1992 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 if (nb_pad < 3 + 8) {
1994 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1995 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01001996 nb_pad -= 3;
1997
1998 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01001999 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002000
2001 /* Write signature header and padding */
2002 *p++ = 0;
2003 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002005 p += nb_pad;
2006 *p++ = 0;
2007
2008 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 if (md_alg == MBEDTLS_MD_NONE) {
2010 memcpy(p, hash, hashlen);
2011 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002012 }
2013
2014 /* Signing hashed data, add corresponding ASN.1 structure
2015 *
2016 * DigestInfo ::= SEQUENCE {
2017 * digestAlgorithm DigestAlgorithmIdentifier,
2018 * digest Digest }
2019 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2020 * Digest ::= OCTET STRING
2021 *
2022 * Schematic:
2023 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2024 * TAG-NULL + LEN [ NULL ] ]
2025 * TAG-OCTET + LEN [ HASH ] ]
2026 */
2027 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002029 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002031 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002032 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002034 p += oid_size;
2035 *p++ = MBEDTLS_ASN1_NULL;
2036 *p++ = 0x00;
2037 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002038 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002040 p += hashlen;
2041
2042 /* Just a sanity-check, should be automatic
2043 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (p != dst + dst_len) {
2045 mbedtls_platform_zeroize(dst, dst_len);
2046 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002047 }
2048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002050}
2051
Paul Bakkerb3869132013-02-28 17:21:01 +01002052/*
2053 * Do an RSA operation to sign the message digest
2054 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2056 int (*f_rng)(void *, unsigned char *, size_t),
2057 void *p_rng,
2058 mbedtls_md_type_t md_alg,
2059 unsigned int hashlen,
2060 const unsigned char *hash,
2061 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002062{
Janos Follath24eed8d2019-11-22 13:21:35 +00002063 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002064 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002067 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2071 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2072 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002073
Hanno Beckerfdf38032017-09-06 12:35:55 +01002074 /*
2075 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2076 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2079 ctx->len, sig)) != 0) {
2080 return ret;
2081 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002082
Hanno Beckerfdf38032017-09-06 12:35:55 +01002083 /* Private key operation
2084 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002085 * In order to prevent Lenstra's attack, make the signature in a
2086 * temporary buffer and check it before returning it.
2087 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 sig_try = mbedtls_calloc(1, ctx->len);
2090 if (sig_try == NULL) {
2091 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002092 }
2093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 verif = mbedtls_calloc(1, ctx->len);
2095 if (verif == NULL) {
2096 mbedtls_free(sig_try);
2097 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2098 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2101 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2102
2103 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002104 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2105 goto cleanup;
2106 }
2107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002109
2110cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002111 mbedtls_zeroize_and_free(sig_try, ctx->len);
2112 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 if (ret != 0) {
2115 memset(sig, '!', ctx->len);
2116 }
2117 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002118}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002120
2121/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 * Do an RSA operation to sign the message digest
2123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002124int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2125 int (*f_rng)(void *, unsigned char *, size_t),
2126 void *p_rng,
2127 mbedtls_md_type_t md_alg,
2128 unsigned int hashlen,
2129 const unsigned char *hash,
2130 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002131{
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002133 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137#if defined(MBEDTLS_PKCS1_V15)
2138 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2140 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002141#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143#if defined(MBEDTLS_PKCS1_V21)
2144 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2146 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002147#endif
2148
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002152}
2153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002155/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002156 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002158int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2159 mbedtls_md_type_t md_alg,
2160 unsigned int hashlen,
2161 const unsigned char *hash,
2162 mbedtls_md_type_t mgf1_hash_id,
2163 int expected_salt_len,
2164 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002165{
Janos Follath24eed8d2019-11-22 13:21:35 +00002166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002167 size_t siglen;
2168 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002169 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002170 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002171 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002172 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002176 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002178
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 siglen = ctx->len;
2180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (siglen < 16 || siglen > sizeof(buf)) {
2182 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 if (ret != 0) {
2188 return ret;
2189 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
2191 p = buf;
2192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 if (buf[siglen - 1] != 0xBC) {
2194 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002195 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (md_alg != MBEDTLS_MD_NONE) {
2198 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002199 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (exp_hashlen == 0) {
2201 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2202 }
2203
2204 if (hashlen != exp_hashlen) {
2205 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2206 }
2207 }
2208
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002209 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (hlen == 0) {
2211 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2212 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002213
Simon Butcher02037452016-03-01 21:19:12 +00002214 /*
2215 * Note: EMSA-PSS verification is over the length of N - 1 bits
2216 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (buf[0] >> (8 - siglen * 8 + msb)) {
2220 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2221 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002222
Simon Butcher02037452016-03-01 21:19:12 +00002223 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002225 p++;
2226 siglen -= 1;
2227 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 if (siglen < hlen + 2) {
2230 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2231 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002232 hash_start = p + siglen - hlen - 1;
2233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2235 if (ret != 0) {
2236 return ret;
2237 }
Paul Bakker02303e82013-01-03 11:08:31 +01002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002242 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 if (*p++ != 0x01) {
2246 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2247 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002248
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002249 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2252 observed_salt_len != (size_t) expected_salt_len) {
2253 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002254 }
2255
Simon Butcher02037452016-03-01 21:19:12 +00002256 /*
2257 * Generate H = Hash( M' )
2258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2260 result, mgf1_hash_id);
2261 if (ret != 0) {
2262 return ret;
2263 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 if (memcmp(hash_start, result, hlen) != 0) {
2266 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2267 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002270}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002271
2272/*
2273 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2274 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002275int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2276 mbedtls_md_type_t md_alg,
2277 unsigned int hashlen,
2278 const unsigned char *hash,
2279 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002280{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002281 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002283 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002288 : md_alg;
2289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2291 md_alg, hashlen, hash,
2292 mgf1_hash_id,
2293 MBEDTLS_RSA_SALT_LEN_ANY,
2294 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002295
2296}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002300/*
2301 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002303int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2304 mbedtls_md_type_t md_alg,
2305 unsigned int hashlen,
2306 const unsigned char *hash,
2307 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002308{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002309 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002310 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002311 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002314 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002316
2317 sig_len = ctx->len;
2318
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002319 /*
2320 * Prepare expected PKCS1 v1.5 encoding of hash.
2321 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2324 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002325 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2326 goto cleanup;
2327 }
2328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2330 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002331 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002333
2334 /*
2335 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2336 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 ret = mbedtls_rsa_public(ctx, sig, encoded);
2339 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002340 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002342
Simon Butcher02037452016-03-01 21:19:12 +00002343 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002344 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002345 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2348 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002349 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2350 goto cleanup;
2351 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002352
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002353cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002356 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002357 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002360 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002361 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002366
2367/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002368 * Do an RSA operation and check the message digest
2369 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002370int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2371 mbedtls_md_type_t md_alg,
2372 unsigned int hashlen,
2373 const unsigned char *hash,
2374 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002375{
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002377 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381#if defined(MBEDTLS_PKCS1_V15)
2382 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2384 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002385#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387#if defined(MBEDTLS_PKCS1_V21)
2388 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2390 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002391#endif
2392
2393 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002395 }
2396}
2397
2398/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002399 * Copy the components of an RSA key
2400 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002401int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002402{
Janos Follath24eed8d2019-11-22 13:21:35 +00002403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002404
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002405 dst->len = src->len;
2406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2408 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2411 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2412 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002413
2414#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2416 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2417 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2418 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2419 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002420#endif
2421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2425 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002426
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002427 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002428 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002429
2430cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 if (ret != 0) {
2432 mbedtls_rsa_free(dst);
2433 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002436}
2437
2438/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 * Free the components of an RSA key
2440 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002441void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002442{
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002444 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 mbedtls_mpi_free(&ctx->Vi);
2448 mbedtls_mpi_free(&ctx->Vf);
2449 mbedtls_mpi_free(&ctx->RN);
2450 mbedtls_mpi_free(&ctx->D);
2451 mbedtls_mpi_free(&ctx->Q);
2452 mbedtls_mpi_free(&ctx->P);
2453 mbedtls_mpi_free(&ctx->E);
2454 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002455
Hanno Becker33c30a02017-08-23 07:00:22 +01002456#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 mbedtls_mpi_free(&ctx->RQ);
2458 mbedtls_mpi_free(&ctx->RP);
2459 mbedtls_mpi_free(&ctx->QP);
2460 mbedtls_mpi_free(&ctx->DQ);
2461 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002462#endif /* MBEDTLS_RSA_NO_CRT */
2463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002465 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (ctx->ver != 0) {
2467 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002468 ctx->ver = 0;
2469 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002470#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002471}
2472
Hanno Beckerab377312017-08-23 16:24:51 +01002473#endif /* !MBEDTLS_RSA_ALT */
2474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
Paul Bakker5121ce52009-01-03 21:22:43 +00002477
2478/*
2479 * Example RSA-1024 keypair, for test purposes
2480 */
2481#define KEY_LEN 128
2482
2483#define RSA_N "9292758453063D803DD603D5E777D788" \
2484 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2485 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2486 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2487 "93A89813FBF3C4F8066D2D800F7C38A8" \
2488 "1AE31942917403FF4946B0A83D3D3E05" \
2489 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2490 "5E94BB77B07507233A0BC7BAC8F90F79"
2491
2492#define RSA_E "10001"
2493
2494#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2495 "66CA472BC44D253102F8B4A9D3BFA750" \
2496 "91386C0077937FE33FA3252D28855837" \
2497 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2498 "DF79C5CE07EE72C7F123142198164234" \
2499 "CABB724CF78B8173B9F880FC86322407" \
2500 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2501 "071513A1E85B5DFA031F21ECAE91A34D"
2502
2503#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2504 "2C01CAD19EA484A87EA4377637E75500" \
2505 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2506 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2507
2508#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2509 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2510 "910E4168387E3C30AA1E00C339A79508" \
2511 "8452DD96A9A5EA5D9DCA68DA636032AF"
2512
Paul Bakker5121ce52009-01-03 21:22:43 +00002513#define PT_LEN 24
2514#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2515 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002518static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002519{
gufe44c2620da2020-08-03 17:56:50 +02002520#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002521 size_t i;
2522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002524 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 }
Paul Bakker545570e2010-07-18 09:00:25 +00002526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002528 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002530#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002532 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002536#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002541
Paul Bakker5121ce52009-01-03 21:22:43 +00002542/*
2543 * Checkup routine
2544 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002545int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002546{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002547 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002549 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 unsigned char rsa_plaintext[PT_LEN];
2552 unsigned char rsa_decrypted[PT_LEN];
2553 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002554#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002555 unsigned char sha1sum[20];
2556#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
Hanno Becker3a701162017-08-22 13:52:43 +01002558 mbedtls_mpi K;
2559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 mbedtls_mpi_init(&K);
2561 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2564 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2565 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2566 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2567 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2568 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2569 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2570 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2571 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2572 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002573
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (verbose != 0) {
2577 mbedtls_printf(" RSA key validation: ");
2578 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2581 mbedtls_rsa_check_privkey(&rsa) != 0) {
2582 if (verbose != 0) {
2583 mbedtls_printf("failed\n");
2584 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Hanno Becker5bc87292017-05-03 15:09:31 +01002586 ret = 1;
2587 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 }
2589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (verbose != 0) {
2591 mbedtls_printf("passed\n PKCS#1 encryption : ");
2592 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2597 PT_LEN, rsa_plaintext,
2598 rsa_ciphertext) != 0) {
2599 if (verbose != 0) {
2600 mbedtls_printf("failed\n");
2601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Hanno Becker5bc87292017-05-03 15:09:31 +01002603 ret = 1;
2604 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 }
2606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 if (verbose != 0) {
2608 mbedtls_printf("passed\n PKCS#1 decryption : ");
2609 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2612 &len, rsa_ciphertext, rsa_decrypted,
2613 sizeof(rsa_decrypted)) != 0) {
2614 if (verbose != 0) {
2615 mbedtls_printf("failed\n");
2616 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002617
Hanno Becker5bc87292017-05-03 15:09:31 +01002618 ret = 1;
2619 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 }
2621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2623 if (verbose != 0) {
2624 mbedtls_printf("failed\n");
2625 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
Hanno Becker5bc87292017-05-03 15:09:31 +01002627 ret = 1;
2628 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 }
2630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 if (verbose != 0) {
2632 mbedtls_printf("passed\n");
2633 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002634
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002635#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 if (verbose != 0) {
2637 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002638 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002640 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2641 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (verbose != 0) {
2643 mbedtls_printf("failed\n");
2644 }
2645
2646 return 1;
2647 }
2648
2649 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2650 MBEDTLS_MD_SHA1, 20,
2651 sha1sum, rsa_ciphertext) != 0) {
2652 if (verbose != 0) {
2653 mbedtls_printf("failed\n");
2654 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Hanno Becker5bc87292017-05-03 15:09:31 +01002656 ret = 1;
2657 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002658 }
2659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 if (verbose != 0) {
2661 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2665 sha1sum, rsa_ciphertext) != 0) {
2666 if (verbose != 0) {
2667 mbedtls_printf("failed\n");
2668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Hanno Becker5bc87292017-05-03 15:09:31 +01002670 ret = 1;
2671 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 }
2673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 if (verbose != 0) {
2675 mbedtls_printf("passed\n");
2676 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002677#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 if (verbose != 0) {
2680 mbedtls_printf("\n");
2681 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002682
Paul Bakker3d8fb632014-04-17 12:42:41 +02002683cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 mbedtls_mpi_free(&K);
2685 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02002687 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002690}
2691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694#endif /* MBEDTLS_RSA_C */