blob: b81039ceb9ec57a697c19615b72740100ec26e64 [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
Valerio Setti44ff9502024-02-01 16:51:05 +010049int mbedtls_rsa_key_parse(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
50{
51 int ret, version;
52 size_t len;
53 unsigned char *p, *end;
54
55 mbedtls_mpi T;
56 mbedtls_mpi_init(&T);
57
58 p = (unsigned char *) key;
59 end = p + keylen;
60
61 /*
62 * This function parses the RSAPrivateKey (PKCS#1)
63 *
64 * RSAPrivateKey ::= SEQUENCE {
65 * version Version,
66 * modulus INTEGER, -- n
67 * publicExponent INTEGER, -- e
68 * privateExponent INTEGER, -- d
69 * prime1 INTEGER, -- p
70 * prime2 INTEGER, -- q
71 * exponent1 INTEGER, -- d mod (p-1)
72 * exponent2 INTEGER, -- d mod (q-1)
73 * coefficient INTEGER, -- (inverse of q) mod p
74 * otherPrimeInfos OtherPrimeInfos OPTIONAL
75 * }
76 */
77 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
78 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
79 return ret;
80 }
81
82 end = p + len;
83
84 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
85 return ret;
86 }
87
88 if (version != 0) {
89 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
90 }
91
92 /* Import N */
93 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
94 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
95 NULL, NULL)) != 0) {
96 goto cleanup;
97 }
98
99 /* Import E */
100 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
101 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
102 NULL, &T)) != 0) {
103 goto cleanup;
104 }
105
106 /* Import D */
107 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
108 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
109 &T, NULL)) != 0) {
110 goto cleanup;
111 }
112
113 /* Import P */
114 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
115 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
116 NULL, NULL)) != 0) {
117 goto cleanup;
118 }
119
120 /* Import Q */
121 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
122 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
123 NULL, NULL)) != 0) {
124 goto cleanup;
125 }
126
127#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
128 /*
129 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
130 * that they can be easily recomputed from D, P and Q. However by
131 * parsing them from the PKCS1 structure it is possible to avoid
132 * recalculating them which both reduces the overhead of loading
133 * RSA private keys into memory and also avoids side channels which
134 * can arise when computing those values, since all of D, P, and Q
135 * are secret. See https://eprint.iacr.org/2020/055 for a
136 * description of one such attack.
137 */
138
139 /* Import DP */
140 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
141 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
142 goto cleanup;
143 }
144
145 /* Import DQ */
146 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
147 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
148 goto cleanup;
149 }
150
151 /* Import QP */
152 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
153 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
154 goto cleanup;
155 }
156
157#else
158 /* Verify existence of the CRT params */
159 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
160 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
161 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
162 goto cleanup;
163 }
164#endif
165
166 /* rsa_complete() doesn't complete anything with the default
167 * implementation but is still called:
168 * - for the benefit of alternative implementation that may want to
169 * pre-compute stuff beyond what's provided (eg Montgomery factors)
170 * - as is also sanity-checks the key
171 *
172 * Furthermore, we also check the public part for consistency with
173 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
174 */
175 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
176 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
177 goto cleanup;
178 }
179
180 if (p != end) {
181 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
182 }
183
184cleanup:
185
186 mbedtls_mpi_free(&T);
187
188 if (ret != 0) {
189 mbedtls_rsa_free(rsa);
190 }
191
192 return ret;
193}
194
195int mbedtls_rsa_pubkey_parse(mbedtls_rsa_context *rsa, unsigned char **p,
196 const unsigned char *end)
197{
198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
199 size_t len;
200
201 /*
202 * RSAPublicKey ::= SEQUENCE {
203 * modulus INTEGER, -- n
204 * publicExponent INTEGER -- e
205 * }
206 */
207
208 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
209 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
210 return ret;
211 }
212
213 if (*p + len != end) {
214 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
215 }
216
217 /* Import N */
218 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
219 return ret;
220 }
221
222 if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0,
223 NULL, 0, NULL, 0)) != 0) {
224 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
225 }
226
227 *p += len;
228
229 /* Import E */
230 if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
231 return ret;
232 }
233
234 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
235 NULL, 0, *p, len)) != 0) {
236 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
237 }
238
239 *p += len;
240
241 if (mbedtls_rsa_complete(rsa) != 0 ||
242 mbedtls_rsa_check_pubkey(rsa) != 0) {
243 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
244 }
245
246 if (*p != end) {
247 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
248 }
249
250 return 0;
251}
252
253int mbedtls_rsa_key_write(const mbedtls_rsa_context *rsa, unsigned char *start,
254 unsigned char **p)
255{
256 size_t len = 0;
257 int ret;
258
259 mbedtls_mpi T; /* Temporary holding the exported parameters */
260
261 /*
262 * Export the parameters one after another to avoid simultaneous copies.
263 */
264
265 mbedtls_mpi_init(&T);
266
267 /* Export QP */
268 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
269 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
270 goto end_of_export;
271 }
272 len += ret;
273
274 /* Export DQ */
275 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
276 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
277 goto end_of_export;
278 }
279 len += ret;
280
281 /* Export DP */
282 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
283 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
284 goto end_of_export;
285 }
286 len += ret;
287
288 /* Export Q */
289 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, &T, NULL, NULL)) != 0 ||
290 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
291 goto end_of_export;
292 }
293 len += ret;
294
295 /* Export P */
296 if ((ret = mbedtls_rsa_export(rsa, NULL, &T, NULL, NULL, NULL)) != 0 ||
297 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
298 goto end_of_export;
299 }
300 len += ret;
301
302 /* Export D */
303 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, &T, NULL)) != 0 ||
304 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
305 goto end_of_export;
306 }
307 len += ret;
308
309 /* Export E */
310 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
311 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
312 goto end_of_export;
313 }
314 len += ret;
315
316 /* Export N */
317 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
318 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
319 goto end_of_export;
320 }
321 len += ret;
322
323end_of_export:
324
325 mbedtls_mpi_free(&T);
326 if (ret < 0) {
327 return ret;
328 }
329
330 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
331 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
332 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
333 MBEDTLS_ASN1_CONSTRUCTED |
334 MBEDTLS_ASN1_SEQUENCE));
335
336 return (int) len;
337}
338
339/*
340 * RSAPublicKey ::= SEQUENCE {
341 * modulus INTEGER, -- n
342 * publicExponent INTEGER -- e
343 * }
344 */
345int mbedtls_rsa_pubkey_write(const mbedtls_rsa_context *rsa, unsigned char *start,
346 unsigned char **p)
347{
348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
349 size_t len = 0;
350 mbedtls_mpi T;
351
352 mbedtls_mpi_init(&T);
353
354 /* Export E */
355 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
356 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
357 goto end_of_export;
358 }
359 len += ret;
360
361 /* Export N */
362 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
363 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
364 goto end_of_export;
365 }
366 len += ret;
367
368end_of_export:
369
370 mbedtls_mpi_free(&T);
371 if (ret < 0) {
372 return ret;
373 }
374
375 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
376 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
377 MBEDTLS_ASN1_SEQUENCE));
378
379 return (int) len;
380}
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100381
382#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
383
384/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
385 * operation (EME-PKCS1-v1_5 decoding).
386 *
387 * \note The return value from this function is a sensitive value
388 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
389 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
390 * is often a situation that an attacker can provoke and leaking which
391 * one is the result is precisely the information the attacker wants.
392 *
393 * \param input The input buffer which is the payload inside PKCS#1v1.5
394 * encryption padding, called the "encoded message EM"
395 * by the terminology.
396 * \param ilen The length of the payload in the \p input buffer.
397 * \param output The buffer for the payload, called "message M" by the
398 * PKCS#1 terminology. This must be a writable buffer of
399 * length \p output_max_len bytes.
400 * \param olen The address at which to store the length of
401 * the payload. This must not be \c NULL.
402 * \param output_max_len The length in bytes of the output buffer \p output.
403 *
404 * \return \c 0 on success.
405 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
406 * The output buffer is too small for the unpadded payload.
407 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
408 * The input doesn't contain properly formatted padding.
409 */
410static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
411 size_t ilen,
412 unsigned char *output,
413 size_t output_max_len,
414 size_t *olen)
415{
416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
417 size_t i, plaintext_max_size;
418
419 /* The following variables take sensitive values: their value must
420 * not leak into the observable behavior of the function other than
421 * the designated outputs (output, olen, return value). Otherwise
422 * this would open the execution of the function to
423 * side-channel-based variants of the Bleichenbacher padding oracle
424 * attack. Potential side channels include overall timing, memory
425 * access patterns (especially visible to an adversary who has access
426 * to a shared memory cache), and branches (especially visible to
427 * an adversary who has access to a shared code cache or to a shared
428 * branch predictor). */
429 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100430 mbedtls_ct_condition_t bad;
431 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100432 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100433 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100434
435 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
436 : output_max_len;
437
438 /* Check and get padding length in constant time and constant
439 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100440 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100441
442
443 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
444 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100445 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100446
447 /* Read the whole buffer. Set pad_done to nonzero if we find
448 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100449 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100450 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100451 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100452 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100453 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100454 }
455
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100456 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100457 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100458
459 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100460 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100461
462 /* If the padding is valid, set plaintext_size to the number of
463 * remaining bytes after stripping the padding. If the padding
464 * is invalid, avoid leaking this fact through the size of the
465 * output: use the maximum message size that fits in the output
466 * buffer. Do it without branches to avoid leaking the padding
467 * validity through timing. RSA keys are small enough that all the
468 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100469 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100470 bad, (unsigned) plaintext_max_size,
471 (unsigned) (ilen - pad_count - 3));
472
473 /* Set output_too_large to 0 if the plaintext fits in the output
474 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100475 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100476 plaintext_max_size);
477
478 /* Set ret without branches to avoid timing attacks. Return:
479 * - INVALID_PADDING if the padding is bad (bad != 0).
480 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
481 * plaintext does not fit in the output buffer.
482 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100483 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100484 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100485 MBEDTLS_ERR_RSA_INVALID_PADDING,
486 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100487 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100488
489 /* If the padding is bad or the plaintext is too large, zero the
490 * data that we're about to copy to the output buffer.
491 * We need to copy the same amount of data
492 * from the same buffer whether the padding is good or not to
493 * avoid leaking the padding validity through overall timing or
494 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100495 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100496
497 /* If the plaintext is too large, truncate it to the buffer size.
498 * Copy anyway to avoid revealing the length through timing, because
499 * revealing the length is as bad as revealing the padding validity
500 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100501 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100502 (unsigned) plaintext_max_size,
503 (unsigned) plaintext_size);
504
505 /* Move the plaintext to the leftmost position where it can start in
506 * the working buffer, i.e. make it start plaintext_max_size from
507 * the end of the buffer. Do this with a memory access trace that
508 * does not depend on the plaintext size. After this move, the
509 * starting location of the plaintext is no longer sensitive
510 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100511 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
512 plaintext_max_size,
513 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100514
515 /* Finally copy the decrypted plaintext plus trailing zeros into the output
516 * buffer. If output_max_len is 0, then output may be an invalid pointer
517 * and the result of memcpy() would be undefined; prevent undefined
518 * behavior making sure to depend only on output_max_len (the size of the
519 * user-provided output buffer), which is independent from plaintext
520 * length, validity of padding, success of the decryption, and other
521 * secrets. */
522 if (output_max_len != 0) {
523 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
524 }
525
526 /* Report the amount of data we copied to the output buffer. In case
527 * of errors (bad padding or output too large), the value of *olen
528 * when this function returns is not specified. Making it equivalent
529 * to the good case limits the risks of leaking the padding validity. */
530 *olen = plaintext_size;
531
532 return ret;
533}
534
535#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
536
Hanno Beckera565f542017-10-11 11:00:19 +0100537#if !defined(MBEDTLS_RSA_ALT)
538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
540 const mbedtls_mpi *N,
541 const mbedtls_mpi *P, const mbedtls_mpi *Q,
542 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100543{
Janos Follath24eed8d2019-11-22 13:21:35 +0000544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
547 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
548 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
549 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
550 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
551 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100552 }
553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (N != NULL) {
555 ctx->len = mbedtls_mpi_size(&ctx->N);
556 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100559}
560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
562 unsigned char const *N, size_t N_len,
563 unsigned char const *P, size_t P_len,
564 unsigned char const *Q, size_t Q_len,
565 unsigned char const *D, size_t D_len,
566 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100567{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000568 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (N != NULL) {
571 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
572 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100573 }
574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (P != NULL) {
576 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
577 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if (Q != NULL) {
580 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
581 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (D != NULL) {
584 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
585 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (E != NULL) {
588 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
589 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100590
591cleanup:
592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (ret != 0) {
594 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
595 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100598}
599
Hanno Becker705fc682017-10-10 17:57:02 +0100600/*
601 * Checks whether the context fields are set in such a way
602 * that the RSA primitives will be able to execute without error.
603 * It does *not* make guarantees for consistency of the parameters.
604 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100605static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
606 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100607{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100608#if !defined(MBEDTLS_RSA_NO_CRT)
609 /* blinding_needed is only used for NO_CRT to decide whether
610 * P,Q need to be present or not. */
611 ((void) blinding_needed);
612#endif
613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
615 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
616 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000617 }
Hanno Becker705fc682017-10-10 17:57:02 +0100618
619 /*
620 * 1. Modular exponentiation needs positive, odd moduli.
621 */
622
623 /* Modular exponentiation wrt. N is always used for
624 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
626 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
627 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100628 }
629
630#if !defined(MBEDTLS_RSA_NO_CRT)
631 /* Modular exponentiation for P and Q is only
632 * used for private key operations and if CRT
633 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (is_priv &&
635 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
636 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
637 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
638 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
639 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100640 }
641#endif /* !MBEDTLS_RSA_NO_CRT */
642
643 /*
644 * 2. Exponents must be positive
645 */
646
647 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
649 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
650 }
Hanno Becker705fc682017-10-10 17:57:02 +0100651
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100652#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100653 /* For private key operations, use D or DP & DQ
654 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
656 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
657 }
Hanno Becker705fc682017-10-10 17:57:02 +0100658#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (is_priv &&
660 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
661 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
662 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100663 }
664#endif /* MBEDTLS_RSA_NO_CRT */
665
666 /* Blinding shouldn't make exponents negative either,
667 * so check that P, Q >= 1 if that hasn't yet been
668 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100669#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (is_priv && blinding_needed &&
671 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
672 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
673 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100674 }
675#endif
676
677 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100678 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100679#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (is_priv &&
681 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
682 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100683 }
684#endif
685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100687}
688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100690{
691 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500692 int have_N, have_P, have_Q, have_D, have_E;
693#if !defined(MBEDTLS_RSA_NO_CRT)
694 int have_DP, have_DQ, have_QP;
695#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500696 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
699 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
700 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
701 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
702 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500703
704#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
706 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
707 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500708#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100709
Hanno Becker617c1ae2017-08-23 14:11:24 +0100710 /*
711 * Check whether provided parameters are enough
712 * to deduce all others. The following incomplete
713 * parameter sets for private keys are supported:
714 *
715 * (1) P, Q missing.
716 * (2) D and potentially N missing.
717 *
718 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100719
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500720 n_missing = have_P && have_Q && have_D && have_E;
721 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
722 d_missing = have_P && have_Q && !have_D && have_E;
723 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100724
725 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500726 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (!is_priv && !is_pub) {
729 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
730 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100731
732 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100733 * Step 1: Deduce N if P, Q are provided.
734 */
735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (!have_N && have_P && have_Q) {
737 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
738 &ctx->Q)) != 0) {
739 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100740 }
741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100743 }
744
745 /*
746 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100747 */
748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (pq_missing) {
750 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
751 &ctx->P, &ctx->Q);
752 if (ret != 0) {
753 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
754 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 } else if (d_missing) {
757 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
758 &ctx->Q,
759 &ctx->E,
760 &ctx->D)) != 0) {
761 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100762 }
763 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100764
Hanno Becker617c1ae2017-08-23 14:11:24 +0100765 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100766 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100767 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100768 */
769
Hanno Becker23344b52017-08-23 07:43:27 +0100770#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (is_priv && !(have_DP && have_DQ && have_QP)) {
772 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
773 &ctx->DP, &ctx->DQ, &ctx->QP);
774 if (ret != 0) {
775 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
776 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100777 }
Hanno Becker23344b52017-08-23 07:43:27 +0100778#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100779
780 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100781 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100782 */
783
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100785}
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
788 unsigned char *N, size_t N_len,
789 unsigned char *P, size_t P_len,
790 unsigned char *Q, size_t Q_len,
791 unsigned char *D, size_t D_len,
792 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100793{
794 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500795 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100796
797 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500798 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
800 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
801 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
802 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
803 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100806 /* If we're trying to export private parameters for a public key,
807 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (P != NULL || Q != NULL || D != NULL) {
809 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
810 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100811
812 }
813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (N != NULL) {
815 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
816 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (P != NULL) {
819 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
820 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (Q != NULL) {
823 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
824 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (D != NULL) {
827 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
828 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (E != NULL) {
831 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
832 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100833
834cleanup:
835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100837}
838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
840 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
841 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100842{
Janos Follath24eed8d2019-11-22 13:21:35 +0000843 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500844 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100845
846 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500847 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
849 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
850 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
851 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
852 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100855 /* If we're trying to export private parameters for a public key,
856 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (P != NULL || Q != NULL || D != NULL) {
858 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
859 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100860
861 }
862
863 /* Export all requested core parameters. */
864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
866 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
867 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
868 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
869 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
870 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100871 }
872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100874}
875
876/*
877 * Export CRT parameters
878 * This must also be implemented if CRT is not used, for being able to
879 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
880 * can be used in this case.
881 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
883 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100884{
Janos Follath24eed8d2019-11-22 13:21:35 +0000885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500886 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100887
888 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500889 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
891 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
892 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
893 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
894 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (!is_priv) {
897 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
898 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100899
Hanno Beckerdc95c892017-08-23 06:57:02 +0100900#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100901 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
903 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
904 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
905 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100906 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100907#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
909 DP, DQ, QP)) != 0) {
910 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100911 }
912#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100915}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100916
Paul Bakker5121ce52009-01-03 21:22:43 +0000917/*
918 * Initialize an RSA context
919 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000921{
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000923
Ronald Cronc1905a12021-06-05 11:11:14 +0200924 ctx->padding = MBEDTLS_RSA_PKCS_V15;
925 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100928 /* Set ctx->ver to nonzero to indicate that the mutex has been
929 * initialized and will need to be freed. */
930 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200932#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000933}
934
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100935/*
936 * Set padding for an existing RSA context
937 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
939 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100940{
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200942#if defined(MBEDTLS_PKCS1_V15)
943 case MBEDTLS_RSA_PKCS_V15:
944 break;
945#endif
946
947#if defined(MBEDTLS_PKCS1_V21)
948 case MBEDTLS_RSA_PKCS_V21:
949 break;
950#endif
951 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200953 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200954
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200955#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
957 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200958 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200959 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 return MBEDTLS_ERR_RSA_INVALID_PADDING;
961 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200962 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200963#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500964
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100965 ctx->padding = padding;
966 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100969}
970
Hanno Becker617c1ae2017-08-23 14:11:24 +0100971/*
Yanray Wang83548b52023-03-15 16:46:34 +0800972 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +0800973 */
974int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
975{
Yanray Wang644b9012023-03-15 16:50:31 +0800976 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +0800977}
978
979/*
Yanray Wang12cb3962023-03-01 10:20:02 +0800980 * Get hash identifier of mbedtls_md_type_t type
981 */
Yanray Wangd41684e2023-03-17 18:54:22 +0800982int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +0800983{
Yanray Wang644b9012023-03-15 16:50:31 +0800984 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +0800985}
986
987/*
Hanno Becker617c1ae2017-08-23 14:11:24 +0100988 * Get length in bytes of RSA modulus
989 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100991{
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100993}
994
Valerio Settib328c442024-01-23 10:48:45 +0100995/*
996 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
997 *
998 * The value zero is:
999 * - never a valid value for an RSA parameter
1000 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
1001 *
1002 * Since values can't be omitted in PKCS#1, passing a zero value to
1003 * rsa_complete() would be incorrect, so reject zero values early.
1004 */
1005static int asn1_get_nonzero_mpi(unsigned char **p,
1006 const unsigned char *end,
1007 mbedtls_mpi *X)
1008{
1009 int ret;
1010
1011 ret = mbedtls_asn1_get_mpi(p, end, X);
1012 if (ret != 0) {
1013 return ret;
1014 }
1015
1016 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
Valerio Settidccfd362024-01-23 17:07:59 +01001017 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Valerio Settib328c442024-01-23 10:48:45 +01001018 }
1019
1020 return 0;
1021}
1022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
1025/*
1026 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001027 *
1028 * This generation method follows the RSA key pair generation procedure of
1029 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001031int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
1032 int (*f_rng)(void *, unsigned char *, size_t),
1033 void *p_rng,
1034 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +00001035{
Janos Follath24eed8d2019-11-22 13:21:35 +00001036 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -08001037 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +01001038 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001039
Janos Follathb8fc1b02018-09-03 15:37:01 +01001040 /*
1041 * If the modulus is 1024 bit long or shorter, then the security strength of
1042 * the RSA algorithm is less than or equal to 80 bits and therefore an error
1043 * rate of 2^-80 is sufficient.
1044 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +01001046 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 }
Janos Follathb8fc1b02018-09-03 15:37:01 +01001048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 mbedtls_mpi_init(&H);
1050 mbedtls_mpi_init(&G);
1051 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001053 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +01001054 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1055 goto cleanup;
1056 }
1057
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001058 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1060 goto cleanup;
1061 }
1062
1063 /*
1064 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001065 * 1. |P-Q| > 2^( nbits / 2 - 100 )
1066 * 2. GCD( E, (P-1)*(Q-1) ) == 1
1067 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 do {
1072 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
1073 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
1076 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001078 /* 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 +01001079 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
1080 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001083
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001084 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if (H.s < 0) {
1086 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
1087 }
Janos Follathef441782016-09-21 13:18:12 +01001088
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001089 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
1091 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
1092 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001093
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001094 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
1096 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -08001097 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001099
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001100 /* 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 +01001101 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
1102 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
1103 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 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 -08001106 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001108
1109 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001112 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
1114 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001119
Jethro Beekman97f95c92018-02-13 15:50:36 -08001120#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001122 * DP = D mod (P - 1)
1123 * DQ = D mod (Q - 1)
1124 * QP = Q^-1 mod P
1125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
1127 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001128#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001129
Hanno Becker83aad1f2017-08-23 06:45:10 +01001130 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
1133cleanup:
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 mbedtls_mpi_free(&H);
1136 mbedtls_mpi_free(&G);
1137 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 if (ret != 0) {
1140 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +01001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if ((-ret & ~0x7f) == 0) {
1143 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
1144 }
1145 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 }
1147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001149}
1150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
1153/*
1154 * Check a public RSA key
1155 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001157{
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
1159 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001160 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
1163 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
1167 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
1168 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
1169 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1170 }
1171
1172 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001173}
1174
1175/*
Hanno Becker705fc682017-10-10 17:57:02 +01001176 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001178int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001179{
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
1181 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
1182 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 }
Paul Bakker48377d92013-08-30 12:06:24 +02001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
1186 &ctx->D, &ctx->E, NULL, NULL) != 0) {
1187 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001189
Hanno Beckerb269a852017-08-25 08:03:21 +01001190#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
1192 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
1193 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +01001194 }
1195#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001198}
1199
1200/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001201 * Check if contexts holding a public and private key match
1202 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001203int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
1204 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001205{
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
1207 mbedtls_rsa_check_privkey(prv) != 0) {
1208 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001209 }
1210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
1212 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
1213 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001214 }
1215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001217}
1218
1219/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 * Do an RSA public key operation
1221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
1223 const unsigned char *input,
1224 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001225{
Janos Follath24eed8d2019-11-22 13:21:35 +00001226 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001227 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
1231 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1232 }
Hanno Becker705fc682017-10-10 17:57:02 +01001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001235
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001236#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1238 return ret;
1239 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001240#endif
1241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001245 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1246 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 }
1248
1249 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
1251 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
1253cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1256 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1257 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001258#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (ret != 0) {
1263 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
1264 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001267}
1268
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001269/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001270 * Generate or update blinding values, see section 10 of:
1271 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001272 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001273 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001274 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001275static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1276 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001277{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001278 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001279 mbedtls_mpi R;
1280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001284 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1286 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1287 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1288 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001289
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001290 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001291 }
1292
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001293 /* Unblinding value: Vf = random number, invertible mod N */
1294 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +02001296 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
1297 goto cleanup;
1298 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 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 +02001301
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001302 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
1304 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
1305 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001306
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001307 /* At this point, Vi is invertible mod N if and only if both Vf and R
1308 * are invertible mod N. If one of them isn't, we don't need to know
1309 * which one, we just loop and choose new values for both of them.
1310 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
1312 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001313 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -05001317
1318 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
1320 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001321
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001322 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001323 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 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 +02001325
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001326
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001327cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001331}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001332
Paul Bakker5121ce52009-01-03 21:22:43 +00001333/*
Janos Follathe81102e2017-03-22 13:38:28 +00001334 * Exponent blinding supposed to prevent side-channel attacks using multiple
1335 * traces of measurements to recover the RSA key. The more collisions are there,
1336 * the more bits of the key can be recovered. See [3].
1337 *
1338 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001339 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001340 *
1341 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001342 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001343 *
1344 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1345 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1346 * Thus in this sense with 28 byte blinding the security is not reduced by
1347 * side-channel attacks like the one in [3])
1348 *
1349 * This countermeasure does not help if the key recovery is possible with a
1350 * single trace.
1351 */
1352#define RSA_EXPONENT_BLINDING 28
1353
1354/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 * Do an RSA private key operation
1356 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1358 int (*f_rng)(void *, unsigned char *, size_t),
1359 void *p_rng,
1360 const unsigned char *input,
1361 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001362{
Janos Follath24eed8d2019-11-22 13:21:35 +00001363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001364 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001365
1366 /* Temporary holding the result */
1367 mbedtls_mpi T;
1368
1369 /* Temporaries holding P-1, Q-1 and the
1370 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001371 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001372
1373#if !defined(MBEDTLS_RSA_NO_CRT)
1374 /* Temporaries holding the results mod p resp. mod q. */
1375 mbedtls_mpi TP, TQ;
1376
1377 /* Temporaries holding the blinded exponents for
1378 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001379 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001380
1381 /* Pointers to actual exponents to be used - either the unblinded
1382 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +00001383 mbedtls_mpi *DP = &ctx->DP;
1384 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +01001385#else
1386 /* Temporary holding the blinded exponent (if used). */
1387 mbedtls_mpi D_blind;
1388
1389 /* Pointer to actual exponent to be used - either the unblinded
1390 * or the blinded one, depending on the presence of a PRNG. */
1391 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +01001392#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001393
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001394 /* Temporaries holding the initial input and the double
1395 * checked result; should be the same in the end. */
1396 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if (f_rng == NULL) {
1399 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1400 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 if (rsa_check_context(ctx, 1 /* private key checks */,
1403 1 /* blinding on */) != 0) {
1404 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001405 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001406
Hanno Becker06811ce2017-05-03 15:10:34 +01001407#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1409 return ret;
1410 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001411#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001412
Hanno Becker06811ce2017-05-03 15:10:34 +01001413 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001415
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 mbedtls_mpi_init(&P1);
1417 mbedtls_mpi_init(&Q1);
1418 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001419
Janos Follathe81102e2017-03-22 13:38:28 +00001420#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001422#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 mbedtls_mpi_init(&DP_blind);
1424 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001425#endif
1426
Hanno Becker06811ce2017-05-03 15:10:34 +01001427#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001429#endif
1430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 mbedtls_mpi_init(&I);
1432 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001433
1434 /* End of MPI initialization */
1435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1437 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001438 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1439 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 }
1441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +01001443
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001444 /*
1445 * Blinding
1446 * T = T * Vi mod N
1447 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1449 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1450 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001451
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001452 /*
1453 * Exponent blinding
1454 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1456 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001457
Janos Follathf9203b42017-03-22 15:13:15 +00001458#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001459 /*
1460 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1461 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1463 f_rng, p_rng));
1464 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1465 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1466 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +00001467
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001468 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001469#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001470 /*
1471 * DP_blind = ( P - 1 ) * R + DP
1472 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1474 f_rng, p_rng));
1475 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1476 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1477 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001478
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001479 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001480
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001481 /*
1482 * DQ_blind = ( Q - 1 ) * R + DQ
1483 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1485 f_rng, p_rng));
1486 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1487 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1488 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001489
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001490 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001491#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001495#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001496 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001497 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001499 * TP = input ^ dP mod P
1500 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1504 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001505
1506 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001507 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1510 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1511 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001512
1513 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001514 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001515 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1517 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001519
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001520 /*
1521 * Unblind
1522 * T = T * Vf mod N
1523 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1525 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001526
Hanno Becker2dec5e82017-10-03 07:49:52 +01001527 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1529 &ctx->N, &ctx->RN));
1530 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001531 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1532 goto cleanup;
1533 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001534
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
1538cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1541 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1542 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001543#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 mbedtls_mpi_free(&P1);
1546 mbedtls_mpi_free(&Q1);
1547 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001548
Janos Follathe81102e2017-03-22 13:38:28 +00001549#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001551#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 mbedtls_mpi_free(&DP_blind);
1553 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001554#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001557
1558#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001560#endif
1561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 mbedtls_mpi_free(&C);
1563 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (ret != 0 && ret >= -0x007f) {
1566 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1567 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001570}
1571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001573/**
1574 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1575 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001576 * \param dst buffer to mask
1577 * \param dlen length of destination buffer
1578 * \param src source of the mask generation
1579 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001580 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001581 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001582static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1583 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001584{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001585 unsigned char counter[4];
1586 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001587 unsigned int hlen;
1588 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001589 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001590 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001591 const mbedtls_md_info_t *md_info;
1592 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 mbedtls_md_init(&md_ctx);
1595 md_info = mbedtls_md_info_from_type(md_alg);
1596 if (md_info == NULL) {
1597 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1598 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 mbedtls_md_init(&md_ctx);
1601 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001602 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 memset(mask, 0, sizeof(mask));
1608 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001609
Simon Butcher02037452016-03-01 21:19:12 +00001610 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001611 p = dst;
1612
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001614 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001616 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001620 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 }
1622 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 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, counter, 4)) != 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_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001633 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001635
1636 counter[3]++;
1637
1638 dlen -= use_len;
1639 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001640
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001641exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001646}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001647
1648/**
1649 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1650 *
1651 * \param hash the input hash
1652 * \param hlen length of the input hash
1653 * \param salt the input salt
1654 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001655 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001656 * \param md_alg message digest to use
1657 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001658static int hash_mprime(const unsigned char *hash, size_t hlen,
1659 const unsigned char *salt, size_t slen,
1660 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001661{
1662 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001663
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001664 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001665 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1668 if (md_info == NULL) {
1669 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1670 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 mbedtls_md_init(&md_ctx);
1673 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001674 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 }
1676 if ((ret = mbedtls_md_starts(&md_ctx)) != 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_update(&md_ctx, zeros, sizeof(zeros))) != 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, hash, hlen)) != 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, salt, slen)) != 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_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001689 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001691
1692exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001696}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001697
1698/**
1699 * Compute a hash.
1700 *
1701 * \param md_alg algorithm to use
1702 * \param input input message to hash
1703 * \param ilen input length
1704 * \param output the output buffer - must be large enough for \p md_alg
1705 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001706static int compute_hash(mbedtls_md_type_t md_alg,
1707 const unsigned char *input, size_t ilen,
1708 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001709{
1710 const mbedtls_md_info_t *md_info;
1711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 md_info = mbedtls_md_info_from_type(md_alg);
1713 if (md_info == NULL) {
1714 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1715 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001718}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001722/*
1723 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1724 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001725int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1726 int (*f_rng)(void *, unsigned char *, size_t),
1727 void *p_rng,
1728 const unsigned char *label, size_t label_len,
1729 size_t ilen,
1730 const unsigned char *input,
1731 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001732{
1733 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001735 unsigned char *p = output;
1736 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 if (f_rng == NULL) {
1739 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1740 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001741
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001742 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (hlen == 0) {
1744 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1745 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001746
1747 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001748
Simon Butcher02037452016-03-01 21:19:12 +00001749 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1751 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1752 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001753
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001755
1756 *p++ = 0;
1757
Simon Butcher02037452016-03-01 21:19:12 +00001758 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1760 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1761 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001762
1763 p += hlen;
1764
Simon Butcher02037452016-03-01 21:19:12 +00001765 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1767 if (ret != 0) {
1768 return ret;
1769 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001770 p += hlen;
1771 p += olen - 2 * hlen - 2 - ilen;
1772 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (ilen != 0) {
1774 memcpy(p, input, ilen);
1775 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001776
Simon Butcher02037452016-03-01 21:19:12 +00001777 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001779 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 return ret;
1781 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001782
Simon Butcher02037452016-03-01 21:19:12 +00001783 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001785 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 return ret;
1787 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001790}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001794/*
1795 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1796 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001797int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1798 int (*f_rng)(void *, unsigned char *, size_t),
1799 void *p_rng, size_t ilen,
1800 const unsigned char *input,
1801 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001802{
1803 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001805 unsigned char *p = output;
1806
Paul Bakkerb3869132013-02-28 17:21:01 +01001807 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001808
Simon Butcher02037452016-03-01 21:19:12 +00001809 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 if (ilen + 11 < ilen || olen < ilen + 11) {
1811 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1812 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001813
1814 nb_pad = olen - 3 - ilen;
1815
1816 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 if (f_rng == NULL) {
1819 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1820 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001821
1822 *p++ = MBEDTLS_RSA_CRYPT;
1823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001825 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001826
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001827 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 ret = f_rng(p_rng, p, 1);
1829 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001830
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001831 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 if (rng_dl == 0 || ret != 0) {
1833 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1834 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001835
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001836 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001837 }
1838
1839 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if (ilen != 0) {
1841 memcpy(p, input, ilen);
1842 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001845}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001847
Paul Bakker5121ce52009-01-03 21:22:43 +00001848/*
1849 * Add the message padding, then do an RSA operation
1850 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001851int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1852 int (*f_rng)(void *, unsigned char *, size_t),
1853 void *p_rng,
1854 size_t ilen,
1855 const unsigned char *input,
1856 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001857{
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859#if defined(MBEDTLS_PKCS1_V15)
1860 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1862 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001863#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865#if defined(MBEDTLS_PKCS1_V21)
1866 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1868 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001869#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
1871 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001873 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001874}
1875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001877/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001878 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001880int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1881 int (*f_rng)(void *, unsigned char *, size_t),
1882 void *p_rng,
1883 const unsigned char *label, size_t label_len,
1884 size_t *olen,
1885 const unsigned char *input,
1886 unsigned char *output,
1887 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001888{
Janos Follath24eed8d2019-11-22 13:21:35 +00001889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001890 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001891 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001892 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001894 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001895 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001896
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001897 /*
1898 * Parameters sanity checks
1899 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1901 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1902 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001903
1904 ilen = ctx->len;
1905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 if (ilen < 16 || ilen > sizeof(buf)) {
1907 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1908 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001909
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001910 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 if (hlen == 0) {
1912 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1913 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001914
Janos Follathc17cda12016-02-11 11:08:18 +00001915 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 if (2 * hlen + 2 > ilen) {
1917 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1918 }
Janos Follathc17cda12016-02-11 11:08:18 +00001919
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001920 /*
1921 * RSA operation
1922 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001926 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001929 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001930 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001931 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001932 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001934 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 /* DB: Apply dbMask to maskedDB */
1936 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001937 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001938 goto cleanup;
1939 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001940
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001941 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1943 label, label_len, lhash);
1944 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001945 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001947
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001948 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001949 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001950 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 p = buf;
1952
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001953 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001954
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001955 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001956
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001957 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001958 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001959 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001960
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001961 /* Get zero-padding len, but always read till end of buffer
1962 * (minus one, for the 01 byte) */
1963 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001964 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001966 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1967 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001968 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001969
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001970 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001971 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001972
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001973 /*
1974 * The only information "leaked" is whether the padding was correct or not
1975 * (eg, no data is copied if it was not correct). This meets the
1976 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1977 * the different error conditions.
1978 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001979 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001980 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1981 goto cleanup;
1982 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001983
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001984 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001985 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1986 goto cleanup;
1987 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001988
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001989 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 if (*olen != 0) {
1991 memcpy(output, p, *olen);
1992 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001993 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001994
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001995cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 mbedtls_platform_zeroize(buf, sizeof(buf));
1997 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001998
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002000}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002004/*
2005 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
2006 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002007int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
2008 int (*f_rng)(void *, unsigned char *, size_t),
2009 void *p_rng,
2010 size_t *olen,
2011 const unsigned char *input,
2012 unsigned char *output,
2013 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002014{
2015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2016 size_t ilen;
2017 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
2018
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002019 ilen = ctx->len;
2020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2022 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2023 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 if (ilen < 16 || ilen > sizeof(buf)) {
2026 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2027 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002032 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
2036 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002037
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002038cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002042}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002044
2045/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002046 * Do an RSA operation, then remove the message padding
2047 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002048int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
2049 int (*f_rng)(void *, unsigned char *, size_t),
2050 void *p_rng,
2051 size_t *olen,
2052 const unsigned char *input,
2053 unsigned char *output,
2054 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01002055{
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057#if defined(MBEDTLS_PKCS1_V15)
2058 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
2060 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02002061#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063#if defined(MBEDTLS_PKCS1_V21)
2064 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
2066 olen, input, output,
2067 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01002068#endif
2069
2070 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002072 }
2073}
2074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002076static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2077 int (*f_rng)(void *, unsigned char *, size_t),
2078 void *p_rng,
2079 mbedtls_md_type_t md_alg,
2080 unsigned int hashlen,
2081 const unsigned char *hash,
2082 int saltlen,
2083 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002084{
2085 size_t olen;
2086 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002087 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002088 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00002089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002090 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002091 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002092
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002094 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 if (f_rng == NULL) {
2098 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2099 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002100
2101 olen = ctx->len;
2102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002104 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002105 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 if (exp_hashlen == 0) {
2107 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2108 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 if (hashlen != exp_hashlen) {
2111 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2112 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002113 }
2114
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002115 hash_id = (mbedtls_md_type_t) ctx->hash_id;
2116 if (hash_id == MBEDTLS_MD_NONE) {
2117 hash_id = md_alg;
2118 }
2119 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 if (hlen == 0) {
2121 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2122 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
2125 /* Calculate the largest possible salt length, up to the hash size.
2126 * Normally this is the hash length, which is the maximum salt length
2127 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
2128 * enough room, use the maximum salt length that fits. The constraint is
2129 * that the hash length plus the salt length plus 2 bytes must be at most
2130 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
2131 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002132 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (olen < hlen + min_slen + 2) {
2134 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2135 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002136 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002138 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 }
2140 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
2141 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2142 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02002143 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002144 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01002147
Simon Butcher02037452016-03-01 21:19:12 +00002148 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002150 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01002151 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002152
2153 /* Generate salt of length slen in place in the encoded message */
2154 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
2156 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
2157 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02002158
Paul Bakkerb3869132013-02-28 17:21:01 +01002159 p += slen;
2160
Simon Butcher02037452016-03-01 21:19:12 +00002161 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002162 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (ret != 0) {
2164 return ret;
2165 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002166
Simon Butcher02037452016-03-01 21:19:12 +00002167 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002169 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002171
Simon Butcher02037452016-03-01 21:19:12 +00002172 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002173 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 if (ret != 0) {
2175 return ret;
2176 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2179 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01002180
2181 p += hlen;
2182 *p++ = 0xBC;
2183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002185}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002186
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002187static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2188 int (*f_rng)(void *, unsigned char *, size_t),
2189 void *p_rng,
2190 mbedtls_md_type_t md_alg,
2191 unsigned int hashlen,
2192 const unsigned char *hash,
2193 int saltlen,
2194 unsigned char *sig)
2195{
2196 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2197 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2198 }
2199 if (ctx->hash_id == MBEDTLS_MD_NONE) {
2200 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2201 }
2202 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
2203 sig);
2204}
2205
2206int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2207 int (*f_rng)(void *, unsigned char *, size_t),
2208 void *p_rng,
2209 mbedtls_md_type_t md_alg,
2210 unsigned int hashlen,
2211 const unsigned char *hash,
2212 unsigned char *sig)
2213{
2214 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
2215 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2216}
2217
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002218/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02002219 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
2220 * the option to pass in the salt length.
2221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002222int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
2223 int (*f_rng)(void *, unsigned char *, size_t),
2224 void *p_rng,
2225 mbedtls_md_type_t md_alg,
2226 unsigned int hashlen,
2227 const unsigned char *hash,
2228 int saltlen,
2229 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02002230{
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2232 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02002233}
2234
Cédric Meuterf3fab332020-04-25 11:30:45 +02002235/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002236 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
2237 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002238int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2239 int (*f_rng)(void *, unsigned char *, size_t),
2240 void *p_rng,
2241 mbedtls_md_type_t md_alg,
2242 unsigned int hashlen,
2243 const unsigned char *hash,
2244 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002245{
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2247 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002252/*
2253 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
2254 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002255
2256/* Construct a PKCS v1.5 encoding of a hashed message
2257 *
2258 * This is used both for signature generation and verification.
2259 *
2260 * Parameters:
2261 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002262 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002263 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002264 * - hash: Buffer containing the hashed message or the raw data.
2265 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002266 * - dst: Buffer to hold the encoded message.
2267 *
2268 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002269 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002270 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002271 *
2272 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002273static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
2274 unsigned int hashlen,
2275 const unsigned char *hash,
2276 size_t dst_len,
2277 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01002278{
2279 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002280 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002281 unsigned char *p = dst;
2282 const char *oid = NULL;
2283
2284 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002286 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 if (md_size == 0) {
2288 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2289 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2292 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2293 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 if (hashlen != md_size) {
2296 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2297 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002298
2299 /* Double-check that 8 + hashlen + oid_size can be used as a
2300 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01002302 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 10 + hashlen + oid_size < 10 + hashlen) {
2304 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2305 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002306
2307 /*
2308 * Static bounds check:
2309 * - Need 10 bytes for five tag-length pairs.
2310 * (Insist on 1-byte length encodings to protect against variants of
2311 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2312 * - Need hashlen bytes for hash
2313 * - Need oid_size bytes for hash alg OID.
2314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (nb_pad < 10 + hashlen + oid_size) {
2316 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2317 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002318 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 } else {
2320 if (nb_pad < hashlen) {
2321 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2322 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002323
2324 nb_pad -= hashlen;
2325 }
2326
Hanno Becker2b2f8982017-09-27 17:10:03 +01002327 /* Need space for signature header and padding delimiter (3 bytes),
2328 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if (nb_pad < 3 + 8) {
2330 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2331 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002332 nb_pad -= 3;
2333
2334 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002335 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002336
2337 /* Write signature header and padding */
2338 *p++ = 0;
2339 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002341 p += nb_pad;
2342 *p++ = 0;
2343
2344 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 if (md_alg == MBEDTLS_MD_NONE) {
2346 memcpy(p, hash, hashlen);
2347 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002348 }
2349
2350 /* Signing hashed data, add corresponding ASN.1 structure
2351 *
2352 * DigestInfo ::= SEQUENCE {
2353 * digestAlgorithm DigestAlgorithmIdentifier,
2354 * digest Digest }
2355 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2356 * Digest ::= OCTET STRING
2357 *
2358 * Schematic:
2359 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2360 * TAG-NULL + LEN [ NULL ] ]
2361 * TAG-OCTET + LEN [ HASH ] ]
2362 */
2363 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002365 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002367 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002368 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002370 p += oid_size;
2371 *p++ = MBEDTLS_ASN1_NULL;
2372 *p++ = 0x00;
2373 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002374 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002376 p += hashlen;
2377
2378 /* Just a sanity-check, should be automatic
2379 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 if (p != dst + dst_len) {
2381 mbedtls_platform_zeroize(dst, dst_len);
2382 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002383 }
2384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002386}
2387
Paul Bakkerb3869132013-02-28 17:21:01 +01002388/*
2389 * Do an RSA operation to sign the message digest
2390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002391int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2392 int (*f_rng)(void *, unsigned char *, size_t),
2393 void *p_rng,
2394 mbedtls_md_type_t md_alg,
2395 unsigned int hashlen,
2396 const unsigned char *hash,
2397 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002398{
Janos Follath24eed8d2019-11-22 13:21:35 +00002399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002400 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002403 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2407 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2408 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002409
Hanno Beckerfdf38032017-09-06 12:35:55 +01002410 /*
2411 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2412 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2415 ctx->len, sig)) != 0) {
2416 return ret;
2417 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002418
Hanno Beckerfdf38032017-09-06 12:35:55 +01002419 /* Private key operation
2420 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002421 * In order to prevent Lenstra's attack, make the signature in a
2422 * temporary buffer and check it before returning it.
2423 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 sig_try = mbedtls_calloc(1, ctx->len);
2426 if (sig_try == NULL) {
2427 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002428 }
2429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 verif = mbedtls_calloc(1, ctx->len);
2431 if (verif == NULL) {
2432 mbedtls_free(sig_try);
2433 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2434 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2437 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2438
2439 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002440 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2441 goto cleanup;
2442 }
2443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002445
2446cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002447 mbedtls_zeroize_and_free(sig_try, ctx->len);
2448 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (ret != 0) {
2451 memset(sig, '!', ctx->len);
2452 }
2453 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002456
2457/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 * Do an RSA operation to sign the message digest
2459 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002460int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2461 int (*f_rng)(void *, unsigned char *, size_t),
2462 void *p_rng,
2463 mbedtls_md_type_t md_alg,
2464 unsigned int hashlen,
2465 const unsigned char *hash,
2466 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002467{
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002469 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473#if defined(MBEDTLS_PKCS1_V15)
2474 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2476 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002477#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#if defined(MBEDTLS_PKCS1_V21)
2480 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2482 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002483#endif
2484
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002488}
2489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002491/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002492 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2495 mbedtls_md_type_t md_alg,
2496 unsigned int hashlen,
2497 const unsigned char *hash,
2498 mbedtls_md_type_t mgf1_hash_id,
2499 int expected_salt_len,
2500 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002501{
Janos Follath24eed8d2019-11-22 13:21:35 +00002502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002503 size_t siglen;
2504 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002505 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002506 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002507 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002508 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002512 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002514
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 siglen = ctx->len;
2516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 if (siglen < 16 || siglen > sizeof(buf)) {
2518 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2519 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (ret != 0) {
2524 return ret;
2525 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
2527 p = buf;
2528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (buf[siglen - 1] != 0xBC) {
2530 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002531 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 if (md_alg != MBEDTLS_MD_NONE) {
2534 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002535 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (exp_hashlen == 0) {
2537 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2538 }
2539
2540 if (hashlen != exp_hashlen) {
2541 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2542 }
2543 }
2544
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002545 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (hlen == 0) {
2547 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2548 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002549
Simon Butcher02037452016-03-01 21:19:12 +00002550 /*
2551 * Note: EMSA-PSS verification is over the length of N - 1 bits
2552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (buf[0] >> (8 - siglen * 8 + msb)) {
2556 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2557 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002558
Simon Butcher02037452016-03-01 21:19:12 +00002559 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002561 p++;
2562 siglen -= 1;
2563 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (siglen < hlen + 2) {
2566 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2567 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002568 hash_start = p + siglen - hlen - 1;
2569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2571 if (ret != 0) {
2572 return ret;
2573 }
Paul Bakker02303e82013-01-03 11:08:31 +01002574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002578 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (*p++ != 0x01) {
2582 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2583 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002584
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002585 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2588 observed_salt_len != (size_t) expected_salt_len) {
2589 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002590 }
2591
Simon Butcher02037452016-03-01 21:19:12 +00002592 /*
2593 * Generate H = Hash( M' )
2594 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2596 result, mgf1_hash_id);
2597 if (ret != 0) {
2598 return ret;
2599 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 if (memcmp(hash_start, result, hlen) != 0) {
2602 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2603 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002606}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002607
2608/*
2609 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2610 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002611int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2612 mbedtls_md_type_t md_alg,
2613 unsigned int hashlen,
2614 const unsigned char *hash,
2615 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002616{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002617 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002619 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002624 : md_alg;
2625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2627 md_alg, hashlen, hash,
2628 mgf1_hash_id,
2629 MBEDTLS_RSA_SALT_LEN_ANY,
2630 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002631
2632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002636/*
2637 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2638 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002639int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2640 mbedtls_md_type_t md_alg,
2641 unsigned int hashlen,
2642 const unsigned char *hash,
2643 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002644{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002645 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002646 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002647 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002650 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002652
2653 sig_len = ctx->len;
2654
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002655 /*
2656 * Prepare expected PKCS1 v1.5 encoding of hash.
2657 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2660 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002661 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2662 goto cleanup;
2663 }
2664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2666 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002667 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002669
2670 /*
2671 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2672 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 ret = mbedtls_rsa_public(ctx, sig, encoded);
2675 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002676 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002678
Simon Butcher02037452016-03-01 21:19:12 +00002679 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002680 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002681 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2684 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002685 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2686 goto cleanup;
2687 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002688
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002689cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002692 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002693 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002696 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002697 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
2703/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002704 * Do an RSA operation and check the message digest
2705 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002706int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2707 mbedtls_md_type_t md_alg,
2708 unsigned int hashlen,
2709 const unsigned char *hash,
2710 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002711{
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002713 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#if defined(MBEDTLS_PKCS1_V15)
2718 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2720 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002721#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723#if defined(MBEDTLS_PKCS1_V21)
2724 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2726 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002727#endif
2728
2729 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002731 }
2732}
2733
2734/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002735 * Copy the components of an RSA key
2736 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002737int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002738{
Janos Follath24eed8d2019-11-22 13:21:35 +00002739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002740
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002741 dst->len = src->len;
2742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2744 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2747 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2748 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002749
2750#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2752 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2753 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2754 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2755 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002756#endif
2757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002759
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2761 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002762
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002763 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002764 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002765
2766cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 if (ret != 0) {
2768 mbedtls_rsa_free(dst);
2769 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002772}
2773
2774/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002775 * Free the components of an RSA key
2776 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002777void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002778{
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002780 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 mbedtls_mpi_free(&ctx->Vi);
2784 mbedtls_mpi_free(&ctx->Vf);
2785 mbedtls_mpi_free(&ctx->RN);
2786 mbedtls_mpi_free(&ctx->D);
2787 mbedtls_mpi_free(&ctx->Q);
2788 mbedtls_mpi_free(&ctx->P);
2789 mbedtls_mpi_free(&ctx->E);
2790 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002791
Hanno Becker33c30a02017-08-23 07:00:22 +01002792#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 mbedtls_mpi_free(&ctx->RQ);
2794 mbedtls_mpi_free(&ctx->RP);
2795 mbedtls_mpi_free(&ctx->QP);
2796 mbedtls_mpi_free(&ctx->DQ);
2797 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002798#endif /* MBEDTLS_RSA_NO_CRT */
2799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002801 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 if (ctx->ver != 0) {
2803 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002804 ctx->ver = 0;
2805 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002806#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002807}
2808
Hanno Beckerab377312017-08-23 16:24:51 +01002809#endif /* !MBEDTLS_RSA_ALT */
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
2814/*
2815 * Example RSA-1024 keypair, for test purposes
2816 */
2817#define KEY_LEN 128
2818
2819#define RSA_N "9292758453063D803DD603D5E777D788" \
2820 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2821 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2822 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2823 "93A89813FBF3C4F8066D2D800F7C38A8" \
2824 "1AE31942917403FF4946B0A83D3D3E05" \
2825 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2826 "5E94BB77B07507233A0BC7BAC8F90F79"
2827
2828#define RSA_E "10001"
2829
2830#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2831 "66CA472BC44D253102F8B4A9D3BFA750" \
2832 "91386C0077937FE33FA3252D28855837" \
2833 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2834 "DF79C5CE07EE72C7F123142198164234" \
2835 "CABB724CF78B8173B9F880FC86322407" \
2836 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2837 "071513A1E85B5DFA031F21ECAE91A34D"
2838
2839#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2840 "2C01CAD19EA484A87EA4377637E75500" \
2841 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2842 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2843
2844#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2845 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2846 "910E4168387E3C30AA1E00C339A79508" \
2847 "8452DD96A9A5EA5D9DCA68DA636032AF"
2848
Paul Bakker5121ce52009-01-03 21:22:43 +00002849#define PT_LEN 24
2850#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2851 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002854static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002855{
gufe44c2620da2020-08-03 17:56:50 +02002856#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002857 size_t i;
2858
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002860 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 }
Paul Bakker545570e2010-07-18 09:00:25 +00002862
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002864 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002866#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002868 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002872#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002875}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002877
Paul Bakker5121ce52009-01-03 21:22:43 +00002878/*
2879 * Checkup routine
2880 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002881int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002882{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002883 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002885 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002887 unsigned char rsa_plaintext[PT_LEN];
2888 unsigned char rsa_decrypted[PT_LEN];
2889 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002890#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002891 unsigned char sha1sum[20];
2892#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
Hanno Becker3a701162017-08-22 13:52:43 +01002894 mbedtls_mpi K;
2895
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 mbedtls_mpi_init(&K);
2897 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2900 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2901 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2902 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2903 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2904 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2905 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2906 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2907 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2908 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 if (verbose != 0) {
2913 mbedtls_printf(" RSA key validation: ");
2914 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2917 mbedtls_rsa_check_privkey(&rsa) != 0) {
2918 if (verbose != 0) {
2919 mbedtls_printf("failed\n");
2920 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Hanno Becker5bc87292017-05-03 15:09:31 +01002922 ret = 1;
2923 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002924 }
2925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 if (verbose != 0) {
2927 mbedtls_printf("passed\n PKCS#1 encryption : ");
2928 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002929
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2933 PT_LEN, rsa_plaintext,
2934 rsa_ciphertext) != 0) {
2935 if (verbose != 0) {
2936 mbedtls_printf("failed\n");
2937 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
Hanno Becker5bc87292017-05-03 15:09:31 +01002939 ret = 1;
2940 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 }
2942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 if (verbose != 0) {
2944 mbedtls_printf("passed\n PKCS#1 decryption : ");
2945 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2948 &len, rsa_ciphertext, rsa_decrypted,
2949 sizeof(rsa_decrypted)) != 0) {
2950 if (verbose != 0) {
2951 mbedtls_printf("failed\n");
2952 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Hanno Becker5bc87292017-05-03 15:09:31 +01002954 ret = 1;
2955 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002956 }
2957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2959 if (verbose != 0) {
2960 mbedtls_printf("failed\n");
2961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Hanno Becker5bc87292017-05-03 15:09:31 +01002963 ret = 1;
2964 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002965 }
2966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 if (verbose != 0) {
2968 mbedtls_printf("passed\n");
2969 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002970
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002971#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 if (verbose != 0) {
2973 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002974 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002976 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2977 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 if (verbose != 0) {
2979 mbedtls_printf("failed\n");
2980 }
2981
2982 return 1;
2983 }
2984
2985 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2986 MBEDTLS_MD_SHA1, 20,
2987 sha1sum, rsa_ciphertext) != 0) {
2988 if (verbose != 0) {
2989 mbedtls_printf("failed\n");
2990 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002991
Hanno Becker5bc87292017-05-03 15:09:31 +01002992 ret = 1;
2993 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 }
2995
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 if (verbose != 0) {
2997 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2998 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002999
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
3001 sha1sum, rsa_ciphertext) != 0) {
3002 if (verbose != 0) {
3003 mbedtls_printf("failed\n");
3004 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003005
Hanno Becker5bc87292017-05-03 15:09:31 +01003006 ret = 1;
3007 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003008 }
3009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 if (verbose != 0) {
3011 mbedtls_printf("passed\n");
3012 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003013#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 if (verbose != 0) {
3016 mbedtls_printf("\n");
3017 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003018
Paul Bakker3d8fb632014-04-17 12:42:41 +02003019cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 mbedtls_mpi_free(&K);
3021 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02003023 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003026}
3027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#endif /* MBEDTLS_RSA_C */