blob: b250e1d491f9afedc552d8e6de2fd04a5f2dd25e [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 Setti201e6432024-02-01 17:19:37 +010049/*
50 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
51 *
52 * The value zero is:
53 * - never a valid value for an RSA parameter
54 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
55 *
56 * Since values can't be omitted in PKCS#1, passing a zero value to
57 * rsa_complete() would be incorrect, so reject zero values early.
58 */
59static int asn1_get_nonzero_mpi(unsigned char **p,
60 const unsigned char *end,
61 mbedtls_mpi *X)
62{
63 int ret;
64
65 ret = mbedtls_asn1_get_mpi(p, end, X);
66 if (ret != 0) {
67 return ret;
68 }
69
70 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
71 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
72 }
73
74 return 0;
75}
76
Valerio Setti135ebde2024-02-01 17:00:29 +010077int mbedtls_rsa_parse_key(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +010078{
79 int ret, version;
80 size_t len;
81 unsigned char *p, *end;
82
83 mbedtls_mpi T;
84 mbedtls_mpi_init(&T);
85
86 p = (unsigned char *) key;
87 end = p + keylen;
88
89 /*
90 * This function parses the RSAPrivateKey (PKCS#1)
91 *
92 * RSAPrivateKey ::= SEQUENCE {
93 * version Version,
94 * modulus INTEGER, -- n
95 * publicExponent INTEGER, -- e
96 * privateExponent INTEGER, -- d
97 * prime1 INTEGER, -- p
98 * prime2 INTEGER, -- q
99 * exponent1 INTEGER, -- d mod (p-1)
100 * exponent2 INTEGER, -- d mod (q-1)
101 * coefficient INTEGER, -- (inverse of q) mod p
102 * otherPrimeInfos OtherPrimeInfos OPTIONAL
103 * }
104 */
105 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
106 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
107 return ret;
108 }
109
110 end = p + len;
111
Valerio Settife329ce2024-02-06 08:00:18 +0100112 if (end > (key + keylen)) {
113 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
114 }
115
Valerio Setti44ff9502024-02-01 16:51:05 +0100116 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
117 return ret;
118 }
119
120 if (version != 0) {
121 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
122 }
123
124 /* Import N */
125 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
126 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
127 NULL, NULL)) != 0) {
128 goto cleanup;
129 }
130
131 /* Import E */
132 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
133 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
134 NULL, &T)) != 0) {
135 goto cleanup;
136 }
137
138 /* Import D */
139 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
140 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
141 &T, NULL)) != 0) {
142 goto cleanup;
143 }
144
145 /* Import P */
146 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
147 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
148 NULL, NULL)) != 0) {
149 goto cleanup;
150 }
151
152 /* Import Q */
153 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
154 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
155 NULL, NULL)) != 0) {
156 goto cleanup;
157 }
158
159#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
160 /*
161 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
162 * that they can be easily recomputed from D, P and Q. However by
163 * parsing them from the PKCS1 structure it is possible to avoid
164 * recalculating them which both reduces the overhead of loading
165 * RSA private keys into memory and also avoids side channels which
166 * can arise when computing those values, since all of D, P, and Q
167 * are secret. See https://eprint.iacr.org/2020/055 for a
168 * description of one such attack.
169 */
170
171 /* Import DP */
172 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
173 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
174 goto cleanup;
175 }
176
177 /* Import DQ */
178 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
179 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
180 goto cleanup;
181 }
182
183 /* Import QP */
184 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
185 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
186 goto cleanup;
187 }
188
189#else
190 /* Verify existence of the CRT params */
191 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
192 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
193 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
194 goto cleanup;
195 }
196#endif
197
198 /* rsa_complete() doesn't complete anything with the default
199 * implementation but is still called:
200 * - for the benefit of alternative implementation that may want to
201 * pre-compute stuff beyond what's provided (eg Montgomery factors)
202 * - as is also sanity-checks the key
203 *
204 * Furthermore, we also check the public part for consistency with
205 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
206 */
207 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
208 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
209 goto cleanup;
210 }
211
212 if (p != end) {
213 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
214 }
215
216cleanup:
217
218 mbedtls_mpi_free(&T);
219
220 if (ret != 0) {
221 mbedtls_rsa_free(rsa);
222 }
223
224 return ret;
225}
226
Valerio Setti201e6432024-02-01 17:19:37 +0100227int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +0100228{
Valerio Setti201e6432024-02-01 17:19:37 +0100229 unsigned char *p = (unsigned char *) key;
230 unsigned char *end = (unsigned char *) (key + keylen);
Valerio Setti44ff9502024-02-01 16:51:05 +0100231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
232 size_t len;
233
234 /*
235 * RSAPublicKey ::= SEQUENCE {
236 * modulus INTEGER, -- n
237 * publicExponent INTEGER -- e
238 * }
239 */
240
Valerio Setti201e6432024-02-01 17:19:37 +0100241 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
Valerio Setti44ff9502024-02-01 16:51:05 +0100242 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
243 return ret;
244 }
245
Valerio Settife329ce2024-02-06 08:00:18 +0100246 end = p + len;
247
248 if (end > (key + keylen)) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100249 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
250 }
251
252 /* Import N */
Valerio Setti201e6432024-02-01 17:19:37 +0100253 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100254 return ret;
255 }
256
Valerio Setti201e6432024-02-01 17:19:37 +0100257 if ((ret = mbedtls_rsa_import_raw(rsa, p, len, NULL, 0, NULL, 0,
Valerio Setti44ff9502024-02-01 16:51:05 +0100258 NULL, 0, NULL, 0)) != 0) {
259 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
260 }
261
Valerio Setti201e6432024-02-01 17:19:37 +0100262 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100263
264 /* Import E */
Valerio Setti201e6432024-02-01 17:19:37 +0100265 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100266 return ret;
267 }
268
269 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
Valerio Setti201e6432024-02-01 17:19:37 +0100270 NULL, 0, p, len)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100271 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
272 }
273
Valerio Setti201e6432024-02-01 17:19:37 +0100274 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100275
276 if (mbedtls_rsa_complete(rsa) != 0 ||
277 mbedtls_rsa_check_pubkey(rsa) != 0) {
278 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
279 }
280
Valerio Setti201e6432024-02-01 17:19:37 +0100281 if (p != end) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100282 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
283 }
284
285 return 0;
286}
287
Valerio Setti135ebde2024-02-01 17:00:29 +0100288int mbedtls_rsa_write_key(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100289 unsigned char **p)
290{
291 size_t len = 0;
292 int ret;
293
294 mbedtls_mpi T; /* Temporary holding the exported parameters */
295
296 /*
297 * Export the parameters one after another to avoid simultaneous copies.
298 */
299
300 mbedtls_mpi_init(&T);
301
302 /* Export QP */
303 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
304 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
305 goto end_of_export;
306 }
307 len += ret;
308
309 /* Export DQ */
310 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
311 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
312 goto end_of_export;
313 }
314 len += ret;
315
316 /* Export DP */
317 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
318 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
319 goto end_of_export;
320 }
321 len += ret;
322
323 /* Export Q */
324 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, &T, NULL, NULL)) != 0 ||
325 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
326 goto end_of_export;
327 }
328 len += ret;
329
330 /* Export P */
331 if ((ret = mbedtls_rsa_export(rsa, NULL, &T, NULL, NULL, NULL)) != 0 ||
332 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
333 goto end_of_export;
334 }
335 len += ret;
336
337 /* Export D */
338 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, &T, NULL)) != 0 ||
339 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
340 goto end_of_export;
341 }
342 len += ret;
343
344 /* Export E */
345 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
346 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
347 goto end_of_export;
348 }
349 len += ret;
350
351 /* Export N */
352 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
353 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
354 goto end_of_export;
355 }
356 len += ret;
357
358end_of_export:
359
360 mbedtls_mpi_free(&T);
361 if (ret < 0) {
362 return ret;
363 }
364
365 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
366 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
367 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
368 MBEDTLS_ASN1_CONSTRUCTED |
369 MBEDTLS_ASN1_SEQUENCE));
370
371 return (int) len;
372}
373
374/*
375 * RSAPublicKey ::= SEQUENCE {
376 * modulus INTEGER, -- n
377 * publicExponent INTEGER -- e
378 * }
379 */
Valerio Setti135ebde2024-02-01 17:00:29 +0100380int mbedtls_rsa_write_pubkey(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100381 unsigned char **p)
382{
383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
384 size_t len = 0;
385 mbedtls_mpi T;
386
387 mbedtls_mpi_init(&T);
388
389 /* Export E */
390 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
391 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
392 goto end_of_export;
393 }
394 len += ret;
395
396 /* Export N */
397 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
398 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
399 goto end_of_export;
400 }
401 len += ret;
402
403end_of_export:
404
405 mbedtls_mpi_free(&T);
406 if (ret < 0) {
407 return ret;
408 }
409
410 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
411 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
412 MBEDTLS_ASN1_SEQUENCE));
413
414 return (int) len;
415}
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100416
417#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
418
419/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
420 * operation (EME-PKCS1-v1_5 decoding).
421 *
422 * \note The return value from this function is a sensitive value
423 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
424 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
425 * is often a situation that an attacker can provoke and leaking which
426 * one is the result is precisely the information the attacker wants.
427 *
428 * \param input The input buffer which is the payload inside PKCS#1v1.5
429 * encryption padding, called the "encoded message EM"
430 * by the terminology.
431 * \param ilen The length of the payload in the \p input buffer.
432 * \param output The buffer for the payload, called "message M" by the
433 * PKCS#1 terminology. This must be a writable buffer of
434 * length \p output_max_len bytes.
435 * \param olen The address at which to store the length of
436 * the payload. This must not be \c NULL.
437 * \param output_max_len The length in bytes of the output buffer \p output.
438 *
439 * \return \c 0 on success.
440 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
441 * The output buffer is too small for the unpadded payload.
442 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
443 * The input doesn't contain properly formatted padding.
444 */
445static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
446 size_t ilen,
447 unsigned char *output,
448 size_t output_max_len,
449 size_t *olen)
450{
451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
452 size_t i, plaintext_max_size;
453
454 /* The following variables take sensitive values: their value must
455 * not leak into the observable behavior of the function other than
456 * the designated outputs (output, olen, return value). Otherwise
457 * this would open the execution of the function to
458 * side-channel-based variants of the Bleichenbacher padding oracle
459 * attack. Potential side channels include overall timing, memory
460 * access patterns (especially visible to an adversary who has access
461 * to a shared memory cache), and branches (especially visible to
462 * an adversary who has access to a shared code cache or to a shared
463 * branch predictor). */
464 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100465 mbedtls_ct_condition_t bad;
466 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100467 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100468 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100469
470 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
471 : output_max_len;
472
473 /* Check and get padding length in constant time and constant
474 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100475 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100476
477
478 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
479 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100480 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100481
482 /* Read the whole buffer. Set pad_done to nonzero if we find
483 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100484 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100485 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100486 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100487 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100488 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100489 }
490
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100491 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100492 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100493
494 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100495 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100496
497 /* If the padding is valid, set plaintext_size to the number of
498 * remaining bytes after stripping the padding. If the padding
499 * is invalid, avoid leaking this fact through the size of the
500 * output: use the maximum message size that fits in the output
501 * buffer. Do it without branches to avoid leaking the padding
502 * validity through timing. RSA keys are small enough that all the
503 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100504 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100505 bad, (unsigned) plaintext_max_size,
506 (unsigned) (ilen - pad_count - 3));
507
508 /* Set output_too_large to 0 if the plaintext fits in the output
509 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100510 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100511 plaintext_max_size);
512
513 /* Set ret without branches to avoid timing attacks. Return:
514 * - INVALID_PADDING if the padding is bad (bad != 0).
515 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
516 * plaintext does not fit in the output buffer.
517 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100518 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100519 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100520 MBEDTLS_ERR_RSA_INVALID_PADDING,
521 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100522 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100523
524 /* If the padding is bad or the plaintext is too large, zero the
525 * data that we're about to copy to the output buffer.
526 * We need to copy the same amount of data
527 * from the same buffer whether the padding is good or not to
528 * avoid leaking the padding validity through overall timing or
529 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100530 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100531
532 /* If the plaintext is too large, truncate it to the buffer size.
533 * Copy anyway to avoid revealing the length through timing, because
534 * revealing the length is as bad as revealing the padding validity
535 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100536 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100537 (unsigned) plaintext_max_size,
538 (unsigned) plaintext_size);
539
540 /* Move the plaintext to the leftmost position where it can start in
541 * the working buffer, i.e. make it start plaintext_max_size from
542 * the end of the buffer. Do this with a memory access trace that
543 * does not depend on the plaintext size. After this move, the
544 * starting location of the plaintext is no longer sensitive
545 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100546 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
547 plaintext_max_size,
548 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100549
550 /* Finally copy the decrypted plaintext plus trailing zeros into the output
551 * buffer. If output_max_len is 0, then output may be an invalid pointer
552 * and the result of memcpy() would be undefined; prevent undefined
553 * behavior making sure to depend only on output_max_len (the size of the
554 * user-provided output buffer), which is independent from plaintext
555 * length, validity of padding, success of the decryption, and other
556 * secrets. */
557 if (output_max_len != 0) {
558 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
559 }
560
561 /* Report the amount of data we copied to the output buffer. In case
562 * of errors (bad padding or output too large), the value of *olen
563 * when this function returns is not specified. Making it equivalent
564 * to the good case limits the risks of leaking the padding validity. */
565 *olen = plaintext_size;
566
567 return ret;
568}
569
570#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
571
Hanno Beckera565f542017-10-11 11:00:19 +0100572#if !defined(MBEDTLS_RSA_ALT)
573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
575 const mbedtls_mpi *N,
576 const mbedtls_mpi *P, const mbedtls_mpi *Q,
577 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100578{
Janos Follath24eed8d2019-11-22 13:21:35 +0000579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
582 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
583 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
584 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
585 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
586 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100587 }
588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (N != NULL) {
590 ctx->len = mbedtls_mpi_size(&ctx->N);
591 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100594}
595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
597 unsigned char const *N, size_t N_len,
598 unsigned char const *P, size_t P_len,
599 unsigned char const *Q, size_t Q_len,
600 unsigned char const *D, size_t D_len,
601 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100602{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000603 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if (N != NULL) {
606 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
607 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100608 }
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (P != NULL) {
611 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
612 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (Q != NULL) {
615 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
616 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if (D != NULL) {
619 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
620 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (E != NULL) {
623 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
624 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100625
626cleanup:
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (ret != 0) {
629 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
630 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100633}
634
Hanno Becker705fc682017-10-10 17:57:02 +0100635/*
636 * Checks whether the context fields are set in such a way
637 * that the RSA primitives will be able to execute without error.
638 * It does *not* make guarantees for consistency of the parameters.
639 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100640static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
641 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100642{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100643#if !defined(MBEDTLS_RSA_NO_CRT)
644 /* blinding_needed is only used for NO_CRT to decide whether
645 * P,Q need to be present or not. */
646 ((void) blinding_needed);
647#endif
648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
650 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
651 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000652 }
Hanno Becker705fc682017-10-10 17:57:02 +0100653
654 /*
655 * 1. Modular exponentiation needs positive, odd moduli.
656 */
657
658 /* Modular exponentiation wrt. N is always used for
659 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
661 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
662 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100663 }
664
665#if !defined(MBEDTLS_RSA_NO_CRT)
666 /* Modular exponentiation for P and Q is only
667 * used for private key operations and if CRT
668 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (is_priv &&
670 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
671 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
672 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
673 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
674 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100675 }
676#endif /* !MBEDTLS_RSA_NO_CRT */
677
678 /*
679 * 2. Exponents must be positive
680 */
681
682 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
684 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
685 }
Hanno Becker705fc682017-10-10 17:57:02 +0100686
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100687#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100688 /* For private key operations, use D or DP & DQ
689 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
691 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
692 }
Hanno Becker705fc682017-10-10 17:57:02 +0100693#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (is_priv &&
695 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
696 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
697 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100698 }
699#endif /* MBEDTLS_RSA_NO_CRT */
700
701 /* Blinding shouldn't make exponents negative either,
702 * so check that P, Q >= 1 if that hasn't yet been
703 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100704#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (is_priv && blinding_needed &&
706 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
707 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
708 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100709 }
710#endif
711
712 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100713 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100714#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (is_priv &&
716 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
717 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100718 }
719#endif
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100722}
723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100725{
726 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500727 int have_N, have_P, have_Q, have_D, have_E;
728#if !defined(MBEDTLS_RSA_NO_CRT)
729 int have_DP, have_DQ, have_QP;
730#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500731 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
734 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
735 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
736 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
737 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500738
739#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
741 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
742 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500743#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100744
Hanno Becker617c1ae2017-08-23 14:11:24 +0100745 /*
746 * Check whether provided parameters are enough
747 * to deduce all others. The following incomplete
748 * parameter sets for private keys are supported:
749 *
750 * (1) P, Q missing.
751 * (2) D and potentially N missing.
752 *
753 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100754
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500755 n_missing = have_P && have_Q && have_D && have_E;
756 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
757 d_missing = have_P && have_Q && !have_D && have_E;
758 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100759
760 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500761 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 if (!is_priv && !is_pub) {
764 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
765 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100766
767 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100768 * Step 1: Deduce N if P, Q are provided.
769 */
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (!have_N && have_P && have_Q) {
772 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
773 &ctx->Q)) != 0) {
774 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100775 }
776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100778 }
779
780 /*
781 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100782 */
783
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 if (pq_missing) {
785 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
786 &ctx->P, &ctx->Q);
787 if (ret != 0) {
788 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
789 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 } else if (d_missing) {
792 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
793 &ctx->Q,
794 &ctx->E,
795 &ctx->D)) != 0) {
796 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100797 }
798 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100799
Hanno Becker617c1ae2017-08-23 14:11:24 +0100800 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100801 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100802 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100803 */
804
Hanno Becker23344b52017-08-23 07:43:27 +0100805#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if (is_priv && !(have_DP && have_DQ && have_QP)) {
807 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
808 &ctx->DP, &ctx->DQ, &ctx->QP);
809 if (ret != 0) {
810 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
811 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100812 }
Hanno Becker23344b52017-08-23 07:43:27 +0100813#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100814
815 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100816 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100817 */
818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100820}
821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
823 unsigned char *N, size_t N_len,
824 unsigned char *P, size_t P_len,
825 unsigned char *Q, size_t Q_len,
826 unsigned char *D, size_t D_len,
827 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100828{
829 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500830 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100831
832 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500833 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
835 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
836 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
837 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
838 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100841 /* If we're trying to export private parameters for a public key,
842 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (P != NULL || Q != NULL || D != NULL) {
844 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
845 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100846
847 }
848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 if (N != NULL) {
850 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
851 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (P != NULL) {
854 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
855 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (Q != NULL) {
858 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
859 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (D != NULL) {
862 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
863 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (E != NULL) {
866 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
867 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100868
869cleanup:
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100872}
873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
875 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
876 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100877{
Janos Follath24eed8d2019-11-22 13:21:35 +0000878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500879 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100880
881 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500882 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
884 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
885 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
886 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
887 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100890 /* If we're trying to export private parameters for a public key,
891 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if (P != NULL || Q != NULL || D != NULL) {
893 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
894 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100895
896 }
897
898 /* Export all requested core parameters. */
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
901 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
902 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
903 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
904 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
905 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100906 }
907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100909}
910
911/*
912 * Export CRT parameters
913 * This must also be implemented if CRT is not used, for being able to
914 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
915 * can be used in this case.
916 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100917int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
918 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100919{
Janos Follath24eed8d2019-11-22 13:21:35 +0000920 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500921 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100922
923 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500924 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
926 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
927 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
928 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
929 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (!is_priv) {
932 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
933 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100934
Hanno Beckerdc95c892017-08-23 06:57:02 +0100935#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100936 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
938 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
939 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
940 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100941 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100942#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
944 DP, DQ, QP)) != 0) {
945 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100946 }
947#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100950}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100951
Paul Bakker5121ce52009-01-03 21:22:43 +0000952/*
953 * Initialize an RSA context
954 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100955void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000956{
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Ronald Cronc1905a12021-06-05 11:11:14 +0200959 ctx->padding = MBEDTLS_RSA_PKCS_V15;
960 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100963 /* Set ctx->ver to nonzero to indicate that the mutex has been
964 * initialized and will need to be freed. */
965 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200967#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000968}
969
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100970/*
971 * Set padding for an existing RSA context
972 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100973int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
974 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100975{
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200977#if defined(MBEDTLS_PKCS1_V15)
978 case MBEDTLS_RSA_PKCS_V15:
979 break;
980#endif
981
982#if defined(MBEDTLS_PKCS1_V21)
983 case MBEDTLS_RSA_PKCS_V21:
984 break;
985#endif
986 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200988 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200989
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200990#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
992 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200993 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200994 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 return MBEDTLS_ERR_RSA_INVALID_PADDING;
996 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200997 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200998#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500999
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +01001000 ctx->padding = padding;
1001 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +02001002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +01001004}
1005
Hanno Becker617c1ae2017-08-23 14:11:24 +01001006/*
Yanray Wang83548b52023-03-15 16:46:34 +08001007 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +08001008 */
1009int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
1010{
Yanray Wang644b9012023-03-15 16:50:31 +08001011 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +08001012}
1013
1014/*
Yanray Wang12cb3962023-03-01 10:20:02 +08001015 * Get hash identifier of mbedtls_md_type_t type
1016 */
Yanray Wangd41684e2023-03-17 18:54:22 +08001017int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +08001018{
Yanray Wang644b9012023-03-15 16:50:31 +08001019 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +08001020}
1021
1022/*
Hanno Becker617c1ae2017-08-23 14:11:24 +01001023 * Get length in bytes of RSA modulus
1024 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001025size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +01001026{
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +01001028}
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
1032/*
1033 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001034 *
1035 * This generation method follows the RSA key pair generation procedure of
1036 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
1039 int (*f_rng)(void *, unsigned char *, size_t),
1040 void *p_rng,
1041 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +00001042{
Janos Follath24eed8d2019-11-22 13:21:35 +00001043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -08001044 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +01001045 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
Janos Follathb8fc1b02018-09-03 15:37:01 +01001047 /*
1048 * If the modulus is 1024 bit long or shorter, then the security strength of
1049 * the RSA algorithm is less than or equal to 80 bits and therefore an error
1050 * rate of 2^-80 is sufficient.
1051 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +01001053 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 }
Janos Follathb8fc1b02018-09-03 15:37:01 +01001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 mbedtls_mpi_init(&H);
1057 mbedtls_mpi_init(&G);
1058 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001060 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +01001061 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1062 goto cleanup;
1063 }
1064
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001065 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1067 goto cleanup;
1068 }
1069
1070 /*
1071 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001072 * 1. |P-Q| > 2^( nbits / 2 - 100 )
1073 * 2. GCD( E, (P-1)*(Q-1) ) == 1
1074 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 do {
1079 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
1080 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
1083 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001085 /* 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 +01001086 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
1087 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001091 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 if (H.s < 0) {
1093 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
1094 }
Janos Follathef441782016-09-21 13:18:12 +01001095
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001096 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
1098 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
1099 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001100
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001101 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
1103 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -08001104 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001106
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001107 /* 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 +01001108 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
1109 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
1110 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 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 -08001113 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001115
1116 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001119 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
1121 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001126
Jethro Beekman97f95c92018-02-13 15:50:36 -08001127#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +00001128 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 * DP = D mod (P - 1)
1130 * DQ = D mod (Q - 1)
1131 * QP = Q^-1 mod P
1132 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
1134 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001135#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
Hanno Becker83aad1f2017-08-23 06:45:10 +01001137 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
1140cleanup:
1141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 mbedtls_mpi_free(&H);
1143 mbedtls_mpi_free(&G);
1144 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (ret != 0) {
1147 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +01001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if ((-ret & ~0x7f) == 0) {
1150 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
1151 }
1152 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 }
1154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001156}
1157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
1160/*
1161 * Check a public RSA key
1162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001164{
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
1166 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001167 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
1170 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
1174 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
1175 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
1176 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1177 }
1178
1179 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001180}
1181
1182/*
Hanno Becker705fc682017-10-10 17:57:02 +01001183 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001185int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001186{
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
1188 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
1189 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 }
Paul Bakker48377d92013-08-30 12:06:24 +02001191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
1193 &ctx->D, &ctx->E, NULL, NULL) != 0) {
1194 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001196
Hanno Beckerb269a852017-08-25 08:03:21 +01001197#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
1199 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
1200 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +01001201 }
1202#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001205}
1206
1207/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001208 * Check if contexts holding a public and private key match
1209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001210int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
1211 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001212{
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
1214 mbedtls_rsa_check_privkey(prv) != 0) {
1215 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001216 }
1217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
1219 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
1220 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001221 }
1222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001224}
1225
1226/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 * Do an RSA public key operation
1228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001229int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
1230 const unsigned char *input,
1231 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001232{
Janos Follath24eed8d2019-11-22 13:21:35 +00001233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001234 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
1238 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1239 }
Hanno Becker705fc682017-10-10 17:57:02 +01001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001243#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1245 return ret;
1246 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001247#endif
1248
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001252 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1253 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 }
1255
1256 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
1258 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
1260cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1263 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1264 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001265#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (ret != 0) {
1270 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
1271 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001274}
1275
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001276/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001277 * Generate or update blinding values, see section 10 of:
1278 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001279 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001280 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001282static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1283 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001284{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001285 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001286 mbedtls_mpi R;
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001291 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1293 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1294 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1295 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001296
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001297 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001298 }
1299
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001300 /* Unblinding value: Vf = random number, invertible mod N */
1301 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +02001303 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
1304 goto cleanup;
1305 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 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 +02001308
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001309 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
1311 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
1312 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001313
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001314 /* At this point, Vi is invertible mod N if and only if both Vf and R
1315 * are invertible mod N. If one of them isn't, we don't need to know
1316 * which one, we just loop and choose new values for both of them.
1317 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
1319 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001320 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -05001324
1325 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
1327 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001328
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001329 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001330 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 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 +02001332
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001333
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001334cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001338}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001339
Paul Bakker5121ce52009-01-03 21:22:43 +00001340/*
Janos Follathe81102e2017-03-22 13:38:28 +00001341 * Exponent blinding supposed to prevent side-channel attacks using multiple
1342 * traces of measurements to recover the RSA key. The more collisions are there,
1343 * the more bits of the key can be recovered. See [3].
1344 *
1345 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001346 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001347 *
1348 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001349 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001350 *
1351 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1352 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1353 * Thus in this sense with 28 byte blinding the security is not reduced by
1354 * side-channel attacks like the one in [3])
1355 *
1356 * This countermeasure does not help if the key recovery is possible with a
1357 * single trace.
1358 */
1359#define RSA_EXPONENT_BLINDING 28
1360
1361/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 * Do an RSA private key operation
1363 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001364int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1365 int (*f_rng)(void *, unsigned char *, size_t),
1366 void *p_rng,
1367 const unsigned char *input,
1368 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001369{
Janos Follath24eed8d2019-11-22 13:21:35 +00001370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001371 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001372
1373 /* Temporary holding the result */
1374 mbedtls_mpi T;
1375
1376 /* Temporaries holding P-1, Q-1 and the
1377 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001378 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001379
1380#if !defined(MBEDTLS_RSA_NO_CRT)
1381 /* Temporaries holding the results mod p resp. mod q. */
1382 mbedtls_mpi TP, TQ;
1383
1384 /* Temporaries holding the blinded exponents for
1385 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001386 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001387
1388 /* Pointers to actual exponents to be used - either the unblinded
1389 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +00001390 mbedtls_mpi *DP = &ctx->DP;
1391 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +01001392#else
1393 /* Temporary holding the blinded exponent (if used). */
1394 mbedtls_mpi D_blind;
1395
1396 /* Pointer to actual exponent to be used - either the unblinded
1397 * or the blinded one, depending on the presence of a PRNG. */
1398 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +01001399#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001400
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001401 /* Temporaries holding the initial input and the double
1402 * checked result; should be the same in the end. */
1403 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if (f_rng == NULL) {
1406 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1407 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 if (rsa_check_context(ctx, 1 /* private key checks */,
1410 1 /* blinding on */) != 0) {
1411 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001412 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001413
Hanno Becker06811ce2017-05-03 15:10:34 +01001414#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1416 return ret;
1417 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001418#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001419
Hanno Becker06811ce2017-05-03 15:10:34 +01001420 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 mbedtls_mpi_init(&P1);
1424 mbedtls_mpi_init(&Q1);
1425 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001426
Janos Follathe81102e2017-03-22 13:38:28 +00001427#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001429#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 mbedtls_mpi_init(&DP_blind);
1431 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001432#endif
1433
Hanno Becker06811ce2017-05-03 15:10:34 +01001434#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001436#endif
1437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 mbedtls_mpi_init(&I);
1439 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001440
1441 /* End of MPI initialization */
1442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1444 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001445 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1446 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 }
1448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +01001450
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001451 /*
1452 * Blinding
1453 * T = T * Vi mod N
1454 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1456 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1457 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001458
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001459 /*
1460 * Exponent blinding
1461 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1463 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001464
Janos Follathf9203b42017-03-22 15:13:15 +00001465#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001466 /*
1467 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1468 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1470 f_rng, p_rng));
1471 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1472 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1473 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +00001474
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001475 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001476#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001477 /*
1478 * DP_blind = ( P - 1 ) * R + DP
1479 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1481 f_rng, p_rng));
1482 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1483 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1484 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001485
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001486 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001487
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001488 /*
1489 * DQ_blind = ( Q - 1 ) * R + DQ
1490 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1492 f_rng, p_rng));
1493 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1494 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1495 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001496
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001497 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001498#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001502#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001503 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001504 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001506 * TP = input ^ dP mod P
1507 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1511 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001512
1513 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001514 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001515 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1517 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1518 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001519
1520 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001521 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1524 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001526
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001527 /*
1528 * Unblind
1529 * T = T * Vf mod N
1530 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1532 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001533
Hanno Becker2dec5e82017-10-03 07:49:52 +01001534 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1536 &ctx->N, &ctx->RN));
1537 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001538 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1539 goto cleanup;
1540 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001541
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
1545cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1548 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1549 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001550#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 mbedtls_mpi_free(&P1);
1553 mbedtls_mpi_free(&Q1);
1554 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001555
Janos Follathe81102e2017-03-22 13:38:28 +00001556#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001558#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 mbedtls_mpi_free(&DP_blind);
1560 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001561#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001564
1565#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001567#endif
1568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 mbedtls_mpi_free(&C);
1570 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if (ret != 0 && ret >= -0x007f) {
1573 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001577}
1578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001580/**
1581 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1582 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001583 * \param dst buffer to mask
1584 * \param dlen length of destination buffer
1585 * \param src source of the mask generation
1586 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001587 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001589static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1590 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001591{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001592 unsigned char counter[4];
1593 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001594 unsigned int hlen;
1595 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001596 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001597 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001598 const mbedtls_md_info_t *md_info;
1599 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 mbedtls_md_init(&md_ctx);
1602 md_info = mbedtls_md_info_from_type(md_alg);
1603 if (md_info == NULL) {
1604 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1605 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 mbedtls_md_init(&md_ctx);
1608 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001609 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 memset(mask, 0, sizeof(mask));
1615 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001616
Simon Butcher02037452016-03-01 21:19:12 +00001617 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001618 p = dst;
1619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001621 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001623 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001627 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 }
1629 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001630 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 }
1632 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001633 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 }
1635 if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001636 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001640 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001642
1643 counter[3]++;
1644
1645 dlen -= use_len;
1646 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001647
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001648exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001653}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001654
1655/**
1656 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1657 *
1658 * \param hash the input hash
1659 * \param hlen length of the input hash
1660 * \param salt the input salt
1661 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001662 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001663 * \param md_alg message digest to use
1664 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001665static int hash_mprime(const unsigned char *hash, size_t hlen,
1666 const unsigned char *salt, size_t slen,
1667 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001668{
1669 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001670
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001671 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1675 if (md_info == NULL) {
1676 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1677 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 mbedtls_md_init(&md_ctx);
1680 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001681 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 }
1683 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001684 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 }
1686 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001687 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 }
1689 if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001690 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 }
1692 if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001693 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 }
1695 if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001696 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001698
1699exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001703}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001704
1705/**
1706 * Compute a hash.
1707 *
1708 * \param md_alg algorithm to use
1709 * \param input input message to hash
1710 * \param ilen input length
1711 * \param output the output buffer - must be large enough for \p md_alg
1712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001713static int compute_hash(mbedtls_md_type_t md_alg,
1714 const unsigned char *input, size_t ilen,
1715 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001716{
1717 const mbedtls_md_info_t *md_info;
1718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 md_info = mbedtls_md_info_from_type(md_alg);
1720 if (md_info == NULL) {
1721 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1722 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001729/*
1730 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1731 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001732int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1733 int (*f_rng)(void *, unsigned char *, size_t),
1734 void *p_rng,
1735 const unsigned char *label, size_t label_len,
1736 size_t ilen,
1737 const unsigned char *input,
1738 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001739{
1740 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001741 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001742 unsigned char *p = output;
1743 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (f_rng == NULL) {
1746 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1747 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001748
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001749 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 if (hlen == 0) {
1751 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1752 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001753
1754 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001755
Simon Butcher02037452016-03-01 21:19:12 +00001756 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1758 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1759 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001762
1763 *p++ = 0;
1764
Simon Butcher02037452016-03-01 21:19:12 +00001765 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1767 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1768 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001769
1770 p += hlen;
1771
Simon Butcher02037452016-03-01 21:19:12 +00001772 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1774 if (ret != 0) {
1775 return ret;
1776 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001777 p += hlen;
1778 p += olen - 2 * hlen - 2 - ilen;
1779 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 if (ilen != 0) {
1781 memcpy(p, input, ilen);
1782 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001783
Simon Butcher02037452016-03-01 21:19:12 +00001784 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001786 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 return ret;
1788 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001789
Simon Butcher02037452016-03-01 21:19:12 +00001790 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001792 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 return ret;
1794 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001797}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001801/*
1802 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1803 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001804int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1805 int (*f_rng)(void *, unsigned char *, size_t),
1806 void *p_rng, size_t ilen,
1807 const unsigned char *input,
1808 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001809{
1810 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001812 unsigned char *p = output;
1813
Paul Bakkerb3869132013-02-28 17:21:01 +01001814 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001815
Simon Butcher02037452016-03-01 21:19:12 +00001816 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 if (ilen + 11 < ilen || olen < ilen + 11) {
1818 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1819 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001820
1821 nb_pad = olen - 3 - ilen;
1822
1823 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 if (f_rng == NULL) {
1826 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1827 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001828
1829 *p++ = MBEDTLS_RSA_CRYPT;
1830
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001832 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001833
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001834 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 ret = f_rng(p_rng, p, 1);
1836 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001837
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001838 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if (rng_dl == 0 || ret != 0) {
1840 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1841 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001842
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001843 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001844 }
1845
1846 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (ilen != 0) {
1848 memcpy(p, input, ilen);
1849 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001854
Paul Bakker5121ce52009-01-03 21:22:43 +00001855/*
1856 * Add the message padding, then do an RSA operation
1857 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001858int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1859 int (*f_rng)(void *, unsigned char *, size_t),
1860 void *p_rng,
1861 size_t ilen,
1862 const unsigned char *input,
1863 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001864{
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866#if defined(MBEDTLS_PKCS1_V15)
1867 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1869 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001870#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#if defined(MBEDTLS_PKCS1_V21)
1873 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1875 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001876#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
1878 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001881}
1882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001884/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001885 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001887int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1888 int (*f_rng)(void *, unsigned char *, size_t),
1889 void *p_rng,
1890 const unsigned char *label, size_t label_len,
1891 size_t *olen,
1892 const unsigned char *input,
1893 unsigned char *output,
1894 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001895{
Janos Follath24eed8d2019-11-22 13:21:35 +00001896 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001897 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001898 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001899 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001901 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001902 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001903
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001904 /*
1905 * Parameters sanity checks
1906 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1908 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1909 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001910
1911 ilen = ctx->len;
1912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 if (ilen < 16 || ilen > sizeof(buf)) {
1914 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1915 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001916
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001917 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 if (hlen == 0) {
1919 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1920 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001921
Janos Follathc17cda12016-02-11 11:08:18 +00001922 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 if (2 * hlen + 2 > ilen) {
1924 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1925 }
Janos Follathc17cda12016-02-11 11:08:18 +00001926
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001927 /*
1928 * RSA operation
1929 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001933 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001936 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001937 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001938 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001939 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001941 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 /* DB: Apply dbMask to maskedDB */
1943 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001944 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001945 goto cleanup;
1946 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001947
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001948 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1950 label, label_len, lhash);
1951 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001952 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001954
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001955 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001956 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001957 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 p = buf;
1959
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001960 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001961
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001962 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001963
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001964 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001965 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001966 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001967
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001968 /* Get zero-padding len, but always read till end of buffer
1969 * (minus one, for the 01 byte) */
1970 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001971 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001973 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1974 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001975 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001976
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001977 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001978 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001979
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001980 /*
1981 * The only information "leaked" is whether the padding was correct or not
1982 * (eg, no data is copied if it was not correct). This meets the
1983 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1984 * the different error conditions.
1985 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001986 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001987 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1988 goto cleanup;
1989 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001990
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001991 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001992 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1993 goto cleanup;
1994 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001995
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001996 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 if (*olen != 0) {
1998 memcpy(output, p, *olen);
1999 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002000 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002001
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002002cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 mbedtls_platform_zeroize(buf, sizeof(buf));
2004 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002007}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002011/*
2012 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
2013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002014int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
2015 int (*f_rng)(void *, unsigned char *, size_t),
2016 void *p_rng,
2017 size_t *olen,
2018 const unsigned char *input,
2019 unsigned char *output,
2020 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002021{
2022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2023 size_t ilen;
2024 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
2025
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002026 ilen = ctx->len;
2027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2029 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2030 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 if (ilen < 16 || ilen > sizeof(buf)) {
2033 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2034 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002039 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
2043 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002044
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002045cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002049}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002051
2052/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002053 * Do an RSA operation, then remove the message padding
2054 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
2056 int (*f_rng)(void *, unsigned char *, size_t),
2057 void *p_rng,
2058 size_t *olen,
2059 const unsigned char *input,
2060 unsigned char *output,
2061 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01002062{
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064#if defined(MBEDTLS_PKCS1_V15)
2065 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
2067 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02002068#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#if defined(MBEDTLS_PKCS1_V21)
2071 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
2073 olen, input, output,
2074 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01002075#endif
2076
2077 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002079 }
2080}
2081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002083static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2084 int (*f_rng)(void *, unsigned char *, size_t),
2085 void *p_rng,
2086 mbedtls_md_type_t md_alg,
2087 unsigned int hashlen,
2088 const unsigned char *hash,
2089 int saltlen,
2090 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002091{
2092 size_t olen;
2093 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002094 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002095 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00002096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002097 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002098 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002101 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 if (f_rng == NULL) {
2105 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2106 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002107
2108 olen = ctx->len;
2109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002111 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002112 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (exp_hashlen == 0) {
2114 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2115 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if (hashlen != exp_hashlen) {
2118 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2119 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002120 }
2121
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002122 hash_id = (mbedtls_md_type_t) ctx->hash_id;
2123 if (hash_id == MBEDTLS_MD_NONE) {
2124 hash_id = md_alg;
2125 }
2126 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if (hlen == 0) {
2128 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2129 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
2132 /* Calculate the largest possible salt length, up to the hash size.
2133 * Normally this is the hash length, which is the maximum salt length
2134 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
2135 * enough room, use the maximum salt length that fits. The constraint is
2136 * that the hash length plus the salt length plus 2 bytes must be at most
2137 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
2138 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002139 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (olen < hlen + min_slen + 2) {
2141 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2142 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002143 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002145 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 }
2147 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
2148 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2149 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02002150 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002151 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01002154
Simon Butcher02037452016-03-01 21:19:12 +00002155 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002157 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01002158 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002159
2160 /* Generate salt of length slen in place in the encoded message */
2161 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
2163 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
2164 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02002165
Paul Bakkerb3869132013-02-28 17:21:01 +01002166 p += slen;
2167
Simon Butcher02037452016-03-01 21:19:12 +00002168 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002169 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (ret != 0) {
2171 return ret;
2172 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002173
Simon Butcher02037452016-03-01 21:19:12 +00002174 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002176 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002178
Simon Butcher02037452016-03-01 21:19:12 +00002179 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002180 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (ret != 0) {
2182 return ret;
2183 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2186 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01002187
2188 p += hlen;
2189 *p++ = 0xBC;
2190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002192}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002193
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002194static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2195 int (*f_rng)(void *, unsigned char *, size_t),
2196 void *p_rng,
2197 mbedtls_md_type_t md_alg,
2198 unsigned int hashlen,
2199 const unsigned char *hash,
2200 int saltlen,
2201 unsigned char *sig)
2202{
2203 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2204 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2205 }
2206 if (ctx->hash_id == MBEDTLS_MD_NONE) {
2207 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2208 }
2209 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
2210 sig);
2211}
2212
2213int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2214 int (*f_rng)(void *, unsigned char *, size_t),
2215 void *p_rng,
2216 mbedtls_md_type_t md_alg,
2217 unsigned int hashlen,
2218 const unsigned char *hash,
2219 unsigned char *sig)
2220{
2221 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
2222 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2223}
2224
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002225/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02002226 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
2227 * the option to pass in the salt length.
2228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002229int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
2230 int (*f_rng)(void *, unsigned char *, size_t),
2231 void *p_rng,
2232 mbedtls_md_type_t md_alg,
2233 unsigned int hashlen,
2234 const unsigned char *hash,
2235 int saltlen,
2236 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02002237{
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2239 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02002240}
2241
Cédric Meuterf3fab332020-04-25 11:30:45 +02002242/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002243 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
2244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002245int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2246 int (*f_rng)(void *, unsigned char *, size_t),
2247 void *p_rng,
2248 mbedtls_md_type_t md_alg,
2249 unsigned int hashlen,
2250 const unsigned char *hash,
2251 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002252{
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2254 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002259/*
2260 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
2261 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002262
2263/* Construct a PKCS v1.5 encoding of a hashed message
2264 *
2265 * This is used both for signature generation and verification.
2266 *
2267 * Parameters:
2268 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002269 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002270 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002271 * - hash: Buffer containing the hashed message or the raw data.
2272 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002273 * - dst: Buffer to hold the encoded message.
2274 *
2275 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002276 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002277 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002278 *
2279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002280static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
2281 unsigned int hashlen,
2282 const unsigned char *hash,
2283 size_t dst_len,
2284 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01002285{
2286 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002287 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002288 unsigned char *p = dst;
2289 const char *oid = NULL;
2290
2291 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002293 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (md_size == 0) {
2295 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2296 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2299 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2300 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (hashlen != md_size) {
2303 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2304 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002305
2306 /* Double-check that 8 + hashlen + oid_size can be used as a
2307 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01002309 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 10 + hashlen + oid_size < 10 + hashlen) {
2311 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2312 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002313
2314 /*
2315 * Static bounds check:
2316 * - Need 10 bytes for five tag-length pairs.
2317 * (Insist on 1-byte length encodings to protect against variants of
2318 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2319 * - Need hashlen bytes for hash
2320 * - Need oid_size bytes for hash alg OID.
2321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if (nb_pad < 10 + hashlen + oid_size) {
2323 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2324 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002325 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 } else {
2327 if (nb_pad < hashlen) {
2328 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2329 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002330
2331 nb_pad -= hashlen;
2332 }
2333
Hanno Becker2b2f8982017-09-27 17:10:03 +01002334 /* Need space for signature header and padding delimiter (3 bytes),
2335 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (nb_pad < 3 + 8) {
2337 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2338 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002339 nb_pad -= 3;
2340
2341 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002342 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002343
2344 /* Write signature header and padding */
2345 *p++ = 0;
2346 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002348 p += nb_pad;
2349 *p++ = 0;
2350
2351 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 if (md_alg == MBEDTLS_MD_NONE) {
2353 memcpy(p, hash, hashlen);
2354 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002355 }
2356
2357 /* Signing hashed data, add corresponding ASN.1 structure
2358 *
2359 * DigestInfo ::= SEQUENCE {
2360 * digestAlgorithm DigestAlgorithmIdentifier,
2361 * digest Digest }
2362 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2363 * Digest ::= OCTET STRING
2364 *
2365 * Schematic:
2366 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2367 * TAG-NULL + LEN [ NULL ] ]
2368 * TAG-OCTET + LEN [ HASH ] ]
2369 */
2370 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002372 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002374 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002375 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002377 p += oid_size;
2378 *p++ = MBEDTLS_ASN1_NULL;
2379 *p++ = 0x00;
2380 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002381 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002383 p += hashlen;
2384
2385 /* Just a sanity-check, should be automatic
2386 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 if (p != dst + dst_len) {
2388 mbedtls_platform_zeroize(dst, dst_len);
2389 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002390 }
2391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002393}
2394
Paul Bakkerb3869132013-02-28 17:21:01 +01002395/*
2396 * Do an RSA operation to sign the message digest
2397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002398int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2399 int (*f_rng)(void *, unsigned char *, size_t),
2400 void *p_rng,
2401 mbedtls_md_type_t md_alg,
2402 unsigned int hashlen,
2403 const unsigned char *hash,
2404 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002405{
Janos Follath24eed8d2019-11-22 13:21:35 +00002406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002407 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002410 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2414 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2415 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002416
Hanno Beckerfdf38032017-09-06 12:35:55 +01002417 /*
2418 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2419 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2422 ctx->len, sig)) != 0) {
2423 return ret;
2424 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002425
Hanno Beckerfdf38032017-09-06 12:35:55 +01002426 /* Private key operation
2427 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002428 * In order to prevent Lenstra's attack, make the signature in a
2429 * temporary buffer and check it before returning it.
2430 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 sig_try = mbedtls_calloc(1, ctx->len);
2433 if (sig_try == NULL) {
2434 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002435 }
2436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 verif = mbedtls_calloc(1, ctx->len);
2438 if (verif == NULL) {
2439 mbedtls_free(sig_try);
2440 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2441 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2444 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2445
2446 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002447 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2448 goto cleanup;
2449 }
2450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002452
2453cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002454 mbedtls_zeroize_and_free(sig_try, ctx->len);
2455 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 if (ret != 0) {
2458 memset(sig, '!', ctx->len);
2459 }
2460 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002463
2464/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 * Do an RSA operation to sign the message digest
2466 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002467int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2468 int (*f_rng)(void *, unsigned char *, size_t),
2469 void *p_rng,
2470 mbedtls_md_type_t md_alg,
2471 unsigned int hashlen,
2472 const unsigned char *hash,
2473 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002474{
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002476 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480#if defined(MBEDTLS_PKCS1_V15)
2481 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2483 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002484#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#if defined(MBEDTLS_PKCS1_V21)
2487 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2489 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002490#endif
2491
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002495}
2496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002498/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002499 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002501int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2502 mbedtls_md_type_t md_alg,
2503 unsigned int hashlen,
2504 const unsigned char *hash,
2505 mbedtls_md_type_t mgf1_hash_id,
2506 int expected_salt_len,
2507 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002508{
Janos Follath24eed8d2019-11-22 13:21:35 +00002509 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002510 size_t siglen;
2511 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002512 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002513 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002514 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002515 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002519 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002521
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 siglen = ctx->len;
2523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (siglen < 16 || siglen > sizeof(buf)) {
2525 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2526 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (ret != 0) {
2531 return ret;
2532 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
2534 p = buf;
2535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (buf[siglen - 1] != 0xBC) {
2537 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002538 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (md_alg != MBEDTLS_MD_NONE) {
2541 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002542 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 if (exp_hashlen == 0) {
2544 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2545 }
2546
2547 if (hashlen != exp_hashlen) {
2548 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2549 }
2550 }
2551
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002552 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (hlen == 0) {
2554 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2555 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002556
Simon Butcher02037452016-03-01 21:19:12 +00002557 /*
2558 * Note: EMSA-PSS verification is over the length of N - 1 bits
2559 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (buf[0] >> (8 - siglen * 8 + msb)) {
2563 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2564 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002565
Simon Butcher02037452016-03-01 21:19:12 +00002566 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002568 p++;
2569 siglen -= 1;
2570 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (siglen < hlen + 2) {
2573 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2574 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002575 hash_start = p + siglen - hlen - 1;
2576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2578 if (ret != 0) {
2579 return ret;
2580 }
Paul Bakker02303e82013-01-03 11:08:31 +01002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002585 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (*p++ != 0x01) {
2589 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2590 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002591
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002592 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2595 observed_salt_len != (size_t) expected_salt_len) {
2596 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002597 }
2598
Simon Butcher02037452016-03-01 21:19:12 +00002599 /*
2600 * Generate H = Hash( M' )
2601 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2603 result, mgf1_hash_id);
2604 if (ret != 0) {
2605 return ret;
2606 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 if (memcmp(hash_start, result, hlen) != 0) {
2609 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2610 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002613}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002614
2615/*
2616 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2617 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002618int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2619 mbedtls_md_type_t md_alg,
2620 unsigned int hashlen,
2621 const unsigned char *hash,
2622 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002623{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002624 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002626 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002631 : md_alg;
2632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2634 md_alg, hashlen, hash,
2635 mgf1_hash_id,
2636 MBEDTLS_RSA_SALT_LEN_ANY,
2637 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002638
2639}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002643/*
2644 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2645 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2647 mbedtls_md_type_t md_alg,
2648 unsigned int hashlen,
2649 const unsigned char *hash,
2650 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002651{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002652 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002653 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002654 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002657 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002659
2660 sig_len = ctx->len;
2661
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002662 /*
2663 * Prepare expected PKCS1 v1.5 encoding of hash.
2664 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2667 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002668 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2669 goto cleanup;
2670 }
2671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2673 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002674 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002676
2677 /*
2678 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2679 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 ret = mbedtls_rsa_public(ctx, sig, encoded);
2682 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002683 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002685
Simon Butcher02037452016-03-01 21:19:12 +00002686 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002687 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002688 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2691 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002692 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2693 goto cleanup;
2694 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002695
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002696cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002699 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002700 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002703 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002704 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
2710/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002711 * Do an RSA operation and check the message digest
2712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002713int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2714 mbedtls_md_type_t md_alg,
2715 unsigned int hashlen,
2716 const unsigned char *hash,
2717 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002718{
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002720 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724#if defined(MBEDTLS_PKCS1_V15)
2725 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2727 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002728#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730#if defined(MBEDTLS_PKCS1_V21)
2731 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2733 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002734#endif
2735
2736 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002738 }
2739}
2740
2741/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002742 * Copy the components of an RSA key
2743 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002744int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002745{
Janos Follath24eed8d2019-11-22 13:21:35 +00002746 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002747
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002748 dst->len = src->len;
2749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2751 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2754 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2755 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002756
2757#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2759 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2760 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2761 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2762 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002763#endif
2764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2768 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002769
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002770 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002771 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002772
2773cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 if (ret != 0) {
2775 mbedtls_rsa_free(dst);
2776 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002779}
2780
2781/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002782 * Free the components of an RSA key
2783 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002784void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002785{
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002787 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 mbedtls_mpi_free(&ctx->Vi);
2791 mbedtls_mpi_free(&ctx->Vf);
2792 mbedtls_mpi_free(&ctx->RN);
2793 mbedtls_mpi_free(&ctx->D);
2794 mbedtls_mpi_free(&ctx->Q);
2795 mbedtls_mpi_free(&ctx->P);
2796 mbedtls_mpi_free(&ctx->E);
2797 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002798
Hanno Becker33c30a02017-08-23 07:00:22 +01002799#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 mbedtls_mpi_free(&ctx->RQ);
2801 mbedtls_mpi_free(&ctx->RP);
2802 mbedtls_mpi_free(&ctx->QP);
2803 mbedtls_mpi_free(&ctx->DQ);
2804 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002805#endif /* MBEDTLS_RSA_NO_CRT */
2806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002808 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 if (ctx->ver != 0) {
2810 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002811 ctx->ver = 0;
2812 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002813#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002814}
2815
Hanno Beckerab377312017-08-23 16:24:51 +01002816#endif /* !MBEDTLS_RSA_ALT */
2817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
2821/*
2822 * Example RSA-1024 keypair, for test purposes
2823 */
2824#define KEY_LEN 128
2825
2826#define RSA_N "9292758453063D803DD603D5E777D788" \
2827 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2828 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2829 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2830 "93A89813FBF3C4F8066D2D800F7C38A8" \
2831 "1AE31942917403FF4946B0A83D3D3E05" \
2832 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2833 "5E94BB77B07507233A0BC7BAC8F90F79"
2834
2835#define RSA_E "10001"
2836
2837#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2838 "66CA472BC44D253102F8B4A9D3BFA750" \
2839 "91386C0077937FE33FA3252D28855837" \
2840 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2841 "DF79C5CE07EE72C7F123142198164234" \
2842 "CABB724CF78B8173B9F880FC86322407" \
2843 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2844 "071513A1E85B5DFA031F21ECAE91A34D"
2845
2846#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2847 "2C01CAD19EA484A87EA4377637E75500" \
2848 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2849 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2850
2851#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2852 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2853 "910E4168387E3C30AA1E00C339A79508" \
2854 "8452DD96A9A5EA5D9DCA68DA636032AF"
2855
Paul Bakker5121ce52009-01-03 21:22:43 +00002856#define PT_LEN 24
2857#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2858 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002861static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002862{
gufe44c2620da2020-08-03 17:56:50 +02002863#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002864 size_t i;
2865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002867 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 }
Paul Bakker545570e2010-07-18 09:00:25 +00002869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002871 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002873#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002875 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002879#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002880
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002882}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002884
Paul Bakker5121ce52009-01-03 21:22:43 +00002885/*
2886 * Checkup routine
2887 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002888int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002889{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002890 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002892 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002894 unsigned char rsa_plaintext[PT_LEN];
2895 unsigned char rsa_decrypted[PT_LEN];
2896 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002897#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002898 unsigned char sha1sum[20];
2899#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002900
Hanno Becker3a701162017-08-22 13:52:43 +01002901 mbedtls_mpi K;
2902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 mbedtls_mpi_init(&K);
2904 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2907 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2908 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2909 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2910 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2911 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2912 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2913 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2914 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2915 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 if (verbose != 0) {
2920 mbedtls_printf(" RSA key validation: ");
2921 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2924 mbedtls_rsa_check_privkey(&rsa) != 0) {
2925 if (verbose != 0) {
2926 mbedtls_printf("failed\n");
2927 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
Hanno Becker5bc87292017-05-03 15:09:31 +01002929 ret = 1;
2930 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 }
2932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 if (verbose != 0) {
2934 mbedtls_printf("passed\n PKCS#1 encryption : ");
2935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2940 PT_LEN, rsa_plaintext,
2941 rsa_ciphertext) != 0) {
2942 if (verbose != 0) {
2943 mbedtls_printf("failed\n");
2944 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
Hanno Becker5bc87292017-05-03 15:09:31 +01002946 ret = 1;
2947 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002948 }
2949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 if (verbose != 0) {
2951 mbedtls_printf("passed\n PKCS#1 decryption : ");
2952 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2955 &len, rsa_ciphertext, rsa_decrypted,
2956 sizeof(rsa_decrypted)) != 0) {
2957 if (verbose != 0) {
2958 mbedtls_printf("failed\n");
2959 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
Hanno Becker5bc87292017-05-03 15:09:31 +01002961 ret = 1;
2962 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002963 }
2964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2966 if (verbose != 0) {
2967 mbedtls_printf("failed\n");
2968 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
Hanno Becker5bc87292017-05-03 15:09:31 +01002970 ret = 1;
2971 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002972 }
2973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 if (verbose != 0) {
2975 mbedtls_printf("passed\n");
2976 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002977
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002978#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 if (verbose != 0) {
2980 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002981 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002982
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002983 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2984 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 if (verbose != 0) {
2986 mbedtls_printf("failed\n");
2987 }
2988
2989 return 1;
2990 }
2991
2992 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2993 MBEDTLS_MD_SHA1, 20,
2994 sha1sum, rsa_ciphertext) != 0) {
2995 if (verbose != 0) {
2996 mbedtls_printf("failed\n");
2997 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002998
Hanno Becker5bc87292017-05-03 15:09:31 +01002999 ret = 1;
3000 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 }
3002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 if (verbose != 0) {
3004 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
3005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
3008 sha1sum, rsa_ciphertext) != 0) {
3009 if (verbose != 0) {
3010 mbedtls_printf("failed\n");
3011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003012
Hanno Becker5bc87292017-05-03 15:09:31 +01003013 ret = 1;
3014 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003015 }
3016
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 if (verbose != 0) {
3018 mbedtls_printf("passed\n");
3019 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003020#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 if (verbose != 0) {
3023 mbedtls_printf("\n");
3024 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003025
Paul Bakker3d8fb632014-04-17 12:42:41 +02003026cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 mbedtls_mpi_free(&K);
3028 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02003030 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003033}
3034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037#endif /* MBEDTLS_RSA_C */