blob: c8ea980e077d939f7ac9900222342956b2d58851 [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
Valerio Setti447bbce2024-02-07 08:02:03 +0100110 /* mbedtls_asn1_get_tag() already ensures that len is valid (i.e. p+len <= end)*/
Valerio Setti44ff9502024-02-01 16:51:05 +0100111 end = p + len;
112
113 if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
114 return ret;
115 }
116
117 if (version != 0) {
118 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
119 }
120
121 /* Import N */
122 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
123 (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL,
124 NULL, NULL)) != 0) {
125 goto cleanup;
126 }
127
128 /* Import E */
129 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
130 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
131 NULL, &T)) != 0) {
132 goto cleanup;
133 }
134
135 /* Import D */
136 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
137 (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL,
138 &T, NULL)) != 0) {
139 goto cleanup;
140 }
141
142 /* Import P */
143 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
144 (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL,
145 NULL, NULL)) != 0) {
146 goto cleanup;
147 }
148
149 /* Import Q */
150 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
151 (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T,
152 NULL, NULL)) != 0) {
153 goto cleanup;
154 }
155
156#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
157 /*
158 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
159 * that they can be easily recomputed from D, P and Q. However by
160 * parsing them from the PKCS1 structure it is possible to avoid
161 * recalculating them which both reduces the overhead of loading
162 * RSA private keys into memory and also avoids side channels which
163 * can arise when computing those values, since all of D, P, and Q
164 * are secret. See https://eprint.iacr.org/2020/055 for a
165 * description of one such attack.
166 */
167
168 /* Import DP */
169 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
170 (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) {
171 goto cleanup;
172 }
173
174 /* Import DQ */
175 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
176 (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) {
177 goto cleanup;
178 }
179
180 /* Import QP */
181 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
182 (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) {
183 goto cleanup;
184 }
185
186#else
187 /* Verify existence of the CRT params */
188 if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
189 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
190 (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
191 goto cleanup;
192 }
193#endif
194
195 /* rsa_complete() doesn't complete anything with the default
196 * implementation but is still called:
197 * - for the benefit of alternative implementation that may want to
198 * pre-compute stuff beyond what's provided (eg Montgomery factors)
199 * - as is also sanity-checks the key
200 *
201 * Furthermore, we also check the public part for consistency with
202 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
203 */
204 if ((ret = mbedtls_rsa_complete(rsa)) != 0 ||
205 (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) {
206 goto cleanup;
207 }
208
209 if (p != end) {
210 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
211 }
212
213cleanup:
214
215 mbedtls_mpi_free(&T);
216
217 if (ret != 0) {
218 mbedtls_rsa_free(rsa);
219 }
220
221 return ret;
222}
223
Valerio Setti201e6432024-02-01 17:19:37 +0100224int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
Valerio Setti44ff9502024-02-01 16:51:05 +0100225{
Valerio Setti201e6432024-02-01 17:19:37 +0100226 unsigned char *p = (unsigned char *) key;
227 unsigned char *end = (unsigned char *) (key + keylen);
Valerio Setti44ff9502024-02-01 16:51:05 +0100228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
229 size_t len;
230
231 /*
232 * RSAPublicKey ::= SEQUENCE {
233 * modulus INTEGER, -- n
234 * publicExponent INTEGER -- e
235 * }
236 */
237
Valerio Setti201e6432024-02-01 17:19:37 +0100238 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
Valerio Setti44ff9502024-02-01 16:51:05 +0100239 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
240 return ret;
241 }
242
Valerio Setti447bbce2024-02-07 08:02:03 +0100243 /* mbedtls_asn1_get_tag() already ensures that len is valid (i.e. p+len <= end)*/
Valerio Settife329ce2024-02-06 08:00:18 +0100244 end = p + len;
245
Valerio Setti44ff9502024-02-01 16:51:05 +0100246 /* Import N */
Valerio Setti201e6432024-02-01 17:19:37 +0100247 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100248 return ret;
249 }
250
Valerio Setti201e6432024-02-01 17:19:37 +0100251 if ((ret = mbedtls_rsa_import_raw(rsa, p, len, NULL, 0, NULL, 0,
Valerio Setti44ff9502024-02-01 16:51:05 +0100252 NULL, 0, NULL, 0)) != 0) {
253 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
254 }
255
Valerio Setti201e6432024-02-01 17:19:37 +0100256 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100257
258 /* Import E */
Valerio Setti201e6432024-02-01 17:19:37 +0100259 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100260 return ret;
261 }
262
263 if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
Valerio Setti201e6432024-02-01 17:19:37 +0100264 NULL, 0, p, len)) != 0) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100265 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
266 }
267
Valerio Setti201e6432024-02-01 17:19:37 +0100268 p += len;
Valerio Setti44ff9502024-02-01 16:51:05 +0100269
270 if (mbedtls_rsa_complete(rsa) != 0 ||
271 mbedtls_rsa_check_pubkey(rsa) != 0) {
272 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
273 }
274
Valerio Setti201e6432024-02-01 17:19:37 +0100275 if (p != end) {
Valerio Setti44ff9502024-02-01 16:51:05 +0100276 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
277 }
278
279 return 0;
280}
281
Valerio Setti135ebde2024-02-01 17:00:29 +0100282int mbedtls_rsa_write_key(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100283 unsigned char **p)
284{
285 size_t len = 0;
286 int ret;
287
288 mbedtls_mpi T; /* Temporary holding the exported parameters */
289
290 /*
291 * Export the parameters one after another to avoid simultaneous copies.
292 */
293
294 mbedtls_mpi_init(&T);
295
296 /* Export QP */
297 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
298 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
299 goto end_of_export;
300 }
301 len += ret;
302
303 /* Export DQ */
304 if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
305 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
306 goto end_of_export;
307 }
308 len += ret;
309
310 /* Export DP */
311 if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
312 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
313 goto end_of_export;
314 }
315 len += ret;
316
317 /* Export Q */
318 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, &T, NULL, NULL)) != 0 ||
319 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
320 goto end_of_export;
321 }
322 len += ret;
323
324 /* Export P */
325 if ((ret = mbedtls_rsa_export(rsa, NULL, &T, NULL, NULL, NULL)) != 0 ||
326 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
327 goto end_of_export;
328 }
329 len += ret;
330
331 /* Export D */
332 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, &T, NULL)) != 0 ||
333 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
334 goto end_of_export;
335 }
336 len += ret;
337
338 /* Export E */
339 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
340 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
341 goto end_of_export;
342 }
343 len += ret;
344
345 /* Export N */
346 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
347 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
348 goto end_of_export;
349 }
350 len += ret;
351
352end_of_export:
353
354 mbedtls_mpi_free(&T);
355 if (ret < 0) {
356 return ret;
357 }
358
359 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
360 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
361 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
362 MBEDTLS_ASN1_CONSTRUCTED |
363 MBEDTLS_ASN1_SEQUENCE));
364
365 return (int) len;
366}
367
368/*
369 * RSAPublicKey ::= SEQUENCE {
370 * modulus INTEGER, -- n
371 * publicExponent INTEGER -- e
372 * }
373 */
Valerio Setti135ebde2024-02-01 17:00:29 +0100374int mbedtls_rsa_write_pubkey(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Setti44ff9502024-02-01 16:51:05 +0100375 unsigned char **p)
376{
377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
378 size_t len = 0;
379 mbedtls_mpi T;
380
381 mbedtls_mpi_init(&T);
382
383 /* Export E */
384 if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
385 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
386 goto end_of_export;
387 }
388 len += ret;
389
390 /* Export N */
391 if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
392 (ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0) {
393 goto end_of_export;
394 }
395 len += ret;
396
397end_of_export:
398
399 mbedtls_mpi_free(&T);
400 if (ret < 0) {
401 return ret;
402 }
403
404 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
405 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
406 MBEDTLS_ASN1_SEQUENCE));
407
408 return (int) len;
409}
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100410
411#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
412
413/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
414 * operation (EME-PKCS1-v1_5 decoding).
415 *
416 * \note The return value from this function is a sensitive value
417 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
418 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
419 * is often a situation that an attacker can provoke and leaking which
420 * one is the result is precisely the information the attacker wants.
421 *
422 * \param input The input buffer which is the payload inside PKCS#1v1.5
423 * encryption padding, called the "encoded message EM"
424 * by the terminology.
425 * \param ilen The length of the payload in the \p input buffer.
426 * \param output The buffer for the payload, called "message M" by the
427 * PKCS#1 terminology. This must be a writable buffer of
428 * length \p output_max_len bytes.
429 * \param olen The address at which to store the length of
430 * the payload. This must not be \c NULL.
431 * \param output_max_len The length in bytes of the output buffer \p output.
432 *
433 * \return \c 0 on success.
434 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
435 * The output buffer is too small for the unpadded payload.
436 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
437 * The input doesn't contain properly formatted padding.
438 */
439static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
440 size_t ilen,
441 unsigned char *output,
442 size_t output_max_len,
443 size_t *olen)
444{
445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
446 size_t i, plaintext_max_size;
447
448 /* The following variables take sensitive values: their value must
449 * not leak into the observable behavior of the function other than
450 * the designated outputs (output, olen, return value). Otherwise
451 * this would open the execution of the function to
452 * side-channel-based variants of the Bleichenbacher padding oracle
453 * attack. Potential side channels include overall timing, memory
454 * access patterns (especially visible to an adversary who has access
455 * to a shared memory cache), and branches (especially visible to
456 * an adversary who has access to a shared code cache or to a shared
457 * branch predictor). */
458 size_t pad_count = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100459 mbedtls_ct_condition_t bad;
460 mbedtls_ct_condition_t pad_done;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100461 size_t plaintext_size = 0;
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100462 mbedtls_ct_condition_t output_too_large;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100463
464 plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
465 : output_max_len;
466
467 /* Check and get padding length in constant time and constant
468 * memory trace. The first byte must be 0. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100469 bad = mbedtls_ct_bool(input[0]);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100470
471
472 /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
473 * where PS must be at least 8 nonzero bytes. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100474 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100475
476 /* Read the whole buffer. Set pad_done to nonzero if we find
477 * the 0x00 byte and remember the padding length in pad_count. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100478 pad_done = MBEDTLS_CT_FALSE;
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100479 for (i = 2; i < ilen; i++) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100480 mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100481 pad_done = mbedtls_ct_bool_or(pad_done, found);
Dave Rodgman98ddc012023-08-10 12:11:31 +0100482 pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100483 }
484
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100485 /* If pad_done is still zero, there's no data, only unfinished padding. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100486 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100487
488 /* There must be at least 8 bytes of padding. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100489 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100490
491 /* If the padding is valid, set plaintext_size to the number of
492 * remaining bytes after stripping the padding. If the padding
493 * is invalid, avoid leaking this fact through the size of the
494 * output: use the maximum message size that fits in the output
495 * buffer. Do it without branches to avoid leaking the padding
496 * validity through timing. RSA keys are small enough that all the
497 * size_t values involved fit in unsigned int. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100498 plaintext_size = mbedtls_ct_uint_if(
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100499 bad, (unsigned) plaintext_max_size,
500 (unsigned) (ilen - pad_count - 3));
501
502 /* Set output_too_large to 0 if the plaintext fits in the output
503 * buffer and to 1 otherwise. */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100504 output_too_large = mbedtls_ct_uint_gt(plaintext_size,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100505 plaintext_max_size);
506
507 /* Set ret without branches to avoid timing attacks. Return:
508 * - INVALID_PADDING if the padding is bad (bad != 0).
509 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
510 * plaintext does not fit in the output buffer.
511 * - 0 if the padding is correct. */
Dave Rodgmand03f4832023-09-22 09:52:15 +0100512 ret = mbedtls_ct_error_if(
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100513 bad,
Dave Rodgmand03f4832023-09-22 09:52:15 +0100514 MBEDTLS_ERR_RSA_INVALID_PADDING,
515 mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100516 );
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100517
518 /* If the padding is bad or the plaintext is too large, zero the
519 * data that we're about to copy to the output buffer.
520 * We need to copy the same amount of data
521 * from the same buffer whether the padding is good or not to
522 * avoid leaking the padding validity through overall timing or
523 * through memory or cache access patterns. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100524 mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100525
526 /* If the plaintext is too large, truncate it to the buffer size.
527 * Copy anyway to avoid revealing the length through timing, because
528 * revealing the length is as bad as revealing the padding validity
529 * for a Bleichenbacher attack. */
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100530 plaintext_size = mbedtls_ct_uint_if(output_too_large,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100531 (unsigned) plaintext_max_size,
532 (unsigned) plaintext_size);
533
534 /* Move the plaintext to the leftmost position where it can start in
535 * the working buffer, i.e. make it start plaintext_max_size from
536 * the end of the buffer. Do this with a memory access trace that
537 * does not depend on the plaintext size. After this move, the
538 * starting location of the plaintext is no longer sensitive
539 * information. */
Dave Rodgman9f9c3b82023-05-17 12:28:51 +0100540 mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
541 plaintext_max_size,
542 plaintext_max_size - plaintext_size);
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100543
544 /* Finally copy the decrypted plaintext plus trailing zeros into the output
545 * buffer. If output_max_len is 0, then output may be an invalid pointer
546 * and the result of memcpy() would be undefined; prevent undefined
547 * behavior making sure to depend only on output_max_len (the size of the
548 * user-provided output buffer), which is independent from plaintext
549 * length, validity of padding, success of the decryption, and other
550 * secrets. */
551 if (output_max_len != 0) {
552 memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
553 }
554
555 /* Report the amount of data we copied to the output buffer. In case
556 * of errors (bad padding or output too large), the value of *olen
557 * when this function returns is not specified. Making it equivalent
558 * to the good case limits the risks of leaking the padding validity. */
559 *olen = plaintext_size;
560
561 return ret;
562}
563
564#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
565
Hanno Beckera565f542017-10-11 11:00:19 +0100566#if !defined(MBEDTLS_RSA_ALT)
567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
569 const mbedtls_mpi *N,
570 const mbedtls_mpi *P, const mbedtls_mpi *Q,
571 const mbedtls_mpi *D, const mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100572{
Janos Follath24eed8d2019-11-22 13:21:35 +0000573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
576 (P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
577 (Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
578 (D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
579 (E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
580 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100581 }
582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (N != NULL) {
584 ctx->len = mbedtls_mpi_size(&ctx->N);
585 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100588}
589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
591 unsigned char const *N, size_t N_len,
592 unsigned char const *P, size_t P_len,
593 unsigned char const *Q, size_t Q_len,
594 unsigned char const *D, size_t D_len,
595 unsigned char const *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100596{
Hanno Beckerd4d60572018-01-10 07:12:01 +0000597 int ret = 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if (N != NULL) {
600 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->N, N, N_len));
601 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100602 }
603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (P != NULL) {
605 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->P, P, P_len));
606 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (Q != NULL) {
609 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->Q, Q, Q_len));
610 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (D != NULL) {
613 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->D, D, D_len));
614 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (E != NULL) {
617 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->E, E, E_len));
618 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100619
620cleanup:
621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (ret != 0) {
623 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
624 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100627}
628
Hanno Becker705fc682017-10-10 17:57:02 +0100629/*
630 * Checks whether the context fields are set in such a way
631 * that the RSA primitives will be able to execute without error.
632 * It does *not* make guarantees for consistency of the parameters.
633 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
635 int blinding_needed)
Hanno Becker705fc682017-10-10 17:57:02 +0100636{
Hanno Beckerebd2c022017-10-12 10:54:53 +0100637#if !defined(MBEDTLS_RSA_NO_CRT)
638 /* blinding_needed is only used for NO_CRT to decide whether
639 * P,Q need to be present or not. */
640 ((void) blinding_needed);
641#endif
642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
644 ctx->len > MBEDTLS_MPI_MAX_SIZE) {
645 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker3a760a12018-01-05 08:14:49 +0000646 }
Hanno Becker705fc682017-10-10 17:57:02 +0100647
648 /*
649 * 1. Modular exponentiation needs positive, odd moduli.
650 */
651
652 /* Modular exponentiation wrt. N is always used for
653 * RSA public key operations. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
655 mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
656 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100657 }
658
659#if !defined(MBEDTLS_RSA_NO_CRT)
660 /* Modular exponentiation for P and Q is only
661 * used for private key operations and if CRT
662 * is used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (is_priv &&
664 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
665 mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
666 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
667 mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
668 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100669 }
670#endif /* !MBEDTLS_RSA_NO_CRT */
671
672 /*
673 * 2. Exponents must be positive
674 */
675
676 /* Always need E for public key operations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
678 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
679 }
Hanno Becker705fc682017-10-10 17:57:02 +0100680
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100681#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker705fc682017-10-10 17:57:02 +0100682 /* For private key operations, use D or DP & DQ
683 * as (unblinded) exponents. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
685 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
686 }
Hanno Becker705fc682017-10-10 17:57:02 +0100687#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (is_priv &&
689 (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
690 mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
691 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100692 }
693#endif /* MBEDTLS_RSA_NO_CRT */
694
695 /* Blinding shouldn't make exponents negative either,
696 * so check that P, Q >= 1 if that hasn't yet been
697 * done as part of 1. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100698#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (is_priv && blinding_needed &&
700 (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
701 mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
702 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100703 }
704#endif
705
706 /* It wouldn't lead to an error if it wasn't satisfied,
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100707 * but check for QP >= 1 nonetheless. */
Hanno Beckerb82a5b52017-10-11 19:10:23 +0100708#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (is_priv &&
710 mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
711 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Becker705fc682017-10-10 17:57:02 +0100712 }
713#endif
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 return 0;
Hanno Becker705fc682017-10-10 17:57:02 +0100716}
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100719{
720 int ret = 0;
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500721 int have_N, have_P, have_Q, have_D, have_E;
722#if !defined(MBEDTLS_RSA_NO_CRT)
723 int have_DP, have_DQ, have_QP;
724#endif
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500725 int n_missing, pq_missing, d_missing, is_pub, is_priv;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
728 have_P = (mbedtls_mpi_cmp_int(&ctx->P, 0) != 0);
729 have_Q = (mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0);
730 have_D = (mbedtls_mpi_cmp_int(&ctx->D, 0) != 0);
731 have_E = (mbedtls_mpi_cmp_int(&ctx->E, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500732
733#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 have_DP = (mbedtls_mpi_cmp_int(&ctx->DP, 0) != 0);
735 have_DQ = (mbedtls_mpi_cmp_int(&ctx->DQ, 0) != 0);
736 have_QP = (mbedtls_mpi_cmp_int(&ctx->QP, 0) != 0);
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500737#endif
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100738
Hanno Becker617c1ae2017-08-23 14:11:24 +0100739 /*
740 * Check whether provided parameters are enough
741 * to deduce all others. The following incomplete
742 * parameter sets for private keys are supported:
743 *
744 * (1) P, Q missing.
745 * (2) D and potentially N missing.
746 *
747 */
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100748
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500749 n_missing = have_P && have_Q && have_D && have_E;
750 pq_missing = have_N && !have_P && !have_Q && have_D && have_E;
751 d_missing = have_P && have_Q && !have_D && have_E;
752 is_pub = have_N && !have_P && !have_Q && !have_D && have_E;
Hanno Becker2cca6f32017-09-29 11:46:40 +0100753
754 /* These three alternatives are mutually exclusive */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500755 is_priv = n_missing || pq_missing || d_missing;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if (!is_priv && !is_pub) {
758 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
759 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100760
761 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100762 * Step 1: Deduce N if P, Q are provided.
763 */
764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (!have_N && have_P && have_Q) {
766 if ((ret = mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P,
767 &ctx->Q)) != 0) {
768 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100769 }
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Becker2cca6f32017-09-29 11:46:40 +0100772 }
773
774 /*
775 * Step 2: Deduce and verify all remaining core parameters.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100776 */
777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (pq_missing) {
779 ret = mbedtls_rsa_deduce_primes(&ctx->N, &ctx->E, &ctx->D,
780 &ctx->P, &ctx->Q);
781 if (ret != 0) {
782 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
783 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 } else if (d_missing) {
786 if ((ret = mbedtls_rsa_deduce_private_exponent(&ctx->P,
787 &ctx->Q,
788 &ctx->E,
789 &ctx->D)) != 0) {
790 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100791 }
792 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100793
Hanno Becker617c1ae2017-08-23 14:11:24 +0100794 /*
Hanno Becker2cca6f32017-09-29 11:46:40 +0100795 * Step 3: Deduce all additional parameters specific
Hanno Beckere8674892017-10-10 17:56:14 +0100796 * to our current RSA implementation.
Hanno Becker617c1ae2017-08-23 14:11:24 +0100797 */
798
Hanno Becker23344b52017-08-23 07:43:27 +0100799#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (is_priv && !(have_DP && have_DQ && have_QP)) {
801 ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
802 &ctx->DP, &ctx->DQ, &ctx->QP);
803 if (ret != 0) {
804 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
805 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100806 }
Hanno Becker23344b52017-08-23 07:43:27 +0100807#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker617c1ae2017-08-23 14:11:24 +0100808
809 /*
Hanno Becker705fc682017-10-10 17:57:02 +0100810 * Step 3: Basic sanity checks
Hanno Becker617c1ae2017-08-23 14:11:24 +0100811 */
812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 return rsa_check_context(ctx, is_priv, 1);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100814}
815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
817 unsigned char *N, size_t N_len,
818 unsigned char *P, size_t P_len,
819 unsigned char *Q, size_t Q_len,
820 unsigned char *D, size_t D_len,
821 unsigned char *E, size_t E_len)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100822{
823 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500824 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100825
826 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500827 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
829 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
830 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
831 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
832 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100835 /* If we're trying to export private parameters for a public key,
836 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if (P != NULL || Q != NULL || D != NULL) {
838 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
839 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100840
841 }
842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (N != NULL) {
844 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->N, N, N_len));
845 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (P != NULL) {
848 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->P, P, P_len));
849 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (Q != NULL) {
852 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->Q, Q, Q_len));
853 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (D != NULL) {
856 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->D, D, D_len));
857 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (E != NULL) {
860 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->E, E, E_len));
861 }
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100862
863cleanup:
864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 return ret;
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100866}
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
869 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
870 mbedtls_mpi *D, mbedtls_mpi *E)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100871{
Janos Follath24eed8d2019-11-22 13:21:35 +0000872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500873 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100874
875 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500876 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
878 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
879 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
880 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
881 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if (!is_priv) {
Hanno Becker617c1ae2017-08-23 14:11:24 +0100884 /* If we're trying to export private parameters for a public key,
885 * something must be wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (P != NULL || Q != NULL || D != NULL) {
887 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
888 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100889
890 }
891
892 /* Export all requested core parameters. */
893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
895 (P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
896 (Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
897 (D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
898 (E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
899 return ret;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100900 }
901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100903}
904
905/*
906 * Export CRT parameters
907 * This must also be implemented if CRT is not used, for being able to
908 * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt
909 * can be used in this case.
910 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100911int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
912 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100913{
Janos Follath24eed8d2019-11-22 13:21:35 +0000914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500915 int is_priv;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100916
917 /* Check if key is private or public */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500918 is_priv =
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 mbedtls_mpi_cmp_int(&ctx->N, 0) != 0 &&
920 mbedtls_mpi_cmp_int(&ctx->P, 0) != 0 &&
921 mbedtls_mpi_cmp_int(&ctx->Q, 0) != 0 &&
922 mbedtls_mpi_cmp_int(&ctx->D, 0) != 0 &&
923 mbedtls_mpi_cmp_int(&ctx->E, 0) != 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if (!is_priv) {
926 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
927 }
Hanno Becker617c1ae2017-08-23 14:11:24 +0100928
Hanno Beckerdc95c892017-08-23 06:57:02 +0100929#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker617c1ae2017-08-23 14:11:24 +0100930 /* Export all requested blinding parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
932 (DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
933 (QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
934 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Becker617c1ae2017-08-23 14:11:24 +0100935 }
Hanno Beckerdc95c892017-08-23 06:57:02 +0100936#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if ((ret = mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
938 DP, DQ, QP)) != 0) {
939 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_BAD_INPUT_DATA, ret);
Hanno Beckerdc95c892017-08-23 06:57:02 +0100940 }
941#endif
Hanno Becker617c1ae2017-08-23 14:11:24 +0100942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 return 0;
Hanno Becker617c1ae2017-08-23 14:11:24 +0100944}
Hanno Beckere2e8b8d2017-08-23 14:06:45 +0100945
Paul Bakker5121ce52009-01-03 21:22:43 +0000946/*
947 * Initialize an RSA context
948 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000950{
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 memset(ctx, 0, sizeof(mbedtls_rsa_context));
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
Ronald Cronc1905a12021-06-05 11:11:14 +0200953 ctx->padding = MBEDTLS_RSA_PKCS_V15;
954 ctx->hash_id = MBEDTLS_MD_NONE;
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +0100957 /* Set ctx->ver to nonzero to indicate that the mutex has been
958 * initialized and will need to be freed. */
959 ctx->ver = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 mbedtls_mutex_init(&ctx->mutex);
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200961#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000962}
963
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100964/*
965 * Set padding for an existing RSA context
966 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100967int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
968 mbedtls_md_type_t hash_id)
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100969{
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 switch (padding) {
Ronald Cron3a0375f2021-06-08 10:22:28 +0200971#if defined(MBEDTLS_PKCS1_V15)
972 case MBEDTLS_RSA_PKCS_V15:
973 break;
974#endif
975
976#if defined(MBEDTLS_PKCS1_V21)
977 case MBEDTLS_RSA_PKCS_V21:
978 break;
979#endif
980 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Ronald Cron3a0375f2021-06-08 10:22:28 +0200982 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200983
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200984#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if ((padding == MBEDTLS_RSA_PKCS_V21) &&
986 (hash_id != MBEDTLS_MD_NONE)) {
Manuel Pégourié-Gonnardfaa3b4e2022-07-15 13:18:15 +0200987 /* Just make sure this hash is supported in this build. */
Manuel Pégourié-Gonnard28f504e2023-03-30 09:42:10 +0200988 if (mbedtls_md_info_from_type(hash_id) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 return MBEDTLS_ERR_RSA_INVALID_PADDING;
990 }
Ronald Cronea7631b2021-06-03 18:51:59 +0200991 }
Manuel Pégourié-Gonnard3356b892022-07-05 10:25:06 +0200992#endif /* MBEDTLS_PKCS1_V21 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500993
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100994 ctx->padding = padding;
995 ctx->hash_id = hash_id;
Ronald Cronea7631b2021-06-03 18:51:59 +0200996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return 0;
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100998}
999
Hanno Becker617c1ae2017-08-23 14:11:24 +01001000/*
Yanray Wang83548b52023-03-15 16:46:34 +08001001 * Get padding mode of initialized RSA context
Yanray Wanga730df62023-03-01 10:18:19 +08001002 */
1003int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
1004{
Yanray Wang644b9012023-03-15 16:50:31 +08001005 return ctx->padding;
Yanray Wanga730df62023-03-01 10:18:19 +08001006}
1007
1008/*
Yanray Wang12cb3962023-03-01 10:20:02 +08001009 * Get hash identifier of mbedtls_md_type_t type
1010 */
Yanray Wangd41684e2023-03-17 18:54:22 +08001011int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
Yanray Wang12cb3962023-03-01 10:20:02 +08001012{
Yanray Wang644b9012023-03-15 16:50:31 +08001013 return ctx->hash_id;
Yanray Wang12cb3962023-03-01 10:20:02 +08001014}
1015
1016/*
Hanno Becker617c1ae2017-08-23 14:11:24 +01001017 * Get length in bytes of RSA modulus
1018 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
Hanno Becker617c1ae2017-08-23 14:11:24 +01001020{
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 return ctx->len;
Hanno Becker617c1ae2017-08-23 14:11:24 +01001022}
1023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
1026/*
1027 * Generate an RSA keypair
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001028 *
1029 * This generation method follows the RSA key pair generation procedure of
1030 * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001032int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
1033 int (*f_rng)(void *, unsigned char *, size_t),
1034 void *p_rng,
1035 unsigned int nbits, int exponent)
Paul Bakker5121ce52009-01-03 21:22:43 +00001036{
Janos Follath24eed8d2019-11-22 13:21:35 +00001037 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jethro Beekman97f95c92018-02-13 15:50:36 -08001038 mbedtls_mpi H, G, L;
Janos Follathb8fc1b02018-09-03 15:37:01 +01001039 int prime_quality = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Janos Follathb8fc1b02018-09-03 15:37:01 +01001041 /*
1042 * If the modulus is 1024 bit long or shorter, then the security strength of
1043 * the RSA algorithm is less than or equal to 80 bits and therefore an error
1044 * rate of 2^-80 is sufficient.
1045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (nbits > 1024) {
Janos Follathb8fc1b02018-09-03 15:37:01 +01001047 prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 }
Janos Follathb8fc1b02018-09-03 15:37:01 +01001049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 mbedtls_mpi_init(&H);
1051 mbedtls_mpi_init(&G);
1052 mbedtls_mpi_init(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001054 if (exponent < 3 || nbits % 2 != 0) {
Gilles Peskine5e40a7c2021-02-02 21:06:10 +01001055 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1056 goto cleanup;
1057 }
1058
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00001059 if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1061 goto cleanup;
1062 }
1063
1064 /*
1065 * find primes P and Q with Q < P so that:
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001066 * 1. |P-Q| > 2^( nbits / 2 - 100 )
1067 * 2. GCD( E, (P-1)*(Q-1) ) == 1
1068 * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
Paul Bakker5121ce52009-01-03 21:22:43 +00001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 do {
1073 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
1074 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
1077 prime_quality, f_rng, p_rng));
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001079 /* 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 +01001080 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
1081 if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001085 /* not required by any standards, but some users rely on the fact that P > Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (H.s < 0) {
1087 mbedtls_mpi_swap(&ctx->P, &ctx->Q);
1088 }
Janos Follathef441782016-09-21 13:18:12 +01001089
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001090 /* Temporarily replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->P, &ctx->P, 1));
1092 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&ctx->Q, &ctx->Q, 1));
1093 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &ctx->P, &ctx->Q));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001094
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001095 /* check GCD( E, (P-1)*(Q-1) ) == 1 (FIPS 186-4 §B.3.1 criterion 2(a)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->E, &H));
1097 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Jethro Beekman97f95c92018-02-13 15:50:36 -08001098 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001100
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001101 /* 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 +01001102 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, &ctx->P, &ctx->Q));
1103 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&L, NULL, &H, &G));
1104 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->D, &ctx->E, &L));
Jethro Beekman97f95c92018-02-13 15:50:36 -08001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 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 -08001107 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 }
Jethro Beekman97f95c92018-02-13 15:50:36 -08001109
1110 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001112
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001113 /* Restore P,Q */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->P, &ctx->P, 1));
1115 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&ctx->Q, &ctx->Q, 1));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
Jethro Beekmanc645bfe2018-02-14 19:27:13 -08001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 ctx->len = mbedtls_mpi_size(&ctx->N);
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001120
Jethro Beekman97f95c92018-02-13 15:50:36 -08001121#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +00001122 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 * DP = D mod (P - 1)
1124 * DQ = D mod (Q - 1)
1125 * QP = Q^-1 mod P
1126 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
1128 &ctx->DP, &ctx->DQ, &ctx->QP));
Hanno Beckerbee3aae2017-08-23 06:59:15 +01001129#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001130
Hanno Becker83aad1f2017-08-23 06:45:10 +01001131 /* Double-check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
1134cleanup:
1135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 mbedtls_mpi_free(&H);
1137 mbedtls_mpi_free(&G);
1138 mbedtls_mpi_free(&L);
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if (ret != 0) {
1141 mbedtls_rsa_free(ctx);
Chris Jones74392092021-04-01 16:00:01 +01001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if ((-ret & ~0x7f) == 0) {
1144 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
1145 }
1146 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 }
1148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001150}
1151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
1154/*
1155 * Check a public RSA key
1156 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001157int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001158{
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
1160 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001161 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
1164 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Becker98838b02017-10-02 13:16:10 +01001165 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
1168 mbedtls_mpi_bitlen(&ctx->E) < 2 ||
1169 mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
1170 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1171 }
1172
1173 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001174}
1175
1176/*
Hanno Becker705fc682017-10-10 17:57:02 +01001177 * Check for the consistency of all fields in an RSA private key context
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00001180{
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
1182 rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
1183 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 }
Paul Bakker48377d92013-08-30 12:06:24 +02001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
1187 &ctx->D, &ctx->E, NULL, NULL) != 0) {
1188 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001190
Hanno Beckerb269a852017-08-25 08:03:21 +01001191#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
1193 &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
1194 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Hanno Beckerb269a852017-08-25 08:03:21 +01001195 }
1196#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001199}
1200
1201/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001202 * Check if contexts holding a public and private key match
1203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001204int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
1205 const mbedtls_rsa_context *prv)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001206{
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (mbedtls_rsa_check_pubkey(pub) != 0 ||
1208 mbedtls_rsa_check_privkey(prv) != 0) {
1209 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001210 }
1211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
1213 mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
1214 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001215 }
1216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 return 0;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001218}
1219
1220/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 * Do an RSA public key operation
1222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001223int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
1224 const unsigned char *input,
1225 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001226{
Janos Follath24eed8d2019-11-22 13:21:35 +00001227 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001228 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
1232 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1233 }
Hanno Becker705fc682017-10-10 17:57:02 +01001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001236
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001237#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1239 return ret;
1240 }
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001241#endif
1242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001246 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1247 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 }
1249
1250 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
1252 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
1254cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1257 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1258 }
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +01001259#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 if (ret != 0) {
1264 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
1265 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001268}
1269
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001270/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001271 * Generate or update blinding values, see section 10 of:
1272 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +02001273 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001274 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001275 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001276static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1277 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001278{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001279 int ret, count = 0;
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001280 mbedtls_mpi R;
1281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 mbedtls_mpi_init(&R);
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (ctx->Vf.p != NULL) {
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001285 /* We already have blinding values, just update them by squaring */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1287 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1288 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1289 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001290
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001291 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +02001292 }
1293
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001294 /* Unblinding value: Vf = random number, invertible mod N */
1295 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (count++ > 10) {
Manuel Pégourié-Gonnarde288ec02020-07-16 09:23:30 +02001297 ret = MBEDTLS_ERR_RSA_RNG_FAILED;
1298 goto cleanup;
1299 }
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +02001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 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 +02001302
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001303 /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, ctx->len - 1, f_rng, p_rng));
1305 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vf, &R));
1306 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001307
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001308 /* At this point, Vi is invertible mod N if and only if both Vf and R
1309 * are invertible mod N. If one of them isn't, we don't need to know
1310 * which one, we just loop and choose new values for both of them.
1311 * (Each iteration succeeds with overwhelming probability.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 ret = mbedtls_mpi_inv_mod(&ctx->Vi, &ctx->Vi, &ctx->N);
1313 if (ret != 0 && ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001314 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 }
Manuel Pégourié-Gonnardb3e3d792020-06-26 11:03:19 +02001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 } while (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
Peter Kolbusca8b8e72020-09-24 11:11:50 -05001318
1319 /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &R));
1321 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001322
Manuel Pégourié-Gonnard78683962020-07-16 09:48:54 +02001323 /* Blinding value: Vi = Vf^(-e) mod N
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001324 * (Vi already contains Vf^-1 at this point) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 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 +02001326
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001327
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001328cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 mbedtls_mpi_free(&R);
Manuel Pégourié-Gonnard750d3c72020-06-26 11:19:12 +02001330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 return ret;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001332}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001333
Paul Bakker5121ce52009-01-03 21:22:43 +00001334/*
Janos Follathe81102e2017-03-22 13:38:28 +00001335 * Exponent blinding supposed to prevent side-channel attacks using multiple
1336 * traces of measurements to recover the RSA key. The more collisions are there,
1337 * the more bits of the key can be recovered. See [3].
1338 *
1339 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001340 * observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001341 *
1342 * For example with 28 byte blinding to achieve 2 collisions the adversary has
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001343 * to make 2^112 observations on average.
Janos Follathe81102e2017-03-22 13:38:28 +00001344 *
1345 * (With the currently (as of 2017 April) known best algorithms breaking 2048
1346 * bit RSA requires approximately as much time as trying out 2^112 random keys.
1347 * Thus in this sense with 28 byte blinding the security is not reduced by
1348 * side-channel attacks like the one in [3])
1349 *
1350 * This countermeasure does not help if the key recovery is possible with a
1351 * single trace.
1352 */
1353#define RSA_EXPONENT_BLINDING 28
1354
1355/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 * Do an RSA private key operation
1357 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001358int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1359 int (*f_rng)(void *, unsigned char *, size_t),
1360 void *p_rng,
1361 const unsigned char *input,
1362 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001363{
Janos Follath24eed8d2019-11-22 13:21:35 +00001364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001365 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +01001366
1367 /* Temporary holding the result */
1368 mbedtls_mpi T;
1369
1370 /* Temporaries holding P-1, Q-1 and the
1371 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +00001372 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +01001373
1374#if !defined(MBEDTLS_RSA_NO_CRT)
1375 /* Temporaries holding the results mod p resp. mod q. */
1376 mbedtls_mpi TP, TQ;
1377
1378 /* Temporaries holding the blinded exponents for
1379 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +00001380 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +01001381
1382 /* Pointers to actual exponents to be used - either the unblinded
1383 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +00001384 mbedtls_mpi *DP = &ctx->DP;
1385 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +01001386#else
1387 /* Temporary holding the blinded exponent (if used). */
1388 mbedtls_mpi D_blind;
1389
1390 /* Pointer to actual exponent to be used - either the unblinded
1391 * or the blinded one, depending on the presence of a PRNG. */
1392 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +01001393#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +01001394
Hanno Beckerc6075cc2017-08-25 11:45:35 +01001395 /* Temporaries holding the initial input and the double
1396 * checked result; should be the same in the end. */
1397 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 if (f_rng == NULL) {
1400 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1401 }
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (rsa_check_context(ctx, 1 /* private key checks */,
1404 1 /* blinding on */) != 0) {
1405 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerebd2c022017-10-12 10:54:53 +01001406 }
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +01001407
Hanno Becker06811ce2017-05-03 15:10:34 +01001408#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
1410 return ret;
1411 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001412#endif
Janos Follathf9203b42017-03-22 15:13:15 +00001413
Hanno Becker06811ce2017-05-03 15:10:34 +01001414 /* MPI Initialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 mbedtls_mpi_init(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 mbedtls_mpi_init(&P1);
1418 mbedtls_mpi_init(&Q1);
1419 mbedtls_mpi_init(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001420
Janos Follathe81102e2017-03-22 13:38:28 +00001421#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 mbedtls_mpi_init(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001423#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 mbedtls_mpi_init(&DP_blind);
1425 mbedtls_mpi_init(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001426#endif
1427
Hanno Becker06811ce2017-05-03 15:10:34 +01001428#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001430#endif
1431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 mbedtls_mpi_init(&I);
1433 mbedtls_mpi_init(&C);
Hanno Becker06811ce2017-05-03 15:10:34 +01001434
1435 /* End of MPI initialization */
1436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1438 if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +02001439 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1440 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 }
1442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
Hanno Becker06811ce2017-05-03 15:10:34 +01001444
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001445 /*
1446 * Blinding
1447 * T = T * Vi mod N
1448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1450 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1451 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Janos Follathe81102e2017-03-22 13:38:28 +00001452
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001453 /*
1454 * Exponent blinding
1455 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1457 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
Janos Follathe81102e2017-03-22 13:38:28 +00001458
Janos Follathf9203b42017-03-22 15:13:15 +00001459#if defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001460 /*
1461 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1462 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1464 f_rng, p_rng));
1465 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1466 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1467 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
Janos Follathe81102e2017-03-22 13:38:28 +00001468
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001469 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001470#else
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001471 /*
1472 * DP_blind = ( P - 1 ) * R + DP
1473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1475 f_rng, p_rng));
1476 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1477 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1478 &ctx->DP));
Janos Follathf9203b42017-03-22 15:13:15 +00001479
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001480 DP = &DP_blind;
Janos Follathf9203b42017-03-22 15:13:15 +00001481
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001482 /*
1483 * DQ_blind = ( Q - 1 ) * R + DQ
1484 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1486 f_rng, p_rng));
1487 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1488 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1489 &ctx->DQ));
Janos Follathf9203b42017-03-22 15:13:15 +00001490
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001491 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +00001492#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +01001496#else
Paul Bakkeraab30c12013-08-30 11:00:25 +02001497 /*
Janos Follathe81102e2017-03-22 13:38:28 +00001498 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 *
Hanno Becker06811ce2017-05-03 15:10:34 +01001500 * TP = input ^ dP mod P
1501 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 */
Hanno Becker06811ce2017-05-03 15:10:34 +01001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1505 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
Paul Bakker5121ce52009-01-03 21:22:43 +00001506
1507 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001508 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&T, &TP, &TQ));
1511 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->QP));
1512 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &TP, &ctx->P));
Paul Bakker5121ce52009-01-03 21:22:43 +00001513
1514 /*
Hanno Becker06811ce2017-05-03 15:10:34 +01001515 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +00001516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&TP, &T, &ctx->Q));
1518 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +02001520
Manuel Pégourié-Gonnardf0359042021-06-15 11:29:26 +02001521 /*
1522 * Unblind
1523 * T = T * Vf mod N
1524 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1526 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001527
Hanno Becker2dec5e82017-10-03 07:49:52 +01001528 /* Verify the result to prevent glitching attacks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1530 &ctx->N, &ctx->RN));
1531 if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
Hanno Becker06811ce2017-05-03 15:10:34 +01001532 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1533 goto cleanup;
1534 }
Hanno Becker06811ce2017-05-03 15:10:34 +01001535
Paul Bakker5121ce52009-01-03 21:22:43 +00001536 olen = ctx->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
1539cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
1542 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1543 }
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +02001544#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +02001545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 mbedtls_mpi_free(&P1);
1547 mbedtls_mpi_free(&Q1);
1548 mbedtls_mpi_free(&R);
Janos Follathf9203b42017-03-22 15:13:15 +00001549
Janos Follathe81102e2017-03-22 13:38:28 +00001550#if defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 mbedtls_mpi_free(&D_blind);
Janos Follathf9203b42017-03-22 15:13:15 +00001552#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 mbedtls_mpi_free(&DP_blind);
1554 mbedtls_mpi_free(&DQ_blind);
Janos Follathe81102e2017-03-22 13:38:28 +00001555#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 mbedtls_mpi_free(&T);
Hanno Becker06811ce2017-05-03 15:10:34 +01001558
1559#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
Hanno Becker06811ce2017-05-03 15:10:34 +01001561#endif
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 mbedtls_mpi_free(&C);
1564 mbedtls_mpi_free(&I);
Hanno Becker06811ce2017-05-03 15:10:34 +01001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 if (ret != 0 && ret >= -0x007f) {
1567 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1568 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001571}
1572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001574/**
1575 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1576 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00001577 * \param dst buffer to mask
1578 * \param dlen length of destination buffer
1579 * \param src source of the mask generation
1580 * \param slen length of the source buffer
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001581 * \param md_alg message digest to use
Paul Bakker9dcc3222011-03-08 14:16:06 +00001582 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001583static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1584 size_t slen, mbedtls_md_type_t md_alg)
Paul Bakker9dcc3222011-03-08 14:16:06 +00001585{
Paul Bakker9dcc3222011-03-08 14:16:06 +00001586 unsigned char counter[4];
1587 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +00001588 unsigned int hlen;
1589 size_t i, use_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001590 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Andres Amaya Garcia94682d12017-07-20 14:26:37 +01001591 int ret = 0;
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001592 const mbedtls_md_info_t *md_info;
1593 mbedtls_md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 mbedtls_md_init(&md_ctx);
1596 md_info = mbedtls_md_info_from_type(md_alg);
1597 if (md_info == NULL) {
1598 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1599 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 mbedtls_md_init(&md_ctx);
1602 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 }
Manuel Pégourié-Gonnard259c2132022-07-15 12:09:08 +02001605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 hlen = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 memset(mask, 0, sizeof(mask));
1609 memset(counter, 0, 4);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001610
Simon Butcher02037452016-03-01 21:19:12 +00001611 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001612 p = dst;
1613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 while (dlen > 0) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001615 use_len = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (dlen < hlen) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001617 use_len = dlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001621 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 }
1623 if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001624 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 }
1626 if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 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_finish(&md_ctx, mask)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001630 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 for (i = 0; i < use_len; ++i) {
Paul Bakker9dcc3222011-03-08 14:16:06 +00001634 *p++ ^= mask[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001636
1637 counter[3]++;
1638
1639 dlen -= use_len;
1640 }
Gilles Peskine18ac7162017-05-05 19:24:06 +02001641
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001642exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 mbedtls_platform_zeroize(mask, sizeof(mask));
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 mbedtls_md_free(&md_ctx);
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 return ret;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001647}
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001648
1649/**
1650 * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1651 *
1652 * \param hash the input hash
1653 * \param hlen length of the input hash
1654 * \param salt the input salt
1655 * \param slen length of the input salt
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001656 * \param out the output buffer - must be large enough for \p md_alg
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001657 * \param md_alg message digest to use
1658 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001659static int hash_mprime(const unsigned char *hash, size_t hlen,
1660 const unsigned char *salt, size_t slen,
1661 unsigned char *out, mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001662{
1663 const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Manuel Pégourié-Gonnard077ba842022-07-27 10:42:31 +02001664
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001665 mbedtls_md_context_t md_ctx;
Przemek Stekielf98b57f2022-07-29 11:27:46 +02001666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1669 if (md_info == NULL) {
1670 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1671 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 mbedtls_md_init(&md_ctx);
1674 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001675 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 }
1677 if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001678 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 }
1680 if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 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_update(&md_ctx, hash, hlen)) != 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, salt, slen)) != 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_finish(&md_ctx, out)) != 0) {
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001690 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 }
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001692
1693exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 return ret;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02001697}
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001698
1699/**
1700 * Compute a hash.
1701 *
1702 * \param md_alg algorithm to use
1703 * \param input input message to hash
1704 * \param ilen input length
1705 * \param output the output buffer - must be large enough for \p md_alg
1706 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001707static int compute_hash(mbedtls_md_type_t md_alg,
1708 const unsigned char *input, size_t ilen,
1709 unsigned char *output)
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001710{
1711 const mbedtls_md_info_t *md_info;
1712
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 md_info = mbedtls_md_info_from_type(md_alg);
1714 if (md_info == NULL) {
1715 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1716 }
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 return mbedtls_md(md_info, input, ilen, output);
Manuel Pégourié-Gonnard35c09e42022-07-15 13:10:54 +02001719}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001723/*
1724 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1725 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1727 int (*f_rng)(void *, unsigned char *, size_t),
1728 void *p_rng,
1729 const unsigned char *label, size_t label_len,
1730 size_t ilen,
1731 const unsigned char *input,
1732 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001733{
1734 size_t olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001736 unsigned char *p = output;
1737 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (f_rng == NULL) {
1740 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1741 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001742
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001743 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 if (hlen == 0) {
1745 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1746 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001747
1748 olen = ctx->len;
Paul Bakkerb3869132013-02-28 17:21:01 +01001749
Simon Butcher02037452016-03-01 21:19:12 +00001750 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1752 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1753 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 memset(output, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01001756
1757 *p++ = 0;
1758
Simon Butcher02037452016-03-01 21:19:12 +00001759 /* Generate a random octet string seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1761 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1762 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001763
1764 p += hlen;
1765
Simon Butcher02037452016-03-01 21:19:12 +00001766 /* Construct DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1768 if (ret != 0) {
1769 return ret;
1770 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001771 p += hlen;
1772 p += olen - 2 * hlen - 2 - ilen;
1773 *p++ = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if (ilen != 0) {
1775 memcpy(p, input, ilen);
1776 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001777
Simon Butcher02037452016-03-01 21:19:12 +00001778 /* maskedDB: Apply dbMask to DB */
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001780 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 return ret;
1782 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001783
Simon Butcher02037452016-03-01 21:19:12 +00001784 /* maskedSeed: Apply seedMask to seed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
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 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001791}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001795/*
1796 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1797 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001798int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1799 int (*f_rng)(void *, unsigned char *, size_t),
1800 void *p_rng, size_t ilen,
1801 const unsigned char *input,
1802 unsigned char *output)
Paul Bakkerb3869132013-02-28 17:21:01 +01001803{
1804 size_t nb_pad, olen;
Janos Follath24eed8d2019-11-22 13:21:35 +00001805 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01001806 unsigned char *p = output;
1807
Paul Bakkerb3869132013-02-28 17:21:01 +01001808 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +01001809
Simon Butcher02037452016-03-01 21:19:12 +00001810 /* first comparison checks for overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 if (ilen + 11 < ilen || olen < ilen + 11) {
1812 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1813 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001814
1815 nb_pad = olen - 3 - ilen;
1816
1817 *p++ = 0;
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (f_rng == NULL) {
1820 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1821 }
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001822
1823 *p++ = MBEDTLS_RSA_CRYPT;
1824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 while (nb_pad-- > 0) {
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001826 int rng_dl = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001827
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001828 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 ret = f_rng(p_rng, p, 1);
1830 } while (*p == 0 && --rng_dl && ret == 0);
Paul Bakkerb3869132013-02-28 17:21:01 +01001831
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001832 /* Check if RNG failed to generate data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 if (rng_dl == 0 || ret != 0) {
1834 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1835 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001836
Thomas Daubney53e4ac62021-05-13 18:26:49 +01001837 p++;
Paul Bakkerb3869132013-02-28 17:21:01 +01001838 }
1839
1840 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 if (ilen != 0) {
1842 memcpy(p, input, ilen);
1843 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 return mbedtls_rsa_public(ctx, output, output);
Paul Bakkerb3869132013-02-28 17:21:01 +01001846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001848
Paul Bakker5121ce52009-01-03 21:22:43 +00001849/*
1850 * Add the message padding, then do an RSA operation
1851 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001852int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1853 int (*f_rng)(void *, unsigned char *, size_t),
1854 void *p_rng,
1855 size_t ilen,
1856 const unsigned char *input,
1857 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +00001858{
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860#if defined(MBEDTLS_PKCS1_V15)
1861 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1863 ilen, input, output);
Paul Bakker48377d92013-08-30 12:06:24 +02001864#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866#if defined(MBEDTLS_PKCS1_V21)
1867 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1869 ilen, input, output);
Paul Bakker9dcc3222011-03-08 14:16:06 +00001870#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001871
1872 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00001874 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001875}
1876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001878/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001879 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +00001880 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001881int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1882 int (*f_rng)(void *, unsigned char *, size_t),
1883 void *p_rng,
1884 const unsigned char *label, size_t label_len,
1885 size_t *olen,
1886 const unsigned char *input,
1887 unsigned char *output,
1888 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +00001889{
Janos Follath24eed8d2019-11-22 13:21:35 +00001890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001891 size_t ilen, i, pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001892 unsigned char *p;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001893 mbedtls_ct_condition_t bad, in_padding;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02001895 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00001896 unsigned int hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001897
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001898 /*
1899 * Parameters sanity checks
1900 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1902 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1903 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001904
1905 ilen = ctx->len;
1906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 if (ilen < 16 || ilen > sizeof(buf)) {
1908 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1909 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001910
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001911 hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 if (hlen == 0) {
1913 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1914 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001915
Janos Follathc17cda12016-02-11 11:08:18 +00001916 // checking for integer underflow
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 if (2 * hlen + 2 > ilen) {
1918 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1919 }
Janos Follathc17cda12016-02-11 11:08:18 +00001920
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001921 /*
1922 * RSA operation
1923 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 if (ret != 0) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001927 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001929
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001930 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001931 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001932 */
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001933 /* seed: Apply seedMask to maskedSeed */
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001935 (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 /* DB: Apply dbMask to maskedDB */
1937 (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Agathiyan Bragadeesh01ed84a2023-07-13 11:42:41 +01001938 (mbedtls_md_type_t) ctx->hash_id)) != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001939 goto cleanup;
1940 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001941
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001942 /* Generate lHash */
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1944 label, label_len, lhash);
1945 if (ret != 0) {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001946 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001948
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001949 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001950 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001951 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 p = buf;
1953
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001954 bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001955
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001956 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +01001957
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +01001958 /* Check lHash */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001959 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
Dave Rodgman66d6ac92023-09-18 18:35:03 +01001960 p += hlen;
Paul Bakkerb3869132013-02-28 17:21:01 +01001961
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001962 /* Get zero-padding len, but always read till end of buffer
1963 * (minus one, for the 01 byte) */
1964 pad_len = 0;
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001965 in_padding = MBEDTLS_CT_TRUE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 for (i = 0; i < ilen - 2 * hlen - 2; i++) {
Dave Rodgmanc62f7fc2023-09-20 19:06:02 +01001967 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1968 pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001969 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001970
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001971 p += pad_len;
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001972 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
Paul Bakkerb3869132013-02-28 17:21:01 +01001973
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +01001974 /*
1975 * The only information "leaked" is whether the padding was correct or not
1976 * (eg, no data is copied if it was not correct). This meets the
1977 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1978 * the different error conditions.
1979 */
Dave Rodgmanb4e6b412023-09-18 18:46:19 +01001980 if (bad != MBEDTLS_CT_FALSE) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001981 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1982 goto cleanup;
1983 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001984
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001985 if (ilen - ((size_t) (p - buf)) > output_max_len) {
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001986 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1987 goto cleanup;
1988 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001989
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001990 *olen = ilen - ((size_t) (p - buf));
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 if (*olen != 0) {
1992 memcpy(output, p, *olen);
1993 }
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001994 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001995
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001996cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 mbedtls_platform_zeroize(buf, sizeof(buf));
1998 mbedtls_platform_zeroize(lhash, sizeof(lhash));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#if defined(MBEDTLS_PKCS1_V15)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002005/*
2006 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
2007 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002008int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
2009 int (*f_rng)(void *, unsigned char *, size_t),
2010 void *p_rng,
2011 size_t *olen,
2012 const unsigned char *input,
2013 unsigned char *output,
2014 size_t output_max_len)
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002015{
2016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2017 size_t ilen;
2018 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
2019
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002020 ilen = ctx->len;
2021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2023 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2024 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (ilen < 16 || ilen > sizeof(buf)) {
2027 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2028 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 if (ret != 0) {
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002033 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 }
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(buf, ilen,
2037 output, output_max_len, olen);
gabor-mezei-armbef600f2021-09-26 15:20:48 +02002038
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002039cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 mbedtls_platform_zeroize(buf, sizeof(buf));
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002043}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
2046/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002047 * Do an RSA operation, then remove the message padding
2048 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002049int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
2050 int (*f_rng)(void *, unsigned char *, size_t),
2051 void *p_rng,
2052 size_t *olen,
2053 const unsigned char *input,
2054 unsigned char *output,
2055 size_t output_max_len)
Paul Bakkerb3869132013-02-28 17:21:01 +01002056{
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058#if defined(MBEDTLS_PKCS1_V15)
2059 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
2061 input, output, output_max_len);
Paul Bakker48377d92013-08-30 12:06:24 +02002062#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064#if defined(MBEDTLS_PKCS1_V21)
2065 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
2067 olen, input, output,
2068 output_max_len);
Paul Bakkerb3869132013-02-28 17:21:01 +01002069#endif
2070
2071 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002073 }
2074}
2075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#if defined(MBEDTLS_PKCS1_V21)
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002077static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2078 int (*f_rng)(void *, unsigned char *, size_t),
2079 void *p_rng,
2080 mbedtls_md_type_t md_alg,
2081 unsigned int hashlen,
2082 const unsigned char *hash,
2083 int saltlen,
2084 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002085{
2086 size_t olen;
2087 unsigned char *p = sig;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002088 unsigned char *salt = NULL;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002089 size_t slen, min_slen, hlen, offset = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00002090 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002091 size_t msb;
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002092 mbedtls_md_type_t hash_id;
Manuel Pégourié-Gonnardf701acc2022-07-15 12:49:14 +02002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002095 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002097
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (f_rng == NULL) {
2099 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2100 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002101
2102 olen = ctx->len;
2103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 if (md_alg != MBEDTLS_MD_NONE) {
Simon Butcher02037452016-03-01 21:19:12 +00002105 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002106 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (exp_hashlen == 0) {
2108 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2109 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 if (hashlen != exp_hashlen) {
2112 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2113 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002114 }
2115
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002116 hash_id = (mbedtls_md_type_t) ctx->hash_id;
2117 if (hash_id == MBEDTLS_MD_NONE) {
2118 hash_id = md_alg;
2119 }
2120 hlen = mbedtls_md_get_size_from_type(hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 if (hlen == 0) {
2122 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2123 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
2126 /* Calculate the largest possible salt length, up to the hash size.
2127 * Normally this is the hash length, which is the maximum salt length
2128 * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
2129 * enough room, use the maximum salt length that fits. The constraint is
2130 * that the hash length plus the salt length plus 2 bytes must be at most
2131 * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
2132 * (PKCS#1 v2.2) §9.1.1 step 3. */
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002133 min_slen = hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 if (olen < hlen + min_slen + 2) {
2135 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2136 } else if (olen >= hlen + hlen + 2) {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002137 slen = hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 } else {
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002139 slen = olen - hlen - 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 }
2141 } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
2142 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2143 } else {
Cédric Meuter010ddc22020-04-25 09:24:11 +02002144 slen = (size_t) saltlen;
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002145 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 memset(sig, 0, olen);
Paul Bakkerb3869132013-02-28 17:21:01 +01002148
Simon Butcher02037452016-03-01 21:19:12 +00002149 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Jaeden Amero3725bb22018-09-07 19:12:36 +01002151 p += olen - hlen - slen - 2;
Paul Bakkerb3869132013-02-28 17:21:01 +01002152 *p++ = 0x01;
Cédric Meuter668a78d2020-04-30 11:57:04 +02002153
2154 /* Generate salt of length slen in place in the encoded message */
2155 salt = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if ((ret = f_rng(p_rng, salt, slen)) != 0) {
2157 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
2158 }
Cédric Meuter668a78d2020-04-30 11:57:04 +02002159
Paul Bakkerb3869132013-02-28 17:21:01 +01002160 p += slen;
2161
Simon Butcher02037452016-03-01 21:19:12 +00002162 /* Generate H = Hash( M' ) */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002163 ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (ret != 0) {
2165 return ret;
2166 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002167
Simon Butcher02037452016-03-01 21:19:12 +00002168 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002170 offset = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002172
Simon Butcher02037452016-03-01 21:19:12 +00002173 /* maskedDB: Apply dbMask to DB */
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002174 ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 if (ret != 0) {
2176 return ret;
2177 }
Paul Bakkerb3869132013-02-28 17:21:01 +01002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2180 sig[0] &= 0xFF >> (olen * 8 - msb);
Paul Bakkerb3869132013-02-28 17:21:01 +01002181
2182 p += hlen;
2183 *p++ = 0xBC;
2184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002186}
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002187
Tomi Fontanilles573dc232023-12-10 14:57:51 +02002188static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2189 int (*f_rng)(void *, unsigned char *, size_t),
2190 void *p_rng,
2191 mbedtls_md_type_t md_alg,
2192 unsigned int hashlen,
2193 const unsigned char *hash,
2194 int saltlen,
2195 unsigned char *sig)
2196{
2197 if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
2198 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2199 }
2200 if (ctx->hash_id == MBEDTLS_MD_NONE) {
2201 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2202 }
2203 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
2204 sig);
2205}
2206
2207int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
2208 int (*f_rng)(void *, unsigned char *, size_t),
2209 void *p_rng,
2210 mbedtls_md_type_t md_alg,
2211 unsigned int hashlen,
2212 const unsigned char *hash,
2213 unsigned char *sig)
2214{
2215 return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
2216 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2217}
2218
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002219/*
Cédric Meuterf3fab332020-04-25 11:30:45 +02002220 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
2221 * the option to pass in the salt length.
2222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002223int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
2224 int (*f_rng)(void *, unsigned char *, size_t),
2225 void *p_rng,
2226 mbedtls_md_type_t md_alg,
2227 unsigned int hashlen,
2228 const unsigned char *hash,
2229 int saltlen,
2230 unsigned char *sig)
Cédric Meuterf3fab332020-04-25 11:30:45 +02002231{
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2233 hashlen, hash, saltlen, sig);
Cédric Meuterf3fab332020-04-25 11:30:45 +02002234}
2235
Cédric Meuterf3fab332020-04-25 11:30:45 +02002236/*
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002237 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
2238 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002239int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
2240 int (*f_rng)(void *, unsigned char *, size_t),
2241 void *p_rng,
2242 mbedtls_md_type_t md_alg,
2243 unsigned int hashlen,
2244 const unsigned char *hash,
2245 unsigned char *sig)
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002246{
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2248 hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
Cedric Meuter8aa4d752020-04-21 12:49:11 +02002249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002253/*
2254 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
2255 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002256
2257/* Construct a PKCS v1.5 encoding of a hashed message
2258 *
2259 * This is used both for signature generation and verification.
2260 *
2261 * Parameters:
2262 * - md_alg: Identifies the hash algorithm used to generate the given hash;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002263 * MBEDTLS_MD_NONE if raw data is signed.
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002264 * - hashlen: Length of hash. Must match md_alg if that's not NONE.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002265 * - hash: Buffer containing the hashed message or the raw data.
2266 * - dst_len: Length of the encoded message.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002267 * - dst: Buffer to hold the encoded message.
2268 *
2269 * Assumptions:
Gilles Peskine6e3187b2021-06-22 18:39:53 +02002270 * - hash has size hashlen.
Hanno Beckere58d38c2017-09-27 17:09:00 +01002271 * - dst points to a buffer of size at least dst_len.
Hanno Beckerfdf38032017-09-06 12:35:55 +01002272 *
2273 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002274static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
2275 unsigned int hashlen,
2276 const unsigned char *hash,
2277 size_t dst_len,
2278 unsigned char *dst)
Hanno Beckerfdf38032017-09-06 12:35:55 +01002279{
2280 size_t oid_size = 0;
Hanno Beckere58d38c2017-09-27 17:09:00 +01002281 size_t nb_pad = dst_len;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002282 unsigned char *p = dst;
2283 const char *oid = NULL;
2284
2285 /* Are we signing hashed or raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if (md_alg != MBEDTLS_MD_NONE) {
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002287 unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if (md_size == 0) {
2289 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2290 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2293 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2294 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 if (hashlen != md_size) {
2297 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2298 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002299
2300 /* Double-check that 8 + hashlen + oid_size can be used as a
2301 * 1-byte ASN.1 length encoding and that there's no overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (8 + hashlen + oid_size >= 0x80 ||
Hanno Beckerfdf38032017-09-06 12:35:55 +01002303 10 + hashlen < hashlen ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 10 + hashlen + oid_size < 10 + hashlen) {
2305 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2306 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002307
2308 /*
2309 * Static bounds check:
2310 * - Need 10 bytes for five tag-length pairs.
2311 * (Insist on 1-byte length encodings to protect against variants of
2312 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2313 * - Need hashlen bytes for hash
2314 * - Need oid_size bytes for hash alg OID.
2315 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (nb_pad < 10 + hashlen + oid_size) {
2317 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2318 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002319 nb_pad -= 10 + hashlen + oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 } else {
2321 if (nb_pad < hashlen) {
2322 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2323 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002324
2325 nb_pad -= hashlen;
2326 }
2327
Hanno Becker2b2f8982017-09-27 17:10:03 +01002328 /* Need space for signature header and padding delimiter (3 bytes),
2329 * and 8 bytes for the minimal padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 if (nb_pad < 3 + 8) {
2331 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2332 }
Hanno Beckerfdf38032017-09-06 12:35:55 +01002333 nb_pad -= 3;
2334
2335 /* Now nb_pad is the amount of memory to be filled
Hanno Becker2b2f8982017-09-27 17:10:03 +01002336 * with padding, and at least 8 bytes long. */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002337
2338 /* Write signature header and padding */
2339 *p++ = 0;
2340 *p++ = MBEDTLS_RSA_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 memset(p, 0xFF, nb_pad);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002342 p += nb_pad;
2343 *p++ = 0;
2344
2345 /* Are we signing raw data? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 if (md_alg == MBEDTLS_MD_NONE) {
2347 memcpy(p, hash, hashlen);
2348 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002349 }
2350
2351 /* Signing hashed data, add corresponding ASN.1 structure
2352 *
2353 * DigestInfo ::= SEQUENCE {
2354 * digestAlgorithm DigestAlgorithmIdentifier,
2355 * digest Digest }
2356 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2357 * Digest ::= OCTET STRING
2358 *
2359 * Schematic:
2360 * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2361 * TAG-NULL + LEN [ NULL ] ]
2362 * TAG-OCTET + LEN [ HASH ] ]
2363 */
2364 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 *p++ = (unsigned char) (0x08 + oid_size + hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002366 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 *p++ = (unsigned char) (0x04 + oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002368 *p++ = MBEDTLS_ASN1_OID;
Hanno Becker87ae1972018-01-15 15:27:56 +00002369 *p++ = (unsigned char) oid_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 memcpy(p, oid, oid_size);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002371 p += oid_size;
2372 *p++ = MBEDTLS_ASN1_NULL;
2373 *p++ = 0x00;
2374 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Hanno Becker87ae1972018-01-15 15:27:56 +00002375 *p++ = (unsigned char) hashlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 memcpy(p, hash, hashlen);
Hanno Beckerfdf38032017-09-06 12:35:55 +01002377 p += hashlen;
2378
2379 /* Just a sanity-check, should be automatic
2380 * after the initial bounds check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (p != dst + dst_len) {
2382 mbedtls_platform_zeroize(dst, dst_len);
2383 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002384 }
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 return 0;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002387}
2388
Paul Bakkerb3869132013-02-28 17:21:01 +01002389/*
2390 * Do an RSA operation to sign the message digest
2391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2393 int (*f_rng)(void *, unsigned char *, size_t),
2394 void *p_rng,
2395 mbedtls_md_type_t md_alg,
2396 unsigned int hashlen,
2397 const unsigned char *hash,
2398 unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002399{
Janos Follath24eed8d2019-11-22 13:21:35 +00002400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerfdf38032017-09-06 12:35:55 +01002401 unsigned char *sig_try = NULL, *verif = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002404 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2408 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2409 }
Thomas Daubneyd58ed582021-05-21 11:50:39 +01002410
Hanno Beckerfdf38032017-09-06 12:35:55 +01002411 /*
2412 * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2413 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2416 ctx->len, sig)) != 0) {
2417 return ret;
2418 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002419
Hanno Beckerfdf38032017-09-06 12:35:55 +01002420 /* Private key operation
2421 *
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002422 * In order to prevent Lenstra's attack, make the signature in a
2423 * temporary buffer and check it before returning it.
2424 */
Hanno Beckerfdf38032017-09-06 12:35:55 +01002425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 sig_try = mbedtls_calloc(1, ctx->len);
2427 if (sig_try == NULL) {
2428 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
Simon Butcher1285ab52016-01-01 21:42:47 +00002429 }
2430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 verif = mbedtls_calloc(1, ctx->len);
2432 if (verif == NULL) {
2433 mbedtls_free(sig_try);
2434 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2435 }
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2438 MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2439
2440 if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002441 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2442 goto cleanup;
2443 }
2444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 memcpy(sig, sig_try, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002446
2447cleanup:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002448 mbedtls_zeroize_and_free(sig_try, ctx->len);
2449 mbedtls_zeroize_and_free(verif, ctx->len);
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02002450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 if (ret != 0) {
2452 memset(sig, '!', ctx->len);
2453 }
2454 return ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01002455}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002457
2458/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 * Do an RSA operation to sign the message digest
2460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2462 int (*f_rng)(void *, unsigned char *, size_t),
2463 void *p_rng,
2464 mbedtls_md_type_t md_alg,
2465 unsigned int hashlen,
2466 const unsigned char *hash,
2467 unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002468{
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002470 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#if defined(MBEDTLS_PKCS1_V15)
2475 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2477 md_alg, hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002478#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480#if defined(MBEDTLS_PKCS1_V21)
2481 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 return mbedtls_rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
2483 hashlen, hash, sig);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002484#endif
2485
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002489}
2490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00002492/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002493 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002495int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2496 mbedtls_md_type_t md_alg,
2497 unsigned int hashlen,
2498 const unsigned char *hash,
2499 mbedtls_md_type_t mgf1_hash_id,
2500 int expected_salt_len,
2501 const unsigned char *sig)
Paul Bakker5121ce52009-01-03 21:22:43 +00002502{
Janos Follath24eed8d2019-11-22 13:21:35 +00002503 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerb3869132013-02-28 17:21:01 +01002504 size_t siglen;
2505 unsigned char *p;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002506 unsigned char *hash_start;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002507 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +00002508 unsigned int hlen;
Gilles Peskine6a54b022017-10-17 19:02:13 +02002509 size_t observed_salt_len, msb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
Paul Bakkerb3869132013-02-28 17:21:01 +01002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002513 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002515
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 siglen = ctx->len;
2517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (siglen < 16 || siglen > sizeof(buf)) {
2519 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 ret = mbedtls_rsa_public(ctx, sig, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (ret != 0) {
2525 return ret;
2526 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
2528 p = buf;
2529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (buf[siglen - 1] != 0xBC) {
2531 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002532 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 if (md_alg != MBEDTLS_MD_NONE) {
2535 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002536 size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 if (exp_hashlen == 0) {
2538 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2539 }
2540
2541 if (hashlen != exp_hashlen) {
2542 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2543 }
2544 }
2545
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02002546 hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 if (hlen == 0) {
2548 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2549 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002550
Simon Butcher02037452016-03-01 21:19:12 +00002551 /*
2552 * Note: EMSA-PSS verification is over the length of N - 1 bits
2553 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (buf[0] >> (8 - siglen * 8 + msb)) {
2557 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2558 }
Gilles Peskineb00b0da2017-10-19 15:23:49 +02002559
Simon Butcher02037452016-03-01 21:19:12 +00002560 /* Compensate for boundary condition when applying mask */
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 if (msb % 8 == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002562 p++;
2563 siglen -= 1;
2564 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 if (siglen < hlen + 2) {
2567 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2568 }
Gilles Peskine139108a2017-10-18 19:03:42 +02002569 hash_start = p + siglen - hlen - 1;
2570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2572 if (ret != 0) {
2573 return ret;
2574 }
Paul Bakker02303e82013-01-03 11:08:31 +01002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 buf[0] &= 0xFF >> (siglen * 8 - msb);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 while (p < hash_start - 1 && *p == 0) {
Paul Bakkerb3869132013-02-28 17:21:01 +01002579 p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (*p++ != 0x01) {
2583 return MBEDTLS_ERR_RSA_INVALID_PADDING;
2584 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00002585
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002586 observed_salt_len = (size_t) (hash_start - p);
Paul Bakker9dcc3222011-03-08 14:16:06 +00002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2589 observed_salt_len != (size_t) expected_salt_len) {
2590 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002591 }
2592
Simon Butcher02037452016-03-01 21:19:12 +00002593 /*
2594 * Generate H = Hash( M' )
2595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2597 result, mgf1_hash_id);
2598 if (ret != 0) {
2599 return ret;
2600 }
Paul Bakker53019ae2011-03-25 13:58:48 +00002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 if (memcmp(hash_start, result, hlen) != 0) {
2603 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2604 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 return 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01002607}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002608
2609/*
2610 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2611 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002612int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
2613 mbedtls_md_type_t md_alg,
2614 unsigned int hashlen,
2615 const unsigned char *hash,
2616 const unsigned char *sig)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002617{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002618 mbedtls_md_type_t mgf1_hash_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002620 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002625 : md_alg;
2626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 return mbedtls_rsa_rsassa_pss_verify_ext(ctx,
2628 md_alg, hashlen, hash,
2629 mgf1_hash_id,
2630 MBEDTLS_RSA_SALT_LEN_ANY,
2631 sig);
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02002632
2633}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01002637/*
2638 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2639 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002640int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2641 mbedtls_md_type_t md_alg,
2642 unsigned int hashlen,
2643 const unsigned char *hash,
2644 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002645{
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002646 int ret = 0;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002647 size_t sig_len;
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002648 unsigned char *encoded = NULL, *encoded_expected = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01002649
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002651 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002653
2654 sig_len = ctx->len;
2655
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002656 /*
2657 * Prepare expected PKCS1 v1.5 encoding of hash.
2658 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2661 (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002662 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2663 goto cleanup;
2664 }
2665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2667 encoded_expected)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002668 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 }
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002670
2671 /*
2672 * Apply RSA primitive to get what should be PKCS1 encoded hash.
2673 */
Paul Bakkerb3869132013-02-28 17:21:01 +01002674
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 ret = mbedtls_rsa_public(ctx, sig, encoded);
2676 if (ret != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002677 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002679
Simon Butcher02037452016-03-01 21:19:12 +00002680 /*
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002681 * Compare
Simon Butcher02037452016-03-01 21:19:12 +00002682 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2685 sig_len)) != 0) {
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002686 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2687 goto cleanup;
2688 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002689
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002690cleanup:
Paul Bakkerc70b9822013-04-07 22:00:46 +02002691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 if (encoded != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002693 mbedtls_zeroize_and_free(encoded, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002694 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if (encoded_expected != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002697 mbedtls_zeroize_and_free(encoded_expected, sig_len);
Hanno Becker64a8c0a2017-09-06 12:39:49 +01002698 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
2704/*
Paul Bakkerb3869132013-02-28 17:21:01 +01002705 * Do an RSA operation and check the message digest
2706 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002707int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2708 mbedtls_md_type_t md_alg,
2709 unsigned int hashlen,
2710 const unsigned char *hash,
2711 const unsigned char *sig)
Paul Bakkerb3869132013-02-28 17:21:01 +01002712{
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu6a473b22022-08-05 15:49:56 +01002714 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 switch (ctx->padding) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718#if defined(MBEDTLS_PKCS1_V15)
2719 case MBEDTLS_RSA_PKCS_V15:
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2721 hashlen, hash, sig);
Paul Bakker48377d92013-08-30 12:06:24 +02002722#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01002723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724#if defined(MBEDTLS_PKCS1_V21)
2725 case MBEDTLS_RSA_PKCS_V21:
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 return mbedtls_rsa_rsassa_pss_verify(ctx, md_alg,
2727 hashlen, hash, sig);
Paul Bakkerb3869132013-02-28 17:21:01 +01002728#endif
2729
2730 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Paul Bakkerb3869132013-02-28 17:21:01 +01002732 }
2733}
2734
2735/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002736 * Copy the components of an RSA key
2737 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002738int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002739{
Janos Follath24eed8d2019-11-22 13:21:35 +00002740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002741
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002742 dst->len = src->len;
2743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2745 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2748 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2749 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
Hanno Becker33c30a02017-08-23 07:00:22 +01002750
2751#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2753 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2754 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2755 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2756 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
Hanno Becker33c30a02017-08-23 07:00:22 +01002757#endif
2758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2762 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02002763
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002764 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01002765 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002766
2767cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 if (ret != 0) {
2769 mbedtls_rsa_free(dst);
2770 }
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002771
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 return ret;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002773}
2774
2775/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 * Free the components of an RSA key
2777 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002778void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +00002779{
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 if (ctx == NULL) {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002781 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 mbedtls_mpi_free(&ctx->Vi);
2785 mbedtls_mpi_free(&ctx->Vf);
2786 mbedtls_mpi_free(&ctx->RN);
2787 mbedtls_mpi_free(&ctx->D);
2788 mbedtls_mpi_free(&ctx->Q);
2789 mbedtls_mpi_free(&ctx->P);
2790 mbedtls_mpi_free(&ctx->E);
2791 mbedtls_mpi_free(&ctx->N);
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002792
Hanno Becker33c30a02017-08-23 07:00:22 +01002793#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 mbedtls_mpi_free(&ctx->RQ);
2795 mbedtls_mpi_free(&ctx->RP);
2796 mbedtls_mpi_free(&ctx->QP);
2797 mbedtls_mpi_free(&ctx->DQ);
2798 mbedtls_mpi_free(&ctx->DP);
Hanno Becker33c30a02017-08-23 07:00:22 +01002799#endif /* MBEDTLS_RSA_NO_CRT */
2800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801#if defined(MBEDTLS_THREADING_C)
Gilles Peskineeb940592021-02-01 17:57:41 +01002802 /* Free the mutex, but only if it hasn't been freed already. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 if (ctx->ver != 0) {
2804 mbedtls_mutex_free(&ctx->mutex);
Gilles Peskineeb940592021-02-01 17:57:41 +01002805 ctx->ver = 0;
2806 }
Paul Bakkerc9965dc2013-09-29 14:58:17 +02002807#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002808}
2809
Hanno Beckerab377312017-08-23 16:24:51 +01002810#endif /* !MBEDTLS_RSA_ALT */
2811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
2815/*
2816 * Example RSA-1024 keypair, for test purposes
2817 */
2818#define KEY_LEN 128
2819
2820#define RSA_N "9292758453063D803DD603D5E777D788" \
2821 "8ED1D5BF35786190FA2F23EBC0848AEA" \
2822 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2823 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2824 "93A89813FBF3C4F8066D2D800F7C38A8" \
2825 "1AE31942917403FF4946B0A83D3D3E05" \
2826 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2827 "5E94BB77B07507233A0BC7BAC8F90F79"
2828
2829#define RSA_E "10001"
2830
2831#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2832 "66CA472BC44D253102F8B4A9D3BFA750" \
2833 "91386C0077937FE33FA3252D28855837" \
2834 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2835 "DF79C5CE07EE72C7F123142198164234" \
2836 "CABB724CF78B8173B9F880FC86322407" \
2837 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2838 "071513A1E85B5DFA031F21ECAE91A34D"
2839
2840#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2841 "2C01CAD19EA484A87EA4377637E75500" \
2842 "FCB2005C5C7DD6EC4AC023CDA285D796" \
2843 "C3D9E75E1EFC42488BB4F1D13AC30A57"
2844
2845#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2846 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2847 "910E4168387E3C30AA1E00C339A79508" \
2848 "8452DD96A9A5EA5D9DCA68DA636032AF"
2849
Paul Bakker5121ce52009-01-03 21:22:43 +00002850#define PT_LEN 24
2851#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2852 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +01002855static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker545570e2010-07-18 09:00:25 +00002856{
gufe44c2620da2020-08-03 17:56:50 +02002857#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00002858 size_t i;
2859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 if (rng_state != NULL) {
Paul Bakker545570e2010-07-18 09:00:25 +00002861 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 }
Paul Bakker545570e2010-07-18 09:00:25 +00002863
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 for (i = 0; i < len; ++i) {
Paul Bakkera3d195c2011-11-27 21:07:34 +00002865 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002867#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (rng_state != NULL) {
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002869 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 }
Paul Bakkerf96f7b62014-04-30 16:02:38 +02002871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +02002873#endif /* !OpenBSD && !NetBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02002874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 return 0;
Paul Bakker545570e2010-07-18 09:00:25 +00002876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00002878
Paul Bakker5121ce52009-01-03 21:22:43 +00002879/*
2880 * Checkup routine
2881 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002882int mbedtls_rsa_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002883{
Paul Bakker3d8fb632014-04-17 12:42:41 +02002884 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00002886 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00002888 unsigned char rsa_plaintext[PT_LEN];
2889 unsigned char rsa_decrypted[PT_LEN];
2890 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002891#if defined(MBEDTLS_MD_CAN_SHA1)
Paul Bakker5690efc2011-05-26 13:16:06 +00002892 unsigned char sha1sum[20];
2893#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
Hanno Becker3a701162017-08-22 13:52:43 +01002895 mbedtls_mpi K;
2896
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 mbedtls_mpi_init(&K);
2898 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +00002899
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_N));
2901 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, &K, NULL, NULL, NULL, NULL));
2902 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_P));
2903 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, &K, NULL, NULL, NULL));
2904 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_Q));
2905 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, &K, NULL, NULL));
2906 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_D));
2907 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, &K, NULL));
2908 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&K, 16, RSA_E));
2909 MBEDTLS_MPI_CHK(mbedtls_rsa_import(&rsa, NULL, NULL, NULL, NULL, &K));
Hanno Becker3a701162017-08-22 13:52:43 +01002910
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 MBEDTLS_MPI_CHK(mbedtls_rsa_complete(&rsa));
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 if (verbose != 0) {
2914 mbedtls_printf(" RSA key validation: ");
2915 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2918 mbedtls_rsa_check_privkey(&rsa) != 0) {
2919 if (verbose != 0) {
2920 mbedtls_printf("failed\n");
2921 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Hanno Becker5bc87292017-05-03 15:09:31 +01002923 ret = 1;
2924 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 }
2926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 if (verbose != 0) {
2928 mbedtls_printf("passed\n PKCS#1 encryption : ");
2929 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 memcpy(rsa_plaintext, RSA_PT, PT_LEN);
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2934 PT_LEN, rsa_plaintext,
2935 rsa_ciphertext) != 0) {
2936 if (verbose != 0) {
2937 mbedtls_printf("failed\n");
2938 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Hanno Becker5bc87292017-05-03 15:09:31 +01002940 ret = 1;
2941 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002942 }
2943
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 if (verbose != 0) {
2945 mbedtls_printf("passed\n PKCS#1 decryption : ");
2946 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2949 &len, rsa_ciphertext, rsa_decrypted,
2950 sizeof(rsa_decrypted)) != 0) {
2951 if (verbose != 0) {
2952 mbedtls_printf("failed\n");
2953 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
Hanno Becker5bc87292017-05-03 15:09:31 +01002955 ret = 1;
2956 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002957 }
2958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2960 if (verbose != 0) {
2961 mbedtls_printf("failed\n");
2962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Hanno Becker5bc87292017-05-03 15:09:31 +01002964 ret = 1;
2965 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 }
2967
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 if (verbose != 0) {
2969 mbedtls_printf("passed\n");
2970 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02002971
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01002972#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 if (verbose != 0) {
2974 mbedtls_printf(" PKCS#1 data sign : ");
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01002975 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002976
Manuel Pégourié-Gonnardb33ef742023-03-07 00:04:16 +01002977 if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2978 rsa_plaintext, PT_LEN, sha1sum) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 if (verbose != 0) {
2980 mbedtls_printf("failed\n");
2981 }
2982
2983 return 1;
2984 }
2985
2986 if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2987 MBEDTLS_MD_SHA1, 20,
2988 sha1sum, rsa_ciphertext) != 0) {
2989 if (verbose != 0) {
2990 mbedtls_printf("failed\n");
2991 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002992
Hanno Becker5bc87292017-05-03 15:09:31 +01002993 ret = 1;
2994 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002995 }
2996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 if (verbose != 0) {
2998 mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2999 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
3002 sha1sum, rsa_ciphertext) != 0) {
3003 if (verbose != 0) {
3004 mbedtls_printf("failed\n");
3005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Hanno Becker5bc87292017-05-03 15:09:31 +01003007 ret = 1;
3008 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 }
3010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 if (verbose != 0) {
3012 mbedtls_printf("passed\n");
3013 }
Manuel Pégourié-Gonnardc1f10442023-03-16 10:58:19 +01003014#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if (verbose != 0) {
3017 mbedtls_printf("\n");
3018 }
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02003019
Paul Bakker3d8fb632014-04-17 12:42:41 +02003020cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 mbedtls_mpi_free(&K);
3022 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02003024 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#endif /* MBEDTLS_PKCS1_V15 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003027}
3028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031#endif /* MBEDTLS_RSA_C */