blob: 4ff7afacfdf7d8c2c8fa2fc2b4a7f67c9025176a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
Hanno Becker74716312017-10-02 10:00:37 +01007
Paul Bakker5121ce52009-01-03 21:22:43 +00008/*
Simon Butcherbdae02c2016-01-20 00:44:42 +00009 * The following sources were referenced in the design of this implementation
10 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000011 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000012 * [1] A method for obtaining digital signatures and public-key cryptosystems
13 * R Rivest, A Shamir, and L Adleman
14 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
15 *
16 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
17 * Menezes, van Oorschot and Vanstone
18 *
Janos Follathe81102e2017-03-22 13:38:28 +000019 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
20 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
21 * Stefan Mangard
22 * https://arxiv.org/abs/1702.08719v2
23 *
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/rsa.h"
Chris Jones66a4cd42021-03-09 16:04:12 +000031#include "rsa_alt_helpers.h"
Tomi Fontanilles573dc232023-12-10 14:57:51 +020032#include "rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/oid.h"
Valerio Settib328c442024-01-23 10:48:45 +010034#include "mbedtls/asn1write.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050035#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000036#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020037#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020038#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020039#include "md_psa.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
gufe44c2620da2020-08-03 17:56:50 +020043#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000044#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000045#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010048
Dave Rodgman19e8cd02023-05-09 11:10:21 +010049
50#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
51
52/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
53 * operation (EME-PKCS1-v1_5 decoding).
54 *
55 * \note The return value from this function is a sensitive value
56 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
57 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
58 * is often a situation that an attacker can provoke and leaking which
59 * one is the result is precisely the information the attacker wants.
60 *
61 * \param input The input buffer which is the payload inside PKCS#1v1.5
62 * encryption padding, called the "encoded message EM"
63 * by the terminology.
64 * \param ilen The length of the payload in the \p input buffer.
65 * \param output The buffer for the payload, called "message M" by the
66 * PKCS#1 terminology. This must be a writable buffer of
67 * length \p output_max_len bytes.
68 * \param olen The address at which to store the length of
69 * the payload. This must not be \c NULL.
70 * \param output_max_len The length in bytes of the output buffer \p output.
71 *
72 * \return \c 0 on success.
73 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
74 * The output buffer is too small for the unpadded payload.
75 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
76 * The input doesn't contain properly formatted padding.
77 */
78static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
79 size_t ilen,
80 unsigned char *output,
81 size_t output_max_len,
82 size_t *olen)
83{
84 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
85 size_t i, plaintext_max_size;
86
87 /* The following variables take sensitive values: their value must
88 * not leak into the observable behavior of the function other than
89 * the designated outputs (output, olen, return value). Otherwise
90 * this would open the execution of the function to
91 * side-channel-based variants of the Bleichenbacher padding oracle
92 * attack. Potential side channels include overall timing, memory
93 * access patterns (especially visible to an adversary who has access
94 * to a shared memory cache), and branches (especially visible to
95 * an adversary who has access to a shared code cache or to a shared
96 * branch predictor). */
97 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +010098 mbedtls_ct_condition_t bad;
99 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100100 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100101 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100102
103 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
104 : output_max_len;
105
106 /* Check and get padding length in constant time and constant
107 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100108 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100109
110
111 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
112 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100113 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100114
115 /* Read the whole buffer. Set pad_done to nonzero if we find
116 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100117 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100118 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100119 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100120 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100121 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100122 }
123
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100124 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100125 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100126
127 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100128 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100129
130 /* If the padding is valid, set plaintext_size to the number of
131 * remaining bytes after stripping the padding. If the padding
132 * is invalid, avoid leaking this fact through the size of the
133 * output: use the maximum message size that fits in the output
134 * buffer. Do it without branches to avoid leaking the padding
135 * validity through timing. RSA keys are small enough that all the
136 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100137 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100138 bad, (unsigned) plaintext_max_size,
139 (unsigned) (ilen - pad_count - 3));
140
141 /* Set output_too_large to 0 if the plaintext fits in the output
142 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100143 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100144 plaintext_max_size);
145
146 /* Set ret without branches to avoid timing attacks. Return:
147 * - INVALID_PADDING if the padding is bad (bad != 0).
148 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
149 * plaintext does not fit in the output buffer.
150 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100151 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100152 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100153 MBEDTLS_ERR_RSA_INVALID_PADDING,
154 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100155 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100156
157 /* If the padding is bad or the plaintext is too large, zero the
158 * data that we're about to copy to the output buffer.
159 * We need to copy the same amount of data
160 * from the same buffer whether the padding is good or not to
161 * avoid leaking the padding validity through overall timing or
162 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100163 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100164
165 /* If the plaintext is too large, truncate it to the buffer size.
166 * Copy anyway to avoid revealing the length through timing, because
167 * revealing the length is as bad as revealing the padding validity
168 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100169 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100170 (unsigned) plaintext_max_size,
171 (unsigned) plaintext_size);
172
173 /* Move the plaintext to the leftmost position where it can start in
174 * the working buffer, i.e. make it start plaintext_max_size from
175 * the end of the buffer. Do this with a memory access trace that
176 * does not depend on the plaintext size. After this move, the
177 * starting location of the plaintext is no longer sensitive
178 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100179 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
180 plaintext_max_size,
181 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100182
183 /* Finally copy the decrypted plaintext plus trailing zeros into the output
184 * buffer. If output_max_len is 0, then output may be an invalid pointer
185 * and the result of memcpy() would be undefined; prevent undefined
186 * behavior making sure to depend only on output_max_len (the size of the
187 * user-provided output buffer), which is independent from plaintext
188 * length, validity of padding, success of the decryption, and other
189 * secrets. */
190 if (output_max_len != 0) {
191 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
192 }
193
194 /* Report the amount of data we copied to the output buffer. In case
195 * of errors (bad padding or output too large), the value of *olen
196 * when this function returns is not specified. Making it equivalent
197 * to the good case limits the risks of leaking the padding validity. */
198 *olen = plaintext_size;
199
200 return ret;
201}
202
203#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
204
Hanno Beckera565f542017-10-11 11:00:19 +0100205#if !defined(MBEDTLS_RSA_ALT)
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
208 const mbedtls_mpi *N,
209 const mbedtls_mpi *P, const mbedtls_mpi *Q,
210 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100211{
Janos Follath24eed8d2019-11-22 13:21:35 +0000212 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
215 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
216 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
217 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
218 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
219 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100220 }
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (N != NULL) {
223 ctx->len = mbedtls_mpi_size(&ctx->N);
224 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100227}
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
230 unsigned char const *N, size_t N_len,
231 unsigned char const *P, size_t P_len,
232 unsigned char const *Q, size_t Q_len,
233 unsigned char const *D, size_t D_len,
234 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100235{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000236 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if (N != NULL) {
239 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
240 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100241 }
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (P != NULL) {
244 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
245 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (Q != NULL) {
248 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
249 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (D != NULL) {
252 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
253 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (E != NULL) {
256 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
257 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100258
259cleanup:
260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (ret != 0) {
262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
263 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100266}
267
Hanno Becker705fc682017-10-10 17:57:02 +0100268/*
269 * Checks whether the context fields are set in such a way
270 * that the RSA primitives will be able to execute without error.
271 * It does *not* make guarantees for consistency of the parameters.
272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
274 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100275{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100276#if !defined(MBEDTLS_RSA_NO_CRT)
277 /* blinding_needed is only used for NO_CRT to decide whether
278 * P,Q need to be present or not. */
279 ((void) blinding_needed);
280#endif
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
283 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
284 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000285 }
Hanno Becker705fc682017-10-10 17:57:02 +0100286
287 /*
288 * 1. Modular exponentiation needs positive, odd moduli.
289 */
290
291 /* Modular exponentiation wrt. N is always used for
292 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
294 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
295 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100296 }
297
298#if !defined(MBEDTLS_RSA_NO_CRT)
299 /* Modular exponentiation for P and Q is only
300 * used for private key operations and if CRT
301 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (is_priv &&
303 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
304 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
305 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
306 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
307 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100308 }
309#endif /* !MBEDTLS_RSA_NO_CRT */
310
311 /*
312 * 2. Exponents must be positive
313 */
314
315 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
317 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
318 }
Hanno Becker705fc682017-10-10 17:57:02 +0100319
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100320#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100321 /* For private key operations, use D or DP & DQ
322 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
324 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
325 }
Hanno Becker705fc682017-10-10 17:57:02 +0100326#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (is_priv &&
328 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
329 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
330 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100331 }
332#endif /* MBEDTLS_RSA_NO_CRT */
333
334 /* Blinding shouldn't make exponents negative either,
335 * so check that P, Q >= 1 if that hasn't yet been
336 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100337#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (is_priv && blinding_needed &&
339 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
340 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
341 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100342 }
343#endif
344
345 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100346 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100347#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (is_priv &&
349 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
350 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100351 }
352#endif
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100355}
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100358{
359 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500360 int have_N, have_P, have_Q, have_D, have_E;
361#if !defined(MBEDTLS_RSA_NO_CRT)
362 int have_DP, have_DQ, have_QP;
363#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500364 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
367 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
368 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
369 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
370 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500371
372#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
374 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
375 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500376#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100377
Hanno Becker617c1ae2017-08-23 14:11:24 +0100378 /*
379 * Check whether provided parameters are enough
380 * to deduce all others. The following incomplete
381 * parameter sets for private keys are supported:
382 *
383 * (1) P, Q missing.
384 * (2) D and potentially N missing.
385 *
386 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100387
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500388 n_missing = have_P && have_Q && have_D && have_E;
389 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
390 d_missing = have_P && have_Q && !have_D && have_E;
391 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100392
393 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500394 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (!is_priv && !is_pub) {
397 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
398 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100399
400 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100401 * Step 1: Deduce N if P, Q are provided.
402 */
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (!have_N && have_P && have_Q) {
405 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
406 &ctx->Q)) != 0) {
407 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100408 }
409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100411 }
412
413 /*
414 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100415 */
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (pq_missing) {
418 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
419 &ctx->P, &ctx->Q);
420 if (ret != 0) {
421 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
422 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 } else if (d_missing) {
425 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
426 &ctx->Q,
427 &ctx->E,
428 &ctx->D)) != 0) {
429 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100430 }
431 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100432
Hanno Becker617c1ae2017-08-23 14:11:24 +0100433 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100434 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100435 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100436 */
437
Hanno Becker23344b52017-08-23 07:43:27 +0100438#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (is_priv && !(have_DP && have_DQ && have_QP)) {
440 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
441 &ctx->DP, &ctx->DQ, &ctx->QP);
442 if (ret != 0) {
443 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
444 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100445 }
Hanno Becker23344b52017-08-23 07:43:27 +0100446#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100447
448 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100449 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100450 */
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100453}
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
456 unsigned char *N, size_t N_len,
457 unsigned char *P, size_t P_len,
458 unsigned char *Q, size_t Q_len,
459 unsigned char *D, size_t D_len,
460 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100461{
462 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500463 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100464
465 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500466 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
468 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
469 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
470 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
471 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100474 /* If we're trying to export private parameters for a public key,
475 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (P != NULL || Q != NULL || D != NULL) {
477 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
478 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100479
480 }
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if (N != NULL) {
483 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
484 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 if (P != NULL) {
487 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
488 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (Q != NULL) {
491 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
492 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (D != NULL) {
495 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
496 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (E != NULL) {
499 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
500 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100501
502cleanup:
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100505}
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
508 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
509 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100510{
Janos Follath24eed8d2019-11-22 13:21:35 +0000511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500512 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100513
514 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500515 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
517 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
518 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
519 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
520 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100523 /* If we're trying to export private parameters for a public key,
524 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (P != NULL || Q != NULL || D != NULL) {
526 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
527 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100528
529 }
530
531 /* Export all requested core parameters. */
532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
534 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
535 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
536 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
537 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
538 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100539 }
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100542}
543
544/*
545 * Export CRT parameters
546 * This must also be implemented if CRT is not used, for being able to
547 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
548 * can be used in this case.
549 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100550int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
551 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100552{
Janos Follath24eed8d2019-11-22 13:21:35 +0000553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500554 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100555
556 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500557 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
559 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
560 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
561 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
562 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (!is_priv) {
565 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
566 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100567
Hanno Beckerdc95c892017-08-23 06:57:02 +0100568#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100569 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
571 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
572 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
573 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100574 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100575#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
577 DP, DQ, QP)) != 0) {
578 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100579 }
580#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100583}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100584
Paul Bakker5121ce52009-01-03 21:22:43 +0000585/*
586 * Initialize an RSA context
587 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100588void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000589{
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Ronald Cronc1905a12021-06-05 11:11:14 +0200592 ctx->padding = MBEDTLS_RSA_PKCS_V15;
593 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100596 /* Set ctx->ver to nonzero to indicate that the mutex has been
597 * initialized and will need to be freed. */
598 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200600#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000601}
602
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100603/*
604 * Set padding for an existing RSA context
605 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
607 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100608{
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200610#if defined(MBEDTLS_PKCS1_V15)
611 case MBEDTLS_RSA_PKCS_V15:
612 break;
613#endif
614
615#if defined(MBEDTLS_PKCS1_V21)
616 case MBEDTLS_RSA_PKCS_V21:
617 break;
618#endif
619 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200621 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200622
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200623#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
625 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200626 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200627 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return MBEDTLS_ERR_RSA_INVALID_PADDING;
629 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200630 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200631#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500632
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100633 ctx->padding = padding;
634 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100637}
638
Hanno Becker617c1ae2017-08-23 14:11:24 +0100639/*
Yanray Wang83548b52023-03-15 16:46:34 +0800640 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800641 */
642int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
643{
Yanray Wang644b9012023-03-15 16:50:31 +0800644 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +0800645}
646
647/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800648 * Get hash identifier of mbedtls_md_type_t type
649 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800650int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +0800651{
Yanray Wang644b9012023-03-15 16:50:31 +0800652 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +0800653}
654
655/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100656 * Get length in bytes of RSA modulus
657 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100658size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100659{
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100661}
662
Valerio Settib328c442024-01-23 10:48:45 +0100663/*
664 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
665 *
666 * The value zero is:
667 * - never a valid value for an RSA parameter
668 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
669 *
670 * Since values can't be omitted in PKCS#1, passing a zero value to
671 * rsa_complete() would be incorrect, so reject zero values early.
672 */
673static int asn1_get_nonzero_mpi(unsigned char **p,
674 const unsigned char *end,
675 mbedtls_mpi *X)
676{
677 int ret;
678
679 ret = mbedtls_asn1_get_mpi(p, end, X);
680 if (ret != 0) {
681 return ret;
682 }
683
684 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
Valerio Settidccfd362024-01-23 17:07:59 +0100685 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Valerio Settib328c442024-01-23 10:48:45 +0100686 }
687
688 return 0;
689}
690
691/*
692 * Parse a PKCS#1 encoded private RSA key
693 */
694int mbedtls_rsa_key_parse(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
695{
696 int ret, version;
697 size_t len;
698 unsigned char *p, *end;
699
700 mbedtls_mpi T;
701 mbedtls_mpi_init(&T);
702
703 p = (unsigned char *) key;
704 end = p + keylen;
705
706 /*
707 * This function parses the RSAPrivateKey (PKCS#1)
708 *
709 * RSAPrivateKey ::= SEQUENCE {
710 * version Version,
711 * modulus INTEGER, -- n
712 * publicExponent INTEGER, -- e
713 * privateExponent INTEGER, -- d
714 * prime1 INTEGER, -- p
715 * prime2 INTEGER, -- q
716 * exponent1 INTEGER, -- d mod (p-1)
717 * exponent2 INTEGER, -- d mod (q-1)
718 * coefficient INTEGER, -- (inverse of q) mod p
719 * otherPrimeInfos OtherPrimeInfos OPTIONAL
720 * }
721 */
722 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
723 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Valerio Settidccfd362024-01-23 17:07:59 +0100724 return ret;
Valerio Settib328c442024-01-23 10:48:45 +0100725 }
726
727 end = p + len;
728
729 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
Valerio Settidccfd362024-01-23 17:07:59 +0100730 return ret;
Valerio Settib328c442024-01-23 10:48:45 +0100731 }
732
733 if (version != 0) {
Valerio Settidccfd362024-01-23 17:07:59 +0100734 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Valerio Settib328c442024-01-23 10:48:45 +0100735 }
736
737 /* Import N */
738 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
739 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
740 NULL, NULL)) != 0) {
741 goto cleanup;
742 }
743
744 /* Import E */
745 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
746 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
747 NULL, &T)) != 0) {
748 goto cleanup;
749 }
750
751 /* Import D */
752 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
753 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
754 &T, NULL)) != 0) {
755 goto cleanup;
756 }
757
758 /* Import P */
759 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
760 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
761 NULL, NULL)) != 0) {
762 goto cleanup;
763 }
764
765 /* Import Q */
766 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
767 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
768 NULL, NULL)) != 0) {
769 goto cleanup;
770 }
771
772#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
773 /*
774 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
775 * that they can be easily recomputed from D, P and Q. However by
776 * parsing them from the PKCS1 structure it is possible to avoid
777 * recalculating them which both reduces the overhead of loading
778 * RSA private keys into memory and also avoids side channels which
779 * can arise when computing those values, since all of D, P, and Q
780 * are secret. See https://eprint.iacr.org/2020/055 for a
781 * description of one such attack.
782 */
783
784 /* Import DP */
785 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
786 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
787 goto cleanup;
788 }
789
790 /* Import DQ */
791 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
792 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
793 goto cleanup;
794 }
795
796 /* Import QP */
797 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
798 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
799 goto cleanup;
800 }
801
802#else
803 /* Verify existence of the CRT params */
804 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
805 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
806 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
807 goto cleanup;
808 }
809#endif
810
811 /* rsa_complete() doesn't complete anything with the default
812 * implementation but is still called:
813 * - for the benefit of alternative implementation that may want to
814 * pre-compute stuff beyond what's provided (eg Montgomery factors)
815 * - as is also sanity-checks the key
816 *
817 * Furthermore, we also check the public part for consistency with
818 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
819 */
820 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
821 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
822 goto cleanup;
823 }
824
825 if (p != end) {
Valerio Settidccfd362024-01-23 17:07:59 +0100826 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Valerio Settib328c442024-01-23 10:48:45 +0100827 }
828
829cleanup:
830
831 mbedtls_mpi_free(&T);
832
833 if (ret != 0) {
Valerio Settib328c442024-01-23 10:48:45 +0100834 mbedtls_rsa_free(rsa);
835 }
836
837 return ret;
838}
839
840/*
841 * RSAPublicKey ::= SEQUENCE {
842 * modulus INTEGER, -- n
843 * publicExponent INTEGER -- e
844 * }
845 */
846int mbedtls_rsa_pubkey_parse(mbedtls_rsa_context *rsa, unsigned char **p,
847 const unsigned char *end)
848{
849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
850 size_t len;
851
852 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
853 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Valerio Settidccfd362024-01-23 17:07:59 +0100854 return ret;
Valerio Settib328c442024-01-23 10:48:45 +0100855 }
856
857 if (*p + len != end) {
Valerio Settidccfd362024-01-23 17:07:59 +0100858 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Valerio Settib328c442024-01-23 10:48:45 +0100859 }
860
861 /* Import N */
862 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Settidccfd362024-01-23 17:07:59 +0100863 return ret;
Valerio Settib328c442024-01-23 10:48:45 +0100864 }
865
866 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
867 NULL, 0, NULL, 0)) != 0) {
Valerio Settidccfd362024-01-23 17:07:59 +0100868 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Valerio Settib328c442024-01-23 10:48:45 +0100869 }
870
871 *p += len;
872
873 /* Import E */
874 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Settidccfd362024-01-23 17:07:59 +0100875 return ret;
Valerio Settib328c442024-01-23 10:48:45 +0100876 }
877
878 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
879 NULL, 0, *p, len)) != 0) {
Valerio Settidccfd362024-01-23 17:07:59 +0100880 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Valerio Settib328c442024-01-23 10:48:45 +0100881 }
882
883 *p += len;
884
885 if (mbedtls_rsa_complete(rsa) != 0 ||
886 mbedtls_rsa_check_pubkey(rsa) != 0) {
Valerio Settidccfd362024-01-23 17:07:59 +0100887 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Valerio Settib328c442024-01-23 10:48:45 +0100888 }
889
890 if (*p != end) {
Valerio Settidccfd362024-01-23 17:07:59 +0100891 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Valerio Settib328c442024-01-23 10:48:45 +0100892 }
893
894 return 0;
895}
896
897int mbedtls_rsa_key_write(const mbedtls_rsa_context *rsa, unsigned char *start,
898 unsigned char **p)
899{
900 size_t len = 0;
901 int ret;
902
903 mbedtls_mpi T; /* Temporary holding the exported parameters */
904
905 /*
906 * Export the parameters one after another to avoid simultaneous copies.
907 */
908
909 mbedtls_mpi_init(&T);
910
911 /* Export QP */
912 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
913 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
914 goto end_of_export;
915 }
916 len += ret;
917
918 /* Export DQ */
919 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
920 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
921 goto end_of_export;
922 }
923 len += ret;
924
925 /* Export DP */
926 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
927 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
928 goto end_of_export;
929 }
930 len += ret;
931
932 /* Export Q */
933 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, &T, NULL, NULL)) != 0 ||
934 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
935 goto end_of_export;
936 }
937 len += ret;
938
939 /* Export P */
940 if ((ret = mbedtls_rsa_export(rsa, NULL, &T, NULL, NULL, NULL)) != 0 ||
941 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
942 goto end_of_export;
943 }
944 len += ret;
945
946 /* Export D */
947 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, &T, NULL)) != 0 ||
948 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
949 goto end_of_export;
950 }
951 len += ret;
952
953 /* Export E */
954 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
955 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
956 goto end_of_export;
957 }
958 len += ret;
959
960 /* Export N */
961 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
962 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
963 goto end_of_export;
964 }
965 len += ret;
966
967end_of_export:
968
969 mbedtls_mpi_free(&T);
970 if (ret < 0) {
971 return ret;
972 }
973
974 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
975 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
976 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
977 MBEDTLS_ASN1_CONSTRUCTED |
978 MBEDTLS_ASN1_SEQUENCE));
979
980 return (int) len;
981}
982
983/*
984 * RSAPublicKey ::= SEQUENCE {
985 * modulus INTEGER, -- n
986 * publicExponent INTEGER -- e
987 * }
988 */
989int mbedtls_rsa_pubkey_write(const mbedtls_rsa_context *rsa, unsigned char *start,
990 unsigned char **p)
991{
992 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
993 size_t len = 0;
994 mbedtls_mpi T;
995
996 mbedtls_mpi_init(&T);
997
998 /* Export E */
999 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
1000 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
1001 goto end_of_export;
1002 }
1003 len += ret;
1004
1005 /* Export N */
1006 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
1007 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
1008 goto end_of_export;
1009 }
1010 len += ret;
1011
1012end_of_export:
1013
1014 mbedtls_mpi_free(&T);
1015 if (ret < 0) {
1016 return ret;
1017 }
1018
1019 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
1020 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
1021 MBEDTLS_ASN1_SEQUENCE));
1022
1023 return (int) len;
1024}
Hanno Becker617c1ae2017-08-23 14:11:24 +01001025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
1028/*
1029 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001030 *
1031 * This generation method follows the RSA key pair generation procedure of
1032 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001034int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
1035 int (*f_rng)(void *, unsigned char *, size_t),
1036 void *p_rng,
1037 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +00001038{
Janos Follath24eed8d2019-11-22 13:21:35 +00001039 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -08001040 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +01001041 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
Janos Follathb8fc1b02018-09-03 15:37:01 +01001043 /*
1044 * If the modulus is 1024 bit long or shorter, then the security strength of
1045 * the RSA algorithm is less than or equal to 80 bits and therefore an error
1046 * rate of 2^-80 is sufficient.
1047 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +01001049 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 }
Janos Follathb8fc1b02018-09-03 15:37:01 +01001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 mbedtls_mpi_init(&H);
1053 mbedtls_mpi_init(&G);
1054 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001056 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +01001057 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1058 goto cleanup;
1059 }
1060
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001061 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1063 goto cleanup;
1064 }
1065
1066 /*
1067 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001068 * 1. |P-Q| > 2^( nbits / 2 - 100 )
1069 * 2. GCD( E, (P-1)*(Q-1) ) == 1
1070 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001071 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +00001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 do {
1075 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
1076 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
1079 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001080
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001081 /* 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 +01001082 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
1083 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001087 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 if (H.s < 0) {
1089 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
1090 }
Janos Follathef441782016-09-21 13:18:12 +01001091
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001092 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
1094 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
1095 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001096
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001097 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
1099 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -08001100 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001102
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001103 /* 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 +01001104 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
1105 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
1106 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 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 -08001109 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001111
1112 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001115 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
1117 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001122
Jethro Beekman97f95c92018-02-13 15:50:36 -08001123#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001125 * DP = D mod (P - 1)
1126 * DQ = D mod (Q - 1)
1127 * QP = Q^-1 mod P
1128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
1130 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001131#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
Hanno Becker83aad1f2017-08-23 06:45:10 +01001133 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
1136cleanup:
1137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 mbedtls_mpi_free(&H);
1139 mbedtls_mpi_free(&G);
1140 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (ret != 0) {
1143 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +01001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if ((-ret & ~0x7f) == 0) {
1146 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
1147 }
1148 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 }
1150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001152}
1153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
1156/*
1157 * Check a public RSA key
1158 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001159int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001160{
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
1162 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001163 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
1166 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001167 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
1170 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
1171 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
1172 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1173 }
1174
1175 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001176}
1177
1178/*
Hanno Becker705fc682017-10-10 17:57:02 +01001179 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001181int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001182{
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
1184 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
1185 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 }
Paul Bakker48377d92013-08-30 12:06:24 +02001187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
1189 &ctx->D, &ctx->E, NULL, NULL) != 0) {
1190 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001192
Hanno Beckerb269a852017-08-25 08:03:21 +01001193#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
1195 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
1196 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +01001197 }
1198#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001201}
1202
1203/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001204 * Check if contexts holding a public and private key match
1205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001206int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
1207 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001208{
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
1210 mbedtls_rsa_check_privkey(prv) != 0) {
1211 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001212 }
1213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
1215 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
1216 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001217 }
1218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001220}
1221
1222/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 * Do an RSA public key operation
1224 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001225int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
1226 const unsigned char *input,
1227 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001228{
Janos Follath24eed8d2019-11-22 13:21:35 +00001229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001230 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
1234 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1235 }
Hanno Becker705fc682017-10-10 17:57:02 +01001236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001239#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1241 return ret;
1242 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001243#endif
1244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001248 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1249 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 }
1251
1252 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
1254 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
1256cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1259 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1260 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001261#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (ret != 0) {
1266 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
1267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001270}
1271
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001272/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001273 * Generate or update blinding values, see section 10 of:
1274 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001275 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001276 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001277 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001278static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1279 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001280{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001281 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001282 mbedtls_mpi R;
1283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001287 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1289 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1290 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1291 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001292
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001293 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001294 }
1295
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001296 /* Unblinding value: Vf = random number, invertible mod N */
1297 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +02001299 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
1300 goto cleanup;
1301 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 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 +02001304
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001305 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
1307 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
1308 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001309
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001310 /* At this point, Vi is invertible mod N if and only if both Vf and R
1311 * are invertible mod N. If one of them isn't, we don't need to know
1312 * which one, we just loop and choose new values for both of them.
1313 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
1315 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001316 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -05001320
1321 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
1323 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001324
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001325 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001326 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 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 +02001328
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001329
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001330cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001334}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001335
Paul Bakker5121ce52009-01-03 21:22:43 +00001336/*
Janos Follathe81102e2017-03-22 13:38:28 +00001337 * Exponent blinding supposed to prevent side-channel attacks using multiple
1338 * traces of measurements to recover the RSA key. The more collisions are there,
1339 * the more bits of the key can be recovered. See [3].
1340 *
1341 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001342 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001343 *
1344 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001345 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001346 *
1347 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1348 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1349 * Thus in this sense with 28 byte blinding the security is not reduced by
1350 * side-channel attacks like the one in [3])
1351 *
1352 * This countermeasure does not help if the key recovery is possible with a
1353 * single trace.
1354 */
1355#define RSA_EXPONENT_BLINDING 28
1356
1357/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 * Do an RSA private key operation
1359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1361 int (*f_rng)(void *, unsigned char *, size_t),
1362 void *p_rng,
1363 const unsigned char *input,
1364 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001365{
Janos Follath24eed8d2019-11-22 13:21:35 +00001366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001367 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001368
1369 /* Temporary holding the result */
1370 mbedtls_mpi T;
1371
1372 /* Temporaries holding P-1, Q-1 and the
1373 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001374 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001375
1376#if !defined(MBEDTLS_RSA_NO_CRT)
1377 /* Temporaries holding the results mod p resp. mod q. */
1378 mbedtls_mpi TP, TQ;
1379
1380 /* Temporaries holding the blinded exponents for
1381 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001382 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001383
1384 /* Pointers to actual exponents to be used - either the unblinded
1385 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +00001386 mbedtls_mpi *DP = &ctx->DP;
1387 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +01001388#else
1389 /* Temporary holding the blinded exponent (if used). */
1390 mbedtls_mpi D_blind;
1391
1392 /* Pointer to actual exponent to be used - either the unblinded
1393 * or the blinded one, depending on the presence of a PRNG. */
1394 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +01001395#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001396
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001397 /* Temporaries holding the initial input and the double
1398 * checked result; should be the same in the end. */
1399 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if (f_rng == NULL) {
1402 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1403 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if (rsa_check_context(ctx, 1 /* private key checks */,
1406 1 /* blinding on */) != 0) {
1407 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001408 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001409
Hanno Becker06811ce2017-05-03 15:10:34 +01001410#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1412 return ret;
1413 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001414#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001415
Hanno Becker06811ce2017-05-03 15:10:34 +01001416 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 mbedtls_mpi_init(&P1);
1420 mbedtls_mpi_init(&Q1);
1421 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001422
Janos Follathe81102e2017-03-22 13:38:28 +00001423#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001425#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 mbedtls_mpi_init(&DP_blind);
1427 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001428#endif
1429
Hanno Becker06811ce2017-05-03 15:10:34 +01001430#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001432#endif
1433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 mbedtls_mpi_init(&I);
1435 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001436
1437 /* End of MPI initialization */
1438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1440 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001441 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1442 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 }
1444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +01001446
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001447 /*
1448 * Blinding
1449 * T = T * Vi mod N
1450 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1452 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1453 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001454
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001455 /*
1456 * Exponent blinding
1457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1459 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001460
Janos Follathf9203b42017-03-22 15:13:15 +00001461#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001462 /*
1463 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1464 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1466 f_rng, p_rng));
1467 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1468 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1469 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +00001470
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001471 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001472#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001473 /*
1474 * DP_blind = ( P - 1 ) * R + DP
1475 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1477 f_rng, p_rng));
1478 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1479 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1480 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001481
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001482 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001483
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001484 /*
1485 * DQ_blind = ( Q - 1 ) * R + DQ
1486 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1488 f_rng, p_rng));
1489 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1490 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1491 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001492
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001493 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001494#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001498#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001499 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001500 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001502 * TP = input ^ dP mod P
1503 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1507 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
1509 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001510 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1513 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1514 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001515
1516 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001517 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001518 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1520 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001522
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001523 /*
1524 * Unblind
1525 * T = T * Vf mod N
1526 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1528 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001529
Hanno Becker2dec5e82017-10-03 07:49:52 +01001530 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1532 &ctx->N, &ctx->RN));
1533 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001534 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1535 goto cleanup;
1536 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001537
Paul Bakker5121ce52009-01-03 21:22:43 +00001538 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
1541cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1544 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1545 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001546#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 mbedtls_mpi_free(&P1);
1549 mbedtls_mpi_free(&Q1);
1550 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001551
Janos Follathe81102e2017-03-22 13:38:28 +00001552#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001554#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 mbedtls_mpi_free(&DP_blind);
1556 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001557#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001560
1561#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001563#endif
1564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 mbedtls_mpi_free(&C);
1566 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 if (ret != 0 && ret >= -0x007f) {
1569 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1570 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001573}
1574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001576/**
1577 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1578 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001579 * \param dst buffer to mask
1580 * \param dlen length of destination buffer
1581 * \param src source of the mask generation
1582 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001583 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001584 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001585static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1586 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001587{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001588 unsigned char counter[4];
1589 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001590 unsigned int hlen;
1591 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001592 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001593 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001594 const mbedtls_md_info_t *md_info;
1595 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 mbedtls_md_init(&md_ctx);
1598 md_info = mbedtls_md_info_from_type(md_alg);
1599 if (md_info == NULL) {
1600 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1601 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 mbedtls_md_init(&md_ctx);
1604 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001605 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 memset(mask, 0, sizeof(mask));
1611 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001612
Simon Butcher02037452016-03-01 21:19:12 +00001613 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001614 p = dst;
1615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001617 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001619 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 }
1625 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001626 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 }
1628 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 }
1631 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001632 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001636 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001638
1639 counter[3]++;
1640
1641 dlen -= use_len;
1642 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001643
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001644exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001649}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001650
1651/**
1652 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1653 *
1654 * \param hash the input hash
1655 * \param hlen length of the input hash
1656 * \param salt the input salt
1657 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001658 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001659 * \param md_alg message digest to use
1660 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001661static int hash_mprime(const unsigned char *hash, size_t hlen,
1662 const unsigned char *salt, size_t slen,
1663 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001664{
1665 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001666
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001667 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001668 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1671 if (md_info == NULL) {
1672 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1673 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 mbedtls_md_init(&md_ctx);
1676 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001677 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 }
1679 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001680 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 }
1682 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001683 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 }
1685 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001686 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 }
1688 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001689 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 }
1691 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001692 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001694
1695exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001699}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001700
1701/**
1702 * Compute a hash.
1703 *
1704 * \param md_alg algorithm to use
1705 * \param input input message to hash
1706 * \param ilen input length
1707 * \param output the output buffer - must be large enough for \p md_alg
1708 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001709static int compute_hash(mbedtls_md_type_t md_alg,
1710 const unsigned char *input, size_t ilen,
1711 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001712{
1713 const mbedtls_md_info_t *md_info;
1714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 md_info = mbedtls_md_info_from_type(md_alg);
1716 if (md_info == NULL) {
1717 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1718 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001721}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001725/*
1726 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1727 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1729 int (*f_rng)(void *, unsigned char *, size_t),
1730 void *p_rng,
1731 const unsigned char *label, size_t label_len,
1732 size_t ilen,
1733 const unsigned char *input,
1734 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001735{
1736 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001738 unsigned char *p = output;
1739 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 if (f_rng == NULL) {
1742 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1743 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001744
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001745 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (hlen == 0) {
1747 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1748 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001749
1750 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001751
Simon Butcher02037452016-03-01 21:19:12 +00001752 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1754 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1755 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001758
1759 *p++ = 0;
1760
Simon Butcher02037452016-03-01 21:19:12 +00001761 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1763 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1764 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001765
1766 p += hlen;
1767
Simon Butcher02037452016-03-01 21:19:12 +00001768 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1770 if (ret != 0) {
1771 return ret;
1772 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001773 p += hlen;
1774 p += olen - 2 * hlen - 2 - ilen;
1775 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if (ilen != 0) {
1777 memcpy(p, input, ilen);
1778 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001779
Simon Butcher02037452016-03-01 21:19:12 +00001780 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001782 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 return ret;
1784 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001785
Simon Butcher02037452016-03-01 21:19:12 +00001786 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001788 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 return ret;
1790 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001793}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001797/*
1798 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1799 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001800int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1801 int (*f_rng)(void *, unsigned char *, size_t),
1802 void *p_rng, size_t ilen,
1803 const unsigned char *input,
1804 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001805{
1806 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001808 unsigned char *p = output;
1809
Paul Bakkerb3869132013-02-28 17:21:01 +01001810 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001811
Simon Butcher02037452016-03-01 21:19:12 +00001812 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 if (ilen + 11 < ilen || olen < ilen + 11) {
1814 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1815 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001816
1817 nb_pad = olen - 3 - ilen;
1818
1819 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001820
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 if (f_rng == NULL) {
1822 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1823 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001824
1825 *p++ = MBEDTLS_RSA_CRYPT;
1826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001828 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001829
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001830 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 ret = f_rng(p_rng, p, 1);
1832 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001833
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001834 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 if (rng_dl == 0 || ret != 0) {
1836 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1837 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001838
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001839 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001840 }
1841
1842 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (ilen != 0) {
1844 memcpy(p, input, ilen);
1845 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001850
Paul Bakker5121ce52009-01-03 21:22:43 +00001851/*
1852 * Add the message padding, then do an RSA operation
1853 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001854int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1855 int (*f_rng)(void *, unsigned char *, size_t),
1856 void *p_rng,
1857 size_t ilen,
1858 const unsigned char *input,
1859 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001860{
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862#if defined(MBEDTLS_PKCS1_V15)
1863 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1865 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001866#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868#if defined(MBEDTLS_PKCS1_V21)
1869 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1871 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001872#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001873
1874 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001877}
1878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001880/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001881 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001883int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1884 int (*f_rng)(void *, unsigned char *, size_t),
1885 void *p_rng,
1886 const unsigned char *label, size_t label_len,
1887 size_t *olen,
1888 const unsigned char *input,
1889 unsigned char *output,
1890 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001891{
Janos Follath24eed8d2019-11-22 13:21:35 +00001892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001893 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001894 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001895 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001897 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001898 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001899
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001900 /*
1901 * Parameters sanity checks
1902 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1904 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
1907 ilen = ctx->len;
1908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 if (ilen < 16 || ilen > sizeof(buf)) {
1910 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1911 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001912
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001913 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 if (hlen == 0) {
1915 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1916 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001917
Janos Follathc17cda12016-02-11 11:08:18 +00001918 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 if (2 * hlen + 2 > ilen) {
1920 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1921 }
Janos Follathc17cda12016-02-11 11:08:18 +00001922
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001923 /*
1924 * RSA operation
1925 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001929 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001931
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001932 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001933 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001934 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001935 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001937 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 /* DB: Apply dbMask to maskedDB */
1939 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001940 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001941 goto cleanup;
1942 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001943
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001944 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1946 label, label_len, lhash);
1947 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001948 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001950
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001951 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001952 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001953 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 p = buf;
1955
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001956 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001957
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001958 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001959
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001960 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001961 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001962 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001963
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001964 /* Get zero-padding len, but always read till end of buffer
1965 * (minus one, for the 01 byte) */
1966 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001967 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001969 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1970 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001971 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001972
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001973 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001974 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001975
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001976 /*
1977 * The only information "leaked" is whether the padding was correct or not
1978 * (eg, no data is copied if it was not correct). This meets the
1979 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1980 * the different error conditions.
1981 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001982 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001983 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1984 goto cleanup;
1985 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001986
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001987 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001988 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1989 goto cleanup;
1990 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001991
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001992 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 if (*olen != 0) {
1994 memcpy(output, p, *olen);
1995 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001996 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001997
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001998cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 mbedtls_platform_zeroize(buf, sizeof(buf));
2000 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002007/*
2008 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
2009 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002010int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
2011 int (*f_rng)(void *, unsigned char *, size_t),
2012 void *p_rng,
2013 size_t *olen,
2014 const unsigned char *input,
2015 unsigned char *output,
2016 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002017{
2018 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2019 size_t ilen;
2020 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
2021
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002022 ilen = ctx->len;
2023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2025 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2026 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (ilen < 16 || ilen > sizeof(buf)) {
2029 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2030 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002035 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
2039 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002040
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002041cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002047
2048/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002049 * Do an RSA operation, then remove the message padding
2050 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002051int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
2052 int (*f_rng)(void *, unsigned char *, size_t),
2053 void *p_rng,
2054 size_t *olen,
2055 const unsigned char *input,
2056 unsigned char *output,
2057 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01002058{
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060#if defined(MBEDTLS_PKCS1_V15)
2061 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
2063 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02002064#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066#if defined(MBEDTLS_PKCS1_V21)
2067 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
2069 olen, input, output,
2070 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01002071#endif
2072
2073 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002075 }
2076}
2077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002079static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2080 int (*f_rng)(void *, unsigned char *, size_t),
2081 void *p_rng,
2082 mbedtls_md_type_t md_alg,
2083 unsigned int hashlen,
2084 const unsigned char *hash,
2085 int saltlen,
2086 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002087{
2088 size_t olen;
2089 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002090 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002091 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00002092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002093 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002094 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002097 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 if (f_rng == NULL) {
2101 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2102 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002103
2104 olen = ctx->len;
2105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002107 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002108 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if (exp_hashlen == 0) {
2110 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2111 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (hashlen != exp_hashlen) {
2114 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2115 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002116 }
2117
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002118 hash_id = (mbedtls_md_type_t) ctx->hash_id;
2119 if (hash_id == MBEDTLS_MD_NONE) {
2120 hash_id = md_alg;
2121 }
2122 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if (hlen == 0) {
2124 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2125 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
2128 /* Calculate the largest possible salt length, up to the hash size.
2129 * Normally this is the hash length, which is the maximum salt length
2130 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
2131 * enough room, use the maximum salt length that fits. The constraint is
2132 * that the hash length plus the salt length plus 2 bytes must be at most
2133 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
2134 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002135 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 if (olen < hlen + min_slen + 2) {
2137 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2138 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002139 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002141 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 }
2143 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
2144 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2145 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02002146 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002147 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01002150
Simon Butcher02037452016-03-01 21:19:12 +00002151 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002153 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01002154 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002155
2156 /* Generate salt of length slen in place in the encoded message */
2157 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
2159 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
2160 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02002161
Paul Bakkerb3869132013-02-28 17:21:01 +01002162 p += slen;
2163
Simon Butcher02037452016-03-01 21:19:12 +00002164 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002165 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (ret != 0) {
2167 return ret;
2168 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002169
Simon Butcher02037452016-03-01 21:19:12 +00002170 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002172 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002174
Simon Butcher02037452016-03-01 21:19:12 +00002175 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002176 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (ret != 0) {
2178 return ret;
2179 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2182 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01002183
2184 p += hlen;
2185 *p++ = 0xBC;
2186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002188}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002189
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002190static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2191 int (*f_rng)(void *, unsigned char *, size_t),
2192 void *p_rng,
2193 mbedtls_md_type_t md_alg,
2194 unsigned int hashlen,
2195 const unsigned char *hash,
2196 int saltlen,
2197 unsigned char *sig)
2198{
2199 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2200 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2201 }
2202 if (ctx->hash_id == MBEDTLS_MD_NONE) {
2203 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2204 }
2205 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
2206 sig);
2207}
2208
2209int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2210 int (*f_rng)(void *, unsigned char *, size_t),
2211 void *p_rng,
2212 mbedtls_md_type_t md_alg,
2213 unsigned int hashlen,
2214 const unsigned char *hash,
2215 unsigned char *sig)
2216{
2217 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
2218 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2219}
2220
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002221/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02002222 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
2223 * the option to pass in the salt length.
2224 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002225int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
2226 int (*f_rng)(void *, unsigned char *, size_t),
2227 void *p_rng,
2228 mbedtls_md_type_t md_alg,
2229 unsigned int hashlen,
2230 const unsigned char *hash,
2231 int saltlen,
2232 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02002233{
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2235 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02002236}
2237
Cédric Meuterf3fab332020-04-25 11:30:45 +02002238/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002239 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
2240 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002241int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2242 int (*f_rng)(void *, unsigned char *, size_t),
2243 void *p_rng,
2244 mbedtls_md_type_t md_alg,
2245 unsigned int hashlen,
2246 const unsigned char *hash,
2247 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002248{
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2250 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002251}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002255/*
2256 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
2257 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002258
2259/* Construct a PKCS v1.5 encoding of a hashed message
2260 *
2261 * This is used both for signature generation and verification.
2262 *
2263 * Parameters:
2264 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002265 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002266 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002267 * - hash: Buffer containing the hashed message or the raw data.
2268 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002269 * - dst: Buffer to hold the encoded message.
2270 *
2271 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002272 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002273 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002274 *
2275 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002276static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
2277 unsigned int hashlen,
2278 const unsigned char *hash,
2279 size_t dst_len,
2280 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01002281{
2282 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002283 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002284 unsigned char *p = dst;
2285 const char *oid = NULL;
2286
2287 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002289 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 if (md_size == 0) {
2291 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2292 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2295 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2296 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if (hashlen != md_size) {
2299 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2300 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002301
2302 /* Double-check that 8 + hashlen + oid_size can be used as a
2303 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01002305 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 10 + hashlen + oid_size < 10 + hashlen) {
2307 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2308 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002309
2310 /*
2311 * Static bounds check:
2312 * - Need 10 bytes for five tag-length pairs.
2313 * (Insist on 1-byte length encodings to protect against variants of
2314 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2315 * - Need hashlen bytes for hash
2316 * - Need oid_size bytes for hash alg OID.
2317 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 if (nb_pad < 10 + hashlen + oid_size) {
2319 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2320 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002321 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 } else {
2323 if (nb_pad < hashlen) {
2324 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2325 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002326
2327 nb_pad -= hashlen;
2328 }
2329
Hanno Becker2b2f8982017-09-27 17:10:03 +01002330 /* Need space for signature header and padding delimiter (3 bytes),
2331 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (nb_pad < 3 + 8) {
2333 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2334 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002335 nb_pad -= 3;
2336
2337 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002338 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002339
2340 /* Write signature header and padding */
2341 *p++ = 0;
2342 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002344 p += nb_pad;
2345 *p++ = 0;
2346
2347 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 if (md_alg == MBEDTLS_MD_NONE) {
2349 memcpy(p, hash, hashlen);
2350 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002351 }
2352
2353 /* Signing hashed data, add corresponding ASN.1 structure
2354 *
2355 * DigestInfo ::= SEQUENCE {
2356 * digestAlgorithm DigestAlgorithmIdentifier,
2357 * digest Digest }
2358 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2359 * Digest ::= OCTET STRING
2360 *
2361 * Schematic:
2362 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2363 * TAG-NULL + LEN [ NULL ] ]
2364 * TAG-OCTET + LEN [ HASH ] ]
2365 */
2366 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002368 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002370 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002371 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002373 p += oid_size;
2374 *p++ = MBEDTLS_ASN1_NULL;
2375 *p++ = 0x00;
2376 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002377 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002379 p += hashlen;
2380
2381 /* Just a sanity-check, should be automatic
2382 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 if (p != dst + dst_len) {
2384 mbedtls_platform_zeroize(dst, dst_len);
2385 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002386 }
2387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002389}
2390
Paul Bakkerb3869132013-02-28 17:21:01 +01002391/*
2392 * Do an RSA operation to sign the message digest
2393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002394int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2395 int (*f_rng)(void *, unsigned char *, size_t),
2396 void *p_rng,
2397 mbedtls_md_type_t md_alg,
2398 unsigned int hashlen,
2399 const unsigned char *hash,
2400 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002401{
Janos Follath24eed8d2019-11-22 13:21:35 +00002402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002403 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002406 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2410 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2411 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002412
Hanno Beckerfdf38032017-09-06 12:35:55 +01002413 /*
2414 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2415 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2418 ctx->len, sig)) != 0) {
2419 return ret;
2420 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002421
Hanno Beckerfdf38032017-09-06 12:35:55 +01002422 /* Private key operation
2423 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002424 * In order to prevent Lenstra's attack, make the signature in a
2425 * temporary buffer and check it before returning it.
2426 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 sig_try = mbedtls_calloc(1, ctx->len);
2429 if (sig_try == NULL) {
2430 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002431 }
2432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 verif = mbedtls_calloc(1, ctx->len);
2434 if (verif == NULL) {
2435 mbedtls_free(sig_try);
2436 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2437 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2440 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2441
2442 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002443 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2444 goto cleanup;
2445 }
2446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002448
2449cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002450 mbedtls_zeroize_and_free(sig_try, ctx->len);
2451 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 if (ret != 0) {
2454 memset(sig, '!', ctx->len);
2455 }
2456 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002459
2460/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 * Do an RSA operation to sign the message digest
2462 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002463int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2464 int (*f_rng)(void *, unsigned char *, size_t),
2465 void *p_rng,
2466 mbedtls_md_type_t md_alg,
2467 unsigned int hashlen,
2468 const unsigned char *hash,
2469 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002470{
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002472 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476#if defined(MBEDTLS_PKCS1_V15)
2477 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2479 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002480#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#if defined(MBEDTLS_PKCS1_V21)
2483 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2485 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002486#endif
2487
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002491}
2492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002494/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002495 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002497int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2498 mbedtls_md_type_t md_alg,
2499 unsigned int hashlen,
2500 const unsigned char *hash,
2501 mbedtls_md_type_t mgf1_hash_id,
2502 int expected_salt_len,
2503 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002504{
Janos Follath24eed8d2019-11-22 13:21:35 +00002505 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002506 size_t siglen;
2507 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002508 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002509 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002510 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002511 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002515 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002517
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 siglen = ctx->len;
2519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 if (siglen < 16 || siglen > sizeof(buf)) {
2521 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2522 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 if (ret != 0) {
2527 return ret;
2528 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
2530 p = buf;
2531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 if (buf[siglen - 1] != 0xBC) {
2533 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002534 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (md_alg != MBEDTLS_MD_NONE) {
2537 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002538 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (exp_hashlen == 0) {
2540 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2541 }
2542
2543 if (hashlen != exp_hashlen) {
2544 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2545 }
2546 }
2547
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002548 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 if (hlen == 0) {
2550 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2551 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002552
Simon Butcher02037452016-03-01 21:19:12 +00002553 /*
2554 * Note: EMSA-PSS verification is over the length of N - 1 bits
2555 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (buf[0] >> (8 - siglen * 8 + msb)) {
2559 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2560 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002561
Simon Butcher02037452016-03-01 21:19:12 +00002562 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002564 p++;
2565 siglen -= 1;
2566 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (siglen < hlen + 2) {
2569 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2570 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002571 hash_start = p + siglen - hlen - 1;
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2574 if (ret != 0) {
2575 return ret;
2576 }
Paul Bakker02303e82013-01-03 11:08:31 +01002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002581 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 if (*p++ != 0x01) {
2585 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2586 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002587
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002588 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2591 observed_salt_len != (size_t) expected_salt_len) {
2592 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002593 }
2594
Simon Butcher02037452016-03-01 21:19:12 +00002595 /*
2596 * Generate H = Hash( M' )
2597 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2599 result, mgf1_hash_id);
2600 if (ret != 0) {
2601 return ret;
2602 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 if (memcmp(hash_start, result, hlen) != 0) {
2605 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2606 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002609}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002610
2611/*
2612 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2613 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002614int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2615 mbedtls_md_type_t md_alg,
2616 unsigned int hashlen,
2617 const unsigned char *hash,
2618 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002619{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002620 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002622 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002627 : md_alg;
2628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2630 md_alg, hashlen, hash,
2631 mgf1_hash_id,
2632 MBEDTLS_RSA_SALT_LEN_ANY,
2633 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002634
2635}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002639/*
2640 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2641 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2643 mbedtls_md_type_t md_alg,
2644 unsigned int hashlen,
2645 const unsigned char *hash,
2646 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002647{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002648 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002649 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002650 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002651
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002653 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002655
2656 sig_len = ctx->len;
2657
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002658 /*
2659 * Prepare expected PKCS1 v1.5 encoding of hash.
2660 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2663 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002664 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2665 goto cleanup;
2666 }
2667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2669 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002670 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002672
2673 /*
2674 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2675 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 ret = mbedtls_rsa_public(ctx, sig, encoded);
2678 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002679 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002681
Simon Butcher02037452016-03-01 21:19:12 +00002682 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002683 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002684 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2687 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002688 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2689 goto cleanup;
2690 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002691
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002692cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002695 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002696 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002699 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002700 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
2706/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002707 * Do an RSA operation and check the message digest
2708 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002709int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2710 mbedtls_md_type_t md_alg,
2711 unsigned int hashlen,
2712 const unsigned char *hash,
2713 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002714{
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002716 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#if defined(MBEDTLS_PKCS1_V15)
2721 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2723 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002724#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726#if defined(MBEDTLS_PKCS1_V21)
2727 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2729 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002730#endif
2731
2732 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002734 }
2735}
2736
2737/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002738 * Copy the components of an RSA key
2739 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002740int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002741{
Janos Follath24eed8d2019-11-22 13:21:35 +00002742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002743
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002744 dst->len = src->len;
2745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2747 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2750 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2751 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002752
2753#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2755 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2756 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2757 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2758 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002759#endif
2760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002762
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2764 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002765
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002766 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002767 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002768
2769cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 if (ret != 0) {
2771 mbedtls_rsa_free(dst);
2772 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002775}
2776
2777/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 * Free the components of an RSA key
2779 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002780void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002781{
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002783 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 mbedtls_mpi_free(&ctx->Vi);
2787 mbedtls_mpi_free(&ctx->Vf);
2788 mbedtls_mpi_free(&ctx->RN);
2789 mbedtls_mpi_free(&ctx->D);
2790 mbedtls_mpi_free(&ctx->Q);
2791 mbedtls_mpi_free(&ctx->P);
2792 mbedtls_mpi_free(&ctx->E);
2793 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002794
Hanno Becker33c30a02017-08-23 07:00:22 +01002795#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 mbedtls_mpi_free(&ctx->RQ);
2797 mbedtls_mpi_free(&ctx->RP);
2798 mbedtls_mpi_free(&ctx->QP);
2799 mbedtls_mpi_free(&ctx->DQ);
2800 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002801#endif /* MBEDTLS_RSA_NO_CRT */
2802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002804 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 if (ctx->ver != 0) {
2806 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002807 ctx->ver = 0;
2808 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002809#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002810}
2811
Hanno Beckerab377312017-08-23 16:24:51 +01002812#endif /* !MBEDTLS_RSA_ALT */
2813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
2817/*
2818 * Example RSA-1024 keypair, for test purposes
2819 */
2820#define KEY_LEN 128
2821
2822#define RSA_N "9292758453063D803DD603D5E777D788" \
2823 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2824 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2825 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2826 "93A89813FBF3C4F8066D2D800F7C38A8" \
2827 "1AE31942917403FF4946B0A83D3D3E05" \
2828 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2829 "5E94BB77B07507233A0BC7BAC8F90F79"
2830
2831#define RSA_E "10001"
2832
2833#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2834 "66CA472BC44D253102F8B4A9D3BFA750" \
2835 "91386C0077937FE33FA3252D28855837" \
2836 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2837 "DF79C5CE07EE72C7F123142198164234" \
2838 "CABB724CF78B8173B9F880FC86322407" \
2839 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2840 "071513A1E85B5DFA031F21ECAE91A34D"
2841
2842#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2843 "2C01CAD19EA484A87EA4377637E75500" \
2844 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2845 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2846
2847#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2848 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2849 "910E4168387E3C30AA1E00C339A79508" \
2850 "8452DD96A9A5EA5D9DCA68DA636032AF"
2851
Paul Bakker5121ce52009-01-03 21:22:43 +00002852#define PT_LEN 24
2853#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2854 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002857static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002858{
gufe44c2620da2020-08-03 17:56:50 +02002859#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002860 size_t i;
2861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002863 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 }
Paul Bakker545570e2010-07-18 09:00:25 +00002865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002867 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002869#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002871 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002875#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002878}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002880
Paul Bakker5121ce52009-01-03 21:22:43 +00002881/*
2882 * Checkup routine
2883 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002884int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002885{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002886 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002888 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002890 unsigned char rsa_plaintext[PT_LEN];
2891 unsigned char rsa_decrypted[PT_LEN];
2892 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002893#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002894 unsigned char sha1sum[20];
2895#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002896
Hanno Becker3a701162017-08-22 13:52:43 +01002897 mbedtls_mpi K;
2898
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 mbedtls_mpi_init(&K);
2900 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2903 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2904 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2905 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2906 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2907 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2908 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2909 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2910 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2911 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 if (verbose != 0) {
2916 mbedtls_printf(" RSA key validation: ");
2917 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2920 mbedtls_rsa_check_privkey(&rsa) != 0) {
2921 if (verbose != 0) {
2922 mbedtls_printf("failed\n");
2923 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
Hanno Becker5bc87292017-05-03 15:09:31 +01002925 ret = 1;
2926 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 }
2928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 if (verbose != 0) {
2930 mbedtls_printf("passed\n PKCS#1 encryption : ");
2931 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2936 PT_LEN, rsa_plaintext,
2937 rsa_ciphertext) != 0) {
2938 if (verbose != 0) {
2939 mbedtls_printf("failed\n");
2940 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002941
Hanno Becker5bc87292017-05-03 15:09:31 +01002942 ret = 1;
2943 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 }
2945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 if (verbose != 0) {
2947 mbedtls_printf("passed\n PKCS#1 decryption : ");
2948 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2951 &len, rsa_ciphertext, rsa_decrypted,
2952 sizeof(rsa_decrypted)) != 0) {
2953 if (verbose != 0) {
2954 mbedtls_printf("failed\n");
2955 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002956
Hanno Becker5bc87292017-05-03 15:09:31 +01002957 ret = 1;
2958 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002959 }
2960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2962 if (verbose != 0) {
2963 mbedtls_printf("failed\n");
2964 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
Hanno Becker5bc87292017-05-03 15:09:31 +01002966 ret = 1;
2967 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002968 }
2969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 if (verbose != 0) {
2971 mbedtls_printf("passed\n");
2972 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002973
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002974#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 if (verbose != 0) {
2976 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002977 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002979 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2980 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 if (verbose != 0) {
2982 mbedtls_printf("failed\n");
2983 }
2984
2985 return 1;
2986 }
2987
2988 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2989 MBEDTLS_MD_SHA1, 20,
2990 sha1sum, rsa_ciphertext) != 0) {
2991 if (verbose != 0) {
2992 mbedtls_printf("failed\n");
2993 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002994
Hanno Becker5bc87292017-05-03 15:09:31 +01002995 ret = 1;
2996 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 }
2998
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 if (verbose != 0) {
3000 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
3001 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
3004 sha1sum, rsa_ciphertext) != 0) {
3005 if (verbose != 0) {
3006 mbedtls_printf("failed\n");
3007 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Hanno Becker5bc87292017-05-03 15:09:31 +01003009 ret = 1;
3010 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003011 }
3012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 if (verbose != 0) {
3014 mbedtls_printf("passed\n");
3015 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003016#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 if (verbose != 0) {
3019 mbedtls_printf("\n");
3020 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003021
Paul Bakker3d8fb632014-04-17 12:42:41 +02003022cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 mbedtls_mpi_free(&K);
3024 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02003026 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003029}
3030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033#endif /* MBEDTLS_RSA_C */